blob: bc6e9cd0ab7eb47fbe67edc5825498de0ac1bf3f [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
Philip P. Moltmanne78c7712017-06-19 12:57:13 -070019import static android.os.Build.VERSION_CODES.O;
20
21import 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;
Amith Yamasanieeed06c2016-05-03 15:07:03 -070034import android.app.VoiceInteractor.Request;
Jason Monk62515be2014-05-21 16:06:19 -040035import android.app.admin.DevicePolicyManager;
Dianne Hackborn69c6adc2015-06-02 10:52:59 -070036import android.app.assist.AssistContent;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070037import android.content.ComponentCallbacks2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.content.ComponentName;
39import android.content.ContentResolver;
40import android.content.Context;
Jason parks6ed50de2010-08-25 10:18:50 -050041import android.content.CursorLoader;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070042import android.content.IIntentSender;
Adam Powell33b97432010-04-20 10:01:14 -070043import android.content.Intent;
Dianne Hackbornfa82f222009-09-17 15:14:12 -070044import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.content.SharedPreferences;
46import android.content.pm.ActivityInfo;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -080047import android.content.pm.ApplicationInfo;
Adam Powelldd8fab22012-03-22 17:47:27 -070048import android.content.pm.PackageManager;
49import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.content.res.Configuration;
51import android.content.res.Resources;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -070052import android.content.res.TypedArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.database.Cursor;
54import android.graphics.Bitmap;
55import android.graphics.Canvas;
Winson2d476832016-02-17 14:53:46 -080056import android.graphics.Color;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -070057import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.graphics.drawable.Drawable;
59import android.media.AudioManager;
RoboErik55011652014-07-09 15:05:53 -070060import android.media.session.MediaController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.net.Uri;
Adam Powelld3c63a62016-06-09 12:36:16 -070062import android.os.BadParcelableException;
Dianne Hackborn8d374262009-09-14 21:21:52 -070063import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.os.Handler;
66import android.os.IBinder;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -080067import android.os.Looper;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070068import android.os.Parcelable;
Winson Chung1af8eda2016-02-05 17:55:56 +000069import android.os.PersistableBundle;
svetoslavganov75986cf2009-05-14 22:28:01 -070070import android.os.RemoteException;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060071import android.os.ServiceManager.ServiceNotFoundException;
Brad Fitzpatrick75803572011-01-13 14:21:03 -080072import android.os.StrictMode;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -080073import android.os.SystemProperties;
Dianne Hackbornf1c26e22012-08-23 13:54:58 -070074import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.text.Selection;
76import android.text.SpannableStringBuilder;
svetoslavganov75986cf2009-05-14 22:28:01 -070077import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.text.method.TextKeyListener;
Winson Chung1af8eda2016-02-05 17:55:56 +000079import android.transition.Scene;
80import android.transition.TransitionManager;
81import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.util.EventLog;
84import android.util.Log;
Jeff Brown5182c782013-10-15 20:31:52 -070085import android.util.PrintWriterPrinter;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070086import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.util.SparseArray;
Winson Chung1af8eda2016-02-05 17:55:56 +000088import android.util.SuperNotCalledException;
Adam Powell6e346362010-07-23 10:18:23 -070089import android.view.ActionMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.view.ContextMenu;
Adam Powell6e346362010-07-23 10:18:23 -070091import android.view.ContextMenu.ContextMenuInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092import android.view.ContextThemeWrapper;
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -070093import android.view.DragAndDropPermissions;
Winson Chung1af8eda2016-02-05 17:55:56 +000094import android.view.DragEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import android.view.KeyEvent;
Clara Bayarri75e09792015-07-29 16:20:40 +010096import android.view.KeyboardShortcutGroup;
97import android.view.KeyboardShortcutInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.view.LayoutInflater;
99import android.view.Menu;
100import android.view.MenuInflater;
101import android.view.MenuItem;
102import android.view.MotionEvent;
Tim Kilbourn6a975b32015-04-09 17:14:34 -0700103import android.view.SearchEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104import android.view.View;
Adam Powell6e346362010-07-23 10:18:23 -0700105import android.view.View.OnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106import android.view.ViewGroup;
Adam Powell6e346362010-07-23 10:18:23 -0700107import android.view.ViewGroup.LayoutParams;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108import android.view.ViewManager;
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +0000109import android.view.ViewRootImpl;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700110import android.view.ViewRootImpl.ActivityConfigCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import android.view.Window;
Winson Chung1af8eda2016-02-05 17:55:56 +0000112import android.view.Window.WindowControllerCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -0700114import android.view.WindowManagerGlobal;
svetoslavganov75986cf2009-05-14 22:28:01 -0700115import android.view.accessibility.AccessibilityEvent;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700116import android.view.autofill.AutofillManager;
117import android.view.autofill.AutofillPopupWindow;
118import android.view.autofill.IAutofillWindowPresenter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119import android.widget.AdapterView;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -0800120import android.widget.Toast;
Winson Chung1af8eda2016-02-05 17:55:56 +0000121import android.widget.Toolbar;
Clara Bayarri75e09792015-07-29 16:20:40 +0100122
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700123import com.android.internal.annotations.GuardedBy;
124import com.android.internal.app.IVoiceInteractor;
125import com.android.internal.app.ToolbarActionBar;
126import com.android.internal.app.WindowDecorActionBar;
127import com.android.internal.policy.DecorView;
128import com.android.internal.policy.PhoneWindow;
129
Dianne Hackborn625ac272010-09-17 18:29:22 -0700130import java.io.FileDescriptor;
131import java.io.PrintWriter;
Tor Norbyed9273d62013-05-30 15:59:53 -0700132import java.lang.annotation.Retention;
133import java.lang.annotation.RetentionPolicy;
Adam Powell6e346362010-07-23 10:18:23 -0700134import java.util.ArrayList;
135import java.util.HashMap;
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700136import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137
138/**
139 * An activity is a single, focused thing that the user can do. Almost all
140 * activities interact with the user, so the Activity class takes care of
141 * creating a window for you in which you can place your UI with
142 * {@link #setContentView}. While activities are often presented to the user
143 * as full-screen windows, they can also be used in other ways: as floating
144 * windows (via a theme with {@link android.R.attr#windowIsFloating} set)
145 * or embedded inside of another activity (using {@link ActivityGroup}).
146 *
147 * There are two methods almost all subclasses of Activity will implement:
RoboErik55011652014-07-09 15:05:53 -0700148 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 * <ul>
150 * <li> {@link #onCreate} is where you initialize your activity. Most
151 * importantly, here you will usually call {@link #setContentView(int)}
152 * with a layout resource defining your UI, and using {@link #findViewById}
153 * to retrieve the widgets in that UI that you need to interact with
154 * programmatically.
RoboErik55011652014-07-09 15:05:53 -0700155 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 * <li> {@link #onPause} is where you deal with the user leaving your
157 * activity. Most importantly, any changes made by the user should at this
158 * point be committed (usually to the
159 * {@link android.content.ContentProvider} holding the data).
160 * </ul>
161 *
162 * <p>To be of use with {@link android.content.Context#startActivity Context.startActivity()}, all
163 * activity classes must have a corresponding
164 * {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
165 * declaration in their package's <code>AndroidManifest.xml</code>.</p>
RoboErik55011652014-07-09 15:05:53 -0700166 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 * <p>Topics covered here:
168 * <ol>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700169 * <li><a href="#Fragments">Fragments</a>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 * <li><a href="#ActivityLifecycle">Activity Lifecycle</a>
171 * <li><a href="#ConfigurationChanges">Configuration Changes</a>
172 * <li><a href="#StartingActivities">Starting Activities and Getting Results</a>
173 * <li><a href="#SavingPersistentState">Saving Persistent State</a>
174 * <li><a href="#Permissions">Permissions</a>
175 * <li><a href="#ProcessLifecycle">Process Lifecycle</a>
176 * </ol>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700177 *
178 * <div class="special reference">
179 * <h3>Developer Guides</h3>
180 * <p>The Activity class is an important part of an application's overall lifecycle,
181 * and the way activities are launched and put together is a fundamental
182 * part of the platform's application model. For a detailed perspective on the structure of an
183 * Android application and how activities behave, please read the
184 * <a href="{@docRoot}guide/topics/fundamentals.html">Application Fundamentals</a> and
Mark Lufa434852016-08-11 17:40:33 -0700185 * <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back Stack</a>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700186 * developer guides.</p>
187 *
188 * <p>You can also find a detailed discussion about how to create activities in the
Mark Lufa434852016-08-11 17:40:33 -0700189 * <a href="{@docRoot}guide/components/activities.html">Activities</a>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700190 * developer guide.</p>
191 * </div>
192 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700193 * <a name="Fragments"></a>
194 * <h3>Fragments</h3>
195 *
196 * <p>Starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB}, Activity
197 * implementations can make use of the {@link Fragment} class to better
198 * modularize their code, build more sophisticated user interfaces for larger
199 * screens, and help scale their application between small and large screens.
200 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 * <a name="ActivityLifecycle"></a>
202 * <h3>Activity Lifecycle</h3>
203 *
204 * <p>Activities in the system are managed as an <em>activity stack</em>.
205 * When a new activity is started, it is placed on the top of the stack
206 * and becomes the running activity -- the previous activity always remains
207 * below it in the stack, and will not come to the foreground again until
208 * the new activity exits.</p>
RoboErik55011652014-07-09 15:05:53 -0700209 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 * <p>An activity has essentially four states:</p>
211 * <ul>
Trevor Johns682c24e2016-04-12 10:13:47 -0700212 * <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 -0800213 * the stack),
214 * it is <em>active</em> or <em>running</em>. </li>
215 * <li>If an activity has lost focus but is still visible (that is, a new non-full-sized
RoboErik55011652014-07-09 15:05:53 -0700216 * or transparent activity has focus on top of your activity), it
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 * is <em>paused</em>. A paused activity is completely alive (it
218 * maintains all state and member information and remains attached to
219 * the window manager), but can be killed by the system in extreme
220 * low memory situations.
221 * <li>If an activity is completely obscured by another activity,
222 * it is <em>stopped</em>. It still retains all state and member information,
223 * however, it is no longer visible to the user so its window is hidden
224 * and it will often be killed by the system when memory is needed
225 * elsewhere.</li>
226 * <li>If an activity is paused or stopped, the system can drop the activity
227 * from memory by either asking it to finish, or simply killing its
228 * process. When it is displayed again to the user, it must be
229 * completely restarted and restored to its previous state.</li>
230 * </ul>
231 *
232 * <p>The following diagram shows the important state paths of an Activity.
233 * The square rectangles represent callback methods you can implement to
234 * perform operations when the Activity moves between states. The colored
235 * ovals are major states the Activity can be in.</p>
RoboErik55011652014-07-09 15:05:53 -0700236 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 * <p><img src="../../../images/activity_lifecycle.png"
238 * alt="State diagram for an Android Activity Lifecycle." border="0" /></p>
RoboErik55011652014-07-09 15:05:53 -0700239 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 * <p>There are three key loops you may be interested in monitoring within your
241 * activity:
RoboErik55011652014-07-09 15:05:53 -0700242 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 * <ul>
244 * <li>The <b>entire lifetime</b> of an activity happens between the first call
245 * to {@link android.app.Activity#onCreate} through to a single final call
246 * to {@link android.app.Activity#onDestroy}. An activity will do all setup
247 * of "global" state in onCreate(), and release all remaining resources in
248 * onDestroy(). For example, if it has a thread running in the background
249 * to download data from the network, it may create that thread in onCreate()
250 * and then stop the thread in onDestroy().
RoboErik55011652014-07-09 15:05:53 -0700251 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 * <li>The <b>visible lifetime</b> of an activity happens between a call to
253 * {@link android.app.Activity#onStart} until a corresponding call to
254 * {@link android.app.Activity#onStop}. During this time the user can see the
255 * activity on-screen, though it may not be in the foreground and interacting
256 * with the user. Between these two methods you can maintain resources that
257 * are needed to show the activity to the user. For example, you can register
258 * a {@link android.content.BroadcastReceiver} in onStart() to monitor for changes
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900259 * that impact your UI, and unregister it in onStop() when the user no
260 * longer sees what you are displaying. The onStart() and onStop() methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 * can be called multiple times, as the activity becomes visible and hidden
262 * to the user.
RoboErik55011652014-07-09 15:05:53 -0700263 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 * <li>The <b>foreground lifetime</b> of an activity happens between a call to
265 * {@link android.app.Activity#onResume} until a corresponding call to
266 * {@link android.app.Activity#onPause}. During this time the activity is
267 * in front of all other activities and interacting with the user. An activity
268 * can frequently go between the resumed and paused states -- for example when
269 * the device goes to sleep, when an activity result is delivered, when a new
270 * intent is delivered -- so the code in these methods should be fairly
271 * lightweight.
272 * </ul>
RoboErik55011652014-07-09 15:05:53 -0700273 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 * <p>The entire lifecycle of an activity is defined by the following
275 * Activity methods. All of these are hooks that you can override
276 * to do appropriate work when the activity changes state. All
277 * activities will implement {@link android.app.Activity#onCreate}
278 * to do their initial setup; many will also implement
279 * {@link android.app.Activity#onPause} to commit changes to data and
280 * otherwise prepare to stop interacting with the user. You should always
281 * call up to your superclass when implementing these methods.</p>
282 *
283 * </p>
284 * <pre class="prettyprint">
285 * public class Activity extends ApplicationContext {
286 * protected void onCreate(Bundle savedInstanceState);
287 *
288 * protected void onStart();
RoboErik55011652014-07-09 15:05:53 -0700289 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 * protected void onRestart();
291 *
292 * protected void onResume();
293 *
294 * protected void onPause();
295 *
296 * protected void onStop();
297 *
298 * protected void onDestroy();
299 * }
300 * </pre>
301 *
302 * <p>In general the movement through an activity's lifecycle looks like
303 * this:</p>
304 *
305 * <table border="2" width="85%" align="center" frame="hsides" rules="rows">
306 * <colgroup align="left" span="3" />
307 * <colgroup align="left" />
308 * <colgroup align="center" />
309 * <colgroup align="center" />
310 *
311 * <thead>
312 * <tr><th colspan="3">Method</th> <th>Description</th> <th>Killable?</th> <th>Next</th></tr>
313 * </thead>
314 *
315 * <tbody>
smain@google.com10e29b82016-06-23 12:09:30 -0700316 * <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 -0800317 * <td>Called when the activity is first created.
318 * This is where you should do all of your normal static set up:
319 * create views, bind data to lists, etc. This method also
320 * provides you with a Bundle containing the activity's previously
321 * frozen state, if there was one.
322 * <p>Always followed by <code>onStart()</code>.</td>
323 * <td align="center">No</td>
324 * <td align="center"><code>onStart()</code></td>
325 * </tr>
326 *
327 * <tr><td rowspan="5" style="border-left: none; border-right: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
smain@google.com10e29b82016-06-23 12:09:30 -0700328 * <td colspan="2" align="left" border="0">{@link android.app.Activity#onRestart onRestart()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 * <td>Called after your activity has been stopped, prior to it being
330 * started again.
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 *
smain@google.com10e29b82016-06-23 12:09:30 -0700336 * <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 -0800337 * <td>Called when the activity is becoming visible to the user.
338 * <p>Followed by <code>onResume()</code> if the activity comes
339 * to the foreground, or <code>onStop()</code> if it becomes hidden.</td>
340 * <td align="center">No</td>
341 * <td align="center"><code>onResume()</code> or <code>onStop()</code></td>
342 * </tr>
343 *
344 * <tr><td rowspan="2" style="border-left: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
smain@google.com10e29b82016-06-23 12:09:30 -0700345 * <td align="left" border="0">{@link android.app.Activity#onResume onResume()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 * <td>Called when the activity will start
347 * interacting with the user. At this point your activity is at
348 * the top of the activity stack, with user input going to it.
349 * <p>Always followed by <code>onPause()</code>.</td>
350 * <td align="center">No</td>
351 * <td align="center"><code>onPause()</code></td>
352 * </tr>
353 *
smain@google.com10e29b82016-06-23 12:09:30 -0700354 * <tr><td align="left" border="0">{@link android.app.Activity#onPause onPause()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 * <td>Called when the system is about to start resuming a previous
356 * activity. This is typically used to commit unsaved changes to
357 * persistent data, stop animations and other things that may be consuming
358 * CPU, etc. Implementations of this method must be very quick because
359 * the next activity will not be resumed until this method returns.
360 * <p>Followed by either <code>onResume()</code> if the activity
361 * returns back to the front, or <code>onStop()</code> if it becomes
362 * invisible to the user.</td>
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800363 * <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 -0800364 * <td align="center"><code>onResume()</code> or<br>
365 * <code>onStop()</code></td>
366 * </tr>
367 *
smain@google.com10e29b82016-06-23 12:09:30 -0700368 * <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 -0800369 * <td>Called when the activity is no longer visible to the user, because
370 * another activity has been resumed and is covering this one. This
371 * may happen either because a new activity is being started, an existing
372 * one is being brought in front of this one, or this one is being
373 * destroyed.
374 * <p>Followed by either <code>onRestart()</code> if
375 * this activity is coming back to interact with the user, or
376 * <code>onDestroy()</code> if this activity is going away.</td>
377 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
378 * <td align="center"><code>onRestart()</code> or<br>
379 * <code>onDestroy()</code></td>
380 * </tr>
381 *
smain@google.com10e29b82016-06-23 12:09:30 -0700382 * <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 -0800383 * <td>The final call you receive before your
384 * activity is destroyed. This can happen either because the
385 * activity is finishing (someone called {@link Activity#finish} on
386 * it, or because the system is temporarily destroying this
387 * instance of the activity to save space. You can distinguish
388 * between these two scenarios with the {@link
389 * Activity#isFinishing} method.</td>
390 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
391 * <td align="center"><em>nothing</em></td>
392 * </tr>
393 * </tbody>
394 * </table>
395 *
396 * <p>Note the "Killable" column in the above table -- for those methods that
397 * are marked as being killable, after that method returns the process hosting the
Kris Giesing8b6216d2014-12-16 17:23:15 -0800398 * 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 -0800399 * of its code being executed. Because of this, you should use the
400 * {@link #onPause} method to write any persistent data (such as user edits)
401 * to storage. In addition, the method
402 * {@link #onSaveInstanceState(Bundle)} is called before placing the activity
403 * in such a background state, allowing you to save away any dynamic instance
404 * state in your activity into the given Bundle, to be later received in
RoboErik55011652014-07-09 15:05:53 -0700405 * {@link #onCreate} if the activity needs to be re-created.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 * See the <a href="#ProcessLifecycle">Process Lifecycle</a>
407 * section for more information on how the lifecycle of a process is tied
408 * to the activities it is hosting. Note that it is important to save
409 * persistent data in {@link #onPause} instead of {@link #onSaveInstanceState}
Daisuke Miyakawa5c40f3f2011-02-15 13:24:36 -0800410 * because the latter is not part of the lifecycle callbacks, so will not
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 * be called in every situation as described in its documentation.</p>
412 *
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800413 * <p class="note">Be aware that these semantics will change slightly between
414 * applications targeting platforms starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB}
415 * vs. those targeting prior platforms. Starting with Honeycomb, an application
416 * is not in the killable state until its {@link #onStop} has returned. This
417 * impacts when {@link #onSaveInstanceState(Bundle)} may be called (it may be
418 * safely called after {@link #onPause()} and allows and application to safely
419 * wait until {@link #onStop()} to save persistent state.</p>
420 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 * <p>For those methods that are not marked as being killable, the activity's
422 * process will not be killed by the system starting from the time the method
423 * is called and continuing after it returns. Thus an activity is in the killable
424 * state, for example, between after <code>onPause()</code> to the start of
425 * <code>onResume()</code>.</p>
426 *
427 * <a name="ConfigurationChanges"></a>
428 * <h3>Configuration Changes</h3>
RoboErik55011652014-07-09 15:05:53 -0700429 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 * <p>If the configuration of the device (as defined by the
431 * {@link Configuration Resources.Configuration} class) changes,
432 * then anything displaying a user interface will need to update to match that
433 * configuration. Because Activity is the primary mechanism for interacting
434 * with the user, it includes special support for handling configuration
435 * changes.</p>
RoboErik55011652014-07-09 15:05:53 -0700436 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 * <p>Unless you specify otherwise, a configuration change (such as a change
438 * in screen orientation, language, input devices, etc) will cause your
439 * current activity to be <em>destroyed</em>, going through the normal activity
440 * lifecycle process of {@link #onPause},
441 * {@link #onStop}, and {@link #onDestroy} as appropriate. If the activity
442 * had been in the foreground or visible to the user, once {@link #onDestroy} is
443 * called in that instance then a new instance of the activity will be
444 * created, with whatever savedInstanceState the previous instance had generated
445 * from {@link #onSaveInstanceState}.</p>
RoboErik55011652014-07-09 15:05:53 -0700446 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 * <p>This is done because any application resource,
448 * including layout files, can change based on any configuration value. Thus
449 * the only safe way to handle a configuration change is to re-retrieve all
450 * resources, including layouts, drawables, and strings. Because activities
451 * must already know how to save their state and re-create themselves from
452 * that state, this is a convenient way to have an activity restart itself
453 * with a new configuration.</p>
RoboErik55011652014-07-09 15:05:53 -0700454 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 * <p>In some special cases, you may want to bypass restarting of your
456 * activity based on one or more types of configuration changes. This is
457 * done with the {@link android.R.attr#configChanges android:configChanges}
458 * attribute in its manifest. For any types of configuration changes you say
459 * that you handle there, you will receive a call to your current activity's
460 * {@link #onConfigurationChanged} method instead of being restarted. If
461 * a configuration change involves any that you do not handle, however, the
462 * activity will still be restarted and {@link #onConfigurationChanged}
463 * will not be called.</p>
RoboErik55011652014-07-09 15:05:53 -0700464 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 * <a name="StartingActivities"></a>
466 * <h3>Starting Activities and Getting Results</h3>
467 *
468 * <p>The {@link android.app.Activity#startActivity}
469 * method is used to start a
470 * new activity, which will be placed at the top of the activity stack. It
471 * takes a single argument, an {@link android.content.Intent Intent},
472 * which describes the activity
473 * to be executed.</p>
474 *
475 * <p>Sometimes you want to get a result back from an activity when it
476 * ends. For example, you may start an activity that lets the user pick
477 * a person in a list of contacts; when it ends, it returns the person
478 * that was selected. To do this, you call the
RoboErik55011652014-07-09 15:05:53 -0700479 * {@link android.app.Activity#startActivityForResult(Intent, int)}
480 * version with a second integer parameter identifying the call. The result
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 * will come back through your {@link android.app.Activity#onActivityResult}
RoboErik55011652014-07-09 15:05:53 -0700482 * method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 *
484 * <p>When an activity exits, it can call
485 * {@link android.app.Activity#setResult(int)}
486 * to return data back to its parent. It must always supply a result code,
487 * which can be the standard results RESULT_CANCELED, RESULT_OK, or any
488 * custom values starting at RESULT_FIRST_USER. In addition, it can optionally
489 * return back an Intent containing any additional data it wants. All of this
490 * information appears back on the
491 * parent's <code>Activity.onActivityResult()</code>, along with the integer
492 * identifier it originally supplied.</p>
493 *
494 * <p>If a child activity fails for any reason (such as crashing), the parent
495 * activity will receive a result with the code RESULT_CANCELED.</p>
496 *
497 * <pre class="prettyprint">
498 * public class MyActivity extends Activity {
499 * ...
500 *
501 * static final int PICK_CONTACT_REQUEST = 0;
502 *
Michael Wright5438e4e2014-07-25 14:05:42 -0700503 * public boolean onKeyDown(int keyCode, KeyEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 * if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
505 * // When the user center presses, let them pick a contact.
506 * startActivityForResult(
507 * new Intent(Intent.ACTION_PICK,
508 * new Uri("content://contacts")),
509 * PICK_CONTACT_REQUEST);
510 * return true;
511 * }
512 * return false;
513 * }
514 *
515 * protected void onActivityResult(int requestCode, int resultCode,
516 * Intent data) {
517 * if (requestCode == PICK_CONTACT_REQUEST) {
518 * if (resultCode == RESULT_OK) {
519 * // A contact was picked. Here we will just display it
520 * // to the user.
521 * startActivity(new Intent(Intent.ACTION_VIEW, data));
522 * }
523 * }
524 * }
525 * }
526 * </pre>
527 *
528 * <a name="SavingPersistentState"></a>
529 * <h3>Saving Persistent State</h3>
530 *
531 * <p>There are generally two kinds of persistent state than an activity
532 * will deal with: shared document-like data (typically stored in a SQLite
533 * database using a {@linkplain android.content.ContentProvider content provider})
534 * and internal state such as user preferences.</p>
535 *
536 * <p>For content provider data, we suggest that activities use a
537 * "edit in place" user model. That is, any edits a user makes are effectively
538 * made immediately without requiring an additional confirmation step.
539 * Supporting this model is generally a simple matter of following two rules:</p>
540 *
541 * <ul>
542 * <li> <p>When creating a new document, the backing database entry or file for
543 * it is created immediately. For example, if the user chooses to write
544 * a new e-mail, a new entry for that e-mail is created as soon as they
545 * start entering data, so that if they go to any other activity after
546 * that point this e-mail will now appear in the list of drafts.</p>
547 * <li> <p>When an activity's <code>onPause()</code> method is called, it should
548 * commit to the backing content provider or file any changes the user
549 * has made. This ensures that those changes will be seen by any other
550 * activity that is about to run. You will probably want to commit
551 * your data even more aggressively at key times during your
552 * activity's lifecycle: for example before starting a new
553 * activity, before finishing your own activity, when the user
554 * switches between input fields, etc.</p>
555 * </ul>
556 *
557 * <p>This model is designed to prevent data loss when a user is navigating
558 * between activities, and allows the system to safely kill an activity (because
559 * system resources are needed somewhere else) at any time after it has been
560 * paused. Note this implies
561 * that the user pressing BACK from your activity does <em>not</em>
562 * mean "cancel" -- it means to leave the activity with its current contents
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800563 * saved away. Canceling edits in an activity must be provided through
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 * some other mechanism, such as an explicit "revert" or "undo" option.</p>
565 *
566 * <p>See the {@linkplain android.content.ContentProvider content package} for
567 * more information about content providers. These are a key aspect of how
568 * different activities invoke and propagate data between themselves.</p>
569 *
570 * <p>The Activity class also provides an API for managing internal persistent state
571 * associated with an activity. This can be used, for example, to remember
572 * the user's preferred initial display in a calendar (day view or week view)
573 * or the user's default home page in a web browser.</p>
574 *
575 * <p>Activity persistent state is managed
576 * with the method {@link #getPreferences},
577 * allowing you to retrieve and
578 * modify a set of name/value pairs associated with the activity. To use
579 * preferences that are shared across multiple application components
580 * (activities, receivers, services, providers), you can use the underlying
581 * {@link Context#getSharedPreferences Context.getSharedPreferences()} method
582 * to retrieve a preferences
583 * object stored under a specific name.
584 * (Note that it is not possible to share settings data across application
585 * packages -- for that you will need a content provider.)</p>
586 *
587 * <p>Here is an excerpt from a calendar activity that stores the user's
588 * preferred view mode in its persistent settings:</p>
589 *
590 * <pre class="prettyprint">
591 * public class CalendarActivity extends Activity {
592 * ...
593 *
594 * static final int DAY_VIEW_MODE = 0;
595 * static final int WEEK_VIEW_MODE = 1;
596 *
597 * private SharedPreferences mPrefs;
598 * private int mCurViewMode;
599 *
600 * protected void onCreate(Bundle savedInstanceState) {
601 * super.onCreate(savedInstanceState);
602 *
603 * SharedPreferences mPrefs = getSharedPreferences();
Quddus Chong3fa98cb2012-04-13 11:17:24 -0700604 * mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 * }
606 *
607 * protected void onPause() {
608 * super.onPause();
RoboErik55011652014-07-09 15:05:53 -0700609 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 * SharedPreferences.Editor ed = mPrefs.edit();
611 * ed.putInt("view_mode", mCurViewMode);
612 * ed.commit();
613 * }
614 * }
615 * </pre>
RoboErik55011652014-07-09 15:05:53 -0700616 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 * <a name="Permissions"></a>
618 * <h3>Permissions</h3>
RoboErik55011652014-07-09 15:05:53 -0700619 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 * <p>The ability to start a particular Activity can be enforced when it is
621 * declared in its
622 * manifest's {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
623 * tag. By doing so, other applications will need to declare a corresponding
624 * {@link android.R.styleable#AndroidManifestUsesPermission &lt;uses-permission&gt;}
625 * element in their own manifest to be able to start that activity.
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800626 *
627 * <p>When starting an Activity you can set {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
628 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
629 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} on the Intent. This will grant the
630 * Activity access to the specific URIs in the Intent. Access will remain
631 * until the Activity has finished (it will remain across the hosting
632 * process being killed and other temporary destruction). As of
633 * {@link android.os.Build.VERSION_CODES#GINGERBREAD}, if the Activity
634 * was already created and a new Intent is being delivered to
635 * {@link #onNewIntent(Intent)}, any newly granted URI permissions will be added
636 * to the existing ones it holds.
637 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 * <p>See the <a href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a>
639 * document for more information on permissions and security in general.
RoboErik55011652014-07-09 15:05:53 -0700640 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 * <a name="ProcessLifecycle"></a>
642 * <h3>Process Lifecycle</h3>
RoboErik55011652014-07-09 15:05:53 -0700643 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 * <p>The Android system attempts to keep application process around for as
645 * long as possible, but eventually will need to remove old processes when
646 * memory runs low. As described in <a href="#ActivityLifecycle">Activity
647 * Lifecycle</a>, the decision about which process to remove is intimately
648 * tied to the state of the user's interaction with it. In general, there
649 * are four states a process can be in based on the activities running in it,
650 * listed here in order of importance. The system will kill less important
651 * processes (the last ones) before it resorts to killing more important
652 * processes (the first ones).
RoboErik55011652014-07-09 15:05:53 -0700653 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 * <ol>
655 * <li> <p>The <b>foreground activity</b> (the activity at the top of the screen
656 * that the user is currently interacting with) is considered the most important.
657 * Its process will only be killed as a last resort, if it uses more memory
658 * than is available on the device. Generally at this point the device has
659 * reached a memory paging state, so this is required in order to keep the user
660 * interface responsive.
661 * <li> <p>A <b>visible activity</b> (an activity that is visible to the user
662 * but not in the foreground, such as one sitting behind a foreground dialog)
663 * is considered extremely important and will not be killed unless that is
664 * required to keep the foreground activity running.
665 * <li> <p>A <b>background activity</b> (an activity that is not visible to
666 * the user and has been paused) is no longer critical, so the system may
667 * safely kill its process to reclaim memory for other foreground or
668 * visible processes. If its process needs to be killed, when the user navigates
669 * back to the activity (making it visible on the screen again), its
670 * {@link #onCreate} method will be called with the savedInstanceState it had previously
671 * supplied in {@link #onSaveInstanceState} so that it can restart itself in the same
672 * state as the user last left it.
673 * <li> <p>An <b>empty process</b> is one hosting no activities or other
674 * application components (such as {@link Service} or
675 * {@link android.content.BroadcastReceiver} classes). These are killed very
676 * quickly by the system as memory becomes low. For this reason, any
677 * background operation you do outside of an activity must be executed in the
678 * context of an activity BroadcastReceiver or Service to ensure that the system
679 * knows it needs to keep your process around.
680 * </ol>
RoboErik55011652014-07-09 15:05:53 -0700681 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 * <p>Sometimes an Activity may need to do a long-running operation that exists
683 * independently of the activity lifecycle itself. An example may be a camera
684 * application that allows you to upload a picture to a web site. The upload
685 * may take a long time, and the application should allow the user to leave
Trevor Johns682c24e2016-04-12 10:13:47 -0700686 * the application while it is executing. To accomplish this, your Activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 * should start a {@link Service} in which the upload takes place. This allows
688 * the system to properly prioritize your process (considering it to be more
689 * important than other non-visible applications) for the duration of the
690 * upload, independent of whether the original activity is paused, stopped,
691 * or finished.
692 */
693public class Activity extends ContextThemeWrapper
Dianne Hackborn625ac272010-09-17 18:29:22 -0700694 implements LayoutInflater.Factory2,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 Window.Callback, KeyEvent.Callback,
Adam Powell117b6952014-05-05 18:14:56 -0700696 OnCreateContextMenuListener, ComponentCallbacks2,
Svet Ganov782043c2017-02-11 00:52:02 +0000697 Window.OnWindowDismissedCallback, WindowControllerCallback,
Felipe Leme640f30a2017-03-06 15:44:06 -0800698 AutofillManager.AutofillClient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 private static final String TAG = "Activity";
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700700 private static final boolean DEBUG_LIFECYCLE = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701
702 /** Standard activity result: operation canceled. */
703 public static final int RESULT_CANCELED = 0;
704 /** Standard activity result: operation succeeded. */
705 public static final int RESULT_OK = -1;
706 /** Start of user-defined activity results. */
707 public static final int RESULT_FIRST_USER = 1;
708
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700709 /** @hide Task isn't finished when activity is finished */
710 public static final int DONT_FINISH_TASK_WITH_ACTIVITY = 0;
Filip Gruszczynskic29ecc82015-10-08 11:08:44 -0700711 /**
712 * @hide Task is finished if the finishing activity is the root of the task. To preserve the
713 * past behavior the task is also removed from recents.
714 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700715 public static final int FINISH_TASK_WITH_ROOT_ACTIVITY = 1;
Filip Gruszczynskic29ecc82015-10-08 11:08:44 -0700716 /**
717 * @hide Task is finished along with the finishing activity, but it is not removed from
718 * recents.
719 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700720 public static final int FINISH_TASK_WITH_ACTIVITY = 2;
721
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700722 static final String FRAGMENTS_TAG = "android:fragments";
Phil Weaver846cda932017-06-15 10:10:06 -0700723 private static final String LAST_AUTOFILL_ID = "android:lastAutofillId";
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700724
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700725 private static final String AUTOFILL_RESET_NEEDED = "@android:autofillResetNeeded";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 private static final String WINDOW_HIERARCHY_TAG = "android:viewHierarchyState";
727 private static final String SAVED_DIALOG_IDS_KEY = "android:savedDialogIds";
728 private static final String SAVED_DIALOGS_TAG = "android:savedDialogs";
729 private static final String SAVED_DIALOG_KEY_PREFIX = "android:dialog_";
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800730 private static final String SAVED_DIALOG_ARGS_KEY_PREFIX = "android:dialog_args_";
Svetoslavffb32b12015-10-15 16:54:00 -0700731 private static final String HAS_CURENT_PERMISSIONS_REQUEST_KEY =
732 "android:hasCurrentPermissionsRequest";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733
Svetoslav970b59c2015-06-09 16:05:21 -0700734 private static final String REQUEST_PERMISSIONS_WHO_PREFIX = "@android:requestPermissions:";
Svet Ganov782043c2017-02-11 00:52:02 +0000735 private static final String AUTO_FILL_AUTH_WHO_PREFIX = "@android:autoFillAuth:";
Svetoslav970b59c2015-06-09 16:05:21 -0700736
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +0100737 private static final String KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME = "com.android.systemui";
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +0100738
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800739 private static class ManagedDialog {
740 Dialog mDialog;
741 Bundle mArgs;
742 }
743 private SparseArray<ManagedDialog> mManagedDialogs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744
745 // set by the thread after the constructor and before onCreate(Bundle savedInstanceState) is called.
746 private Instrumentation mInstrumentation;
747 private IBinder mToken;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700748 private int mIdent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 /*package*/ String mEmbeddedID;
750 private Application mApplication;
Christopher Tateb70f3df2009-04-07 16:07:59 -0700751 /*package*/ Intent mIntent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800752 /*package*/ String mReferrer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 private ComponentName mComponent;
754 /*package*/ ActivityInfo mActivityInfo;
755 /*package*/ ActivityThread mMainThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 Activity mParent;
757 boolean mCalled;
Jeff Hamilton52d32032011-01-08 15:31:26 -0600758 /*package*/ boolean mResumed;
Andrii Kulian58178f22016-03-16 13:44:56 -0700759 /*package*/ boolean mStopped;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 boolean mFinished;
761 boolean mStartedActivity;
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -0700762 private boolean mDestroyed;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700763 private boolean mDoReportFullyDrawn = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700764 /** true if the activity is going through a transient pause */
765 /*package*/ boolean mTemporaryPause = false;
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -0500766 /** true if the activity is being destroyed in order to recreate it with a new configuration */
767 /*package*/ boolean mChangingConfigurations = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 /*package*/ int mConfigChangeFlags;
769 /*package*/ Configuration mCurrentConfig;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100770 private SearchManager mSearchManager;
Adam Powell88ab6972011-07-28 11:25:01 -0700771 private MenuInflater mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700773 /** The autofill manager. Always access via {@link #getAutofillManager()}. */
774 @Nullable private AutofillManager mAutofillManager;
775
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700776 static final class NonConfigurationInstances {
777 Object activity;
778 HashMap<String, Object> children;
Adam Powell44ba79e2016-02-04 16:20:37 -0800779 FragmentManagerNonConfig fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700780 ArrayMap<String, LoaderManager> loaders;
Dianne Hackborn20d94742014-05-29 18:35:45 -0700781 VoiceInteractor voiceInteractor;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700782 }
783 /* package */ NonConfigurationInstances mLastNonConfigurationInstances;
RoboErik55011652014-07-09 15:05:53 -0700784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 private Window mWindow;
786
787 private WindowManager mWindowManager;
788 /*package*/ View mDecor = null;
789 /*package*/ boolean mWindowAdded = false;
790 /*package*/ boolean mVisibleFromServer = false;
791 /*package*/ boolean mVisibleFromClient = true;
Adam Powelle43340c2014-03-17 19:10:43 -0700792 /*package*/ ActionBar mActionBar = null;
Adam Powelldd8fab22012-03-22 17:47:27 -0700793 private boolean mEnableDefaultActionBarUp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794
Dianne Hackborn91097de2014-04-04 18:02:06 -0700795 private VoiceInteractor mVoiceInteractor;
796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 private CharSequence mTitle;
798 private int mTitleColor = 0;
799
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700800 // we must have a handler before the FragmentController is constructed
801 final Handler mHandler = new Handler();
802 final FragmentController mFragments = FragmentController.createController(new HostCallbacks());
Craig Mautneree2e45a2014-06-27 12:10:03 -0700803
Jose Lima4b6c6692014-08-12 17:41:12 -0700804 // Most recent call to requestVisibleBehind().
Bryce Leed59629e2017-04-18 14:35:29 -0700805 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -0700806 boolean mVisibleBehind;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 private static final class ManagedCursor {
809 ManagedCursor(Cursor cursor) {
810 mCursor = cursor;
811 mReleased = false;
812 mUpdated = false;
813 }
814
815 private final Cursor mCursor;
816 private boolean mReleased;
817 private boolean mUpdated;
818 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819
Felipe Leme29a5b0d2016-10-25 14:57:11 -0700820 @GuardedBy("mManagedCursors")
821 private final ArrayList<ManagedCursor> mManagedCursors = new ArrayList<>();
822
823 @GuardedBy("this")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 int mResultCode = RESULT_CANCELED;
Felipe Leme29a5b0d2016-10-25 14:57:11 -0700825 @GuardedBy("this")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 Intent mResultData = null;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700827
Craig Mautner5eda9b32013-07-02 11:58:16 -0700828 private TranslucentConversionListener mTranslucentCallback;
Craig Mautnerbc57cd12013-08-19 15:47:42 -0700829 private boolean mChangeCanvasToTranslucent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830
Tim Kilbourn6a975b32015-04-09 17:14:34 -0700831 private SearchEvent mSearchEvent;
832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 private boolean mTitleReady = false;
Clara Bayarri4423d912015-03-02 19:42:48 +0000834 private int mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835
836 private int mDefaultKeyMode = DEFAULT_KEYS_DISABLE;
837 private SpannableStringBuilder mDefaultKeySsb = null;
RoboErik55011652014-07-09 15:05:53 -0700838
Winsonb6403152016-02-23 13:32:09 -0800839 private ActivityManager.TaskDescription mTaskDescription =
840 new ActivityManager.TaskDescription();
841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 protected static final int[] FOCUSED_STATE_SET = {com.android.internal.R.attr.state_focused};
843
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700844 @SuppressWarnings("unused")
Jeff Brown7e442832011-06-10 18:00:16 -0700845 private final Object mInstanceTracker = StrictMode.trackActivity(this);
846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 private Thread mUiThread;
George Mount62ab9b72014-05-02 13:51:17 -0700848
George Mount1fecfb22014-06-18 14:55:55 -0700849 ActivityTransitionState mActivityTransitionState = new ActivityTransitionState();
George Mount65580562014-08-29 08:15:48 -0700850 SharedElementCallback mEnterTransitionListener = SharedElementCallback.NULL_CALLBACK;
851 SharedElementCallback mExitTransitionListener = SharedElementCallback.NULL_CALLBACK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852
Svetoslavffb32b12015-10-15 16:54:00 -0700853 private boolean mHasCurrentPermissionsRequest;
854
Svet Ganov17db9dc2017-02-21 19:54:31 -0800855 private boolean mAutoFillResetNeeded;
856
Phil Weaver846cda932017-06-15 10:10:06 -0700857 /** The last autofill id that was returned from {@link #getNextAutofillId()} */
858 private int mLastAutofillId = View.LAST_APP_AUTOFILL_ID;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700859
Felipe Leme4753bb02017-03-22 20:24:00 -0700860 private AutofillPopupWindow mAutofillPopupWindow;
861
Dimitry Ivanov4449ef52016-02-25 17:41:13 -0800862 private static native String getDlWarning();
863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 /** Return the intent that started this activity. */
865 public Intent getIntent() {
866 return mIntent;
867 }
868
RoboErik55011652014-07-09 15:05:53 -0700869 /**
870 * Change the intent returned by {@link #getIntent}. This holds a
871 * reference to the given intent; it does not copy it. Often used in
872 * conjunction with {@link #onNewIntent}.
873 *
874 * @param newIntent The new Intent object to return from getIntent
875 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 * @see #getIntent
877 * @see #onNewIntent
RoboErik55011652014-07-09 15:05:53 -0700878 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 public void setIntent(Intent newIntent) {
880 mIntent = newIntent;
881 }
882
883 /** Return the application that owns this activity. */
884 public final Application getApplication() {
885 return mApplication;
886 }
887
888 /** Is this activity embedded inside of another activity? */
889 public final boolean isChild() {
890 return mParent != null;
891 }
RoboErik55011652014-07-09 15:05:53 -0700892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 /** Return the parent activity if this view is an embedded child. */
894 public final Activity getParent() {
895 return mParent;
896 }
897
898 /** Retrieve the window manager for showing custom windows. */
899 public WindowManager getWindowManager() {
900 return mWindowManager;
901 }
902
903 /**
904 * Retrieve the current {@link android.view.Window} for the activity.
905 * This can be used to directly access parts of the Window API that
906 * are not available through Activity/Screen.
RoboErik55011652014-07-09 15:05:53 -0700907 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 * @return Window The current window, or null if the activity is not
909 * visual.
910 */
911 public Window getWindow() {
912 return mWindow;
913 }
914
915 /**
Mark Doliner9525f2a2014-01-02 11:17:47 -0800916 * Return the LoaderManager for this activity, creating it if needed.
Dianne Hackbornc8017682010-07-06 13:34:38 -0700917 */
918 public LoaderManager getLoaderManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700919 return mFragments.getLoaderManager();
Dianne Hackbornc8017682010-07-06 13:34:38 -0700920 }
RoboErik55011652014-07-09 15:05:53 -0700921
Dianne Hackbornc8017682010-07-06 13:34:38 -0700922 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 * Calls {@link android.view.Window#getCurrentFocus} on the
924 * Window of this Activity to return the currently focused view.
RoboErik55011652014-07-09 15:05:53 -0700925 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 * @return View The current View with focus or null.
RoboErik55011652014-07-09 15:05:53 -0700927 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 * @see #getWindow
929 * @see android.view.Window#getCurrentFocus
930 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700931 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 public View getCurrentFocus() {
933 return mWindow != null ? mWindow.getCurrentFocus() : null;
934 }
935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 /**
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700937 * (Create and) return the autofill manager
938 *
939 * @return The autofill manager
940 */
941 @NonNull private AutofillManager getAutofillManager() {
942 if (mAutofillManager == null) {
943 mAutofillManager = getSystemService(AutofillManager.class);
944 }
945
946 return mAutofillManager;
947 }
948
949 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 * Called when the activity is starting. This is where most initialization
951 * should go: calling {@link #setContentView(int)} to inflate the
952 * activity's UI, using {@link #findViewById} to programmatically interact
953 * with widgets in the UI, calling
954 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)} to retrieve
955 * cursors for data being displayed, etc.
RoboErik55011652014-07-09 15:05:53 -0700956 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 * <p>You can call {@link #finish} from within this function, in
958 * which case onDestroy() will be immediately called without any of the rest
959 * of the activity lifecycle ({@link #onStart}, {@link #onResume},
960 * {@link #onPause}, etc) executing.
RoboErik55011652014-07-09 15:05:53 -0700961 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 * <p><em>Derived classes must call through to the super class's
963 * implementation of this method. If they do not, an exception will be
964 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -0700965 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 * @param savedInstanceState If the activity is being re-initialized after
967 * previously being shut down then this Bundle contains the data it most
968 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
RoboErik55011652014-07-09 15:05:53 -0700969 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 * @see #onStart
971 * @see #onSaveInstanceState
972 * @see #onRestoreInstanceState
973 * @see #onPostCreate
974 */
Tor Norbye83c68962015-03-10 20:55:31 -0700975 @MainThread
Tor Norbyec615c6f2015-03-02 10:11:44 -0800976 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -0700977 protected void onCreate(@Nullable Bundle savedInstanceState) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700978 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState);
Bryce Lee39791592017-04-26 09:29:12 -0700979
Bryce Leed1ac18c2017-05-11 07:59:41 -0700980 if (getApplicationInfo().targetSdkVersion > O && mActivityInfo.isFixedOrientation()) {
Bryce Lee39791592017-04-26 09:29:12 -0700981 final TypedArray ta = obtainStyledAttributes(com.android.internal.R.styleable.Window);
982 final boolean isTranslucentOrFloating = ActivityInfo.isTranslucentOrFloating(ta);
983 ta.recycle();
984
985 if (isTranslucentOrFloating) {
986 throw new IllegalStateException(
987 "Only fullscreen opaque activities can request orientation");
988 }
989 }
990
Dianne Hackborn2707d602010-07-09 18:01:20 -0700991 if (mLastNonConfigurationInstances != null) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700992 mFragments.restoreLoaderNonConfig(mLastNonConfigurationInstances.loaders);
Dianne Hackborn2707d602010-07-09 18:01:20 -0700993 }
Adam Powelldd8fab22012-03-22 17:47:27 -0700994 if (mActivityInfo.parentActivityName != null) {
995 if (mActionBar == null) {
996 mEnableDefaultActionBarUp = true;
997 } else {
998 mActionBar.setDefaultDisplayHomeAsUpEnabled(true);
999 }
1000 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001001 if (savedInstanceState != null) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001002 mAutoFillResetNeeded = savedInstanceState.getBoolean(AUTOFILL_RESET_NEEDED, false);
Philip P. Moltmanne78c7712017-06-19 12:57:13 -07001003 mLastAutofillId = savedInstanceState.getInt(LAST_AUTOFILL_ID,
1004 View.LAST_APP_AUTOFILL_ID);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001005
1006 if (mAutoFillResetNeeded) {
1007 getAutofillManager().onCreate(savedInstanceState);
1008 }
1009
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001010 Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
1011 mFragments.restoreAllState(p, mLastNonConfigurationInstances != null
1012 ? mLastNonConfigurationInstances.fragments : null);
1013 }
1014 mFragments.dispatchCreate();
Narayan Kamathff5659f2017-02-02 13:31:33 +00001015 getApplication().dispatchActivityCreated(this, savedInstanceState);
Dianne Hackborn20d94742014-05-29 18:35:45 -07001016 if (mVoiceInteractor != null) {
1017 mVoiceInteractor.attachActivity(this);
1018 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 mCalled = true;
1020 }
1021
1022 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001023 * Same as {@link #onCreate(android.os.Bundle)} but called for those activities created with
Craig Mautner43e52ed2014-06-16 17:18:52 -07001024 * the attribute {@link android.R.attr#persistableMode} set to
1025 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -07001026 *
1027 * @param savedInstanceState if the activity is being re-initialized after
1028 * previously being shut down then this Bundle contains the data it most
1029 * recently supplied in {@link #onSaveInstanceState}.
1030 * <b><i>Note: Otherwise it is null.</i></b>
1031 * @param persistentState if the activity is being re-initialized after
1032 * previously being shut down or powered off then this Bundle contains the data it most
1033 * recently supplied to outPersistentState in {@link #onSaveInstanceState}.
1034 * <b><i>Note: Otherwise it is null.</i></b>
1035 *
1036 * @see #onCreate(android.os.Bundle)
1037 * @see #onStart
1038 * @see #onSaveInstanceState
1039 * @see #onRestoreInstanceState
1040 * @see #onPostCreate
1041 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001042 public void onCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001043 @Nullable PersistableBundle persistentState) {
1044 onCreate(savedInstanceState);
1045 }
1046
1047 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 * The hook for {@link ActivityThread} to restore the state of this activity.
1049 *
1050 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
1051 * {@link #restoreManagedDialogs(android.os.Bundle)}.
1052 *
1053 * @param savedInstanceState contains the saved state
1054 */
1055 final void performRestoreInstanceState(Bundle savedInstanceState) {
1056 onRestoreInstanceState(savedInstanceState);
1057 restoreManagedDialogs(savedInstanceState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 }
1059
1060 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001061 * The hook for {@link ActivityThread} to restore the state of this activity.
1062 *
1063 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
1064 * {@link #restoreManagedDialogs(android.os.Bundle)}.
1065 *
1066 * @param savedInstanceState contains the saved state
1067 * @param persistentState contains the persistable saved state
1068 */
1069 final void performRestoreInstanceState(Bundle savedInstanceState,
1070 PersistableBundle persistentState) {
1071 onRestoreInstanceState(savedInstanceState, persistentState);
1072 if (savedInstanceState != null) {
1073 restoreManagedDialogs(savedInstanceState);
1074 }
1075 }
1076
1077 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 * This method is called after {@link #onStart} when the activity is
1079 * being re-initialized from a previously saved state, given here in
Mike LeBeau305de9d2010-03-11 09:21:08 -08001080 * <var>savedInstanceState</var>. Most implementations will simply use {@link #onCreate}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 * to restore their state, but it is sometimes convenient to do it here
1082 * after all of the initialization has been done or to allow subclasses to
1083 * decide whether to use your default implementation. The default
1084 * implementation of this method performs a restore of any view state that
1085 * had previously been frozen by {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001086 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 * <p>This method is called between {@link #onStart} and
1088 * {@link #onPostCreate}.
RoboErik55011652014-07-09 15:05:53 -07001089 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001091 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 * @see #onCreate
1093 * @see #onPostCreate
1094 * @see #onResume
1095 * @see #onSaveInstanceState
1096 */
1097 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1098 if (mWindow != null) {
1099 Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG);
1100 if (windowState != null) {
1101 mWindow.restoreHierarchyState(windowState);
1102 }
1103 }
1104 }
Craig Mautnera0026042014-04-23 11:45:37 -07001105
1106 /**
1107 * This is the same as {@link #onRestoreInstanceState(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001108 * created with the attribute {@link android.R.attr#persistableMode} set to
1109 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1110 * came from the restored PersistableBundle first
Craig Mautnera0026042014-04-23 11:45:37 -07001111 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1112 *
1113 * <p>This method is called between {@link #onStart} and
1114 * {@link #onPostCreate}.
1115 *
1116 * <p>If this method is called {@link #onRestoreInstanceState(Bundle)} will not be called.
1117 *
1118 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
1119 * @param persistentState the data most recently supplied in {@link #onSaveInstanceState}.
1120 *
1121 * @see #onRestoreInstanceState(Bundle)
1122 * @see #onCreate
1123 * @see #onPostCreate
1124 * @see #onResume
1125 * @see #onSaveInstanceState
1126 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001127 public void onRestoreInstanceState(Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001128 PersistableBundle persistentState) {
1129 if (savedInstanceState != null) {
1130 onRestoreInstanceState(savedInstanceState);
1131 }
1132 }
1133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 /**
1135 * Restore the state of any saved managed dialogs.
1136 *
1137 * @param savedInstanceState The bundle to restore from.
1138 */
1139 private void restoreManagedDialogs(Bundle savedInstanceState) {
1140 final Bundle b = savedInstanceState.getBundle(SAVED_DIALOGS_TAG);
1141 if (b == null) {
1142 return;
1143 }
1144
1145 final int[] ids = b.getIntArray(SAVED_DIALOG_IDS_KEY);
1146 final int numDialogs = ids.length;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001147 mManagedDialogs = new SparseArray<ManagedDialog>(numDialogs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 for (int i = 0; i < numDialogs; i++) {
1149 final Integer dialogId = ids[i];
1150 Bundle dialogState = b.getBundle(savedDialogKeyFor(dialogId));
1151 if (dialogState != null) {
Romain Guye35c2352009-06-19 13:18:12 -07001152 // Calling onRestoreInstanceState() below will invoke dispatchOnCreate
1153 // so tell createDialog() not to do it, otherwise we get an exception
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001154 final ManagedDialog md = new ManagedDialog();
1155 md.mArgs = b.getBundle(savedDialogArgsKeyFor(dialogId));
1156 md.mDialog = createDialog(dialogId, dialogState, md.mArgs);
1157 if (md.mDialog != null) {
1158 mManagedDialogs.put(dialogId, md);
1159 onPrepareDialog(dialogId, md.mDialog, md.mArgs);
1160 md.mDialog.onRestoreInstanceState(dialogState);
1161 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 }
1163 }
1164 }
1165
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001166 private Dialog createDialog(Integer dialogId, Bundle state, Bundle args) {
1167 final Dialog dialog = onCreateDialog(dialogId, args);
Romain Guy764d5332009-06-17 16:52:22 -07001168 if (dialog == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001169 return null;
Romain Guy764d5332009-06-17 16:52:22 -07001170 }
Romain Guy6de4aed2009-07-08 10:54:45 -07001171 dialog.dispatchOnCreate(state);
Romain Guy764d5332009-06-17 16:52:22 -07001172 return dialog;
1173 }
1174
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001175 private static String savedDialogKeyFor(int key) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 return SAVED_DIALOG_KEY_PREFIX + key;
1177 }
1178
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001179 private static String savedDialogArgsKeyFor(int key) {
1180 return SAVED_DIALOG_ARGS_KEY_PREFIX + key;
1181 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182
1183 /**
1184 * Called when activity start-up is complete (after {@link #onStart}
1185 * and {@link #onRestoreInstanceState} have been called). Applications will
1186 * generally not implement this method; it is intended for system
1187 * classes to do final initialization after application code has run.
RoboErik55011652014-07-09 15:05:53 -07001188 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 * <p><em>Derived classes must call through to the super class's
1190 * implementation of this method. If they do not, an exception will be
1191 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001192 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 * @param savedInstanceState If the activity is being re-initialized after
1194 * previously being shut down then this Bundle contains the data it most
1195 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
1196 * @see #onCreate
1197 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001198 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -07001199 protected void onPostCreate(@Nullable Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 if (!isChild()) {
1201 mTitleReady = true;
1202 onTitleChanged(getTitle(), getTitleColor());
1203 }
Winsonb6403152016-02-23 13:32:09 -08001204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 mCalled = true;
1206 }
1207
1208 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001209 * This is the same as {@link #onPostCreate(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001210 * created with the attribute {@link android.R.attr#persistableMode} set to
1211 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -07001212 *
1213 * @param savedInstanceState The data most recently supplied in {@link #onSaveInstanceState}
1214 * @param persistentState The data caming from the PersistableBundle first
1215 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1216 *
1217 * @see #onCreate
1218 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001219 public void onPostCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001220 @Nullable PersistableBundle persistentState) {
1221 onPostCreate(savedInstanceState);
1222 }
1223
1224 /**
RoboErik55011652014-07-09 15:05:53 -07001225 * Called after {@link #onCreate} &mdash; or after {@link #onRestart} when
1226 * the activity had been stopped, but is now again being displayed to the
John Spurlock8a985d22014-02-25 09:40:05 -05001227 * user. It will be followed by {@link #onResume}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 *
1229 * <p><em>Derived classes must call through to the super class's
1230 * implementation of this method. If they do not, an exception will be
1231 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001232 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 * @see #onCreate
1234 * @see #onStop
1235 * @see #onResume
1236 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001237 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 protected void onStart() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001239 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStart " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 mCalled = true;
RoboErik55011652014-07-09 15:05:53 -07001241
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001242 mFragments.doLoaderStart();
Narayan Kamathff5659f2017-02-02 13:31:33 +00001243
1244 getApplication().dispatchActivityStarted(this);
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001245
1246 if (mAutoFillResetNeeded) {
1247 AutofillManager afm = getAutofillManager();
1248 if (afm != null) {
1249 afm.onVisibleForAutofill();
1250 }
1251 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 }
1253
1254 /**
1255 * Called after {@link #onStop} when the current activity is being
1256 * re-displayed to the user (the user has navigated back to it). It will
1257 * be followed by {@link #onStart} and then {@link #onResume}.
1258 *
1259 * <p>For activities that are using raw {@link Cursor} objects (instead of
1260 * creating them through
1261 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)},
1262 * this is usually the place
1263 * where the cursor should be requeried (because you had deactivated it in
1264 * {@link #onStop}.
RoboErik55011652014-07-09 15:05:53 -07001265 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 * <p><em>Derived classes must call through to the super class's
1267 * implementation of this method. If they do not, an exception will be
1268 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001269 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 * @see #onStop
1271 * @see #onStart
1272 * @see #onResume
1273 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001274 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 protected void onRestart() {
1276 mCalled = true;
1277 }
1278
1279 /**
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001280 * Called when an {@link #onResume} is coming up, prior to other pre-resume callbacks
1281 * such as {@link #onNewIntent} and {@link #onActivityResult}. This is primarily intended
1282 * to give the activity a hint that its state is no longer saved -- it will generally
1283 * be called after {@link #onSaveInstanceState} and prior to the activity being
1284 * resumed/started again.
1285 */
1286 public void onStateNotSaved() {
1287 }
1288
1289 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 * Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or
1291 * {@link #onPause}, for your activity to start interacting with the user.
1292 * This is a good place to begin animations, open exclusive-access devices
1293 * (such as the camera), etc.
1294 *
1295 * <p>Keep in mind that onResume is not the best indicator that your activity
1296 * is visible to the user; a system window such as the keyguard may be in
1297 * front. Use {@link #onWindowFocusChanged} to know for certain that your
1298 * activity is visible to the user (for example, to resume a game).
1299 *
1300 * <p><em>Derived classes must call through to the super class's
1301 * implementation of this method. If they do not, an exception will be
1302 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001303 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 * @see #onRestoreInstanceState
1305 * @see #onRestart
1306 * @see #onPostResume
1307 * @see #onPause
1308 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001309 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 protected void onResume() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001311 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " + this);
Narayan Kamathff5659f2017-02-02 13:31:33 +00001312 getApplication().dispatchActivityResumed(this);
George Mountbdc4d8d2016-03-23 13:10:13 -07001313 mActivityTransitionState.onResume(this, isTopOfTask());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 mCalled = true;
1315 }
1316
1317 /**
1318 * Called when activity resume is complete (after {@link #onResume} has
1319 * been called). Applications will generally not implement this method;
1320 * it is intended for system classes to do final setup after application
1321 * resume code has run.
RoboErik55011652014-07-09 15:05:53 -07001322 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 * <p><em>Derived classes must call through to the super class's
1324 * implementation of this method. If they do not, an exception will be
1325 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001326 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 * @see #onResume
1328 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001329 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 protected void onPostResume() {
1331 final Window win = getWindow();
1332 if (win != null) win.makeActive();
Adam Powell50efbed2011-02-08 16:20:15 -08001333 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 mCalled = true;
1335 }
1336
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001337 void setVoiceInteractor(IVoiceInteractor voiceInteractor) {
Amith Yamasanieeed06c2016-05-03 15:07:03 -07001338 if (mVoiceInteractor != null) {
1339 for (Request activeRequest: mVoiceInteractor.getActiveRequests()) {
1340 activeRequest.cancel();
1341 activeRequest.clear();
1342 }
1343 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001344 if (voiceInteractor == null) {
1345 mVoiceInteractor = null;
1346 } else {
1347 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
1348 Looper.myLooper());
1349 }
1350 }
1351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 /**
Phil Weaver846cda932017-06-15 10:10:06 -07001353 * Gets the next autofill ID.
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001354 *
Phil Weaver846cda932017-06-15 10:10:06 -07001355 * <p>All IDs will be bigger than {@link View#LAST_APP_AUTOFILL_ID}. All IDs returned
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001356 * will be unique.
1357 *
1358 * @return A ID that is unique in the activity
1359 *
1360 * {@hide}
1361 */
Phil Weaver846cda932017-06-15 10:10:06 -07001362 public int getNextAutofillId() {
1363 if (mLastAutofillId == Integer.MAX_VALUE - 1) {
1364 mLastAutofillId = View.LAST_APP_AUTOFILL_ID;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001365 }
1366
Phil Weaver846cda932017-06-15 10:10:06 -07001367 mLastAutofillId++;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001368
Phil Weaver846cda932017-06-15 10:10:06 -07001369 return mLastAutofillId;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001370 }
1371
1372 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001373 * Check whether this activity is running as part of a voice interaction with the user.
1374 * If true, it should perform its interaction with the user through the
1375 * {@link VoiceInteractor} returned by {@link #getVoiceInteractor}.
1376 */
1377 public boolean isVoiceInteraction() {
1378 return mVoiceInteractor != null;
1379 }
1380
1381 /**
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001382 * Like {@link #isVoiceInteraction}, but only returns true if this is also the root
1383 * of a voice interaction. That is, returns true if this activity was directly
1384 * started by the voice interaction service as the initiation of a voice interaction.
1385 * Otherwise, for example if it was started by another activity while under voice
1386 * interaction, returns false.
1387 */
1388 public boolean isVoiceInteractionRoot() {
1389 try {
1390 return mVoiceInteractor != null
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001391 && ActivityManager.getService().isRootVoiceInteraction(mToken);
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001392 } catch (RemoteException e) {
1393 }
1394 return false;
1395 }
1396
1397 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001398 * Retrieve the active {@link VoiceInteractor} that the user is going through to
1399 * interact with this activity.
1400 */
1401 public VoiceInteractor getVoiceInteractor() {
1402 return mVoiceInteractor;
1403 }
1404
1405 /**
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001406 * Queries whether the currently enabled voice interaction service supports returning
1407 * a voice interactor for use by the activity. This is valid only for the duration of the
1408 * activity.
1409 *
1410 * @return whether the current voice interaction service supports local voice interaction
1411 */
1412 public boolean isLocalVoiceInteractionSupported() {
1413 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001414 return ActivityManager.getService().supportsLocalVoiceInteraction();
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001415 } catch (RemoteException re) {
1416 }
1417 return false;
1418 }
1419
1420 /**
1421 * Starts a local voice interaction session. When ready,
1422 * {@link #onLocalVoiceInteractionStarted()} is called. You can pass a bundle of private options
1423 * to the registered voice interaction service.
1424 * @param privateOptions a Bundle of private arguments to the current voice interaction service
1425 */
1426 public void startLocalVoiceInteraction(Bundle privateOptions) {
1427 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001428 ActivityManager.getService().startLocalVoiceInteraction(mToken, privateOptions);
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001429 } catch (RemoteException re) {
1430 }
1431 }
1432
1433 /**
1434 * Callback to indicate that {@link #startLocalVoiceInteraction(Bundle)} has resulted in a
1435 * voice interaction session being started. You can now retrieve a voice interactor using
1436 * {@link #getVoiceInteractor()}.
1437 */
1438 public void onLocalVoiceInteractionStarted() {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001439 }
1440
1441 /**
Amith Yamasanid8d967e2016-04-08 09:57:01 -07001442 * Callback to indicate that the local voice interaction has stopped either
1443 * because it was requested through a call to {@link #stopLocalVoiceInteraction()}
1444 * or because it was canceled by the user. The previously acquired {@link VoiceInteractor}
1445 * is no longer valid after this.
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001446 */
1447 public void onLocalVoiceInteractionStopped() {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001448 }
1449
1450 /**
1451 * Request to terminate the current voice interaction that was previously started
Amith Yamasanid8d967e2016-04-08 09:57:01 -07001452 * using {@link #startLocalVoiceInteraction(Bundle)}. When the interaction is
1453 * terminated, {@link #onLocalVoiceInteractionStopped()} will be called.
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001454 */
1455 public void stopLocalVoiceInteraction() {
1456 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001457 ActivityManager.getService().stopLocalVoiceInteraction(mToken);
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001458 } catch (RemoteException re) {
1459 }
1460 }
1461
1462 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 * This is called for activities that set launchMode to "singleTop" in
1464 * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP}
1465 * flag when calling {@link #startActivity}. In either case, when the
1466 * activity is re-launched while at the top of the activity stack instead
1467 * of a new instance of the activity being started, onNewIntent() will be
1468 * called on the existing instance with the Intent that was used to
RoboErik55011652014-07-09 15:05:53 -07001469 * re-launch it.
1470 *
1471 * <p>An activity will always be paused before receiving a new intent, so
1472 * you can count on {@link #onResume} being called after this method.
1473 *
1474 * <p>Note that {@link #getIntent} still returns the original Intent. You
1475 * can use {@link #setIntent} to update it to this new Intent.
1476 *
1477 * @param intent The new intent that was started for the activity.
1478 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 * @see #getIntent
RoboErik55011652014-07-09 15:05:53 -07001480 * @see #setIntent
1481 * @see #onResume
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 */
1483 protected void onNewIntent(Intent intent) {
1484 }
1485
1486 /**
1487 * The hook for {@link ActivityThread} to save the state of this activity.
1488 *
1489 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1490 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1491 *
1492 * @param outState The bundle to save the state to.
1493 */
1494 final void performSaveInstanceState(Bundle outState) {
1495 onSaveInstanceState(outState);
1496 saveManagedDialogs(outState);
George Mount62ab9b72014-05-02 13:51:17 -07001497 mActivityTransitionState.saveState(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07001498 storeHasCurrentPermissionRequest(outState);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001499 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 }
1501
1502 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001503 * The hook for {@link ActivityThread} to save the state of this activity.
1504 *
1505 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1506 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1507 *
1508 * @param outState The bundle to save the state to.
1509 * @param outPersistentState The bundle to save persistent state to.
1510 */
1511 final void performSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
1512 onSaveInstanceState(outState, outPersistentState);
1513 saveManagedDialogs(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07001514 storeHasCurrentPermissionRequest(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07001515 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState +
1516 ", " + outPersistentState);
1517 }
1518
1519 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 * Called to retrieve per-instance state from an activity before being killed
1521 * so that the state can be restored in {@link #onCreate} or
1522 * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method
1523 * will be passed to both).
1524 *
1525 * <p>This method is called before an activity may be killed so that when it
1526 * comes back some time in the future it can restore its state. For example,
1527 * if activity B is launched in front of activity A, and at some point activity
1528 * A is killed to reclaim resources, activity A will have a chance to save the
1529 * current state of its user interface via this method so that when the user
1530 * returns to activity A, the state of the user interface can be restored
1531 * via {@link #onCreate} or {@link #onRestoreInstanceState}.
1532 *
1533 * <p>Do not confuse this method with activity lifecycle callbacks such as
1534 * {@link #onPause}, which is always called when an activity is being placed
1535 * in the background or on its way to destruction, or {@link #onStop} which
1536 * is called before destruction. One example of when {@link #onPause} and
1537 * {@link #onStop} is called and not this method is when a user navigates back
1538 * from activity B to activity A: there is no need to call {@link #onSaveInstanceState}
1539 * on B because that particular instance will never be restored, so the
1540 * system avoids calling it. An example when {@link #onPause} is called and
1541 * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A:
1542 * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't
1543 * killed during the lifetime of B since the state of the user interface of
1544 * A will stay intact.
1545 *
1546 * <p>The default implementation takes care of most of the UI per-instance
1547 * state for you by calling {@link android.view.View#onSaveInstanceState()} on each
1548 * view in the hierarchy that has an id, and by saving the id of the currently
1549 * focused view (all of which is restored by the default implementation of
1550 * {@link #onRestoreInstanceState}). If you override this method to save additional
1551 * information not captured by each individual view, you will likely want to
1552 * call through to the default implementation, otherwise be prepared to save
1553 * all of the state of each view yourself.
1554 *
1555 * <p>If called, this method will occur before {@link #onStop}. There are
1556 * no guarantees about whether it will occur before or after {@link #onPause}.
RoboErik55011652014-07-09 15:05:53 -07001557 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 * @param outState Bundle in which to place your saved state.
RoboErik55011652014-07-09 15:05:53 -07001559 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 * @see #onCreate
1561 * @see #onRestoreInstanceState
1562 * @see #onPause
1563 */
1564 protected void onSaveInstanceState(Bundle outState) {
1565 outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001566
Phil Weaver846cda932017-06-15 10:10:06 -07001567 outState.putInt(LAST_AUTOFILL_ID, mLastAutofillId);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001568 Parcelable p = mFragments.saveAllState();
1569 if (p != null) {
1570 outState.putParcelable(FRAGMENTS_TAG, p);
1571 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07001572 if (mAutoFillResetNeeded) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001573 outState.putBoolean(AUTOFILL_RESET_NEEDED, true);
1574 getAutofillManager().onSaveInstanceState(outState);
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07001575 }
Narayan Kamathff5659f2017-02-02 13:31:33 +00001576 getApplication().dispatchActivitySaveInstanceState(this, outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 }
1578
1579 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001580 * This is the same as {@link #onSaveInstanceState} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001581 * created with the attribute {@link android.R.attr#persistableMode} set to
1582 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1583 * in will be saved and presented in {@link #onCreate(Bundle, PersistableBundle)}
1584 * the first time that this activity is restarted following the next device reboot.
Craig Mautnera0026042014-04-23 11:45:37 -07001585 *
1586 * @param outState Bundle in which to place your saved state.
1587 * @param outPersistentState State which will be saved across reboots.
1588 *
1589 * @see #onSaveInstanceState(Bundle)
1590 * @see #onCreate
1591 * @see #onRestoreInstanceState(Bundle, PersistableBundle)
1592 * @see #onPause
1593 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001594 public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07001595 onSaveInstanceState(outState);
1596 }
1597
1598 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 * Save the state of any managed dialogs.
1600 *
1601 * @param outState place to store the saved state.
1602 */
1603 private void saveManagedDialogs(Bundle outState) {
1604 if (mManagedDialogs == null) {
1605 return;
1606 }
1607
1608 final int numDialogs = mManagedDialogs.size();
1609 if (numDialogs == 0) {
1610 return;
1611 }
1612
1613 Bundle dialogState = new Bundle();
1614
1615 int[] ids = new int[mManagedDialogs.size()];
1616
1617 // save each dialog's bundle, gather the ids
1618 for (int i = 0; i < numDialogs; i++) {
1619 final int key = mManagedDialogs.keyAt(i);
1620 ids[i] = key;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001621 final ManagedDialog md = mManagedDialogs.valueAt(i);
1622 dialogState.putBundle(savedDialogKeyFor(key), md.mDialog.onSaveInstanceState());
1623 if (md.mArgs != null) {
1624 dialogState.putBundle(savedDialogArgsKeyFor(key), md.mArgs);
1625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 }
1627
1628 dialogState.putIntArray(SAVED_DIALOG_IDS_KEY, ids);
1629 outState.putBundle(SAVED_DIALOGS_TAG, dialogState);
1630 }
1631
1632
1633 /**
1634 * Called as part of the activity lifecycle when an activity is going into
1635 * the background, but has not (yet) been killed. The counterpart to
1636 * {@link #onResume}.
1637 *
1638 * <p>When activity B is launched in front of activity A, this callback will
1639 * be invoked on A. B will not be created until A's {@link #onPause} returns,
1640 * so be sure to not do anything lengthy here.
1641 *
1642 * <p>This callback is mostly used for saving any persistent state the
1643 * activity is editing, to present a "edit in place" model to the user and
1644 * making sure nothing is lost if there are not enough resources to start
1645 * the new activity without first killing this one. This is also a good
1646 * place to do things like stop animations and other things that consume a
Pin Ting14a93102012-04-25 11:27:03 +08001647 * noticeable amount of CPU in order to make the switch to the next activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 * as fast as possible, or to close resources that are exclusive access
1649 * such as the camera.
RoboErik55011652014-07-09 15:05:53 -07001650 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 * <p>In situations where the system needs more memory it may kill paused
1652 * processes to reclaim resources. Because of this, you should be sure
1653 * that all of your state is saved by the time you return from
1654 * this function. In general {@link #onSaveInstanceState} is used to save
1655 * per-instance state in the activity and this method is used to store
1656 * global persistent data (in content providers, files, etc.)
RoboErik55011652014-07-09 15:05:53 -07001657 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 * <p>After receiving this call you will usually receive a following call
1659 * to {@link #onStop} (after the next activity has been resumed and
1660 * displayed), however in some cases there will be a direct call back to
1661 * {@link #onResume} without going through the stopped state.
RoboErik55011652014-07-09 15:05:53 -07001662 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 * <p><em>Derived classes must call through to the super class's
1664 * implementation of this method. If they do not, an exception will be
1665 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001666 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 * @see #onResume
1668 * @see #onSaveInstanceState
1669 * @see #onStop
1670 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001671 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 protected void onPause() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001673 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onPause " + this);
Narayan Kamathff5659f2017-02-02 13:31:33 +00001674 getApplication().dispatchActivityPaused(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 mCalled = true;
1676 }
1677
1678 /**
1679 * Called as part of the activity lifecycle when an activity is about to go
1680 * into the background as the result of user choice. For example, when the
1681 * user presses the Home key, {@link #onUserLeaveHint} will be called, but
1682 * when an incoming phone call causes the in-call Activity to be automatically
1683 * brought to the foreground, {@link #onUserLeaveHint} will not be called on
1684 * the activity being interrupted. In cases when it is invoked, this method
1685 * is called right before the activity's {@link #onPause} callback.
RoboErik55011652014-07-09 15:05:53 -07001686 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 * <p>This callback and {@link #onUserInteraction} are intended to help
1688 * activities manage status bar notifications intelligently; specifically,
1689 * for helping activities determine the proper time to cancel a notfication.
RoboErik55011652014-07-09 15:05:53 -07001690 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 * @see #onUserInteraction()
1692 */
1693 protected void onUserLeaveHint() {
1694 }
RoboErik55011652014-07-09 15:05:53 -07001695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 /**
1697 * Generate a new thumbnail for this activity. This method is called before
1698 * pausing the activity, and should draw into <var>outBitmap</var> the
1699 * imagery for the desired thumbnail in the dimensions of that bitmap. It
1700 * can use the given <var>canvas</var>, which is configured to draw into the
1701 * bitmap, for rendering if desired.
RoboErik55011652014-07-09 15:05:53 -07001702 *
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001703 * <p>The default implementation returns fails and does not draw a thumbnail;
1704 * this will result in the platform creating its own thumbnail if needed.
RoboErik55011652014-07-09 15:05:53 -07001705 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 * @param outBitmap The bitmap to contain the thumbnail.
1707 * @param canvas Can be used to render into the bitmap.
RoboErik55011652014-07-09 15:05:53 -07001708 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 * @return Return true if you have drawn into the bitmap; otherwise after
1710 * you return it will be filled with a default thumbnail.
RoboErik55011652014-07-09 15:05:53 -07001711 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 * @see #onCreateDescription
1713 * @see #onSaveInstanceState
1714 * @see #onPause
1715 */
1716 public boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001717 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 }
1719
1720 /**
1721 * Generate a new description for this activity. This method is called
1722 * before pausing the activity and can, if desired, return some textual
1723 * description of its current state to be displayed to the user.
RoboErik55011652014-07-09 15:05:53 -07001724 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 * <p>The default implementation returns null, which will cause you to
1726 * inherit the description from the previous activity. If all activities
1727 * return null, generally the label of the top activity will be used as the
1728 * description.
RoboErik55011652014-07-09 15:05:53 -07001729 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 * @return A description of what the user is doing. It should be short and
1731 * sweet (only a few words).
RoboErik55011652014-07-09 15:05:53 -07001732 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 * @see #onCreateThumbnail
1734 * @see #onSaveInstanceState
1735 * @see #onPause
1736 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001737 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 public CharSequence onCreateDescription() {
1739 return null;
1740 }
1741
1742 /**
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001743 * This is called when the user is requesting an assist, to build a full
1744 * {@link Intent#ACTION_ASSIST} Intent with all of the context of the current
1745 * application. You can override this method to place into the bundle anything
1746 * you would like to appear in the {@link Intent#EXTRA_ASSIST_CONTEXT} part
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07001747 * of the assist Intent.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001748 *
1749 * <p>This function will be called after any global assist callbacks that had
Ying Wang4e0eb222013-04-18 20:39:48 -07001750 * been registered with {@link Application#registerOnProvideAssistDataListener
1751 * Application.registerOnProvideAssistDataListener}.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001752 */
1753 public void onProvideAssistData(Bundle data) {
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07001754 }
1755
1756 /**
1757 * This is called when the user is requesting an assist, to provide references
1758 * to content related to the current activity. Before being called, the
1759 * {@code outContent} Intent is filled with the base Intent of the activity (the Intent
1760 * returned by {@link #getIntent()}). The Intent's extras are stripped of any types
1761 * that are not valid for {@link PersistableBundle} or non-framework Parcelables, and
1762 * the flags {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION} and
1763 * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} are cleared from the Intent.
1764 *
1765 * <p>Custom implementation may adjust the content intent to better reflect the top-level
1766 * context of the activity, and fill in its ClipData with additional content of
1767 * interest that the user is currently viewing. For example, an image gallery application
1768 * that has launched in to an activity allowing the user to swipe through pictures should
1769 * modify the intent to reference the current image they are looking it; such an
1770 * application when showing a list of pictures should add a ClipData that has
1771 * references to all of the pictures currently visible on screen.</p>
1772 *
1773 * @param outContent The assist content to return.
1774 */
1775 public void onProvideAssistContent(AssistContent outContent) {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001776 }
1777
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01001778 /**
Clara Bayarriab591ba2016-05-16 17:48:16 +01001779 * Request the Keyboard Shortcuts screen to show up. This will trigger
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01001780 * {@link #onProvideKeyboardShortcuts} to retrieve the shortcuts for the foreground activity.
1781 */
Clara Bayarriac6f0342016-05-16 14:15:14 +01001782 public final void requestShowKeyboardShortcuts() {
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01001783 Intent intent = new Intent(Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS);
Andrei Stingaceanua3d26932016-07-15 11:12:20 +01001784 intent.setPackage(KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME);
1785 sendBroadcastAsUser(intent, UserHandle.SYSTEM);
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +01001786 }
1787
1788 /**
1789 * Dismiss the Keyboard Shortcuts screen.
1790 */
1791 public final void dismissKeyboardShortcutsHelper() {
1792 Intent intent = new Intent(Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS);
Andrei Stingaceanua3d26932016-07-15 11:12:20 +01001793 intent.setPackage(KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME);
1794 sendBroadcastAsUser(intent, UserHandle.SYSTEM);
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01001795 }
1796
Clara Bayarri75e09792015-07-29 16:20:40 +01001797 @Override
Clara Bayarrifcd7e802016-03-10 12:58:18 +00001798 public void onProvideKeyboardShortcuts(
1799 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
Clara Bayarri75e09792015-07-29 16:20:40 +01001800 if (menu == null) {
1801 return;
1802 }
1803 KeyboardShortcutGroup group = null;
1804 int menuSize = menu.size();
1805 for (int i = 0; i < menuSize; ++i) {
1806 final MenuItem item = menu.getItem(i);
1807 final CharSequence title = item.getTitle();
1808 final char alphaShortcut = item.getAlphabeticShortcut();
Peeyush Agarwale631e322016-10-19 11:41:42 +01001809 final int alphaModifiers = item.getAlphabeticModifiers();
Clara Bayarri75e09792015-07-29 16:20:40 +01001810 if (title != null && alphaShortcut != MIN_VALUE) {
1811 if (group == null) {
Clara Bayarriada88b32016-02-02 18:08:01 +00001812 final int resource = mApplication.getApplicationInfo().labelRes;
1813 group = new KeyboardShortcutGroup(resource != 0 ? getString(resource) : null);
Clara Bayarri75e09792015-07-29 16:20:40 +01001814 }
1815 group.addItem(new KeyboardShortcutInfo(
Peeyush Agarwale631e322016-10-19 11:41:42 +01001816 title, alphaShortcut, alphaModifiers));
Clara Bayarri75e09792015-07-29 16:20:40 +01001817 }
1818 }
1819 if (group != null) {
1820 data.add(group);
1821 }
1822 }
1823
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001824 /**
Dianne Hackborn17f69352015-07-17 18:04:14 -07001825 * Ask to have the current assistant shown to the user. This only works if the calling
1826 * activity is the current foreground activity. It is the same as calling
1827 * {@link android.service.voice.VoiceInteractionService#showSession
1828 * VoiceInteractionService.showSession} and requesting all of the possible context.
1829 * The receiver will always see
1830 * {@link android.service.voice.VoiceInteractionSession#SHOW_SOURCE_APPLICATION} set.
1831 * @return Returns true if the assistant was successfully invoked, else false. For example
1832 * false will be returned if the caller is not the current top activity.
1833 */
1834 public boolean showAssist(Bundle args) {
1835 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001836 return ActivityManager.getService().showAssistFromActivity(mToken, args);
Dianne Hackborn17f69352015-07-17 18:04:14 -07001837 } catch (RemoteException e) {
1838 }
1839 return false;
1840 }
1841
1842 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 * Called when you are no longer visible to the user. You will next
1844 * receive either {@link #onRestart}, {@link #onDestroy}, or nothing,
1845 * depending on later user activity.
RoboErik55011652014-07-09 15:05:53 -07001846 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 * <p><em>Derived classes must call through to the super class's
1848 * implementation of this method. If they do not, an exception will be
1849 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001850 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 * @see #onRestart
1852 * @see #onResume
1853 * @see #onSaveInstanceState
1854 * @see #onDestroy
1855 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001856 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 protected void onStop() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001858 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStop " + this);
Adam Powell50efbed2011-02-08 16:20:15 -08001859 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
George Mount62ab9b72014-05-02 13:51:17 -07001860 mActivityTransitionState.onStop();
Narayan Kamathff5659f2017-02-02 13:31:33 +00001861 getApplication().dispatchActivityStopped(this);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001862 mTranslucentCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 mCalled = true;
Svet Ganov17db9dc2017-02-21 19:54:31 -08001864 if (isFinishing() && mAutoFillResetNeeded) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001865 getAutofillManager().commit();
Felipe Lemebab851c2017-02-03 18:45:08 -08001866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 }
1868
1869 /**
1870 * Perform any final cleanup before an activity is destroyed. This can
1871 * happen either because the activity is finishing (someone called
1872 * {@link #finish} on it, or because the system is temporarily destroying
1873 * this instance of the activity to save space. You can distinguish
1874 * between these two scenarios with the {@link #isFinishing} method.
RoboErik55011652014-07-09 15:05:53 -07001875 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 * <p><em>Note: do not count on this method being called as a place for
1877 * saving data! For example, if an activity is editing data in a content
1878 * provider, those edits should be committed in either {@link #onPause} or
1879 * {@link #onSaveInstanceState}, not here.</em> This method is usually implemented to
1880 * free resources like threads that are associated with an activity, so
1881 * that a destroyed activity does not leave such things around while the
1882 * rest of its application is still running. There are situations where
1883 * the system will simply kill the activity's hosting process without
1884 * calling this method (or any others) in it, so it should not be used to
1885 * do things that are intended to remain around after the process goes
1886 * away.
RoboErik55011652014-07-09 15:05:53 -07001887 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 * <p><em>Derived classes must call through to the super class's
1889 * implementation of this method. If they do not, an exception will be
1890 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001891 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 * @see #onPause
1893 * @see #onStop
1894 * @see #finish
1895 * @see #isFinishing
1896 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001897 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 protected void onDestroy() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001899 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 mCalled = true;
1901
1902 // dismiss any dialogs we are managing.
1903 if (mManagedDialogs != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 final int numDialogs = mManagedDialogs.size();
1905 for (int i = 0; i < numDialogs; i++) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001906 final ManagedDialog md = mManagedDialogs.valueAt(i);
1907 if (md.mDialog.isShowing()) {
1908 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 }
1910 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001911 mManagedDialogs = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913
1914 // close any cursors we are managing.
Makoto Onuki2f6a0182010-02-22 13:26:59 -08001915 synchronized (mManagedCursors) {
1916 int numCursors = mManagedCursors.size();
1917 for (int i = 0; i < numCursors; i++) {
1918 ManagedCursor c = mManagedCursors.get(i);
1919 if (c != null) {
1920 c.mCursor.close();
1921 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 }
Makoto Onuki2f6a0182010-02-22 13:26:59 -08001923 mManagedCursors.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 }
Amith Yamasani49860442010-03-17 20:54:10 -07001925
1926 // Close any open search dialog
1927 if (mSearchManager != null) {
1928 mSearchManager.stopSearch();
1929 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001930
Chris Banes21b25772016-01-04 20:41:59 +00001931 if (mActionBar != null) {
1932 mActionBar.onDestroy();
1933 }
Narayan Kamathff5659f2017-02-02 13:31:33 +00001934
1935 getApplication().dispatchActivityDestroyed(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 }
1937
1938 /**
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07001939 * Report to the system that your app is now fully drawn, purely for diagnostic
1940 * purposes (calling it does not impact the visible behavior of the activity).
1941 * This is only used to help instrument application launch times, so that the
1942 * app can report when it is fully in a usable state; without this, the only thing
1943 * the system itself can determine is the point at which the activity's window
1944 * is <em>first</em> drawn and displayed. To participate in app launch time
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001945 * measurement, you should always call this method after first launch (when
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07001946 * {@link #onCreate(android.os.Bundle)} is called), at the point where you have
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001947 * entirely drawn your UI and populated with all of the significant data. You
1948 * can safely call this method any time after first launch as well, in which case
1949 * it will simply be ignored.
1950 */
1951 public void reportFullyDrawn() {
1952 if (mDoReportFullyDrawn) {
1953 mDoReportFullyDrawn = false;
1954 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001955 ActivityManager.getService().reportActivityFullyDrawn(mToken);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001956 } catch (RemoteException e) {
1957 }
1958 }
1959 }
1960
1961 /**
Wale Ogunwale5f986092015-12-04 15:35:38 -08001962 * Called by the system when the activity changes from fullscreen mode to multi-window mode and
Winson Chung5af42fc2017-03-24 17:11:33 -07001963 * visa-versa. This method provides the same configuration that will be sent in the following
1964 * {@link #onConfigurationChanged(Configuration)} call after the activity enters this mode.
1965 *
Wale Ogunwale5f986092015-12-04 15:35:38 -08001966 * @see android.R.attr#resizeableActivity
1967 *
Andrii Kulian933076d2016-03-29 17:04:42 -07001968 * @param isInMultiWindowMode True if the activity is in multi-window mode.
Winson Chung5af42fc2017-03-24 17:11:33 -07001969 * @param newConfig The new configuration of the activity with the state
1970 * {@param isInMultiWindowMode}.
Wale Ogunwale5f986092015-12-04 15:35:38 -08001971 */
Winson Chung5af42fc2017-03-24 17:11:33 -07001972 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
1973 // Left deliberately empty. There should be no side effects if a direct
1974 // subclass of Activity does not call super.
1975 onMultiWindowModeChanged(isInMultiWindowMode);
1976 }
1977
1978 /**
1979 * Called by the system when the activity changes from fullscreen mode to multi-window mode and
1980 * visa-versa.
1981 *
1982 * @see android.R.attr#resizeableActivity
1983 *
1984 * @param isInMultiWindowMode True if the activity is in multi-window mode.
1985 *
1986 * @deprecated Use {@link #onMultiWindowModeChanged(boolean, Configuration)} instead.
1987 */
1988 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07001989 public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
Adam Powell858cf032016-05-09 15:45:37 -07001990 // Left deliberately empty. There should be no side effects if a direct
1991 // subclass of Activity does not call super.
Wale Ogunwale5f986092015-12-04 15:35:38 -08001992 }
1993
1994 /**
1995 * Returns true if the activity is currently in multi-window mode.
1996 * @see android.R.attr#resizeableActivity
1997 *
1998 * @return True if the activity is in multi-window mode.
1999 */
Andrii Kulian933076d2016-03-29 17:04:42 -07002000 public boolean isInMultiWindowMode() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002001 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002002 return ActivityManager.getService().isInMultiWindowMode(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002003 } catch (RemoteException e) {
2004 }
2005 return false;
2006 }
2007
2008 /**
Winson Chung5af42fc2017-03-24 17:11:33 -07002009 * Called by the system when the activity changes to and from picture-in-picture mode. This
2010 * method provides the same configuration that will be sent in the following
2011 * {@link #onConfigurationChanged(Configuration)} call after the activity enters this mode.
2012 *
Wale Ogunwale5f986092015-12-04 15:35:38 -08002013 * @see android.R.attr#supportsPictureInPicture
2014 *
Andrii Kulian933076d2016-03-29 17:04:42 -07002015 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode.
Winson Chung5af42fc2017-03-24 17:11:33 -07002016 * @param newConfig The new configuration of the activity with the state
2017 * {@param isInPictureInPictureMode}.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002018 */
Winson Chung5af42fc2017-03-24 17:11:33 -07002019 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode,
2020 Configuration newConfig) {
2021 // Left deliberately empty. There should be no side effects if a direct
2022 // subclass of Activity does not call super.
2023 onPictureInPictureModeChanged(isInPictureInPictureMode);
2024 }
2025
2026 /**
2027 * Called by the system when the activity changes to and from picture-in-picture mode.
2028 *
2029 * @see android.R.attr#supportsPictureInPicture
2030 *
2031 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode.
2032 *
2033 * @deprecated Use {@link #onPictureInPictureModeChanged(boolean, Configuration)} instead.
2034 */
2035 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002036 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
Adam Powell858cf032016-05-09 15:45:37 -07002037 // Left deliberately empty. There should be no side effects if a direct
2038 // subclass of Activity does not call super.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002039 }
2040
2041 /**
2042 * Returns true if the activity is currently in picture-in-picture mode.
2043 * @see android.R.attr#supportsPictureInPicture
2044 *
2045 * @return True if the activity is in picture-in-picture mode.
2046 */
Andrii Kulian933076d2016-03-29 17:04:42 -07002047 public boolean isInPictureInPictureMode() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002048 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002049 return ActivityManager.getService().isInPictureInPictureMode(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002050 } catch (RemoteException e) {
2051 }
2052 return false;
2053 }
2054
2055 /**
Winson Chungc2baac02017-01-11 13:34:47 -08002056 * Puts the activity in picture-in-picture mode if possible in the current system state. Any
Winson Chung709904f2017-04-25 11:00:48 -07002057 * prior calls to {@link #setPictureInPictureParams(PictureInPictureParams)} will still apply
2058 * when entering picture-in-picture through this call.
Winson Chungc2baac02017-01-11 13:34:47 -08002059 *
Winson Chung709904f2017-04-25 11:00:48 -07002060 * @see #enterPictureInPictureMode(PictureInPictureParams)
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002061 * @see android.R.attr#supportsPictureInPicture
2062 */
Winson Chung709904f2017-04-25 11:00:48 -07002063 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002064 public void enterPictureInPictureMode() {
Winson Chung709904f2017-04-25 11:00:48 -07002065 enterPictureInPictureMode(new PictureInPictureParams.Builder().build());
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002066 }
2067
Jeff Sharkey000ce802017-04-29 13:13:27 -06002068 /** @removed */
Winson Chung709904f2017-04-25 11:00:48 -07002069 @Deprecated
Winson Chungc2baac02017-01-11 13:34:47 -08002070 public boolean enterPictureInPictureMode(@NonNull PictureInPictureArgs args) {
Jeff Sharkey000ce802017-04-29 13:13:27 -06002071 return enterPictureInPictureMode(PictureInPictureArgs.convert(args));
Winson Chungc2baac02017-01-11 13:34:47 -08002072 }
2073
2074 /**
Winson Chung709904f2017-04-25 11:00:48 -07002075 * Puts the activity in picture-in-picture mode if possible in the current system state. The
2076 * set parameters in {@param params} will be combined with the parameters from prior calls to
2077 * {@link #setPictureInPictureParams(PictureInPictureParams)}.
2078 *
2079 * The system may disallow entering picture-in-picture in various cases, including when the
2080 * activity is not visible, if the screen is locked or if the user has an activity pinned.
2081 *
2082 * @see android.R.attr#supportsPictureInPicture
2083 * @see PictureInPictureParams
2084 *
2085 * @param params non-null parameters to be combined with previously set parameters when entering
2086 * picture-in-picture.
2087 *
2088 * @return true if the system puts this activity into picture-in-picture mode or was already
2089 * in picture-in-picture mode (@see {@link #isInPictureInPictureMode())
2090 */
2091 public boolean enterPictureInPictureMode(@NonNull PictureInPictureParams params) {
2092 try {
2093 if (params == null) {
2094 throw new IllegalArgumentException("Expected non-null picture-in-picture params");
2095 }
Winson Chung709904f2017-04-25 11:00:48 -07002096 return ActivityManagerNative.getDefault().enterPictureInPictureMode(mToken, params);
2097 } catch (RemoteException e) {
2098 return false;
2099 }
2100 }
2101
Jeff Sharkey000ce802017-04-29 13:13:27 -06002102 /** @removed */
2103 @Deprecated
Winson Chung709904f2017-04-25 11:00:48 -07002104 public void setPictureInPictureArgs(@NonNull PictureInPictureArgs args) {
Jeff Sharkey000ce802017-04-29 13:13:27 -06002105 setPictureInPictureParams(PictureInPictureArgs.convert(args));
Winson Chung709904f2017-04-25 11:00:48 -07002106 }
2107
2108 /**
Winson Chungc2baac02017-01-11 13:34:47 -08002109 * Updates the properties of the picture-in-picture activity, or sets it to be used later when
2110 * {@link #enterPictureInPictureMode()} is called.
2111 *
Winson Chung709904f2017-04-25 11:00:48 -07002112 * @param params the new parameters for the picture-in-picture.
Winson Chungc2baac02017-01-11 13:34:47 -08002113 */
Winson Chung709904f2017-04-25 11:00:48 -07002114 public void setPictureInPictureParams(@NonNull PictureInPictureParams params) {
Winson Chungc2baac02017-01-11 13:34:47 -08002115 try {
Winson Chung709904f2017-04-25 11:00:48 -07002116 if (params == null) {
2117 throw new IllegalArgumentException("Expected non-null picture-in-picture params");
Winson Chungc2baac02017-01-11 13:34:47 -08002118 }
Winson Chung709904f2017-04-25 11:00:48 -07002119 ActivityManagerNative.getDefault().setPictureInPictureParams(mToken, params);
Winson Chungb5c41b72016-12-07 15:00:47 -08002120 } catch (RemoteException e) {
2121 }
2122 }
2123
Winson Chung8802eac2017-04-17 14:21:44 -07002124 /**
Winson Chung709904f2017-04-25 11:00:48 -07002125 * Return the number of actions that will be displayed in the picture-in-picture UI when the
2126 * user interacts with the activity currently in picture-in-picture mode. This number may change
2127 * if the global configuration changes (ie. if the device is plugged into an external display),
2128 * but will always be larger than three.
2129 */
2130 public int getMaxNumPictureInPictureActions() {
2131 try {
2132 return ActivityManagerNative.getDefault().getMaxNumPictureInPictureActions(mToken);
2133 } catch (RemoteException e) {
2134 return 0;
2135 }
2136 }
2137
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002138 void dispatchMovedToDisplay(int displayId, Configuration config) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002139 updateDisplay(displayId);
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002140 onMovedToDisplay(displayId, config);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002141 }
2142
2143 /**
2144 * Called by the system when the activity is moved from one display to another without
2145 * recreation. This means that this activity is declared to handle all changes to configuration
2146 * that happened when it was switched to another display, so it wasn't destroyed and created
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002147 * again.
2148 *
2149 * <p>This call will be followed by {@link #onConfigurationChanged(Configuration)} if the
2150 * applied configuration actually changed. It is up to app developer to choose whether to handle
2151 * the change in this method or in the following {@link #onConfigurationChanged(Configuration)}
2152 * call.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002153 *
2154 * <p>Use this callback to track changes to the displays if some activity functionality relies
2155 * on an association with some display properties.
2156 *
2157 * @param displayId The id of the display to which activity was moved.
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002158 * @param config Configuration of the activity resources on new display after move.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002159 *
2160 * @see #onConfigurationChanged(Configuration)
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002161 * @see View#onMovedToDisplay(int, Configuration)
Andrii Kuliancadacce2017-04-26 18:09:54 -07002162 * @hide
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002163 */
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002164 public void onMovedToDisplay(int displayId, Configuration config) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002165 }
2166
Winson Chungb5c41b72016-12-07 15:00:47 -08002167 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 * Called by the system when the device configuration changes while your
2169 * activity is running. Note that this will <em>only</em> be called if
2170 * you have selected configurations you would like to handle with the
2171 * {@link android.R.attr#configChanges} attribute in your manifest. If
2172 * any configuration change occurs that is not selected to be reported
2173 * by that attribute, then instead of reporting it the system will stop
2174 * and restart the activity (to have it launched with the new
2175 * configuration).
RoboErik55011652014-07-09 15:05:53 -07002176 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 * <p>At the time that this function has been called, your Resources
2178 * object will have been updated to return resource values matching the
2179 * new configuration.
RoboErik55011652014-07-09 15:05:53 -07002180 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 * @param newConfig The new device configuration.
2182 */
2183 public void onConfigurationChanged(Configuration newConfig) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002184 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onConfigurationChanged " + this + ": " + newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 mCalled = true;
Bjorn Bringert444c7272009-07-06 21:32:50 +01002186
Dianne Hackborn9d071802010-12-08 14:49:15 -08002187 mFragments.dispatchConfigurationChanged(newConfig);
2188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 if (mWindow != null) {
2190 // Pass the configuration changed event to the window
2191 mWindow.onConfigurationChanged(newConfig);
2192 }
Adam Powell45c0b192011-07-28 15:11:57 -07002193
2194 if (mActionBar != null) {
2195 // Do this last; the action bar will need to access
2196 // view changes from above.
2197 mActionBar.onConfigurationChanged(newConfig);
2198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 }
RoboErik55011652014-07-09 15:05:53 -07002200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 /**
2202 * If this activity is being destroyed because it can not handle a
2203 * configuration parameter being changed (and thus its
2204 * {@link #onConfigurationChanged(Configuration)} method is
2205 * <em>not</em> being called), then you can use this method to discover
2206 * the set of changes that have occurred while in the process of being
2207 * destroyed. Note that there is no guarantee that these will be
2208 * accurate (other changes could have happened at any time), so you should
2209 * only use this as an optimization hint.
RoboErik55011652014-07-09 15:05:53 -07002210 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 * @return Returns a bit field of the configuration parameters that are
2212 * changing, as defined by the {@link android.content.res.Configuration}
2213 * class.
2214 */
2215 public int getChangingConfigurations() {
2216 return mConfigChangeFlags;
2217 }
RoboErik55011652014-07-09 15:05:53 -07002218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 /**
2220 * Retrieve the non-configuration instance data that was previously
2221 * returned by {@link #onRetainNonConfigurationInstance()}. This will
2222 * be available from the initial {@link #onCreate} and
2223 * {@link #onStart} calls to the new instance, allowing you to extract
2224 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002225 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 * <p>Note that the data you retrieve here should <em>only</em> be used
2227 * as an optimization for handling configuration changes. You should always
2228 * be able to handle getting a null pointer back, and an activity must
2229 * still be able to restore itself to its previous state (through the
2230 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2231 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002232 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08002233 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002234 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08002235 * available on older platforms through the Android support libraries.
2236 *
2237 * @return the object previously returned by {@link #onRetainNonConfigurationInstance()}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002239 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 public Object getLastNonConfigurationInstance() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002241 return mLastNonConfigurationInstances != null
2242 ? mLastNonConfigurationInstances.activity : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 }
RoboErik55011652014-07-09 15:05:53 -07002244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 /**
2246 * Called by the system, as part of destroying an
2247 * activity due to a configuration change, when it is known that a new
2248 * instance will immediately be created for the new configuration. You
2249 * can return any object you like here, including the activity instance
2250 * itself, which can later be retrieved by calling
2251 * {@link #getLastNonConfigurationInstance()} in the new activity
2252 * instance.
RoboErik55011652014-07-09 15:05:53 -07002253 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002254 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2255 * or later, consider instead using a {@link Fragment} with
2256 * {@link Fragment#setRetainInstance(boolean)
2257 * Fragment.setRetainInstance(boolean}.</em>
2258 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 * <p>This function is called purely as an optimization, and you must
2260 * not rely on it being called. When it is called, a number of guarantees
2261 * will be made to help optimize configuration switching:
2262 * <ul>
2263 * <li> The function will be called between {@link #onStop} and
2264 * {@link #onDestroy}.
2265 * <li> A new instance of the activity will <em>always</em> be immediately
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002266 * created after this one's {@link #onDestroy()} is called. In particular,
2267 * <em>no</em> messages will be dispatched during this time (when the returned
2268 * object does not have an activity to be associated with).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 * <li> The object you return here will <em>always</em> be available from
2270 * the {@link #getLastNonConfigurationInstance()} method of the following
2271 * activity instance as described there.
2272 * </ul>
RoboErik55011652014-07-09 15:05:53 -07002273 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 * <p>These guarantees are designed so that an activity can use this API
2275 * to propagate extensive state from the old to new activity instance, from
2276 * loaded bitmaps, to network connections, to evenly actively running
2277 * threads. Note that you should <em>not</em> propagate any data that
2278 * may change based on the configuration, including any data loaded from
2279 * resources such as strings, layouts, or drawables.
RoboErik55011652014-07-09 15:05:53 -07002280 *
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002281 * <p>The guarantee of no message handling during the switch to the next
2282 * activity simplifies use with active objects. For example if your retained
2283 * state is an {@link android.os.AsyncTask} you are guaranteed that its
2284 * call back functions (like {@link android.os.AsyncTask#onPostExecute}) will
2285 * not be called from the call here until you execute the next instance's
2286 * {@link #onCreate(Bundle)}. (Note however that there is of course no such
2287 * guarantee for {@link android.os.AsyncTask#doInBackground} since that is
2288 * running in a separate thread.)
2289 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08002290 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002291 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08002292 * available on older platforms through the Android support libraries.
2293 *
2294 * @return any Object holding the desired state to propagate to the
2295 * next activity instance
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 */
2297 public Object onRetainNonConfigurationInstance() {
2298 return null;
2299 }
RoboErik55011652014-07-09 15:05:53 -07002300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 /**
2302 * Retrieve the non-configuration instance data that was previously
2303 * returned by {@link #onRetainNonConfigurationChildInstances()}. This will
2304 * be available from the initial {@link #onCreate} and
2305 * {@link #onStart} calls to the new instance, allowing you to extract
2306 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002307 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 * <p>Note that the data you retrieve here should <em>only</em> be used
2309 * as an optimization for handling configuration changes. You should always
2310 * be able to handle getting a null pointer back, and an activity must
2311 * still be able to restore itself to its previous state (through the
2312 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2313 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002314 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 * @return Returns the object previously returned by
2316 * {@link #onRetainNonConfigurationChildInstances()}
2317 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002318 @Nullable
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002319 HashMap<String, Object> getLastNonConfigurationChildInstances() {
2320 return mLastNonConfigurationInstances != null
2321 ? mLastNonConfigurationInstances.children : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 }
RoboErik55011652014-07-09 15:05:53 -07002323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 /**
2325 * This method is similar to {@link #onRetainNonConfigurationInstance()} except that
2326 * it should return either a mapping from child activity id strings to arbitrary objects,
2327 * or null. This method is intended to be used by Activity framework subclasses that control a
2328 * set of child activities, such as ActivityGroup. The same guarantees and restrictions apply
2329 * as for {@link #onRetainNonConfigurationInstance()}. The default implementation returns null.
2330 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002331 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 HashMap<String,Object> onRetainNonConfigurationChildInstances() {
2333 return null;
2334 }
RoboErik55011652014-07-09 15:05:53 -07002335
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002336 NonConfigurationInstances retainNonConfigurationInstances() {
2337 Object activity = onRetainNonConfigurationInstance();
2338 HashMap<String, Object> children = onRetainNonConfigurationChildInstances();
Adam Powell44ba79e2016-02-04 16:20:37 -08002339 FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig();
Adam Powellcbade7f2016-04-15 11:14:37 -07002340
2341 // We're already stopped but we've been asked to retain.
2342 // Our fragments are taken care of but we need to mark the loaders for retention.
2343 // In order to do this correctly we need to restart the loaders first before
2344 // handing them off to the next activity.
2345 mFragments.doLoaderStart();
2346 mFragments.doLoaderStop(true);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002347 ArrayMap<String, LoaderManager> loaders = mFragments.retainLoaderNonConfig();
Adam Powellcbade7f2016-04-15 11:14:37 -07002348
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002349 if (activity == null && children == null && fragments == null && loaders == null
Dianne Hackborn20d94742014-05-29 18:35:45 -07002350 && mVoiceInteractor == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002351 return null;
2352 }
RoboErik55011652014-07-09 15:05:53 -07002353
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002354 NonConfigurationInstances nci = new NonConfigurationInstances();
2355 nci.activity = activity;
2356 nci.children = children;
2357 nci.fragments = fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002358 nci.loaders = loaders;
Dianne Hackborn57dd7372015-07-27 18:11:14 -07002359 if (mVoiceInteractor != null) {
2360 mVoiceInteractor.retainInstance();
2361 nci.voiceInteractor = mVoiceInteractor;
2362 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002363 return nci;
2364 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 public void onLowMemory() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002367 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onLowMemory " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 mCalled = true;
Dianne Hackborn9d071802010-12-08 14:49:15 -08002369 mFragments.dispatchLowMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002371
2372 public void onTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002373 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onTrimMemory " + this + ": " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002374 mCalled = true;
2375 mFragments.dispatchTrimMemory(level);
2376 }
2377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002379 * Return the FragmentManager for interacting with fragments associated
2380 * with this activity.
2381 */
2382 public FragmentManager getFragmentManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002383 return mFragments.getFragmentManager();
Dianne Hackborn9e14e9f32010-07-14 11:07:38 -07002384 }
RoboErik55011652014-07-09 15:05:53 -07002385
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002386 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07002387 * Called when a Fragment is being attached to this activity, immediately
2388 * after the call to its {@link Fragment#onAttach Fragment.onAttach()}
2389 * method and before {@link Fragment#onCreate Fragment.onCreate()}.
2390 */
2391 public void onAttachFragment(Fragment fragment) {
2392 }
RoboErik55011652014-07-09 15:05:53 -07002393
Dianne Hackbornc8017682010-07-06 13:34:38 -07002394 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 * Wrapper around
2396 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
2397 * that gives the resulting {@link Cursor} to call
2398 * {@link #startManagingCursor} so that the activity will manage its
2399 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07002400 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002401 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2402 * or later, consider instead using {@link LoaderManager} instead, available
2403 * via {@link #getLoaderManager()}.</em>
2404 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002405 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
2406 * this method, because the activity will do that for you at the appropriate time. However, if
2407 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
2408 * not</em> automatically close the cursor and, in that case, you must call
2409 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002410 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 * @param uri The URI of the content provider to query.
2412 * @param projection List of columns to return.
2413 * @param selection SQL WHERE clause.
2414 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07002415 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07002417 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
2419 * @see #startManagingCursor
2420 * @hide
Jason parks6ed50de2010-08-25 10:18:50 -05002421 *
2422 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002423 */
Jason parks6ed50de2010-08-25 10:18:50 -05002424 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07002425 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
2426 String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 Cursor c = getContentResolver().query(uri, projection, selection, null, sortOrder);
2428 if (c != null) {
2429 startManagingCursor(c);
2430 }
2431 return c;
2432 }
2433
2434 /**
2435 * Wrapper around
2436 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
2437 * that gives the resulting {@link Cursor} to call
2438 * {@link #startManagingCursor} so that the activity will manage its
2439 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07002440 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002441 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2442 * or later, consider instead using {@link LoaderManager} instead, available
2443 * via {@link #getLoaderManager()}.</em>
2444 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002445 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
2446 * this method, because the activity will do that for you at the appropriate time. However, if
2447 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
2448 * not</em> automatically close the cursor and, in that case, you must call
2449 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002450 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 * @param uri The URI of the content provider to query.
2452 * @param projection List of columns to return.
2453 * @param selection SQL WHERE clause.
2454 * @param selectionArgs The arguments to selection, if any ?s are pesent
2455 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07002456 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07002458 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
2460 * @see #startManagingCursor
Jason parks6ed50de2010-08-25 10:18:50 -05002461 *
2462 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 */
Jason parks6ed50de2010-08-25 10:18:50 -05002464 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07002465 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
2466 String[] selectionArgs, String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
2468 if (c != null) {
2469 startManagingCursor(c);
2470 }
2471 return c;
2472 }
2473
2474 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002475 * This method allows the activity to take care of managing the given
2476 * {@link Cursor}'s lifecycle for you based on the activity's lifecycle.
2477 * That is, when the activity is stopped it will automatically call
2478 * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted
2479 * it will call {@link Cursor#requery} for you. When the activity is
2480 * destroyed, all managed Cursors will be closed automatically.
RoboErik55011652014-07-09 15:05:53 -07002481 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002482 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2483 * or later, consider instead using {@link LoaderManager} instead, available
2484 * via {@link #getLoaderManager()}.</em>
2485 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002486 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on cursor obtained from
2487 * {@link #managedQuery}, because the activity will do that for you at the appropriate time.
2488 * However, if you call {@link #stopManagingCursor} on a cursor from a managed query, the system
2489 * <em>will not</em> automatically close the cursor and, in that case, you must call
2490 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002491 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002492 * @param c The Cursor to be managed.
RoboErik55011652014-07-09 15:05:53 -07002493 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 * @see #managedQuery(android.net.Uri , String[], String, String[], String)
2495 * @see #stopManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002496 *
2497 * @deprecated Use the new {@link android.content.CursorLoader} class with
2498 * {@link LoaderManager} instead; this is also
2499 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002500 */
Jason parks6ed50de2010-08-25 10:18:50 -05002501 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502 public void startManagingCursor(Cursor c) {
2503 synchronized (mManagedCursors) {
2504 mManagedCursors.add(new ManagedCursor(c));
2505 }
2506 }
2507
2508 /**
2509 * Given a Cursor that was previously given to
2510 * {@link #startManagingCursor}, stop the activity's management of that
2511 * cursor.
RoboErik55011652014-07-09 15:05:53 -07002512 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002513 * <p><strong>Warning:</strong> After calling this method on a cursor from a managed query,
RoboErik55011652014-07-09 15:05:53 -07002514 * the system <em>will not</em> automatically close the cursor and you must call
Joe Fernandez8ef69702011-09-02 14:32:22 -07002515 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002516 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 * @param c The Cursor that was being managed.
RoboErik55011652014-07-09 15:05:53 -07002518 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 * @see #startManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002520 *
2521 * @deprecated Use the new {@link android.content.CursorLoader} class with
2522 * {@link LoaderManager} instead; this is also
2523 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002524 */
Jason parks6ed50de2010-08-25 10:18:50 -05002525 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 public void stopManagingCursor(Cursor c) {
2527 synchronized (mManagedCursors) {
2528 final int N = mManagedCursors.size();
2529 for (int i=0; i<N; i++) {
2530 ManagedCursor mc = mManagedCursors.get(i);
2531 if (mc.mCursor == c) {
2532 mManagedCursors.remove(i);
2533 break;
2534 }
2535 }
2536 }
2537 }
2538
2539 /**
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07002540 * @deprecated As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}
2541 * this is a no-op.
Dianne Hackborn4f3867e2010-12-14 22:09:51 -08002542 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 */
Dianne Hackbornd3efa392010-09-01 17:34:12 -07002544 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 public void setPersistent(boolean isPersistent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 }
2547
2548 /**
Alan Viverettedb7423c2017-03-31 13:13:58 -04002549 * Finds a view that was identified by the {@code android:id} XML attribute
2550 * that was processed in {@link #onCreate}.
2551 * <p>
2552 * <strong>Note:</strong> In most cases -- depending on compiler support --
2553 * the resulting view is automatically cast to the target class type. If
2554 * the target class type is unconstrained, an explicit cast may be
2555 * necessary.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 *
Alan Viverettedb7423c2017-03-31 13:13:58 -04002557 * @param id the ID to search for
2558 * @return a view with given ID if found, or {@code null} otherwise
2559 * @see View#findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 */
Scott Kennedyc0519552015-02-11 15:33:10 -08002561 @Nullable
Alan Viverettedb7423c2017-03-31 13:13:58 -04002562 public <T extends View> T findViewById(@IdRes int id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002563 return getWindow().findViewById(id);
2564 }
RoboErik55011652014-07-09 15:05:53 -07002565
Adam Powell33b97432010-04-20 10:01:14 -07002566 /**
2567 * Retrieve a reference to this activity's ActionBar.
Adam Powell42c0fe82010-08-10 16:36:56 -07002568 *
Adam Powell33b97432010-04-20 10:01:14 -07002569 * @return The Activity's ActionBar, or null if it does not have one.
2570 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002571 @Nullable
Adam Powell33b97432010-04-20 10:01:14 -07002572 public ActionBar getActionBar() {
Adam Powelle43340c2014-03-17 19:10:43 -07002573 initWindowDecorActionBar();
Adam Powell33b97432010-04-20 10:01:14 -07002574 return mActionBar;
2575 }
Adam Powelle43340c2014-03-17 19:10:43 -07002576
2577 /**
2578 * Set a {@link android.widget.Toolbar Toolbar} to act as the {@link ActionBar} for this
2579 * Activity window.
2580 *
2581 * <p>When set to a non-null value the {@link #getActionBar()} method will return
2582 * an {@link ActionBar} object that can be used to control the given toolbar as if it were
2583 * a traditional window decor action bar. The toolbar's menu will be populated with the
2584 * Activity's options menu and the navigation button will be wired through the standard
2585 * {@link android.R.id#home home} menu select action.</p>
2586 *
2587 * <p>In order to use a Toolbar within the Activity's window content the application
2588 * must not request the window feature {@link Window#FEATURE_ACTION_BAR FEATURE_ACTION_BAR}.</p>
2589 *
Chris Banesc7d6c322016-01-27 14:09:16 +00002590 * @param toolbar Toolbar to set as the Activity's action bar, or {@code null} to clear it
Adam Powelle43340c2014-03-17 19:10:43 -07002591 */
Adam Powell37780142014-06-01 13:31:00 -07002592 public void setActionBar(@Nullable Toolbar toolbar) {
Chris Banes21b25772016-01-04 20:41:59 +00002593 final ActionBar ab = getActionBar();
2594 if (ab instanceof WindowDecorActionBar) {
Adam Powelle43340c2014-03-17 19:10:43 -07002595 throw new IllegalStateException("This Activity already has an action bar supplied " +
2596 "by the window decor. Do not request Window.FEATURE_ACTION_BAR and set " +
2597 "android:windowActionBar to false in your theme to use a Toolbar instead.");
2598 }
Chris Banes21b25772016-01-04 20:41:59 +00002599
2600 // If we reach here then we're setting a new action bar
2601 // First clear out the MenuInflater to make sure that it is valid for the new Action Bar
Chris Banes601bb992015-06-22 10:05:38 +01002602 mMenuInflater = null;
2603
Chris Banes21b25772016-01-04 20:41:59 +00002604 // If we have an action bar currently, destroy it
2605 if (ab != null) {
2606 ab.onDestroy();
2607 }
2608
Chris Banesc7d6c322016-01-27 14:09:16 +00002609 if (toolbar != null) {
2610 final ToolbarActionBar tbab = new ToolbarActionBar(toolbar, getTitle(), this);
2611 mActionBar = tbab;
2612 mWindow.setCallback(tbab.getWrappedWindowCallback());
2613 } else {
2614 mActionBar = null;
2615 // Re-set the original window callback since we may have already set a Toolbar wrapper
2616 mWindow.setCallback(this);
2617 }
2618
2619 invalidateOptionsMenu();
Adam Powelle43340c2014-03-17 19:10:43 -07002620 }
RoboErik55011652014-07-09 15:05:53 -07002621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 /**
Adam Powell33b97432010-04-20 10:01:14 -07002623 * Creates a new ActionBar, locates the inflated ActionBarView,
2624 * initializes the ActionBar with the view, and sets mActionBar.
2625 */
Adam Powelle43340c2014-03-17 19:10:43 -07002626 private void initWindowDecorActionBar() {
Adam Powell89e06452010-06-23 20:24:52 -07002627 Window window = getWindow();
Adam Powella593d982011-05-13 14:09:54 -07002628
2629 // Initializing the window decor can change window feature flags.
2630 // Make sure that we have the correct set before performing the test below.
2631 window.getDecorView();
2632
Adam Powell9b4c8042010-08-10 15:36:44 -07002633 if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) {
Adam Powell33b97432010-04-20 10:01:14 -07002634 return;
2635 }
Adam Powell04fe6eb2013-05-31 14:39:48 -07002636
Adam Powelle43340c2014-03-17 19:10:43 -07002637 mActionBar = new WindowDecorActionBar(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07002638 mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp);
Adam Powell04fe6eb2013-05-31 14:39:48 -07002639
2640 mWindow.setDefaultIcon(mActivityInfo.getIconResource());
2641 mWindow.setDefaultLogo(mActivityInfo.getLogoResource());
Adam Powell33b97432010-04-20 10:01:14 -07002642 }
RoboErik55011652014-07-09 15:05:53 -07002643
Adam Powell33b97432010-04-20 10:01:14 -07002644 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 * Set the activity content from a layout resource. The resource will be
2646 * inflated, adding all top-level views to the activity.
Romain Guy482b34a62011-01-20 10:59:28 -08002647 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 * @param layoutResID Resource ID to be inflated.
RoboErik55011652014-07-09 15:05:53 -07002649 *
Romain Guy482b34a62011-01-20 10:59:28 -08002650 * @see #setContentView(android.view.View)
2651 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002653 public void setContentView(@LayoutRes int layoutResID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 getWindow().setContentView(layoutResID);
Adam Powelle43340c2014-03-17 19:10:43 -07002655 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656 }
2657
2658 /**
2659 * Set the activity content to an explicit view. This view is placed
2660 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08002661 * view hierarchy. When calling this method, the layout parameters of the
2662 * specified view are ignored. Both the width and the height of the view are
2663 * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use
2664 * your own layout parameters, invoke
2665 * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)}
2666 * instead.
RoboErik55011652014-07-09 15:05:53 -07002667 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002668 * @param view The desired content to display.
Romain Guy482b34a62011-01-20 10:59:28 -08002669 *
2670 * @see #setContentView(int)
2671 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672 */
2673 public void setContentView(View view) {
2674 getWindow().setContentView(view);
Adam Powelle43340c2014-03-17 19:10:43 -07002675 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 }
2677
2678 /**
2679 * Set the activity content to an explicit view. This view is placed
2680 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08002681 * view hierarchy.
RoboErik55011652014-07-09 15:05:53 -07002682 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 * @param view The desired content to display.
2684 * @param params Layout parameters for the view.
Romain Guy482b34a62011-01-20 10:59:28 -08002685 *
2686 * @see #setContentView(android.view.View)
2687 * @see #setContentView(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 */
2689 public void setContentView(View view, ViewGroup.LayoutParams params) {
2690 getWindow().setContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07002691 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 }
2693
2694 /**
2695 * Add an additional content view to the activity. Added after any existing
2696 * ones in the activity -- existing views are NOT removed.
RoboErik55011652014-07-09 15:05:53 -07002697 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 * @param view The desired content to display.
2699 * @param params Layout parameters for the view.
2700 */
2701 public void addContentView(View view, ViewGroup.LayoutParams params) {
2702 getWindow().addContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07002703 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 }
2705
2706 /**
Adam Powellcfbe9be2013-11-06 14:58:58 -08002707 * Retrieve the {@link TransitionManager} responsible for default transitions in this window.
2708 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2709 *
2710 * <p>This method will return non-null after content has been initialized (e.g. by using
2711 * {@link #setContentView}) if {@link Window#FEATURE_CONTENT_TRANSITIONS} has been granted.</p>
2712 *
2713 * @return This window's content TransitionManager or null if none is set.
2714 */
2715 public TransitionManager getContentTransitionManager() {
2716 return getWindow().getTransitionManager();
2717 }
2718
2719 /**
2720 * Set the {@link TransitionManager} to use for default transitions in this window.
2721 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2722 *
2723 * @param tm The TransitionManager to use for scene changes.
2724 */
2725 public void setContentTransitionManager(TransitionManager tm) {
2726 getWindow().setTransitionManager(tm);
2727 }
2728
2729 /**
2730 * Retrieve the {@link Scene} representing this window's current content.
2731 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2732 *
2733 * <p>This method will return null if the current content is not represented by a Scene.</p>
2734 *
2735 * @return Current Scene being shown or null
2736 */
2737 public Scene getContentScene() {
2738 return getWindow().getContentScene();
2739 }
2740
2741 /**
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002742 * Sets whether this activity is finished when touched outside its window's
2743 * bounds.
2744 */
2745 public void setFinishOnTouchOutside(boolean finish) {
2746 mWindow.setCloseOnTouchOutside(finish);
2747 }
Tor Norbyed9273d62013-05-30 15:59:53 -07002748
2749 /** @hide */
Jeff Sharkey6503bd82017-04-19 23:24:18 -06002750 @IntDef(prefix = { "DEFAULT_KEYS_" }, value = {
Tor Norbyed9273d62013-05-30 15:59:53 -07002751 DEFAULT_KEYS_DISABLE,
2752 DEFAULT_KEYS_DIALER,
2753 DEFAULT_KEYS_SHORTCUT,
2754 DEFAULT_KEYS_SEARCH_LOCAL,
Jeff Sharkey6503bd82017-04-19 23:24:18 -06002755 DEFAULT_KEYS_SEARCH_GLOBAL
2756 })
Tor Norbyed9273d62013-05-30 15:59:53 -07002757 @Retention(RetentionPolicy.SOURCE)
2758 @interface DefaultKeyMode {}
2759
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002760 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 * Use with {@link #setDefaultKeyMode} to turn off default handling of
2762 * keys.
RoboErik55011652014-07-09 15:05:53 -07002763 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 * @see #setDefaultKeyMode
2765 */
2766 static public final int DEFAULT_KEYS_DISABLE = 0;
2767 /**
2768 * Use with {@link #setDefaultKeyMode} to launch the dialer during default
2769 * key handling.
RoboErik55011652014-07-09 15:05:53 -07002770 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 * @see #setDefaultKeyMode
2772 */
2773 static public final int DEFAULT_KEYS_DIALER = 1;
2774 /**
2775 * Use with {@link #setDefaultKeyMode} to execute a menu shortcut in
2776 * default key handling.
RoboErik55011652014-07-09 15:05:53 -07002777 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 * <p>That is, the user does not need to hold down the menu key to execute menu shortcuts.
RoboErik55011652014-07-09 15:05:53 -07002779 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 * @see #setDefaultKeyMode
2781 */
2782 static public final int DEFAULT_KEYS_SHORTCUT = 2;
2783 /**
2784 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
2785 * will start an application-defined search. (If the application or activity does not
2786 * actually define a search, the the keys will be ignored.)
RoboErik55011652014-07-09 15:05:53 -07002787 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07002789 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 * @see #setDefaultKeyMode
2791 */
2792 static public final int DEFAULT_KEYS_SEARCH_LOCAL = 3;
2793
2794 /**
2795 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
2796 * will start a global search (typically web search, but some platforms may define alternate
2797 * methods for global search)
RoboErik55011652014-07-09 15:05:53 -07002798 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07002800 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 * @see #setDefaultKeyMode
2802 */
2803 static public final int DEFAULT_KEYS_SEARCH_GLOBAL = 4;
2804
2805 /**
2806 * Select the default key handling for this activity. This controls what
2807 * will happen to key events that are not otherwise handled. The default
2808 * mode ({@link #DEFAULT_KEYS_DISABLE}) will simply drop them on the
2809 * floor. Other modes allow you to launch the dialer
2810 * ({@link #DEFAULT_KEYS_DIALER}), execute a shortcut in your options
2811 * menu without requiring the menu key be held down
RoboErik55011652014-07-09 15:05:53 -07002812 * ({@link #DEFAULT_KEYS_SHORTCUT}), or launch a search ({@link #DEFAULT_KEYS_SEARCH_LOCAL}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 * and {@link #DEFAULT_KEYS_SEARCH_GLOBAL}).
RoboErik55011652014-07-09 15:05:53 -07002814 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 * <p>Note that the mode selected here does not impact the default
2816 * handling of system keys, such as the "back" and "menu" keys, and your
2817 * activity and its views always get a first chance to receive and handle
2818 * all application keys.
RoboErik55011652014-07-09 15:05:53 -07002819 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 * @param mode The desired default key mode constant.
RoboErik55011652014-07-09 15:05:53 -07002821 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 * @see #onKeyDown
2823 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002824 public final void setDefaultKeyMode(@DefaultKeyMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 mDefaultKeyMode = mode;
RoboErik55011652014-07-09 15:05:53 -07002826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 // Some modes use a SpannableStringBuilder to track & dispatch input events
2828 // This list must remain in sync with the switch in onKeyDown()
2829 switch (mode) {
2830 case DEFAULT_KEYS_DISABLE:
2831 case DEFAULT_KEYS_SHORTCUT:
2832 mDefaultKeySsb = null; // not used in these modes
2833 break;
2834 case DEFAULT_KEYS_DIALER:
2835 case DEFAULT_KEYS_SEARCH_LOCAL:
2836 case DEFAULT_KEYS_SEARCH_GLOBAL:
2837 mDefaultKeySsb = new SpannableStringBuilder();
2838 Selection.setSelection(mDefaultKeySsb,0);
2839 break;
2840 default:
2841 throw new IllegalArgumentException();
2842 }
2843 }
2844
2845 /**
2846 * Called when a key was pressed down and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07002847 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 * is inside a TextView will not trigger the event (unless it is a navigation
2849 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07002850 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 * <p>If the focused view didn't want this event, this method is called.
2852 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07002853 * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK}
2854 * by calling {@link #onBackPressed()}, though the behavior varies based
2855 * on the application compatibility mode: for
2856 * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications,
2857 * it will set up the dispatch to call {@link #onKeyUp} where the action
2858 * will be performed; for earlier applications, it will perform the
2859 * action immediately in on-down, as those versions of the platform
2860 * behaved.
RoboErik55011652014-07-09 15:05:53 -07002861 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07002862 * <p>Other additional default key handling may be performed
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002863 * if configured with {@link #setDefaultKeyMode}.
RoboErik55011652014-07-09 15:05:53 -07002864 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07002866 * further, or <code>false</code> to indicate that you have not handled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 * this event and it should continue to be propagated.
2868 * @see #onKeyUp
2869 * @see android.view.KeyEvent
2870 */
2871 public boolean onKeyDown(int keyCode, KeyEvent event) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002872 if (keyCode == KeyEvent.KEYCODE_BACK) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002873 if (getApplicationInfo().targetSdkVersion
2874 >= Build.VERSION_CODES.ECLAIR) {
2875 event.startTracking();
2876 } else {
2877 onBackPressed();
2878 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 return true;
2880 }
RoboErik55011652014-07-09 15:05:53 -07002881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) {
2883 return false;
2884 } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) {
Jose Lima7a22fc62015-01-23 17:24:22 -08002885 Window w = getWindow();
2886 if (w.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
2887 w.performPanelShortcut(Window.FEATURE_OPTIONS_PANEL, keyCode, event,
2888 Menu.FLAG_ALWAYS_PERFORM_CLOSE)) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002889 return true;
2890 }
2891 return false;
Evan Roskyc46203782017-02-17 13:11:53 -08002892 } else if (keyCode == KeyEvent.KEYCODE_TAB) {
2893 // Don't consume TAB here since it's used for navigation. Arrow keys
2894 // aren't considered "typing keys" so they already won't get consumed.
2895 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 } else {
2897 // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_*
2898 boolean clearSpannable = false;
2899 boolean handled;
2900 if ((event.getRepeatCount() != 0) || event.isSystem()) {
2901 clearSpannable = true;
2902 handled = false;
2903 } else {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002904 handled = TextKeyListener.getInstance().onKeyDown(
2905 null, mDefaultKeySsb, keyCode, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 if (handled && mDefaultKeySsb.length() > 0) {
2907 // something useable has been typed - dispatch it now.
2908
2909 final String str = mDefaultKeySsb.toString();
2910 clearSpannable = true;
RoboErik55011652014-07-09 15:05:53 -07002911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 switch (mDefaultKeyMode) {
2913 case DEFAULT_KEYS_DIALER:
2914 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str));
2915 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
RoboErik55011652014-07-09 15:05:53 -07002916 startActivity(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 break;
2918 case DEFAULT_KEYS_SEARCH_LOCAL:
2919 startSearch(str, false, null, false);
2920 break;
2921 case DEFAULT_KEYS_SEARCH_GLOBAL:
2922 startSearch(str, false, null, true);
2923 break;
2924 }
2925 }
2926 }
2927 if (clearSpannable) {
2928 mDefaultKeySsb.clear();
2929 mDefaultKeySsb.clearSpans();
2930 Selection.setSelection(mDefaultKeySsb,0);
2931 }
2932 return handled;
2933 }
2934 }
2935
2936 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002937 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
2938 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
2939 * the event).
2940 */
2941 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
2942 return false;
2943 }
2944
2945 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 * Called when a key was released and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07002947 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002948 * is inside a TextView will not trigger the event (unless it is a navigation
2949 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07002950 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002951 * <p>The default implementation handles KEYCODE_BACK to stop the activity
2952 * and go back.
RoboErik55011652014-07-09 15:05:53 -07002953 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07002955 * further, or <code>false</code> to indicate that you have not handled
2956 * this event and it should continue to be propagated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 * @see #onKeyDown
2958 * @see KeyEvent
2959 */
2960 public boolean onKeyUp(int keyCode, KeyEvent event) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002961 if (getApplicationInfo().targetSdkVersion
2962 >= Build.VERSION_CODES.ECLAIR) {
2963 if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
2964 && !event.isCanceled()) {
2965 onBackPressed();
2966 return true;
2967 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 return false;
2970 }
2971
2972 /**
2973 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
2974 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
2975 * the event).
2976 */
2977 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
2978 return false;
2979 }
RoboErik55011652014-07-09 15:05:53 -07002980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002982 * Called when the activity has detected the user's press of the back
2983 * key. The default implementation simply finishes the current activity,
2984 * but you can override this to do whatever you want.
2985 */
2986 public void onBackPressed() {
Adam Powell07a74542014-05-30 15:52:44 -07002987 if (mActionBar != null && mActionBar.collapseActionView()) {
2988 return;
2989 }
2990
George Mount27b0dc02017-02-21 10:24:09 -08002991 FragmentManager fragmentManager = mFragments.getFragmentManager();
2992
2993 if (fragmentManager.isStateSaved() || !fragmentManager.popBackStackImmediate()) {
Craig Mautner73f843d2014-05-19 09:42:28 -07002994 finishAfterTransition();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07002995 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002996 }
Jeff Brown64da12a2011-01-04 19:57:47 -08002997
2998 /**
2999 * Called when a key shortcut event is not handled by any of the views in the Activity.
3000 * Override this method to implement global key shortcuts for the Activity.
3001 * Key shortcuts can also be implemented by setting the
3002 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
3003 *
3004 * @param keyCode The value in event.getKeyCode().
3005 * @param event Description of the key event.
3006 * @return True if the key shortcut was handled.
3007 */
3008 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
Abodunrinwa Tokia04b7ad2015-06-30 17:44:04 -07003009 // Let the Action Bar have a chance at handling the shortcut.
3010 ActionBar actionBar = getActionBar();
3011 return (actionBar != null && actionBar.onKeyShortcut(keyCode, event));
Jeff Brown64da12a2011-01-04 19:57:47 -08003012 }
3013
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003014 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003015 * Called when a touch screen event was not handled by any of the views
3016 * under it. This is most useful to process touch events that happen
3017 * outside of your window bounds, where there is no view to receive it.
RoboErik55011652014-07-09 15:05:53 -07003018 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003019 * @param event The touch screen event being processed.
RoboErik55011652014-07-09 15:05:53 -07003020 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 * @return Return true if you have consumed the event, false if you haven't.
3022 * The default implementation always returns false.
3023 */
3024 public boolean onTouchEvent(MotionEvent event) {
Dianne Hackborncfaf8872011-01-18 13:57:54 -08003025 if (mWindow.shouldCloseOnTouch(this, event)) {
3026 finish();
3027 return true;
3028 }
RoboErik55011652014-07-09 15:05:53 -07003029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003030 return false;
3031 }
RoboErik55011652014-07-09 15:05:53 -07003032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033 /**
3034 * Called when the trackball was moved and not handled by any of the
3035 * views inside of the activity. So, for example, if the trackball moves
3036 * while focus is on a button, you will receive a call here because
3037 * buttons do not normally do anything with trackball events. The call
3038 * here happens <em>before</em> trackball movements are converted to
3039 * DPAD key events, which then get sent back to the view hierarchy, and
3040 * will be processed at the point for things like focus navigation.
RoboErik55011652014-07-09 15:05:53 -07003041 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003042 * @param event The trackball event being processed.
RoboErik55011652014-07-09 15:05:53 -07003043 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003044 * @return Return true if you have consumed the event, false if you haven't.
3045 * The default implementation always returns false.
3046 */
3047 public boolean onTrackballEvent(MotionEvent event) {
3048 return false;
3049 }
Jeff Browncb1404e2011-01-15 18:14:15 -08003050
3051 /**
3052 * Called when a generic motion event was not handled by any of the
3053 * views inside of the activity.
3054 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08003055 * Generic motion events describe joystick movements, mouse hovers, track pad
3056 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08003057 * {@link MotionEvent#getSource() source} of the motion event specifies
3058 * the class of input that was received. Implementations of this method
3059 * must examine the bits in the source before processing the event.
3060 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08003061 * </p><p>
3062 * Generic motion events with source class
3063 * {@link android.view.InputDevice#SOURCE_CLASS_POINTER}
3064 * are delivered to the view under the pointer. All other generic motion events are
3065 * delivered to the focused view.
3066 * </p><p>
3067 * See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to
3068 * handle this event.
Jeff Browncb1404e2011-01-15 18:14:15 -08003069 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08003070 *
3071 * @param event The generic motion event being processed.
3072 *
3073 * @return Return true if you have consumed the event, false if you haven't.
3074 * The default implementation always returns false.
3075 */
3076 public boolean onGenericMotionEvent(MotionEvent event) {
3077 return false;
3078 }
3079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003080 /**
3081 * Called whenever a key, touch, or trackball event is dispatched to the
3082 * activity. Implement this method if you wish to know that the user has
3083 * interacted with the device in some way while your activity is running.
3084 * This callback and {@link #onUserLeaveHint} are intended to help
3085 * activities manage status bar notifications intelligently; specifically,
3086 * for helping activities determine the proper time to cancel a notfication.
RoboErik55011652014-07-09 15:05:53 -07003087 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003088 * <p>All calls to your activity's {@link #onUserLeaveHint} callback will
3089 * be accompanied by calls to {@link #onUserInteraction}. This
3090 * ensures that your activity will be told of relevant user activity such
3091 * as pulling down the notification pane and touching an item there.
RoboErik55011652014-07-09 15:05:53 -07003092 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003093 * <p>Note that this callback will be invoked for the touch down action
3094 * that begins a touch gesture, but may not be invoked for the touch-moved
3095 * and touch-up actions that follow.
RoboErik55011652014-07-09 15:05:53 -07003096 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097 * @see #onUserLeaveHint()
3098 */
3099 public void onUserInteraction() {
3100 }
RoboErik55011652014-07-09 15:05:53 -07003101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
3103 // Update window manager if: we have a view, that view is
3104 // attached to its parent (which will be a RootView), and
3105 // this activity is not embedded.
3106 if (mParent == null) {
3107 View decor = mDecor;
3108 if (decor != null && decor.getParent() != null) {
3109 getWindowManager().updateViewLayout(decor, params);
3110 }
3111 }
3112 }
3113
3114 public void onContentChanged() {
3115 }
3116
3117 /**
3118 * Called when the current {@link Window} of the activity gains or loses
3119 * focus. This is the best indicator of whether this activity is visible
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003120 * to the user. The default implementation clears the key tracking
3121 * state, so should always be called.
RoboErik55011652014-07-09 15:05:53 -07003122 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003123 * <p>Note that this provides information about global focus state, which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003124 * is managed independently of activity lifecycles. As such, while focus
3125 * changes will generally have some relation to lifecycle changes (an
3126 * activity that is stopped will not generally get window focus), you
3127 * should not rely on any particular order between the callbacks here and
3128 * those in the other lifecycle methods such as {@link #onResume}.
RoboErik55011652014-07-09 15:05:53 -07003129 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003130 * <p>As a general rule, however, a resumed activity will have window
3131 * focus... unless it has displayed other dialogs or popups that take
3132 * input focus, in which case the activity itself will not have focus
3133 * when the other windows have it. Likewise, the system may display
3134 * system-level windows (such as the status bar notification panel or
3135 * a system alert) which will temporarily take window input focus without
3136 * pausing the foreground activity.
3137 *
3138 * @param hasFocus Whether the window of this activity has focus.
RoboErik55011652014-07-09 15:05:53 -07003139 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003140 * @see #hasWindowFocus()
3141 * @see #onResume
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003142 * @see View#onWindowFocusChanged(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143 */
3144 public void onWindowFocusChanged(boolean hasFocus) {
3145 }
RoboErik55011652014-07-09 15:05:53 -07003146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 /**
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003148 * Called when the main window associated with the activity has been
3149 * attached to the window manager.
3150 * See {@link View#onAttachedToWindow() View.onAttachedToWindow()}
3151 * for more information.
3152 * @see View#onAttachedToWindow
3153 */
3154 public void onAttachedToWindow() {
3155 }
RoboErik55011652014-07-09 15:05:53 -07003156
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003157 /**
3158 * Called when the main window associated with the activity has been
3159 * detached from the window manager.
3160 * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()}
3161 * for more information.
3162 * @see View#onDetachedFromWindow
3163 */
3164 public void onDetachedFromWindow() {
3165 }
RoboErik55011652014-07-09 15:05:53 -07003166
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003167 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168 * Returns true if this activity's <em>main</em> window currently has window focus.
3169 * Note that this is not the same as the view itself having focus.
RoboErik55011652014-07-09 15:05:53 -07003170 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003171 * @return True if this activity's main window currently has window focus.
RoboErik55011652014-07-09 15:05:53 -07003172 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003173 * @see #onWindowAttributesChanged(android.view.WindowManager.LayoutParams)
3174 */
3175 public boolean hasWindowFocus() {
3176 Window w = getWindow();
3177 if (w != null) {
3178 View d = w.getDecorView();
3179 if (d != null) {
3180 return d.hasWindowFocus();
3181 }
3182 }
3183 return false;
3184 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003185
3186 /**
3187 * Called when the main window associated with the activity has been dismissed.
Adam Powell117b6952014-05-05 18:14:56 -07003188 * @hide
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003189 */
Adam Powell117b6952014-05-05 18:14:56 -07003190 @Override
Ned Burns7d6cb912016-12-02 17:25:33 -05003191 public void onWindowDismissed(boolean finishTask, boolean suppressWindowTransition) {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07003192 finish(finishTask ? FINISH_TASK_WITH_ACTIVITY : DONT_FINISH_TASK_WITH_ACTIVITY);
Ned Burns7d6cb912016-12-02 17:25:33 -05003193 if (suppressWindowTransition) {
3194 overridePendingTransition(0, 0);
3195 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003196 }
RoboErik55011652014-07-09 15:05:53 -07003197
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003198
Skuhnece2faa52015-08-11 10:36:38 -07003199 /**
Filip Gruszczynski411c06f2016-01-07 09:44:44 -08003200 * Moves the activity from
3201 * {@link android.app.ActivityManager.StackId#FREEFORM_WORKSPACE_STACK_ID} to
3202 * {@link android.app.ActivityManager.StackId#FULLSCREEN_WORKSPACE_STACK_ID} stack.
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003203 *
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003204 * @hide
3205 */
3206 @Override
Filip Gruszczynski411c06f2016-01-07 09:44:44 -08003207 public void exitFreeformMode() throws RemoteException {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003208 ActivityManager.getService().exitFreeformMode(mToken);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003209 }
3210
3211 /** Returns the current stack Id for the window.
3212 * @hide
3213 */
3214 @Override
3215 public int getWindowStackId() throws RemoteException {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003216 return ActivityManager.getService().getActivityStackId(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 }
3218
3219 /**
Jaewan Kimd98dcab2016-05-03 02:52:18 +09003220 * Puts the activity in picture-in-picture mode if the activity supports.
3221 * @see android.R.attr#supportsPictureInPicture
3222 * @hide
3223 */
3224 @Override
3225 public void enterPictureInPictureModeIfPossible() {
Winson Chungd3395382016-12-13 11:49:09 -08003226 if (mActivityInfo.supportsPictureInPicture()) {
Jaewan Kimd98dcab2016-05-03 02:52:18 +09003227 enterPictureInPictureMode();
3228 }
3229 }
3230
3231 /**
RoboErik55011652014-07-09 15:05:53 -07003232 * Called to process key events. You can override this to intercept all
3233 * key events before they are dispatched to the window. Be sure to call
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 * this implementation for key events that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003235 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003236 * @param event The key event.
RoboErik55011652014-07-09 15:05:53 -07003237 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 * @return boolean Return true if this event was consumed.
3239 */
3240 public boolean dispatchKeyEvent(KeyEvent event) {
3241 onUserInteraction();
Adam Powell07a74542014-05-30 15:52:44 -07003242
3243 // Let action bars open menus in response to the menu key prioritized over
3244 // the window handling it
George Mount5beb26172015-12-15 13:36:01 -08003245 final int keyCode = event.getKeyCode();
3246 if (keyCode == KeyEvent.KEYCODE_MENU &&
Adam Powell07a74542014-05-30 15:52:44 -07003247 mActionBar != null && mActionBar.onMenuKeyEvent(event)) {
3248 return true;
3249 }
3250
Dianne Hackborn8d374262009-09-14 21:21:52 -07003251 Window win = getWindow();
3252 if (win.superDispatchKeyEvent(event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 return true;
3254 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07003255 View decor = mDecor;
3256 if (decor == null) decor = win.getDecorView();
3257 return event.dispatch(this, decor != null
3258 ? decor.getKeyDispatcherState() : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003259 }
3260
3261 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08003262 * Called to process a key shortcut event.
3263 * You can override this to intercept all key shortcut events before they are
3264 * dispatched to the window. Be sure to call this implementation for key shortcut
3265 * events that should be handled normally.
3266 *
3267 * @param event The key shortcut event.
3268 * @return True if this event was consumed.
3269 */
3270 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
3271 onUserInteraction();
3272 if (getWindow().superDispatchKeyShortcutEvent(event)) {
3273 return true;
3274 }
3275 return onKeyShortcut(event.getKeyCode(), event);
3276 }
3277
3278 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003279 * Called to process touch screen events. You can override this to
3280 * intercept all touch screen events before they are dispatched to the
3281 * window. Be sure to call this implementation for touch screen events
3282 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003283 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 * @param ev The touch screen event.
RoboErik55011652014-07-09 15:05:53 -07003285 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 * @return boolean Return true if this event was consumed.
3287 */
3288 public boolean dispatchTouchEvent(MotionEvent ev) {
3289 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
3290 onUserInteraction();
3291 }
3292 if (getWindow().superDispatchTouchEvent(ev)) {
3293 return true;
3294 }
3295 return onTouchEvent(ev);
3296 }
RoboErik55011652014-07-09 15:05:53 -07003297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003298 /**
3299 * Called to process trackball events. You can override this to
3300 * intercept all trackball events before they are dispatched to the
3301 * window. Be sure to call this implementation for trackball events
3302 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003303 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003304 * @param ev The trackball event.
RoboErik55011652014-07-09 15:05:53 -07003305 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 * @return boolean Return true if this event was consumed.
3307 */
3308 public boolean dispatchTrackballEvent(MotionEvent ev) {
3309 onUserInteraction();
3310 if (getWindow().superDispatchTrackballEvent(ev)) {
3311 return true;
3312 }
3313 return onTrackballEvent(ev);
3314 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003315
Jeff Browncb1404e2011-01-15 18:14:15 -08003316 /**
3317 * Called to process generic motion events. You can override this to
3318 * intercept all generic motion events before they are dispatched to the
3319 * window. Be sure to call this implementation for generic motion events
3320 * that should be handled normally.
3321 *
3322 * @param ev The generic motion event.
3323 *
3324 * @return boolean Return true if this event was consumed.
3325 */
3326 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
3327 onUserInteraction();
3328 if (getWindow().superDispatchGenericMotionEvent(ev)) {
3329 return true;
3330 }
3331 return onGenericMotionEvent(ev);
3332 }
3333
svetoslavganov75986cf2009-05-14 22:28:01 -07003334 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3335 event.setClassName(getClass().getName());
3336 event.setPackageName(getPackageName());
3337
3338 LayoutParams params = getWindow().getAttributes();
Romain Guy980a9382010-01-08 15:06:28 -08003339 boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) &&
3340 (params.height == LayoutParams.MATCH_PARENT);
svetoslavganov75986cf2009-05-14 22:28:01 -07003341 event.setFullScreen(isFullScreen);
3342
3343 CharSequence title = getTitle();
3344 if (!TextUtils.isEmpty(title)) {
3345 event.getText().add(title);
3346 }
3347
3348 return true;
3349 }
3350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003351 /**
3352 * Default implementation of
3353 * {@link android.view.Window.Callback#onCreatePanelView}
3354 * for activities. This
3355 * simply returns null so that all panel sub-windows will have the default
3356 * menu behavior.
3357 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003358 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359 public View onCreatePanelView(int featureId) {
3360 return null;
3361 }
3362
3363 /**
3364 * Default implementation of
3365 * {@link android.view.Window.Callback#onCreatePanelMenu}
3366 * for activities. This calls through to the new
3367 * {@link #onCreateOptionsMenu} method for the
3368 * {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
3369 * so that subclasses of Activity don't need to deal with feature codes.
3370 */
3371 public boolean onCreatePanelMenu(int featureId, Menu menu) {
3372 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003373 boolean show = onCreateOptionsMenu(menu);
3374 show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater());
3375 return show;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003376 }
3377 return false;
3378 }
3379
3380 /**
3381 * Default implementation of
3382 * {@link android.view.Window.Callback#onPreparePanel}
3383 * for activities. This
3384 * calls through to the new {@link #onPrepareOptionsMenu} method for the
3385 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
3386 * panel, so that subclasses of
3387 * Activity don't need to deal with feature codes.
3388 */
3389 public boolean onPreparePanel(int featureId, View view, Menu menu) {
3390 if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) {
3391 boolean goforit = onPrepareOptionsMenu(menu);
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003392 goforit |= mFragments.dispatchPrepareOptionsMenu(menu);
Adam Powellef31e7c2012-05-08 09:59:40 -07003393 return goforit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 }
3395 return true;
3396 }
3397
3398 /**
3399 * {@inheritDoc}
RoboErik55011652014-07-09 15:05:53 -07003400 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 * @return The default implementation returns true.
3402 */
3403 public boolean onMenuOpened(int featureId, Menu menu) {
Adam Powell8515ee82010-11-30 14:09:55 -08003404 if (featureId == Window.FEATURE_ACTION_BAR) {
Adam Powelle43340c2014-03-17 19:10:43 -07003405 initWindowDecorActionBar();
Adam Powell049dd3d2010-12-02 13:43:59 -08003406 if (mActionBar != null) {
3407 mActionBar.dispatchMenuVisibilityChanged(true);
3408 } else {
3409 Log.e(TAG, "Tried to open action bar menu with no action bar");
3410 }
Adam Powell8515ee82010-11-30 14:09:55 -08003411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003412 return true;
3413 }
3414
3415 /**
3416 * Default implementation of
3417 * {@link android.view.Window.Callback#onMenuItemSelected}
3418 * for activities. This calls through to the new
3419 * {@link #onOptionsItemSelected} method for the
3420 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
3421 * panel, so that subclasses of
3422 * Activity don't need to deal with feature codes.
3423 */
3424 public boolean onMenuItemSelected(int featureId, MenuItem item) {
zobject686a8052012-12-14 21:11:08 +09003425 CharSequence titleCondensed = item.getTitleCondensed();
3426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427 switch (featureId) {
3428 case Window.FEATURE_OPTIONS_PANEL:
3429 // Put event logging here so it gets called even if subclass
3430 // doesn't call through to superclass's implmeentation of each
3431 // of these methods below
zobject686a8052012-12-14 21:11:08 +09003432 if(titleCondensed != null) {
3433 EventLog.writeEvent(50000, 0, titleCondensed.toString());
3434 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003435 if (onOptionsItemSelected(item)) {
3436 return true;
3437 }
Adam Powell04d58112012-04-09 10:22:12 -07003438 if (mFragments.dispatchOptionsItemSelected(item)) {
3439 return true;
3440 }
3441 if (item.getItemId() == android.R.id.home && mActionBar != null &&
3442 (mActionBar.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
3443 if (mParent == null) {
Adam Powell07304f5292012-05-07 17:32:44 -07003444 return onNavigateUp();
Adam Powell04d58112012-04-09 10:22:12 -07003445 } else {
Adam Powell07304f5292012-05-07 17:32:44 -07003446 return mParent.onNavigateUpFromChild(this);
Adam Powell04d58112012-04-09 10:22:12 -07003447 }
Adam Powell04d58112012-04-09 10:22:12 -07003448 }
3449 return false;
RoboErik55011652014-07-09 15:05:53 -07003450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 case Window.FEATURE_CONTEXT_MENU:
zobject686a8052012-12-14 21:11:08 +09003452 if(titleCondensed != null) {
3453 EventLog.writeEvent(50000, 1, titleCondensed.toString());
3454 }
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07003455 if (onContextItemSelected(item)) {
3456 return true;
3457 }
3458 return mFragments.dispatchContextItemSelected(item);
Adam Powell8515ee82010-11-30 14:09:55 -08003459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003460 default:
3461 return false;
3462 }
3463 }
RoboErik55011652014-07-09 15:05:53 -07003464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003465 /**
3466 * Default implementation of
3467 * {@link android.view.Window.Callback#onPanelClosed(int, Menu)} for
3468 * activities. This calls through to {@link #onOptionsMenuClosed(Menu)}
3469 * method for the {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
3470 * so that subclasses of Activity don't need to deal with feature codes.
3471 * For context menus ({@link Window#FEATURE_CONTEXT_MENU}), the
3472 * {@link #onContextMenuClosed(Menu)} will be called.
3473 */
3474 public void onPanelClosed(int featureId, Menu menu) {
3475 switch (featureId) {
3476 case Window.FEATURE_OPTIONS_PANEL:
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003477 mFragments.dispatchOptionsMenuClosed(menu);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003478 onOptionsMenuClosed(menu);
3479 break;
RoboErik55011652014-07-09 15:05:53 -07003480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003481 case Window.FEATURE_CONTEXT_MENU:
3482 onContextMenuClosed(menu);
3483 break;
Adam Powell8515ee82010-11-30 14:09:55 -08003484
3485 case Window.FEATURE_ACTION_BAR:
Adam Powelle43340c2014-03-17 19:10:43 -07003486 initWindowDecorActionBar();
Adam Powell8515ee82010-11-30 14:09:55 -08003487 mActionBar.dispatchMenuVisibilityChanged(false);
3488 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003489 }
3490 }
3491
3492 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003493 * Declare that the options menu has changed, so should be recreated.
3494 * The {@link #onCreateOptionsMenu(Menu)} method will be called the next
3495 * time it needs to be displayed.
3496 */
3497 public void invalidateOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08003498 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3499 (mActionBar == null || !mActionBar.invalidateOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07003500 mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
3501 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003502 }
RoboErik55011652014-07-09 15:05:53 -07003503
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003504 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003505 * Initialize the contents of the Activity's standard options menu. You
3506 * should place your menu items in to <var>menu</var>.
RoboErik55011652014-07-09 15:05:53 -07003507 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003508 * <p>This is only called once, the first time the options menu is
3509 * displayed. To update the menu every time it is displayed, see
3510 * {@link #onPrepareOptionsMenu}.
RoboErik55011652014-07-09 15:05:53 -07003511 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003512 * <p>The default implementation populates the menu with standard system
RoboErik55011652014-07-09 15:05:53 -07003513 * menu items. These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that
3514 * they will be correctly ordered with application-defined menu items.
3515 * Deriving classes should always call through to the base implementation.
3516 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 * <p>You can safely hold on to <var>menu</var> (and any items created
3518 * from it), making modifications to it as desired, until the next
3519 * time onCreateOptionsMenu() is called.
RoboErik55011652014-07-09 15:05:53 -07003520 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003521 * <p>When you add items to the menu, you can implement the Activity's
3522 * {@link #onOptionsItemSelected} method to handle them there.
RoboErik55011652014-07-09 15:05:53 -07003523 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003524 * @param menu The options menu in which you place your items.
RoboErik55011652014-07-09 15:05:53 -07003525 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003526 * @return You must return true for the menu to be displayed;
3527 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07003528 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003529 * @see #onPrepareOptionsMenu
3530 * @see #onOptionsItemSelected
3531 */
3532 public boolean onCreateOptionsMenu(Menu menu) {
3533 if (mParent != null) {
3534 return mParent.onCreateOptionsMenu(menu);
3535 }
3536 return true;
3537 }
3538
3539 /**
3540 * Prepare the Screen's standard options menu to be displayed. This is
3541 * called right before the menu is shown, every time it is shown. You can
3542 * use this method to efficiently enable/disable items or otherwise
3543 * dynamically modify the contents.
RoboErik55011652014-07-09 15:05:53 -07003544 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003545 * <p>The default implementation updates the system menu items based on the
3546 * activity's state. Deriving classes should always call through to the
3547 * base class implementation.
RoboErik55011652014-07-09 15:05:53 -07003548 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003549 * @param menu The options menu as last shown or first initialized by
3550 * onCreateOptionsMenu().
RoboErik55011652014-07-09 15:05:53 -07003551 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003552 * @return You must return true for the menu to be displayed;
3553 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07003554 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003555 * @see #onCreateOptionsMenu
3556 */
3557 public boolean onPrepareOptionsMenu(Menu menu) {
3558 if (mParent != null) {
3559 return mParent.onPrepareOptionsMenu(menu);
3560 }
3561 return true;
3562 }
3563
3564 /**
3565 * This hook is called whenever an item in your options menu is selected.
3566 * The default implementation simply returns false to have the normal
3567 * processing happen (calling the item's Runnable or sending a message to
3568 * its Handler as appropriate). You can use this method for any items
3569 * for which you would like to do processing without those other
3570 * facilities.
RoboErik55011652014-07-09 15:05:53 -07003571 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003572 * <p>Derived classes should call through to the base class for it to
Adam Powelldd8fab22012-03-22 17:47:27 -07003573 * perform the default menu handling.</p>
RoboErik55011652014-07-09 15:05:53 -07003574 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 * @param item The menu item that was selected.
RoboErik55011652014-07-09 15:05:53 -07003576 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003577 * @return boolean Return false to allow normal menu processing to
3578 * proceed, true to consume it here.
RoboErik55011652014-07-09 15:05:53 -07003579 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003580 * @see #onCreateOptionsMenu
3581 */
3582 public boolean onOptionsItemSelected(MenuItem item) {
3583 if (mParent != null) {
3584 return mParent.onOptionsItemSelected(item);
3585 }
3586 return false;
3587 }
3588
3589 /**
Adam Powelldd8fab22012-03-22 17:47:27 -07003590 * This method is called whenever the user chooses to navigate Up within your application's
3591 * activity hierarchy from the action bar.
3592 *
3593 * <p>If the attribute {@link android.R.attr#parentActivityName parentActivityName}
3594 * was specified in the manifest for this activity or an activity-alias to it,
3595 * default Up navigation will be handled automatically. If any activity
3596 * along the parent chain requires extra Intent arguments, the Activity subclass
3597 * should override the method {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}
3598 * to supply those arguments.</p>
3599 *
Mark Lufa434852016-08-11 17:40:33 -07003600 * <p>See <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back Stack</a>
Adam Powelldd8fab22012-03-22 17:47:27 -07003601 * from the developer guide and <a href="{@docRoot}design/patterns/navigation.html">Navigation</a>
3602 * from the design guide for more information about navigating within your app.</p>
3603 *
3604 * <p>See the {@link TaskStackBuilder} class and the Activity methods
3605 * {@link #getParentActivityIntent()}, {@link #shouldUpRecreateTask(Intent)}, and
3606 * {@link #navigateUpTo(Intent)} for help implementing custom Up navigation.
3607 * The AppNavigation sample application in the Android SDK is also available for reference.</p>
3608 *
3609 * @return true if Up navigation completed successfully and this Activity was finished,
3610 * false otherwise.
3611 */
3612 public boolean onNavigateUp() {
3613 // Automatically handle hierarchical Up navigation if the proper
3614 // metadata is available.
3615 Intent upIntent = getParentActivityIntent();
3616 if (upIntent != null) {
Adam Powell0fc5b2b2012-07-18 18:20:29 -07003617 if (mActivityInfo.taskAffinity == null) {
3618 // Activities with a null affinity are special; they really shouldn't
3619 // specify a parent activity intent in the first place. Just finish
3620 // the current activity and call it a day.
3621 finish();
3622 } else if (shouldUpRecreateTask(upIntent)) {
Adam Powellf78a8442012-05-01 18:09:32 -07003623 TaskStackBuilder b = TaskStackBuilder.create(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07003624 onCreateNavigateUpTaskStack(b);
3625 onPrepareNavigateUpTaskStack(b);
3626 b.startActivities();
Adam Powell3d193d92012-05-15 16:51:55 -07003627
3628 // We can't finishAffinity if we have a result.
3629 // Fall back and simply finish the current activity instead.
3630 if (mResultCode != RESULT_CANCELED || mResultData != null) {
3631 // Tell the developer what's going on to avoid hair-pulling.
3632 Log.i(TAG, "onNavigateUp only finishing topmost activity to return a result");
3633 finish();
3634 } else {
3635 finishAffinity();
3636 }
Adam Powelldd8fab22012-03-22 17:47:27 -07003637 } else {
3638 navigateUpTo(upIntent);
3639 }
3640 return true;
3641 }
3642 return false;
3643 }
3644
3645 /**
3646 * This is called when a child activity of this one attempts to navigate up.
3647 * The default implementation simply calls onNavigateUp() on this activity (the parent).
3648 *
3649 * @param child The activity making the call.
3650 */
3651 public boolean onNavigateUpFromChild(Activity child) {
3652 return onNavigateUp();
3653 }
3654
3655 /**
3656 * Define the synthetic task stack that will be generated during Up navigation from
3657 * a different task.
3658 *
3659 * <p>The default implementation of this method adds the parent chain of this activity
3660 * as specified in the manifest to the supplied {@link TaskStackBuilder}. Applications
3661 * may choose to override this method to construct the desired task stack in a different
3662 * way.</p>
3663 *
Adam Powellf0195952012-05-02 21:38:54 -07003664 * <p>This method will be invoked by the default implementation of {@link #onNavigateUp()}
3665 * if {@link #shouldUpRecreateTask(Intent)} returns true when supplied with the intent
3666 * returned by {@link #getParentActivityIntent()}.</p>
3667 *
Adam Powelldd8fab22012-03-22 17:47:27 -07003668 * <p>Applications that wish to supply extra Intent parameters to the parent stack defined
3669 * by the manifest should override {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}.</p>
3670 *
3671 * @param builder An empty TaskStackBuilder - the application should add intents representing
3672 * the desired task stack
3673 */
3674 public void onCreateNavigateUpTaskStack(TaskStackBuilder builder) {
3675 builder.addParentStack(this);
3676 }
3677
3678 /**
3679 * Prepare the synthetic task stack that will be generated during Up navigation
3680 * from a different task.
3681 *
3682 * <p>This method receives the {@link TaskStackBuilder} with the constructed series of
3683 * Intents as generated by {@link #onCreateNavigateUpTaskStack(TaskStackBuilder)}.
3684 * If any extra data should be added to these intents before launching the new task,
3685 * the application should override this method and add that data here.</p>
3686 *
3687 * @param builder A TaskStackBuilder that has been populated with Intents by
3688 * onCreateNavigateUpTaskStack.
3689 */
3690 public void onPrepareNavigateUpTaskStack(TaskStackBuilder builder) {
3691 }
3692
3693 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003694 * This hook is called whenever the options menu is being closed (either by the user canceling
3695 * the menu with the back/menu button, or when an item is selected).
RoboErik55011652014-07-09 15:05:53 -07003696 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003697 * @param menu The options menu as last shown or first initialized by
3698 * onCreateOptionsMenu().
3699 */
3700 public void onOptionsMenuClosed(Menu menu) {
3701 if (mParent != null) {
3702 mParent.onOptionsMenuClosed(menu);
3703 }
3704 }
RoboErik55011652014-07-09 15:05:53 -07003705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003706 /**
3707 * Programmatically opens the options menu. If the options menu is already
3708 * open, this method does nothing.
3709 */
3710 public void openOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08003711 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3712 (mActionBar == null || !mActionBar.openOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07003713 mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null);
3714 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003715 }
RoboErik55011652014-07-09 15:05:53 -07003716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003717 /**
3718 * Progammatically closes the options menu. If the options menu is already
3719 * closed, this method does nothing.
3720 */
3721 public void closeOptionsMenu() {
Evan Rosky41823d12017-03-21 14:42:35 -07003722 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3723 (mActionBar == null || !mActionBar.closeOptionsMenu())) {
Jose Lima7a22fc62015-01-23 17:24:22 -08003724 mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL);
3725 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003726 }
3727
3728 /**
3729 * Called when a context menu for the {@code view} is about to be shown.
3730 * Unlike {@link #onCreateOptionsMenu(Menu)}, this will be called every
3731 * time the context menu is about to be shown and should be populated for
3732 * the view (or item inside the view for {@link AdapterView} subclasses,
3733 * this can be found in the {@code menuInfo})).
3734 * <p>
3735 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
3736 * item has been selected.
3737 * <p>
3738 * It is not safe to hold onto the context menu after this method returns.
Scott Maincdf51062013-01-08 20:03:05 -08003739 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003740 */
3741 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
3742 }
3743
3744 /**
3745 * Registers a context menu to be shown for the given view (multiple views
3746 * can show the context menu). This method will set the
3747 * {@link OnCreateContextMenuListener} on the view to this activity, so
3748 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
3749 * called when it is time to show the context menu.
RoboErik55011652014-07-09 15:05:53 -07003750 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003751 * @see #unregisterForContextMenu(View)
3752 * @param view The view that should show a context menu.
3753 */
3754 public void registerForContextMenu(View view) {
3755 view.setOnCreateContextMenuListener(this);
3756 }
RoboErik55011652014-07-09 15:05:53 -07003757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003758 /**
3759 * Prevents a context menu to be shown for the given view. This method will remove the
3760 * {@link OnCreateContextMenuListener} on the view.
RoboErik55011652014-07-09 15:05:53 -07003761 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003762 * @see #registerForContextMenu(View)
3763 * @param view The view that should stop showing a context menu.
3764 */
3765 public void unregisterForContextMenu(View view) {
3766 view.setOnCreateContextMenuListener(null);
3767 }
RoboErik55011652014-07-09 15:05:53 -07003768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003769 /**
3770 * Programmatically opens the context menu for a particular {@code view}.
3771 * The {@code view} should have been added via
3772 * {@link #registerForContextMenu(View)}.
RoboErik55011652014-07-09 15:05:53 -07003773 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003774 * @param view The view to show the context menu for.
3775 */
3776 public void openContextMenu(View view) {
3777 view.showContextMenu();
3778 }
RoboErik55011652014-07-09 15:05:53 -07003779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003780 /**
3781 * Programmatically closes the most recently opened context menu, if showing.
3782 */
3783 public void closeContextMenu() {
Jose Limabd769a12015-01-28 10:43:15 -08003784 if (mWindow.hasFeature(Window.FEATURE_CONTEXT_MENU)) {
3785 mWindow.closePanel(Window.FEATURE_CONTEXT_MENU);
3786 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003787 }
RoboErik55011652014-07-09 15:05:53 -07003788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003789 /**
3790 * This hook is called whenever an item in a context menu is selected. The
3791 * default implementation simply returns false to have the normal processing
3792 * happen (calling the item's Runnable or sending a message to its Handler
3793 * as appropriate). You can use this method for any items for which you
3794 * would like to do processing without those other facilities.
3795 * <p>
3796 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
3797 * View that added this menu item.
3798 * <p>
3799 * Derived classes should call through to the base class for it to perform
3800 * the default menu handling.
RoboErik55011652014-07-09 15:05:53 -07003801 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003802 * @param item The context menu item that was selected.
3803 * @return boolean Return false to allow normal context menu processing to
3804 * proceed, true to consume it here.
3805 */
3806 public boolean onContextItemSelected(MenuItem item) {
3807 if (mParent != null) {
3808 return mParent.onContextItemSelected(item);
3809 }
3810 return false;
3811 }
3812
3813 /**
3814 * This hook is called whenever the context menu is being closed (either by
3815 * the user canceling the menu with the back/menu button, or when an item is
3816 * selected).
RoboErik55011652014-07-09 15:05:53 -07003817 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003818 * @param menu The context menu that is being closed.
3819 */
3820 public void onContextMenuClosed(Menu menu) {
3821 if (mParent != null) {
3822 mParent.onContextMenuClosed(menu);
3823 }
3824 }
3825
3826 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003827 * @deprecated Old no-arguments version of {@link #onCreateDialog(int, Bundle)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003828 */
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003829 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003830 protected Dialog onCreateDialog(int id) {
3831 return null;
3832 }
3833
3834 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003835 * Callback for creating dialogs that are managed (saved and restored) for you
3836 * by the activity. The default implementation calls through to
3837 * {@link #onCreateDialog(int)} for compatibility.
3838 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003839 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3840 * or later, consider instead using a {@link DialogFragment} instead.</em>
3841 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003842 * <p>If you use {@link #showDialog(int)}, the activity will call through to
3843 * this method the first time, and hang onto it thereafter. Any dialog
3844 * that is created by this method will automatically be saved and restored
3845 * for you, including whether it is showing.
3846 *
3847 * <p>If you would like the activity to manage saving and restoring dialogs
3848 * for you, you should override this method and handle any ids that are
3849 * passed to {@link #showDialog}.
3850 *
3851 * <p>If you would like an opportunity to prepare your dialog before it is shown,
3852 * override {@link #onPrepareDialog(int, Dialog, Bundle)}.
3853 *
3854 * @param id The id of the dialog.
3855 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
3856 * @return The dialog. If you return null, the dialog will not be created.
3857 *
3858 * @see #onPrepareDialog(int, Dialog, Bundle)
3859 * @see #showDialog(int, Bundle)
3860 * @see #dismissDialog(int)
3861 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003862 *
3863 * @deprecated Use the new {@link DialogFragment} class with
3864 * {@link FragmentManager} instead; this is also
3865 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003866 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003867 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07003868 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003869 protected Dialog onCreateDialog(int id, Bundle args) {
3870 return onCreateDialog(id);
3871 }
3872
3873 /**
3874 * @deprecated Old no-arguments version of
3875 * {@link #onPrepareDialog(int, Dialog, Bundle)}.
3876 */
3877 @Deprecated
3878 protected void onPrepareDialog(int id, Dialog dialog) {
3879 dialog.setOwnerActivity(this);
3880 }
3881
3882 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003883 * Provides an opportunity to prepare a managed dialog before it is being
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003884 * shown. The default implementation calls through to
3885 * {@link #onPrepareDialog(int, Dialog)} for compatibility.
RoboErik55011652014-07-09 15:05:53 -07003886 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003887 * <p>
3888 * Override this if you need to update a managed dialog based on the state
3889 * of the application each time it is shown. For example, a time picker
3890 * dialog might want to be updated with the current time. You should call
3891 * through to the superclass's implementation. The default implementation
3892 * will set this Activity as the owner activity on the Dialog.
RoboErik55011652014-07-09 15:05:53 -07003893 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003894 * @param id The id of the managed dialog.
3895 * @param dialog The dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003896 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
3897 * @see #onCreateDialog(int, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003898 * @see #showDialog(int)
3899 * @see #dismissDialog(int)
3900 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003901 *
3902 * @deprecated Use the new {@link DialogFragment} class with
3903 * {@link FragmentManager} instead; this is also
3904 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003905 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003906 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003907 protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
3908 onPrepareDialog(id, dialog);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003909 }
3910
3911 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003912 * Simple version of {@link #showDialog(int, Bundle)} that does not
3913 * take any arguments. Simply calls {@link #showDialog(int, Bundle)}
3914 * with null arguments.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003915 *
3916 * @deprecated Use the new {@link DialogFragment} class with
3917 * {@link FragmentManager} instead; this is also
3918 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003919 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003920 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003921 public final void showDialog(int id) {
3922 showDialog(id, null);
3923 }
3924
3925 /**
3926 * Show a dialog managed by this activity. A call to {@link #onCreateDialog(int, Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003927 * will be made with the same id the first time this is called for a given
3928 * id. From thereafter, the dialog will be automatically saved and restored.
3929 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003930 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3931 * or later, consider instead using a {@link DialogFragment} instead.</em>
3932 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003933 * <p>Each time a dialog is shown, {@link #onPrepareDialog(int, Dialog, Bundle)} will
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003934 * be made to provide an opportunity to do any timely preparation.
3935 *
3936 * @param id The id of the managed dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003937 * @param args Arguments to pass through to the dialog. These will be saved
3938 * and restored for you. Note that if the dialog is already created,
3939 * {@link #onCreateDialog(int, Bundle)} will not be called with the new
3940 * arguments but {@link #onPrepareDialog(int, Dialog, Bundle)} will be.
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08003941 * If you need to rebuild the dialog, call {@link #removeDialog(int)} first.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003942 * @return Returns true if the Dialog was created; false is returned if
3943 * it is not created because {@link #onCreateDialog(int, Bundle)} returns false.
RoboErik55011652014-07-09 15:05:53 -07003944 *
Joe Onorato37296dc2009-07-31 17:58:55 -07003945 * @see Dialog
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003946 * @see #onCreateDialog(int, Bundle)
3947 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003948 * @see #dismissDialog(int)
3949 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003950 *
3951 * @deprecated Use the new {@link DialogFragment} class with
3952 * {@link FragmentManager} instead; this is also
3953 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003954 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003955 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003956 public final boolean showDialog(int id, Bundle args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003957 if (mManagedDialogs == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003958 mManagedDialogs = new SparseArray<ManagedDialog>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003959 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003960 ManagedDialog md = mManagedDialogs.get(id);
3961 if (md == null) {
3962 md = new ManagedDialog();
3963 md.mDialog = createDialog(id, null, args);
3964 if (md.mDialog == null) {
3965 return false;
3966 }
3967 mManagedDialogs.put(id, md);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003968 }
RoboErik55011652014-07-09 15:05:53 -07003969
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003970 md.mArgs = args;
3971 onPrepareDialog(id, md.mDialog, args);
3972 md.mDialog.show();
3973 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003974 }
3975
3976 /**
3977 * Dismiss a dialog that was previously shown via {@link #showDialog(int)}.
3978 *
3979 * @param id The id of the managed dialog.
3980 *
3981 * @throws IllegalArgumentException if the id was not previously shown via
3982 * {@link #showDialog(int)}.
3983 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003984 * @see #onCreateDialog(int, Bundle)
3985 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003986 * @see #showDialog(int)
3987 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003988 *
3989 * @deprecated Use the new {@link DialogFragment} class with
3990 * {@link FragmentManager} instead; this is also
3991 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003992 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003993 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003994 public final void dismissDialog(int id) {
3995 if (mManagedDialogs == null) {
3996 throw missingDialog(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003997 }
RoboErik55011652014-07-09 15:05:53 -07003998
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003999 final ManagedDialog md = mManagedDialogs.get(id);
4000 if (md == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004001 throw missingDialog(id);
4002 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004003 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004004 }
4005
4006 /**
4007 * Creates an exception to throw if a user passed in a dialog id that is
4008 * unexpected.
4009 */
4010 private IllegalArgumentException missingDialog(int id) {
4011 return new IllegalArgumentException("no dialog with id " + id + " was ever "
4012 + "shown via Activity#showDialog");
4013 }
4014
4015 /**
4016 * Removes any internal references to a dialog managed by this Activity.
4017 * If the dialog is showing, it will dismiss it as part of the clean up.
4018 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004019 * <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 -08004020 * want to avoid the overhead of saving and restoring it in the future.
4021 *
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07004022 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, this function
4023 * will not throw an exception if you try to remove an ID that does not
4024 * currently have an associated dialog.</p>
RoboErik55011652014-07-09 15:05:53 -07004025 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004026 * @param id The id of the managed dialog.
4027 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004028 * @see #onCreateDialog(int, Bundle)
4029 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004030 * @see #showDialog(int)
4031 * @see #dismissDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004032 *
4033 * @deprecated Use the new {@link DialogFragment} class with
4034 * {@link FragmentManager} instead; this is also
4035 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004036 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004037 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004038 public final void removeDialog(int id) {
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07004039 if (mManagedDialogs != null) {
4040 final ManagedDialog md = mManagedDialogs.get(id);
4041 if (md != null) {
4042 md.mDialog.dismiss();
4043 mManagedDialogs.remove(id);
4044 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004045 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004046 }
4047
4048 /**
4049 * This hook is called when the user signals the desire to start a search.
RoboErik55011652014-07-09 15:05:53 -07004050 *
Bjorn Bringert6266e402009-09-25 14:25:41 +01004051 * <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 -07004052 * menu item, search button, or other widgets within your activity. Unless overidden,
Bjorn Bringert6266e402009-09-25 14:25:41 +01004053 * calling this function is the same as calling
4054 * {@link #startSearch startSearch(null, false, null, false)}, which launches
4055 * search for the current activity as specified in its manifest, see {@link SearchManager}.
RoboErik55011652014-07-09 15:05:53 -07004056 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004057 * <p>You can override this function to force global search, e.g. in response to a dedicated
4058 * search key, or to block search entirely (by simply returning false).
RoboErik55011652014-07-09 15:05:53 -07004059 *
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004060 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_TELEVISION}, the default
4061 * implementation changes to simply return false and you must supply your own custom
4062 * implementation if you want to support search.</p>
4063 *
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004064 * @param searchEvent The {@link SearchEvent} that signaled this search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004065 * @return Returns {@code true} if search launched, and {@code false} if the activity does
4066 * not respond to search. The default implementation always returns {@code true}, except
4067 * when in {@link Configuration#UI_MODE_TYPE_TELEVISION} mode where it returns false.
RoboErik55011652014-07-09 15:05:53 -07004068 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004069 * @see android.app.SearchManager
4070 */
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004071 public boolean onSearchRequested(@Nullable SearchEvent searchEvent) {
4072 mSearchEvent = searchEvent;
4073 boolean result = onSearchRequested();
4074 mSearchEvent = null;
4075 return result;
4076 }
4077
4078 /**
4079 * @see #onSearchRequested(SearchEvent)
4080 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004081 public boolean onSearchRequested() {
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004082 if ((getResources().getConfiguration().uiMode&Configuration.UI_MODE_TYPE_MASK)
4083 != Configuration.UI_MODE_TYPE_TELEVISION) {
4084 startSearch(null, false, null, false);
4085 return true;
4086 } else {
4087 return false;
4088 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004089 }
RoboErik55011652014-07-09 15:05:53 -07004090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004091 /**
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004092 * During the onSearchRequested() callbacks, this function will return the
4093 * {@link SearchEvent} that triggered the callback, if it exists.
4094 *
4095 * @return SearchEvent The SearchEvent that triggered the {@link
4096 * #onSearchRequested} callback.
4097 */
4098 public final SearchEvent getSearchEvent() {
4099 return mSearchEvent;
4100 }
4101
4102 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004103 * This hook is called to launch the search UI.
RoboErik55011652014-07-09 15:05:53 -07004104 *
4105 * <p>It is typically called from onSearchRequested(), either directly from
4106 * Activity.onSearchRequested() or from an overridden version in any given
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004107 * Activity. If your goal is simply to activate search, it is preferred to call
Tor Norbyed9273d62013-05-30 15:59:53 -07004108 * onSearchRequested(), which may have been overridden elsewhere in your Activity. If your goal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004109 * is to inject specific data such as context data, it is preferred to <i>override</i>
4110 * onSearchRequested(), so that any callers to it will benefit from the override.
RoboErik55011652014-07-09 15:05:53 -07004111 *
4112 * @param initialQuery Any non-null non-empty string will be inserted as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004113 * pre-entered text in the search query box.
Tor Norbyed9273d62013-05-30 15:59:53 -07004114 * @param selectInitialQuery If true, the initial query will be preselected, which means that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004115 * any further typing will replace it. This is useful for cases where an entire pre-formed
4116 * query is being inserted. If false, the selection point will be placed at the end of the
4117 * inserted query. This is useful when the inserted query is text that the user entered,
4118 * and the user would expect to be able to keep typing. <i>This parameter is only meaningful
4119 * if initialQuery is a non-empty string.</i>
RoboErik55011652014-07-09 15:05:53 -07004120 * @param appSearchData An application can insert application-specific
4121 * context here, in order to improve quality or specificity of its own
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004122 * searches. This data will be returned with SEARCH intent(s). Null if
4123 * no extra data is required.
4124 * @param globalSearch If false, this will only launch the search that has been specifically
RoboErik55011652014-07-09 15:05:53 -07004125 * defined by the application (which is usually defined as a local search). If no default
Mike LeBeaucfa419b2009-08-17 10:56:02 -07004126 * search is defined in the current application or activity, global search will be launched.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004127 * If true, this will always launch a platform-global (e.g. web-based) search instead.
RoboErik55011652014-07-09 15:05:53 -07004128 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004129 * @see android.app.SearchManager
4130 * @see #onSearchRequested
4131 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004132 public void startSearch(@Nullable String initialQuery, boolean selectInitialQuery,
4133 @Nullable Bundle appSearchData, boolean globalSearch) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07004134 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01004135 mSearchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
RoboErik55011652014-07-09 15:05:53 -07004136 appSearchData, globalSearch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004137 }
4138
4139 /**
krosaend2d60142009-08-17 08:56:48 -07004140 * Similar to {@link #startSearch}, but actually fires off the search query after invoking
4141 * the search dialog. Made available for testing purposes.
4142 *
4143 * @param query The query to trigger. If empty, the request will be ignored.
4144 * @param appSearchData An application can insert application-specific
4145 * context here, in order to improve quality or specificity of its own
4146 * searches. This data will be returned with SEARCH intent(s). Null if
4147 * no extra data is required.
krosaend2d60142009-08-17 08:56:48 -07004148 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004149 public void triggerSearch(String query, @Nullable Bundle appSearchData) {
krosaend2d60142009-08-17 08:56:48 -07004150 ensureSearchManager();
Bjorn Bringertb782a2f2009-10-01 09:57:33 +01004151 mSearchManager.triggerSearch(query, getComponentName(), appSearchData);
krosaend2d60142009-08-17 08:56:48 -07004152 }
4153
4154 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004155 * Request that key events come to this activity. Use this if your
4156 * activity has no views with focus, but the activity still wants
4157 * a chance to process key events.
RoboErik55011652014-07-09 15:05:53 -07004158 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004159 * @see android.view.Window#takeKeyEvents
4160 */
4161 public void takeKeyEvents(boolean get) {
4162 getWindow().takeKeyEvents(get);
4163 }
4164
4165 /**
4166 * Enable extended window features. This is a convenience for calling
4167 * {@link android.view.Window#requestFeature getWindow().requestFeature()}.
RoboErik55011652014-07-09 15:05:53 -07004168 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004169 * @param featureId The desired feature as defined in
4170 * {@link android.view.Window}.
4171 * @return Returns true if the requested feature is supported and now
4172 * enabled.
RoboErik55011652014-07-09 15:05:53 -07004173 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004174 * @see android.view.Window#requestFeature
4175 */
4176 public final boolean requestWindowFeature(int featureId) {
4177 return getWindow().requestFeature(featureId);
4178 }
4179
4180 /**
4181 * Convenience for calling
4182 * {@link android.view.Window#setFeatureDrawableResource}.
4183 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07004184 public final void setFeatureDrawableResource(int featureId, @DrawableRes int resId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004185 getWindow().setFeatureDrawableResource(featureId, resId);
4186 }
4187
4188 /**
4189 * Convenience for calling
4190 * {@link android.view.Window#setFeatureDrawableUri}.
4191 */
4192 public final void setFeatureDrawableUri(int featureId, Uri uri) {
4193 getWindow().setFeatureDrawableUri(featureId, uri);
4194 }
4195
4196 /**
4197 * Convenience for calling
4198 * {@link android.view.Window#setFeatureDrawable(int, Drawable)}.
4199 */
4200 public final void setFeatureDrawable(int featureId, Drawable drawable) {
4201 getWindow().setFeatureDrawable(featureId, drawable);
4202 }
4203
4204 /**
4205 * Convenience for calling
4206 * {@link android.view.Window#setFeatureDrawableAlpha}.
4207 */
4208 public final void setFeatureDrawableAlpha(int featureId, int alpha) {
4209 getWindow().setFeatureDrawableAlpha(featureId, alpha);
4210 }
4211
4212 /**
4213 * Convenience for calling
4214 * {@link android.view.Window#getLayoutInflater}.
4215 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004216 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004217 public LayoutInflater getLayoutInflater() {
4218 return getWindow().getLayoutInflater();
4219 }
4220
4221 /**
4222 * Returns a {@link MenuInflater} with this context.
4223 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004224 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004225 public MenuInflater getMenuInflater() {
Adam Powell88ab6972011-07-28 11:25:01 -07004226 // Make sure that action views can get an appropriate theme.
4227 if (mMenuInflater == null) {
Adam Powelle43340c2014-03-17 19:10:43 -07004228 initWindowDecorActionBar();
Adam Powell88ab6972011-07-28 11:25:01 -07004229 if (mActionBar != null) {
Dianne Hackborn92751972012-05-18 19:22:14 -07004230 mMenuInflater = new MenuInflater(mActionBar.getThemedContext(), this);
Adam Powell88ab6972011-07-28 11:25:01 -07004231 } else {
4232 mMenuInflater = new MenuInflater(this);
4233 }
4234 }
4235 return mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004236 }
4237
4238 @Override
Filip Gruszczynski0daf2102015-09-29 08:39:07 -07004239 public void setTheme(int resid) {
4240 super.setTheme(resid);
4241 mWindow.setTheme(resid);
4242 }
4243
4244 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07004245 protected void onApplyThemeResource(Resources.Theme theme, @StyleRes int resid,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004246 boolean first) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004247 if (mParent == null) {
4248 super.onApplyThemeResource(theme, resid, first);
4249 } else {
4250 try {
4251 theme.setTo(mParent.getTheme());
4252 } catch (Exception e) {
4253 // Empty
4254 }
4255 theme.applyStyle(resid, false);
4256 }
Winson073a5262016-02-29 16:04:40 -08004257
4258 // Get the primary color and update the TaskDescription for this activity
4259 TypedArray a = theme.obtainStyledAttributes(
4260 com.android.internal.R.styleable.ActivityTaskDescription);
4261 if (mTaskDescription.getPrimaryColor() == 0) {
4262 int colorPrimary = a.getColor(
4263 com.android.internal.R.styleable.ActivityTaskDescription_colorPrimary, 0);
4264 if (colorPrimary != 0 && Color.alpha(colorPrimary) == 0xFF) {
4265 mTaskDescription.setPrimaryColor(colorPrimary);
4266 }
4267 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02004268
4269 int colorBackground = a.getColor(
4270 com.android.internal.R.styleable.ActivityTaskDescription_colorBackground, 0);
4271 if (colorBackground != 0 && Color.alpha(colorBackground) == 0xFF) {
4272 mTaskDescription.setBackgroundColor(colorBackground);
Winson073a5262016-02-29 16:04:40 -08004273 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02004274
4275 final int statusBarColor = a.getColor(
4276 com.android.internal.R.styleable.ActivityTaskDescription_statusBarColor, 0);
4277 if (statusBarColor != 0) {
4278 mTaskDescription.setStatusBarColor(statusBarColor);
4279 }
4280
4281 final int navigationBarColor = a.getColor(
4282 com.android.internal.R.styleable.ActivityTaskDescription_navigationBarColor, 0);
4283 if (navigationBarColor != 0) {
4284 mTaskDescription.setNavigationBarColor(navigationBarColor);
4285 }
4286
Winson073a5262016-02-29 16:04:40 -08004287 a.recycle();
4288 setTaskDescription(mTaskDescription);
Winson Chung1af8eda2016-02-05 17:55:56 +00004289 }
4290
4291 /**
Svetoslavc6d1c342015-02-26 14:44:43 -08004292 * Requests permissions to be granted to this application. These permissions
4293 * must be requested in your manifest, they should not be granted to your app,
4294 * and they should have protection level {@link android.content.pm.PermissionInfo
4295 * #PROTECTION_DANGEROUS dangerous}, regardless whether they are declared by
4296 * the platform or a third-party app.
4297 * <p>
4298 * Normal permissions {@link android.content.pm.PermissionInfo#PROTECTION_NORMAL}
4299 * are granted at install time if requested in the manifest. Signature permissions
4300 * {@link android.content.pm.PermissionInfo#PROTECTION_SIGNATURE} are granted at
4301 * install time if requested in the manifest and the signature of your app matches
4302 * the signature of the app declaring the permissions.
4303 * </p>
4304 * <p>
4305 * If your app does not have the requested permissions the user will be presented
4306 * with UI for accepting them. After the user has accepted or rejected the
4307 * requested permissions you will receive a callback on {@link
4308 * #onRequestPermissionsResult(int, String[], int[])} reporting whether the
4309 * permissions were granted or not.
4310 * </p>
4311 * <p>
4312 * Note that requesting a permission does not guarantee it will be granted and
4313 * your app should be able to run without having this permission.
4314 * </p>
4315 * <p>
4316 * This method may start an activity allowing the user to choose which permissions
4317 * to grant and which to reject. Hence, you should be prepared that your activity
4318 * may be paused and resumed. Further, granting some permissions may require
4319 * a restart of you application. In such a case, the system will recreate the
4320 * activity stack before delivering the result to {@link
4321 * #onRequestPermissionsResult(int, String[], int[])}.
4322 * </p>
4323 * <p>
4324 * When checking whether you have a permission you should use {@link
4325 * #checkSelfPermission(String)}.
4326 * </p>
4327 * <p>
Svet Ganovd7ad54e2015-08-21 06:18:09 -07004328 * Calling this API for permissions already granted to your app would show UI
4329 * to the user to decide whether the app can still hold these permissions. This
4330 * can be useful if the way your app uses data guarded by the permissions
4331 * changes significantly.
4332 * </p>
4333 * <p>
Svet Ganov255aece2015-07-03 16:03:53 -07004334 * You cannot request a permission if your activity sets {@link
4335 * android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
4336 * <code>true</code> because in this case the activity would not receive
4337 * result callbacks including {@link #onRequestPermissionsResult(int, String[], int[])}.
4338 * </p>
4339 * <p>
Trevor Johns682c24e2016-04-12 10:13:47 -07004340 * The <a href="http://developer.android.com/samples/RuntimePermissions/index.html">
4341 * RuntimePermissions</a> sample app demonstrates how to use this method to
4342 * request permissions at run time.
Svetoslavc6d1c342015-02-26 14:44:43 -08004343 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08004344 *
Svet Ganovf66381c2016-02-18 20:02:36 -08004345 * @param permissions The requested permissions. Must me non-null and not empty.
Svetoslavc6d1c342015-02-26 14:44:43 -08004346 * @param requestCode Application specific request code to match with a result
4347 * reported to {@link #onRequestPermissionsResult(int, String[], int[])}.
Svet Ganov255aece2015-07-03 16:03:53 -07004348 * Should be >= 0.
Svetoslavc6d1c342015-02-26 14:44:43 -08004349 *
Paul Miller77a0e132016-02-11 13:46:49 -08004350 * @throws IllegalArgumentException if requestCode is negative.
4351 *
Svetoslavc6d1c342015-02-26 14:44:43 -08004352 * @see #onRequestPermissionsResult(int, String[], int[])
4353 * @see #checkSelfPermission(String)
Svetoslavaa048b62015-06-02 15:08:48 -07004354 * @see #shouldShowRequestPermissionRationale(String)
Svetoslavc6d1c342015-02-26 14:44:43 -08004355 */
4356 public final void requestPermissions(@NonNull String[] permissions, int requestCode) {
Paul Miller77a0e132016-02-11 13:46:49 -08004357 if (requestCode < 0) {
4358 throw new IllegalArgumentException("requestCode should be >= 0");
4359 }
Svetoslavffb32b12015-10-15 16:54:00 -07004360 if (mHasCurrentPermissionsRequest) {
4361 Log.w(TAG, "Can reqeust only one set of permissions at a time");
4362 // Dispatch the callback with empty arrays which means a cancellation.
4363 onRequestPermissionsResult(requestCode, new String[0], new int[0]);
4364 return;
4365 }
Svetoslavc6d1c342015-02-26 14:44:43 -08004366 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
Svetoslav970b59c2015-06-09 16:05:21 -07004367 startActivityForResult(REQUEST_PERMISSIONS_WHO_PREFIX, intent, requestCode, null);
Svetoslavffb32b12015-10-15 16:54:00 -07004368 mHasCurrentPermissionsRequest = true;
Svetoslavc6d1c342015-02-26 14:44:43 -08004369 }
4370
4371 /**
4372 * Callback for the result from requesting permissions. This method
4373 * is invoked for every call on {@link #requestPermissions(String[], int)}.
Svet Ganov0f45c7c2015-07-16 16:28:51 -07004374 * <p>
4375 * <strong>Note:</strong> It is possible that the permissions request interaction
4376 * with the user is interrupted. In this case you will receive empty permissions
4377 * and results arrays which should be treated as a cancellation.
4378 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08004379 *
4380 * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}.
4381 * @param permissions The requested permissions. Never null.
4382 * @param grantResults The grant results for the corresponding permissions
4383 * which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED}
4384 * or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null.
4385 *
4386 * @see #requestPermissions(String[], int)
4387 */
4388 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
4389 @NonNull int[] grantResults) {
4390 /* callback - no nothing */
4391 }
4392
4393 /**
Svetoslav20770dd2015-05-29 15:43:04 -07004394 * Gets whether you should show UI with rationale for requesting a permission.
4395 * You should do this only if you do not have the permission and the context in
4396 * which the permission is requested does not clearly communicate to the user
4397 * what would be the benefit from granting this permission.
4398 * <p>
4399 * For example, if you write a camera app, requesting the camera permission
4400 * would be expected by the user and no rationale for why it is requested is
4401 * needed. If however, the app needs location for tagging photos then a non-tech
4402 * savvy user may wonder how location is related to taking photos. In this case
4403 * you may choose to show UI with rationale of requesting this permission.
4404 * </p>
4405 *
4406 * @param permission A permission your app wants to request.
4407 * @return Whether you can show permission rationale UI.
4408 *
4409 * @see #checkSelfPermission(String)
4410 * @see #requestPermissions(String[], int)
4411 * @see #onRequestPermissionsResult(int, String[], int[])
4412 */
Svetoslav9ce9a152015-06-02 17:59:31 -07004413 public boolean shouldShowRequestPermissionRationale(@NonNull String permission) {
Svetoslav20770dd2015-05-29 15:43:04 -07004414 return getPackageManager().shouldShowRequestPermissionRationale(permission);
4415 }
4416
4417 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004418 * Same as calling {@link #startActivityForResult(Intent, int, Bundle)}
4419 * with no options.
4420 *
4421 * @param intent The intent to start.
4422 * @param requestCode If >= 0, this code will be returned in
4423 * onActivityResult() when the activity exits.
4424 *
4425 * @throws android.content.ActivityNotFoundException
4426 *
George Mount0a778ed2013-12-13 13:35:36 -08004427 * @see #startActivity
Dianne Hackborna4972e92012-03-14 10:38:05 -07004428 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004429 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07004430 startActivityForResult(intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004431 }
4432
4433 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004434 * Launch an activity for which you would like a result when it finished.
4435 * When this activity exits, your
George Mount0a778ed2013-12-13 13:35:36 -08004436 * onActivityResult() method will be called with the given requestCode.
RoboErik55011652014-07-09 15:05:53 -07004437 * Using a negative requestCode is the same as calling
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438 * {@link #startActivity} (the activity is not launched as a sub-activity).
Dianne Hackborna4972e92012-03-14 10:38:05 -07004439 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004440 * <p>Note that this method should only be used with Intent protocols
4441 * that are defined to return a result. In other protocols (such as
4442 * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
4443 * not get the result when you expect. For example, if the activity you
Mark Renoufcca6d052016-12-02 14:11:18 -05004444 * are launching uses {@link Intent#FLAG_ACTIVITY_NEW_TASK}, it will not
4445 * run in your task and thus you will immediately receive a cancel result.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004446 *
RoboErik55011652014-07-09 15:05:53 -07004447 * <p>As a special case, if you call startActivityForResult() with a requestCode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004448 * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
George Mount0a778ed2013-12-13 13:35:36 -08004449 * activity, then your window will not be displayed until a result is
4450 * returned back from the started activity. This is to avoid visible
4451 * flickering when redirecting to another activity.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004452 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004453 * <p>This method throws {@link android.content.ActivityNotFoundException}
4454 * if there was no Activity found to run the given Intent.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004455 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004456 * @param intent The intent to start.
4457 * @param requestCode If >= 0, this code will be returned in
4458 * onActivityResult() when the activity exits.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004459 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08004460 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004461 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004462 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004463 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07004464 *
George Mount0a778ed2013-12-13 13:35:36 -08004465 * @see #startActivity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004466 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004467 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
4468 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004469 if (mParent == null) {
George Mount413739e2016-06-08 07:13:37 -07004470 options = transferSpringboardActivityOptions(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004471 Instrumentation.ActivityResult ar =
4472 mInstrumentation.execStartActivity(
4473 this, mMainThread.getApplicationThread(), mToken, this,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004474 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004475 if (ar != null) {
4476 mMainThread.sendActivityResult(
4477 mToken, mEmbeddedID, requestCode, ar.getResultCode(),
4478 ar.getResultData());
4479 }
4480 if (requestCode >= 0) {
4481 // If this start is requesting a result, we can avoid making
4482 // the activity visible until the result is received. Setting
4483 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4484 // activity hidden during this time, to avoid flickering.
4485 // This can only be done when a result is requested because
4486 // that guarantees we will get information back when the
4487 // activity is finished, no matter what happens to it.
4488 mStartedActivity = true;
4489 }
Adam Powell14874662013-07-18 19:42:41 -07004490
George Mount41725de2015-04-09 08:23:05 -07004491 cancelInputsAndStartExitTransition(options);
Adam Powell14874662013-07-18 19:42:41 -07004492 // TODO Consider clearing/flushing other event sources and events for child windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004493 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004494 if (options != null) {
4495 mParent.startActivityFromChild(this, intent, requestCode, options);
4496 } else {
4497 // Note we want to go through this method for compatibility with
4498 // existing applications that may have overridden it.
4499 mParent.startActivityFromChild(this, intent, requestCode);
4500 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004501 }
George Mount41725de2015-04-09 08:23:05 -07004502 }
4503
4504 /**
4505 * Cancels pending inputs and if an Activity Transition is to be run, starts the transition.
4506 *
4507 * @param options The ActivityOptions bundle used to start an Activity.
4508 */
4509 private void cancelInputsAndStartExitTransition(Bundle options) {
4510 final View decor = mWindow != null ? mWindow.peekDecorView() : null;
4511 if (decor != null) {
4512 decor.cancelPendingInputEvents();
4513 }
George Mountff243282014-07-07 16:12:07 -07004514 if (options != null && !isTopOfTask()) {
4515 mActivityTransitionState.startExitOutTransition(this, options);
4516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004517 }
4518
Chet Haaseb64e777f2016-12-09 07:17:10 -08004519 /**
4520 * Returns whether there are any activity transitions currently running on this
4521 * activity. A return value of {@code true} can mean that either an enter or
4522 * exit transition is running, including whether the background of the activity
4523 * is animating as a part of that transition.
4524 *
4525 * @return true if a transition is currently running on this activity, false otherwise.
4526 */
4527 public boolean isActivityTransitionRunning() {
4528 return mActivityTransitionState.isTransitionRunning();
4529 }
4530
George Mount413739e2016-06-08 07:13:37 -07004531 private Bundle transferSpringboardActivityOptions(Bundle options) {
4532 if (options == null && (mWindow != null && !mWindow.isActive())) {
4533 final ActivityOptions activityOptions = getActivityOptions();
George Mount96383782016-06-08 15:13:52 -07004534 if (activityOptions != null &&
4535 activityOptions.getAnimationType() == ActivityOptions.ANIM_SCENE_TRANSITION) {
George Mount413739e2016-06-08 07:13:37 -07004536 return activityOptions.toBundle();
4537 }
4538 }
4539 return options;
4540 }
4541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004542 /**
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004543 * @hide Implement to provide correct calling token.
4544 */
Alexandra Gherghina83231452014-07-04 12:05:20 +01004545 public void startActivityForResultAsUser(Intent intent, int requestCode, UserHandle user) {
4546 startActivityForResultAsUser(intent, requestCode, null, user);
4547 }
4548
4549 /**
4550 * @hide Implement to provide correct calling token.
4551 */
4552 public void startActivityForResultAsUser(Intent intent, int requestCode,
4553 @Nullable Bundle options, UserHandle user) {
Tony Mak96d26fe2017-04-11 20:05:39 +01004554 startActivityForResultAsUser(intent, mEmbeddedID, requestCode, options, user);
4555 }
4556
4557 /**
4558 * @hide Implement to provide correct calling token.
4559 */
4560 public void startActivityForResultAsUser(Intent intent, String resultWho, int requestCode,
4561 @Nullable Bundle options, UserHandle user) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01004562 if (mParent != null) {
4563 throw new RuntimeException("Can't be called from a child");
4564 }
George Mount413739e2016-06-08 07:13:37 -07004565 options = transferSpringboardActivityOptions(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01004566 Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity(
Tony Mak96d26fe2017-04-11 20:05:39 +01004567 this, mMainThread.getApplicationThread(), mToken, resultWho, intent, requestCode,
Alexandra Gherghina83231452014-07-04 12:05:20 +01004568 options, user);
4569 if (ar != null) {
4570 mMainThread.sendActivityResult(
4571 mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData());
4572 }
4573 if (requestCode >= 0) {
4574 // If this start is requesting a result, we can avoid making
4575 // the activity visible until the result is received. Setting
4576 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4577 // activity hidden during this time, to avoid flickering.
4578 // This can only be done when a result is requested because
4579 // that guarantees we will get information back when the
4580 // activity is finished, no matter what happens to it.
4581 mStartedActivity = true;
4582 }
4583
George Mount41725de2015-04-09 08:23:05 -07004584 cancelInputsAndStartExitTransition(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01004585 }
4586
4587 /**
4588 * @hide Implement to provide correct calling token.
4589 */
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004590 public void startActivityAsUser(Intent intent, UserHandle user) {
4591 startActivityAsUser(intent, null, user);
4592 }
4593
4594 /**
4595 * @hide Implement to provide correct calling token.
4596 */
4597 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
4598 if (mParent != null) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01004599 throw new RuntimeException("Can't be called from a child");
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004600 }
George Mount413739e2016-06-08 07:13:37 -07004601 options = transferSpringboardActivityOptions(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004602 Instrumentation.ActivityResult ar =
4603 mInstrumentation.execStartActivity(
Tony Mak96d26fe2017-04-11 20:05:39 +01004604 this, mMainThread.getApplicationThread(), mToken, mEmbeddedID,
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004605 intent, -1, options, user);
4606 if (ar != null) {
4607 mMainThread.sendActivityResult(
4608 mToken, mEmbeddedID, -1, ar.getResultCode(),
4609 ar.getResultData());
4610 }
George Mount41725de2015-04-09 08:23:05 -07004611 cancelInputsAndStartExitTransition(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004612 }
4613
4614 /**
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004615 * Start a new activity as if it was started by the activity that started our
4616 * current activity. This is for the resolver and chooser activities, which operate
4617 * as intermediaries that dispatch their intent to the target the user selects -- to
4618 * do this, they must perform all security checks including permission grants as if
4619 * their launch had come from the original activity.
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004620 * @param intent The Intent to start.
4621 * @param options ActivityOptions or null.
4622 * @param ignoreTargetSecurity If true, the activity manager will not check whether the
4623 * caller it is doing the start is, is actually allowed to start the target activity.
4624 * If you set this to true, you must set an explicit component in the Intent and do any
4625 * appropriate security checks yourself.
4626 * @param userId The user the new activity should run as.
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004627 * @hide
4628 */
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004629 public void startActivityAsCaller(Intent intent, @Nullable Bundle options,
4630 boolean ignoreTargetSecurity, int userId) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004631 if (mParent != null) {
4632 throw new RuntimeException("Can't be called from a child");
4633 }
George Mount413739e2016-06-08 07:13:37 -07004634 options = transferSpringboardActivityOptions(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004635 Instrumentation.ActivityResult ar =
4636 mInstrumentation.execStartActivityAsCaller(
4637 this, mMainThread.getApplicationThread(), mToken, this,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004638 intent, -1, options, ignoreTargetSecurity, userId);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004639 if (ar != null) {
4640 mMainThread.sendActivityResult(
4641 mToken, mEmbeddedID, -1, ar.getResultCode(),
4642 ar.getResultData());
4643 }
George Mount41725de2015-04-09 08:23:05 -07004644 cancelInputsAndStartExitTransition(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004645 }
4646
4647 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004648 * Same as calling {@link #startIntentSenderForResult(IntentSender, int,
4649 * Intent, int, int, int, Bundle)} with no options.
4650 *
4651 * @param intent The IntentSender to launch.
4652 * @param requestCode If >= 0, this code will be returned in
4653 * onActivityResult() when the activity exits.
4654 * @param fillInIntent If non-null, this will be provided as the
4655 * intent parameter to {@link IntentSender#sendIntent}.
4656 * @param flagsMask Intent flags in the original IntentSender that you
4657 * would like to change.
4658 * @param flagsValues Desired values for any bits set in
4659 * <var>flagsMask</var>
4660 * @param extraFlags Always set to 0.
4661 */
4662 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07004663 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackborna4972e92012-03-14 10:38:05 -07004664 throws IntentSender.SendIntentException {
4665 startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask,
4666 flagsValues, extraFlags, null);
4667 }
4668
4669 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004670 * Like {@link #startActivityForResult(Intent, int)}, but allowing you
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004671 * to use a IntentSender to describe the activity to be started. If
4672 * the IntentSender is for an activity, that activity will be started
4673 * as if you had called the regular {@link #startActivityForResult(Intent, int)}
4674 * here; otherwise, its associated action will be executed (such as
4675 * sending a broadcast) as if you had called
4676 * {@link IntentSender#sendIntent IntentSender.sendIntent} on it.
RoboErik55011652014-07-09 15:05:53 -07004677 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004678 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004679 * @param requestCode If >= 0, this code will be returned in
4680 * onActivityResult() when the activity exits.
4681 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004682 * intent parameter to {@link IntentSender#sendIntent}.
4683 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004684 * would like to change.
4685 * @param flagsValues Desired values for any bits set in
4686 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004687 * @param extraFlags Always set to 0.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004688 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08004689 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004690 * Context.startActivity(Intent, Bundle)} for more details. If options
4691 * have also been supplied by the IntentSender, options given here will
4692 * override any that conflict with those given by the IntentSender.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004693 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004694 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07004695 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004696 Bundle options) throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004697 if (mParent == null) {
Clara Bayarria0c2dc32016-04-12 12:00:15 +01004698 startIntentSenderForResultInner(intent, mEmbeddedID, requestCode, fillInIntent,
4699 flagsMask, flagsValues, options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004700 } else if (options != null) {
4701 mParent.startIntentSenderFromChild(this, intent, requestCode,
4702 fillInIntent, flagsMask, flagsValues, extraFlags, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004703 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004704 // Note we want to go through this call for compatibility with
4705 // existing applications that may have overridden the method.
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004706 mParent.startIntentSenderFromChild(this, intent, requestCode,
4707 fillInIntent, flagsMask, flagsValues, extraFlags);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004708 }
4709 }
4710
Clara Bayarria0c2dc32016-04-12 12:00:15 +01004711 private void startIntentSenderForResultInner(IntentSender intent, String who, int requestCode,
4712 Intent fillInIntent, int flagsMask, int flagsValues,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004713 Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004714 throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004715 try {
4716 String resolvedType = null;
4717 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004718 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07004719 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004720 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
4721 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004722 int result = ActivityManager.getService()
Dianne Hackborn3e6e3852017-05-19 16:12:08 -07004723 .startActivityIntentSender(mMainThread.getApplicationThread(),
4724 intent != null ? intent.getTarget() : null,
4725 intent != null ? intent.getWhitelistToken() : null,
Clara Bayarria0c2dc32016-04-12 12:00:15 +01004726 fillInIntent, resolvedType, mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004727 requestCode, flagsMask, flagsValues, options);
4728 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004729 throw new IntentSender.SendIntentException();
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004730 }
4731 Instrumentation.checkStartActivityResult(result, null);
4732 } catch (RemoteException e) {
4733 }
4734 if (requestCode >= 0) {
4735 // If this start is requesting a result, we can avoid making
4736 // the activity visible until the result is received. Setting
4737 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4738 // activity hidden during this time, to avoid flickering.
4739 // This can only be done when a result is requested because
4740 // that guarantees we will get information back when the
4741 // activity is finished, no matter what happens to it.
4742 mStartedActivity = true;
4743 }
4744 }
4745
4746 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004747 * Same as {@link #startActivity(Intent, Bundle)} with no options
4748 * specified.
4749 *
4750 * @param intent The intent to start.
4751 *
4752 * @throws android.content.ActivityNotFoundException
4753 *
Mark Lufa434852016-08-11 17:40:33 -07004754 * @see #startActivity(Intent, Bundle)
Dianne Hackborna4972e92012-03-14 10:38:05 -07004755 * @see #startActivityForResult
4756 */
4757 @Override
4758 public void startActivity(Intent intent) {
George Mounte1803372014-02-26 19:00:52 +00004759 this.startActivity(intent, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004760 }
4761
4762 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004763 * Launch a new activity. You will not receive any information about when
4764 * the activity exits. This implementation overrides the base version,
4765 * providing information about
4766 * the activity performing the launch. Because of this additional
4767 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
4768 * required; if not specified, the new activity will be added to the
4769 * task of the caller.
RoboErik55011652014-07-09 15:05:53 -07004770 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004771 * <p>This method throws {@link android.content.ActivityNotFoundException}
4772 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07004773 *
4774 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004775 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08004776 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004777 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004778 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004779 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07004780 *
Mark Lufa434852016-08-11 17:40:33 -07004781 * @see #startActivity(Intent)
RoboErik55011652014-07-09 15:05:53 -07004782 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004783 */
4784 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07004785 public void startActivity(Intent intent, @Nullable Bundle options) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004786 if (options != null) {
4787 startActivityForResult(intent, -1, options);
4788 } else {
4789 // Note we want to go through this call for compatibility with
4790 // applications that may have overridden the method.
4791 startActivityForResult(intent, -1);
4792 }
4793 }
4794
4795 /**
4796 * Same as {@link #startActivities(Intent[], Bundle)} with no options
4797 * specified.
4798 *
4799 * @param intents The intents to start.
4800 *
4801 * @throws android.content.ActivityNotFoundException
4802 *
Mark Lufa434852016-08-11 17:40:33 -07004803 * @see #startActivities(Intent[], Bundle)
Dianne Hackborna4972e92012-03-14 10:38:05 -07004804 * @see #startActivityForResult
4805 */
4806 @Override
4807 public void startActivities(Intent[] intents) {
4808 startActivities(intents, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004809 }
4810
4811 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004812 * Launch a new activity. You will not receive any information about when
4813 * the activity exits. This implementation overrides the base version,
4814 * providing information about
4815 * the activity performing the launch. Because of this additional
4816 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
4817 * required; if not specified, the new activity will be added to the
4818 * task of the caller.
4819 *
4820 * <p>This method throws {@link android.content.ActivityNotFoundException}
4821 * if there was no Activity found to run the given Intent.
4822 *
4823 * @param intents The intents to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004824 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08004825 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004826 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004827 *
4828 * @throws android.content.ActivityNotFoundException
4829 *
Mark Lufa434852016-08-11 17:40:33 -07004830 * @see #startActivities(Intent[])
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004831 * @see #startActivityForResult
4832 */
4833 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07004834 public void startActivities(Intent[] intents, @Nullable Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004835 mInstrumentation.execStartActivities(this, mMainThread.getApplicationThread(),
Dianne Hackborna4972e92012-03-14 10:38:05 -07004836 mToken, this, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004837 }
4838
4839 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004840 * Same as calling {@link #startIntentSender(IntentSender, Intent, int, int, int, Bundle)}
4841 * with no options.
RoboErik55011652014-07-09 15:05:53 -07004842 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004843 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004844 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004845 * intent parameter to {@link IntentSender#sendIntent}.
4846 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004847 * would like to change.
4848 * @param flagsValues Desired values for any bits set in
4849 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004850 * @param extraFlags Always set to 0.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004851 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004852 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07004853 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004854 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004855 startIntentSender(intent, fillInIntent, flagsMask, flagsValues,
4856 extraFlags, null);
4857 }
4858
4859 /**
4860 * Like {@link #startActivity(Intent, Bundle)}, but taking a IntentSender
4861 * to start; see
4862 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int, Bundle)}
4863 * for more information.
4864 *
4865 * @param intent The IntentSender to launch.
4866 * @param fillInIntent If non-null, this will be provided as the
4867 * intent parameter to {@link IntentSender#sendIntent}.
4868 * @param flagsMask Intent flags in the original IntentSender that you
4869 * would like to change.
4870 * @param flagsValues Desired values for any bits set in
4871 * <var>flagsMask</var>
4872 * @param extraFlags Always set to 0.
4873 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08004874 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004875 * Context.startActivity(Intent, Bundle)} for more details. If options
4876 * have also been supplied by the IntentSender, options given here will
4877 * override any that conflict with those given by the IntentSender.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004878 */
4879 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07004880 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004881 Bundle options) throws IntentSender.SendIntentException {
4882 if (options != null) {
4883 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
4884 flagsValues, extraFlags, options);
4885 } else {
4886 // Note we want to go through this call for compatibility with
4887 // applications that may have overridden the method.
4888 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
4889 flagsValues, extraFlags);
4890 }
4891 }
4892
4893 /**
4894 * Same as calling {@link #startActivityIfNeeded(Intent, int, Bundle)}
4895 * with no options.
4896 *
4897 * @param intent The intent to start.
4898 * @param requestCode If >= 0, this code will be returned in
4899 * onActivityResult() when the activity exits, as described in
4900 * {@link #startActivityForResult}.
4901 *
4902 * @return If a new activity was launched then true is returned; otherwise
4903 * false is returned and you must handle the Intent yourself.
4904 *
4905 * @see #startActivity
4906 * @see #startActivityForResult
4907 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004908 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
4909 int requestCode) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004910 return startActivityIfNeeded(intent, requestCode, null);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004911 }
4912
4913 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004914 * A special variation to launch an activity only if a new activity
4915 * instance is needed to handle the given Intent. In other words, this is
RoboErik55011652014-07-09 15:05:53 -07004916 * just like {@link #startActivityForResult(Intent, int)} except: if you are
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004917 * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or
RoboErik55011652014-07-09 15:05:53 -07004918 * singleTask or singleTop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004919 * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode},
RoboErik55011652014-07-09 15:05:53 -07004920 * and the activity
4921 * that handles <var>intent</var> is the same as your currently running
4922 * activity, then a new instance is not needed. In this case, instead of
4923 * the normal behavior of calling {@link #onNewIntent} this function will
4924 * return and you can handle the Intent yourself.
4925 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004926 * <p>This function can only be called from a top-level activity; if it is
4927 * called from a child activity, a runtime exception will be thrown.
RoboErik55011652014-07-09 15:05:53 -07004928 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004929 * @param intent The intent to start.
4930 * @param requestCode If >= 0, this code will be returned in
4931 * onActivityResult() when the activity exits, as described in
4932 * {@link #startActivityForResult}.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004933 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08004934 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004935 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004936 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004937 * @return If a new activity was launched then true is returned; otherwise
4938 * false is returned and you must handle the Intent yourself.
RoboErik55011652014-07-09 15:05:53 -07004939 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004940 * @see #startActivity
4941 * @see #startActivityForResult
4942 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004943 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
4944 int requestCode, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004945 if (mParent == null) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004946 int result = ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004947 try {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07004948 Uri referrer = onProvideReferrer();
4949 if (referrer != null) {
4950 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
4951 }
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004952 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07004953 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004954 result = ActivityManager.getService()
Dianne Hackbornf265ea92013-01-31 15:00:51 -08004955 .startActivity(mMainThread.getApplicationThread(), getBasePackageName(),
Jeff Hao1b012d32014-08-20 10:35:34 -07004956 intent, intent.resolveTypeIfNeeded(getContentResolver()), mToken,
4957 mEmbeddedID, requestCode, ActivityManager.START_FLAG_ONLY_IF_NEEDED,
4958 null, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004959 } catch (RemoteException e) {
4960 // Empty
4961 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08004962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004963 Instrumentation.checkStartActivityResult(result, intent);
Siva Velusamy92a8b222012-03-09 16:24:04 -08004964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004965 if (requestCode >= 0) {
4966 // If this start is requesting a result, we can avoid making
4967 // the activity visible until the result is received. Setting
4968 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4969 // activity hidden during this time, to avoid flickering.
4970 // This can only be done when a result is requested because
4971 // that guarantees we will get information back when the
4972 // activity is finished, no matter what happens to it.
4973 mStartedActivity = true;
4974 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07004975 return result != ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004976 }
4977
4978 throw new UnsupportedOperationException(
4979 "startActivityIfNeeded can only be called from a top-level activity");
4980 }
4981
4982 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004983 * Same as calling {@link #startNextMatchingActivity(Intent, Bundle)} with
4984 * no options.
4985 *
4986 * @param intent The intent to dispatch to the next activity. For
4987 * correct behavior, this must be the same as the Intent that started
4988 * your own activity; the only changes you can make are to the extras
4989 * inside of it.
4990 *
4991 * @return Returns a boolean indicating whether there was another Activity
4992 * to start: true if there was a next activity to start, false if there
4993 * wasn't. In general, if true is returned you will then want to call
4994 * finish() on yourself.
4995 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004996 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004997 return startNextMatchingActivity(intent, null);
4998 }
4999
5000 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005001 * Special version of starting an activity, for use when you are replacing
5002 * other activity components. You can use this to hand the Intent off
5003 * to the next Activity that can handle it. You typically call this in
5004 * {@link #onCreate} with the Intent returned by {@link #getIntent}.
RoboErik55011652014-07-09 15:05:53 -07005005 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005006 * @param intent The intent to dispatch to the next activity. For
5007 * correct behavior, this must be the same as the Intent that started
5008 * your own activity; the only changes you can make are to the extras
5009 * inside of it.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005010 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005011 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005012 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005013 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005014 * @return Returns a boolean indicating whether there was another Activity
5015 * to start: true if there was a next activity to start, false if there
5016 * wasn't. In general, if true is returned you will then want to call
5017 * finish() on yourself.
5018 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005019 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent,
5020 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005021 if (mParent == null) {
5022 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005023 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005024 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005025 return ActivityManager.getService()
Dianne Hackborna4972e92012-03-14 10:38:05 -07005026 .startNextMatchingActivity(mToken, intent, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005027 } catch (RemoteException e) {
5028 // Empty
5029 }
5030 return false;
5031 }
5032
5033 throw new UnsupportedOperationException(
5034 "startNextMatchingActivity can only be called from a top-level activity");
5035 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07005036
5037 /**
5038 * Same as calling {@link #startActivityFromChild(Activity, Intent, int, Bundle)}
5039 * with no options.
5040 *
5041 * @param child The activity making the call.
5042 * @param intent The intent to start.
5043 * @param requestCode Reply request code. < 0 if reply is not requested.
5044 *
5045 * @throws android.content.ActivityNotFoundException
5046 *
5047 * @see #startActivity
5048 * @see #startActivityForResult
5049 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005050 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005051 int requestCode) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07005052 startActivityFromChild(child, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005053 }
5054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005055 /**
RoboErik55011652014-07-09 15:05:53 -07005056 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005057 * {@link #startActivity} or {@link #startActivityForResult} method.
RoboErik55011652014-07-09 15:05:53 -07005058 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005059 * <p>This method throws {@link android.content.ActivityNotFoundException}
5060 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005061 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005062 * @param child The activity making the call.
5063 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005064 * @param requestCode Reply request code. < 0 if reply is not requested.
5065 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005066 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005067 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005068 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005069 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07005070 *
5071 * @see #startActivity
5072 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005073 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005074 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005075 int requestCode, @Nullable Bundle options) {
George Mount413739e2016-06-08 07:13:37 -07005076 options = transferSpringboardActivityOptions(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005077 Instrumentation.ActivityResult ar =
5078 mInstrumentation.execStartActivity(
5079 this, mMainThread.getApplicationThread(), mToken, child,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005080 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005081 if (ar != null) {
5082 mMainThread.sendActivityResult(
5083 mToken, child.mEmbeddedID, requestCode,
5084 ar.getResultCode(), ar.getResultData());
5085 }
George Mount41725de2015-04-09 08:23:05 -07005086 cancelInputsAndStartExitTransition(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005087 }
5088
5089 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005090 * Same as calling {@link #startActivityFromFragment(Fragment, Intent, int, Bundle)}
5091 * with no options.
5092 *
5093 * @param fragment The fragment making the call.
5094 * @param intent The intent to start.
5095 * @param requestCode Reply request code. < 0 if reply is not requested.
5096 *
5097 * @throws android.content.ActivityNotFoundException
5098 *
5099 * @see Fragment#startActivity
5100 * @see Fragment#startActivityForResult
5101 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005102 public void startActivityFromFragment(@NonNull Fragment fragment,
5103 @RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07005104 startActivityFromFragment(fragment, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005105 }
5106
5107 /**
RoboErik55011652014-07-09 15:05:53 -07005108 * This is called when a Fragment in this activity calls its
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005109 * {@link Fragment#startActivity} or {@link Fragment#startActivityForResult}
5110 * method.
RoboErik55011652014-07-09 15:05:53 -07005111 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005112 * <p>This method throws {@link android.content.ActivityNotFoundException}
5113 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005114 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005115 * @param fragment The fragment making the call.
5116 * @param intent The intent to start.
RoboErik55011652014-07-09 15:05:53 -07005117 * @param requestCode Reply request code. < 0 if reply is not requested.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005118 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005119 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005120 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005121 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005122 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07005123 *
5124 * @see Fragment#startActivity
5125 * @see Fragment#startActivityForResult
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005126 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005127 public void startActivityFromFragment(@NonNull Fragment fragment,
5128 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005129 startActivityForResult(fragment.mWho, intent, requestCode, options);
5130 }
5131
5132 /**
5133 * @hide
5134 */
Tony Mak96d26fe2017-04-11 20:05:39 +01005135 public void startActivityAsUserFromFragment(@NonNull Fragment fragment,
5136 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options,
5137 UserHandle user) {
5138 startActivityForResultAsUser(intent, fragment.mWho, requestCode, options, user);
5139 }
5140
5141 /**
5142 * @hide
5143 */
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005144 @Override
5145 public void startActivityForResult(
5146 String who, Intent intent, int requestCode, @Nullable Bundle options) {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005147 Uri referrer = onProvideReferrer();
5148 if (referrer != null) {
5149 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
5150 }
George Mount413739e2016-06-08 07:13:37 -07005151 options = transferSpringboardActivityOptions(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005152 Instrumentation.ActivityResult ar =
5153 mInstrumentation.execStartActivity(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005154 this, mMainThread.getApplicationThread(), mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005155 intent, requestCode, options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005156 if (ar != null) {
5157 mMainThread.sendActivityResult(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005158 mToken, who, requestCode,
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005159 ar.getResultCode(), ar.getResultData());
5160 }
George Mount41725de2015-04-09 08:23:05 -07005161 cancelInputsAndStartExitTransition(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005162 }
5163
5164 /**
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005165 * @hide
5166 */
5167 @Override
5168 public boolean canStartActivityForResult() {
5169 return true;
5170 }
5171
5172 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005173 * Same as calling {@link #startIntentSenderFromChild(Activity, IntentSender,
5174 * int, Intent, int, int, int, Bundle)} with no options.
5175 */
5176 public void startIntentSenderFromChild(Activity child, IntentSender intent,
5177 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
5178 int extraFlags)
5179 throws IntentSender.SendIntentException {
5180 startIntentSenderFromChild(child, intent, requestCode, fillInIntent,
5181 flagsMask, flagsValues, extraFlags, null);
5182 }
5183
5184 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005185 * Like {@link #startActivityFromChild(Activity, Intent, int)}, but
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005186 * taking a IntentSender; see
Dianne Hackbornae22c052009-09-17 18:46:22 -07005187 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005188 * for more information.
5189 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005190 public void startIntentSenderFromChild(Activity child, IntentSender intent,
5191 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
Tor Norbyed9273d62013-05-30 15:59:53 -07005192 int extraFlags, @Nullable Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005193 throws IntentSender.SendIntentException {
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005194 startIntentSenderForResultInner(intent, child.mEmbeddedID, requestCode, fillInIntent,
5195 flagsMask, flagsValues, options);
5196 }
5197
5198 /**
5199 * Like {@link #startIntentSenderFromChild}, but taking a Fragment; see
5200 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
5201 * for more information.
5202 *
5203 * @hide
5204 */
5205 public void startIntentSenderFromChildFragment(Fragment child, IntentSender intent,
5206 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
5207 int extraFlags, @Nullable Bundle options)
5208 throws IntentSender.SendIntentException {
5209 startIntentSenderForResultInner(intent, child.mWho, requestCode, fillInIntent,
5210 flagsMask, flagsValues, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005211 }
5212
5213 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005214 * Call immediately after one of the flavors of {@link #startActivity(Intent)}
5215 * or {@link #finish} to specify an explicit transition animation to
5216 * perform next.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005217 *
5218 * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative
5219 * to using this with starting activities is to supply the desired animation
5220 * information through a {@link ActivityOptions} bundle to
Mark Lufa434852016-08-11 17:40:33 -07005221 * {@link #startActivity(Intent, Bundle)} or a related function. This allows
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005222 * you to specify a custom animation even when starting an activity from
5223 * outside the context of the current top activity.
5224 *
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005225 * @param enterAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07005226 * the incoming activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005227 * @param exitAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07005228 * the outgoing activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005229 */
5230 public void overridePendingTransition(int enterAnim, int exitAnim) {
5231 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005232 ActivityManager.getService().overridePendingTransition(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005233 mToken, getPackageName(), enterAnim, exitAnim);
5234 } catch (RemoteException e) {
5235 }
5236 }
RoboErik55011652014-07-09 15:05:53 -07005237
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005238 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005239 * Call this to set the result that your activity will return to its
5240 * caller.
RoboErik55011652014-07-09 15:05:53 -07005241 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005242 * @param resultCode The result code to propagate back to the originating
5243 * activity, often RESULT_CANCELED or RESULT_OK
RoboErik55011652014-07-09 15:05:53 -07005244 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005245 * @see #RESULT_CANCELED
5246 * @see #RESULT_OK
5247 * @see #RESULT_FIRST_USER
5248 * @see #setResult(int, Intent)
5249 */
5250 public final void setResult(int resultCode) {
5251 synchronized (this) {
5252 mResultCode = resultCode;
5253 mResultData = null;
5254 }
5255 }
5256
5257 /**
5258 * Call this to set the result that your activity will return to its
5259 * caller.
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005260 *
5261 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, the Intent
5262 * you supply here can have {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
5263 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
5264 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} set. This will grant the
5265 * Activity receiving the result access to the specific URIs in the Intent.
5266 * Access will remain until the Activity has finished (it will remain across the hosting
5267 * process being killed and other temporary destruction) and will be added
5268 * to any existing set of URI permissions it already holds.
5269 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005270 * @param resultCode The result code to propagate back to the originating
5271 * activity, often RESULT_CANCELED or RESULT_OK
5272 * @param data The data to propagate back to the originating activity.
RoboErik55011652014-07-09 15:05:53 -07005273 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005274 * @see #RESULT_CANCELED
5275 * @see #RESULT_OK
5276 * @see #RESULT_FIRST_USER
5277 * @see #setResult(int)
5278 */
5279 public final void setResult(int resultCode, Intent data) {
5280 synchronized (this) {
5281 mResultCode = resultCode;
5282 mResultData = data;
5283 }
5284 }
5285
5286 /**
Dianne Hackborn85d558c2014-11-04 10:31:54 -08005287 * Return information about who launched this activity. If the launching Intent
5288 * contains an {@link android.content.Intent#EXTRA_REFERRER Intent.EXTRA_REFERRER},
5289 * that will be returned as-is; otherwise, if known, an
5290 * {@link Intent#URI_ANDROID_APP_SCHEME android-app:} referrer URI containing the
5291 * package name that started the Intent will be returned. This may return null if no
5292 * referrer can be identified -- it is neither explicitly specified, nor is it known which
5293 * application package was involved.
5294 *
5295 * <p>If called while inside the handling of {@link #onNewIntent}, this function will
5296 * return the referrer that submitted that new intent to the activity. Otherwise, it
5297 * always returns the referrer of the original Intent.</p>
5298 *
5299 * <p>Note that this is <em>not</em> a security feature -- you can not trust the
5300 * referrer information, applications can spoof it.</p>
5301 */
5302 @Nullable
5303 public Uri getReferrer() {
5304 Intent intent = getIntent();
Adam Powelld3c63a62016-06-09 12:36:16 -07005305 try {
5306 Uri referrer = intent.getParcelableExtra(Intent.EXTRA_REFERRER);
5307 if (referrer != null) {
5308 return referrer;
5309 }
5310 String referrerName = intent.getStringExtra(Intent.EXTRA_REFERRER_NAME);
5311 if (referrerName != null) {
5312 return Uri.parse(referrerName);
5313 }
5314 } catch (BadParcelableException e) {
5315 Log.w(TAG, "Cannot read referrer from intent;"
5316 + " intent extras contain unknown custom Parcelable objects");
Dianne Hackborn85d558c2014-11-04 10:31:54 -08005317 }
5318 if (mReferrer != null) {
5319 return new Uri.Builder().scheme("android-app").authority(mReferrer).build();
5320 }
5321 return null;
5322 }
5323
5324 /**
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005325 * Override to generate the desired referrer for the content currently being shown
5326 * by the app. The default implementation returns null, meaning the referrer will simply
5327 * be the android-app: of the package name of this activity. Return a non-null Uri to
5328 * have that supplied as the {@link Intent#EXTRA_REFERRER} of any activities started from it.
5329 */
5330 public Uri onProvideReferrer() {
5331 return null;
5332 }
5333
5334 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005335 * Return the name of the package that invoked this activity. This is who
5336 * the data in {@link #setResult setResult()} will be sent to. You can
5337 * use this information to validate that the recipient is allowed to
5338 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07005339 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005340 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07005341 * did not use the {@link #startActivityForResult}
5342 * form that includes a request code), then the calling package will be
Dianne Hackborn7b924672013-04-11 18:08:42 -07005343 * null.</p>
5344 *
5345 * <p class="note">Note: prior to {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
5346 * the result from this method was unstable. If the process hosting the calling
5347 * package was no longer running, it would return null instead of the proper package
5348 * name. You can use {@link #getCallingActivity()} and retrieve the package name
5349 * from that instead.</p>
RoboErik55011652014-07-09 15:05:53 -07005350 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005351 * @return The package of the activity that will receive your
5352 * reply, or null if none.
5353 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005354 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005355 public String getCallingPackage() {
5356 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005357 return ActivityManager.getService().getCallingPackage(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005358 } catch (RemoteException e) {
5359 return null;
5360 }
5361 }
5362
5363 /**
5364 * Return the name of the activity that invoked this activity. This is
5365 * who the data in {@link #setResult setResult()} will be sent to. You
5366 * can use this information to validate that the recipient is allowed to
5367 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07005368 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005369 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07005370 * did not use the {@link #startActivityForResult}
5371 * form that includes a request code), then the calling package will be
5372 * null.
5373 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005374 * @return The ComponentName of the activity that will receive your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005375 * reply, or null if none.
5376 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005377 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005378 public ComponentName getCallingActivity() {
5379 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005380 return ActivityManager.getService().getCallingActivity(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005381 } catch (RemoteException e) {
5382 return null;
5383 }
5384 }
5385
5386 /**
5387 * Control whether this activity's main window is visible. This is intended
5388 * only for the special case of an activity that is not going to show a
5389 * UI itself, but can't just finish prior to onResume() because it needs
5390 * to wait for a service binding or such. Setting this to false allows
5391 * you to prevent your UI from being shown during that time.
RoboErik55011652014-07-09 15:05:53 -07005392 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005393 * <p>The default value for this is taken from the
5394 * {@link android.R.attr#windowNoDisplay} attribute of the activity's theme.
5395 */
5396 public void setVisible(boolean visible) {
5397 if (mVisibleFromClient != visible) {
5398 mVisibleFromClient = visible;
5399 if (mVisibleFromServer) {
5400 if (visible) makeVisible();
5401 else mDecor.setVisibility(View.INVISIBLE);
5402 }
5403 }
5404 }
RoboErik55011652014-07-09 15:05:53 -07005405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005406 void makeVisible() {
5407 if (!mWindowAdded) {
5408 ViewManager wm = getWindowManager();
5409 wm.addView(mDecor, getWindow().getAttributes());
5410 mWindowAdded = true;
5411 }
5412 mDecor.setVisibility(View.VISIBLE);
5413 }
RoboErik55011652014-07-09 15:05:53 -07005414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005415 /**
5416 * Check to see whether this activity is in the process of finishing,
5417 * either because you called {@link #finish} on it or someone else
5418 * has requested that it finished. This is often used in
5419 * {@link #onPause} to determine whether the activity is simply pausing or
5420 * completely finishing.
RoboErik55011652014-07-09 15:05:53 -07005421 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005422 * @return If the activity is finishing, returns true; else returns false.
RoboErik55011652014-07-09 15:05:53 -07005423 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005424 * @see #finish
5425 */
5426 public boolean isFinishing() {
5427 return mFinished;
5428 }
5429
5430 /**
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07005431 * Returns true if the final {@link #onDestroy()} call has been made
5432 * on the Activity, so this instance is now dead.
5433 */
5434 public boolean isDestroyed() {
5435 return mDestroyed;
5436 }
5437
5438 /**
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05005439 * Check to see whether this activity is in the process of being destroyed in order to be
5440 * recreated with a new configuration. This is often used in
5441 * {@link #onStop} to determine whether the state needs to be cleaned up or will be passed
5442 * on to the next instance of the activity via {@link #onRetainNonConfigurationInstance()}.
RoboErik55011652014-07-09 15:05:53 -07005443 *
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05005444 * @return If the activity is being torn down in order to be recreated with a new configuration,
5445 * returns true; else returns false.
5446 */
5447 public boolean isChangingConfigurations() {
5448 return mChangingConfigurations;
5449 }
5450
5451 /**
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08005452 * Cause this Activity to be recreated with a new instance. This results
5453 * in essentially the same flow as when the Activity is created due to
5454 * a configuration change -- the current instance will go through its
5455 * lifecycle to {@link #onDestroy} and a new instance then created after it.
5456 */
5457 public void recreate() {
5458 if (mParent != null) {
5459 throw new IllegalStateException("Can only be called on top-level activity");
5460 }
5461 if (Looper.myLooper() != mMainThread.getLooper()) {
5462 throw new IllegalStateException("Must be called from main thread");
5463 }
Chong Zhangfec694e2016-08-09 12:57:38 -07005464 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005465 ActivityManager.getService().requestActivityRelaunch(mToken);
Chong Zhangfec694e2016-08-09 12:57:38 -07005466 } catch (RemoteException e) {
5467 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08005468 }
5469
5470 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005471 * Finishes the current activity and specifies whether to remove the task associated with this
5472 * activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005473 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005474 private void finish(int finishTask) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005475 if (mParent == null) {
5476 int resultCode;
5477 Intent resultData;
5478 synchronized (this) {
5479 resultCode = mResultCode;
5480 resultData = mResultData;
5481 }
Joe Onorato43a17652011-04-06 19:22:23 -07005482 if (false) Log.v(TAG, "Finishing self: token=" + mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005483 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04005484 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07005485 resultData.prepareToLeaveProcess(this);
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04005486 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005487 if (ActivityManager.getService()
Winson Chung3b3f4642014-04-22 10:08:18 -07005488 .finishActivity(mToken, resultCode, resultData, finishTask)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005489 mFinished = true;
5490 }
5491 } catch (RemoteException e) {
5492 // Empty
5493 }
5494 } else {
5495 mParent.finishFromChild(this);
5496 }
5497 }
5498
5499 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005500 * Call this when your activity is done and should be closed. The
5501 * ActivityResult is propagated back to whoever launched you via
5502 * onActivityResult().
5503 */
5504 public void finish() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005505 finish(DONT_FINISH_TASK_WITH_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07005506 }
5507
5508 /**
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07005509 * Finish this activity as well as all activities immediately below it
5510 * in the current task that have the same affinity. This is typically
5511 * used when an application can be launched on to another task (such as
5512 * from an ACTION_VIEW of a content type it understands) and the user
5513 * has used the up navigation to switch out of the current task and in
5514 * to its own task. In this case, if the user has navigated down into
5515 * any other activities of the second application, all of those should
5516 * be removed from the original task as part of the task switch.
5517 *
5518 * <p>Note that this finish does <em>not</em> allow you to deliver results
5519 * to the previous activity, and an exception will be thrown if you are trying
5520 * to do so.</p>
5521 */
5522 public void finishAffinity() {
5523 if (mParent != null) {
5524 throw new IllegalStateException("Can not be called from an embedded activity");
5525 }
5526 if (mResultCode != RESULT_CANCELED || mResultData != null) {
5527 throw new IllegalStateException("Can not be called to deliver a result");
5528 }
5529 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005530 if (ActivityManager.getService().finishActivityAffinity(mToken)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07005531 mFinished = true;
5532 }
5533 } catch (RemoteException e) {
5534 // Empty
5535 }
5536 }
5537
5538 /**
RoboErik55011652014-07-09 15:05:53 -07005539 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005540 * {@link #finish} method. The default implementation simply calls
5541 * finish() on this activity (the parent), finishing the entire group.
RoboErik55011652014-07-09 15:05:53 -07005542 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005543 * @param child The activity making the call.
RoboErik55011652014-07-09 15:05:53 -07005544 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005545 * @see #finish
5546 */
5547 public void finishFromChild(Activity child) {
5548 finish();
5549 }
5550
5551 /**
George Mountcb4b7d92014-02-25 10:47:55 -08005552 * Reverses the Activity Scene entry Transition and triggers the calling Activity
5553 * to reverse its exit Transition. When the exit Transition completes,
5554 * {@link #finish()} is called. If no entry Transition was used, finish() is called
5555 * immediately and the Activity exit Transition is run.
George Mount62ab9b72014-05-02 13:51:17 -07005556 * @see android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, android.util.Pair[])
George Mountcb4b7d92014-02-25 10:47:55 -08005557 */
Craig Mautner73f843d2014-05-19 09:42:28 -07005558 public void finishAfterTransition() {
George Mount62ab9b72014-05-02 13:51:17 -07005559 if (!mActivityTransitionState.startExitBackTransition(this)) {
George Mount31a21722014-03-24 17:44:36 -07005560 finish();
5561 }
George Mountcb4b7d92014-02-25 10:47:55 -08005562 }
5563
5564 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005565 * Force finish another activity that you had previously started with
5566 * {@link #startActivityForResult}.
RoboErik55011652014-07-09 15:05:53 -07005567 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005568 * @param requestCode The request code of the activity that you had
5569 * given to startActivityForResult(). If there are multiple
5570 * activities started with this request code, they
5571 * will all be finished.
5572 */
5573 public void finishActivity(int requestCode) {
5574 if (mParent == null) {
5575 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005576 ActivityManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005577 .finishSubActivity(mToken, mEmbeddedID, requestCode);
5578 } catch (RemoteException e) {
5579 // Empty
5580 }
5581 } else {
5582 mParent.finishActivityFromChild(this, requestCode);
5583 }
5584 }
5585
5586 /**
5587 * This is called when a child activity of this one calls its
5588 * finishActivity().
RoboErik55011652014-07-09 15:05:53 -07005589 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005590 * @param child The activity making the call.
5591 * @param requestCode Request code that had been used to start the
5592 * activity.
5593 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005594 public void finishActivityFromChild(@NonNull Activity child, int requestCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005595 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005596 ActivityManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005597 .finishSubActivity(mToken, child.mEmbeddedID, requestCode);
5598 } catch (RemoteException e) {
5599 // Empty
5600 }
5601 }
5602
5603 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005604 * Call this when your activity is done and should be closed and the task should be completely
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005605 * removed as a part of finishing the root activity of the task.
Winson Chung3b3f4642014-04-22 10:08:18 -07005606 */
5607 public void finishAndRemoveTask() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005608 finish(FINISH_TASK_WITH_ROOT_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07005609 }
5610
5611 /**
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005612 * Ask that the local app instance of this activity be released to free up its memory.
5613 * This is asking for the activity to be destroyed, but does <b>not</b> finish the activity --
5614 * a new instance of the activity will later be re-created if needed due to the user
5615 * navigating back to it.
5616 *
5617 * @return Returns true if the activity was in a state that it has started the process
5618 * of destroying its current instance; returns false if for any reason this could not
5619 * be done: it is currently visible to the user, it is already being destroyed, it is
5620 * being finished, it hasn't yet saved its state, etc.
5621 */
5622 public boolean releaseInstance() {
5623 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005624 return ActivityManager.getService().releaseActivityInstance(mToken);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005625 } catch (RemoteException e) {
5626 // Empty
5627 }
5628 return false;
5629 }
5630
5631 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005632 * Called when an activity you launched exits, giving you the requestCode
5633 * you started it with, the resultCode it returned, and any additional
5634 * data from it. The <var>resultCode</var> will be
5635 * {@link #RESULT_CANCELED} if the activity explicitly returned that,
5636 * didn't return any result, or crashed during its operation.
RoboErik55011652014-07-09 15:05:53 -07005637 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005638 * <p>You will receive this call immediately before onResume() when your
5639 * activity is re-starting.
RoboErik55011652014-07-09 15:05:53 -07005640 *
Ricardo Cervera92f6a742014-04-04 11:17:06 -07005641 * <p>This method is never invoked if your activity sets
5642 * {@link android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
5643 * <code>true</code>.
Ricardo Cervera93f94c22015-01-16 09:54:30 -08005644 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005645 * @param requestCode The integer request code originally supplied to
5646 * startActivityForResult(), allowing you to identify who this
5647 * result came from.
5648 * @param resultCode The integer result code returned by the child activity
5649 * through its setResult().
5650 * @param data An Intent, which can return result data to the caller
5651 * (various data can be attached to Intent "extras").
RoboErik55011652014-07-09 15:05:53 -07005652 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005653 * @see #startActivityForResult
5654 * @see #createPendingResult
5655 * @see #setResult(int)
5656 */
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005657 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005658 }
5659
5660 /**
George Mount62ab9b72014-05-02 13:51:17 -07005661 * Called when an activity you launched with an activity transition exposes this
5662 * Activity through a returning activity transition, giving you the resultCode
5663 * and any additional data from it. This method will only be called if the activity
5664 * set a result code other than {@link #RESULT_CANCELED} and it supports activity
George Mount9826f632014-09-11 08:50:09 -07005665 * transitions with {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount62ab9b72014-05-02 13:51:17 -07005666 *
5667 * <p>The purpose of this function is to let the called Activity send a hint about
5668 * its state so that this underlying Activity can prepare to be exposed. A call to
5669 * this method does not guarantee that the called Activity has or will be exiting soon.
5670 * It only indicates that it will expose this Activity's Window and it has
5671 * some data to pass to prepare it.</p>
5672 *
5673 * @param resultCode The integer result code returned by the child activity
5674 * through its setResult().
5675 * @param data An Intent, which can return result data to the caller
5676 * (various data can be attached to Intent "extras").
5677 */
Craig Mautner7a629c22014-09-04 14:57:04 -07005678 public void onActivityReenter(int resultCode, Intent data) {
George Mount62ab9b72014-05-02 13:51:17 -07005679 }
5680
5681 /**
RoboErik55011652014-07-09 15:05:53 -07005682 * Create a new PendingIntent object which you can hand to others
5683 * for them to use to send result data back to your
5684 * {@link #onActivityResult} callback. The created object will be either
5685 * one-shot (becoming invalid after a result is sent back) or multiple
5686 * (allowing any number of results to be sent through it).
5687 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005688 * @param requestCode Private request code for the sender that will be
5689 * associated with the result data when it is returned. The sender can not
5690 * modify this value, allowing you to identify incoming results.
5691 * @param data Default data to supply in the result, which may be modified
5692 * by the sender.
5693 * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT PendingIntent.FLAG_ONE_SHOT},
5694 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE},
5695 * {@link PendingIntent#FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT},
5696 * {@link PendingIntent#FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT},
5697 * or any of the flags as supported by
5698 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
5699 * of the intent that can be supplied when the actual send happens.
RoboErik55011652014-07-09 15:05:53 -07005700 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005701 * @return Returns an existing or new PendingIntent matching the given
5702 * parameters. May return null only if
5703 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE} has been
5704 * supplied.
RoboErik55011652014-07-09 15:05:53 -07005705 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005706 * @see PendingIntent
5707 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005708 public PendingIntent createPendingResult(int requestCode, @NonNull Intent data,
5709 @PendingIntent.Flags int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005710 String packageName = getPackageName();
5711 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07005712 data.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005713 IIntentSender target =
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005714 ActivityManager.getService().getIntentSender(
Dianne Hackborna4972e92012-03-14 10:38:05 -07005715 ActivityManager.INTENT_SENDER_ACTIVITY_RESULT, packageName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005716 mParent == null ? mToken : mParent.mToken,
Dianne Hackborn41203752012-08-31 14:05:51 -07005717 mEmbeddedID, requestCode, new Intent[] { data }, null, flags, null,
5718 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005719 return target != null ? new PendingIntent(target) : null;
5720 } catch (RemoteException e) {
5721 // Empty
5722 }
5723 return null;
5724 }
5725
5726 /**
5727 * Change the desired orientation of this activity. If the activity
5728 * is currently in the foreground or otherwise impacting the screen
5729 * orientation, the screen will immediately be changed (possibly causing
5730 * the activity to be restarted). Otherwise, this will be used the next
5731 * time the activity is visible.
RoboErik55011652014-07-09 15:05:53 -07005732 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005733 * @param requestedOrientation An orientation constant as used in
5734 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
5735 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005736 public void setRequestedOrientation(@ActivityInfo.ScreenOrientation int requestedOrientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005737 if (mParent == null) {
5738 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005739 ActivityManager.getService().setRequestedOrientation(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005740 mToken, requestedOrientation);
5741 } catch (RemoteException e) {
5742 // Empty
5743 }
5744 } else {
5745 mParent.setRequestedOrientation(requestedOrientation);
5746 }
5747 }
RoboErik55011652014-07-09 15:05:53 -07005748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005749 /**
5750 * Return the current requested orientation of the activity. This will
5751 * either be the orientation requested in its component's manifest, or
5752 * the last requested orientation given to
5753 * {@link #setRequestedOrientation(int)}.
RoboErik55011652014-07-09 15:05:53 -07005754 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005755 * @return Returns an orientation constant as used in
5756 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
5757 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005758 @ActivityInfo.ScreenOrientation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005759 public int getRequestedOrientation() {
5760 if (mParent == null) {
5761 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005762 return ActivityManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005763 .getRequestedOrientation(mToken);
5764 } catch (RemoteException e) {
5765 // Empty
5766 }
5767 } else {
5768 return mParent.getRequestedOrientation();
5769 }
5770 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
5771 }
RoboErik55011652014-07-09 15:05:53 -07005772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005773 /**
5774 * Return the identifier of the task this activity is in. This identifier
5775 * will remain the same for the lifetime of the activity.
RoboErik55011652014-07-09 15:05:53 -07005776 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005777 * @return Task identifier, an opaque integer.
5778 */
5779 public int getTaskId() {
5780 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005781 return ActivityManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005782 .getTaskForActivity(mToken, false);
5783 } catch (RemoteException e) {
5784 return -1;
5785 }
5786 }
5787
5788 /**
5789 * Return whether this activity is the root of a task. The root is the
5790 * first activity in a task.
RoboErik55011652014-07-09 15:05:53 -07005791 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005792 * @return True if this is the root activity, else false.
5793 */
Winson Chung4d8681f2017-05-23 16:22:08 -07005794 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005795 public boolean isTaskRoot() {
5796 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005797 return ActivityManager.getService().getTaskForActivity(mToken, true) >= 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005798 } catch (RemoteException e) {
5799 return false;
5800 }
5801 }
5802
5803 /**
5804 * Move the task containing this activity to the back of the activity
5805 * stack. The activity's order within the task is unchanged.
RoboErik55011652014-07-09 15:05:53 -07005806 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005807 * @param nonRoot If false then this only works if the activity is the root
5808 * of a task; if true it will work for any activity in
5809 * a task.
RoboErik55011652014-07-09 15:05:53 -07005810 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005811 * @return If the task was moved (or it was already at the
5812 * back) true is returned, else false.
5813 */
5814 public boolean moveTaskToBack(boolean nonRoot) {
5815 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005816 return ActivityManager.getService().moveActivityTaskToBack(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005817 mToken, nonRoot);
5818 } catch (RemoteException e) {
5819 // Empty
5820 }
5821 return false;
5822 }
5823
5824 /**
5825 * Returns class name for this activity with the package prefix removed.
5826 * This is the default name used to read and write settings.
RoboErik55011652014-07-09 15:05:53 -07005827 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005828 * @return The local class name.
5829 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005830 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005831 public String getLocalClassName() {
5832 final String pkg = getPackageName();
5833 final String cls = mComponent.getClassName();
5834 int packageLen = pkg.length();
5835 if (!cls.startsWith(pkg) || cls.length() <= packageLen
5836 || cls.charAt(packageLen) != '.') {
5837 return cls;
5838 }
5839 return cls.substring(packageLen+1);
5840 }
RoboErik55011652014-07-09 15:05:53 -07005841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005842 /**
5843 * Returns complete component name of this activity.
RoboErik55011652014-07-09 15:05:53 -07005844 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005845 * @return Returns the complete component name for this activity
5846 */
5847 public ComponentName getComponentName()
5848 {
5849 return mComponent;
5850 }
5851
5852 /**
5853 * Retrieve a {@link SharedPreferences} object for accessing preferences
5854 * that are private to this activity. This simply calls the underlying
5855 * {@link #getSharedPreferences(String, int)} method by passing in this activity's
5856 * class name as the preferences name.
RoboErik55011652014-07-09 15:05:53 -07005857 *
5858 * @param mode Operating mode. Use {@link #MODE_PRIVATE} for the default
Jeff Sharkey634dc422016-01-30 17:44:15 -07005859 * operation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005860 *
5861 * @return Returns the single SharedPreferences instance that can be used
5862 * to retrieve and modify the preference values.
5863 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06005864 public SharedPreferences getPreferences(@Context.PreferencesMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005865 return getSharedPreferences(getLocalClassName(), mode);
5866 }
RoboErik55011652014-07-09 15:05:53 -07005867
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005868 private void ensureSearchManager() {
5869 if (mSearchManager != null) {
5870 return;
5871 }
RoboErik55011652014-07-09 15:05:53 -07005872
Jeff Sharkey49ca5292016-05-10 12:54:45 -06005873 try {
5874 mSearchManager = new SearchManager(this, null);
5875 } catch (ServiceNotFoundException e) {
5876 throw new IllegalStateException(e);
5877 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005878 }
Tor Norbyed9273d62013-05-30 15:59:53 -07005879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005880 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07005881 public Object getSystemService(@ServiceName @NonNull String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005882 if (getBaseContext() == null) {
5883 throw new IllegalStateException(
5884 "System services not available to Activities before onCreate()");
5885 }
5886
5887 if (WINDOW_SERVICE.equals(name)) {
5888 return mWindowManager;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01005889 } else if (SEARCH_SERVICE.equals(name)) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005890 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01005891 return mSearchManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005892 }
5893 return super.getSystemService(name);
5894 }
5895
5896 /**
5897 * Change the title associated with this activity. If this is a
5898 * top-level activity, the title for its window will change. If it
5899 * is an embedded activity, the parent can do whatever it wants
5900 * with it.
5901 */
5902 public void setTitle(CharSequence title) {
5903 mTitle = title;
5904 onTitleChanged(title, mTitleColor);
5905
5906 if (mParent != null) {
5907 mParent.onChildTitleChanged(this, title);
5908 }
5909 }
5910
5911 /**
5912 * Change the title associated with this activity. If this is a
5913 * top-level activity, the title for its window will change. If it
5914 * is an embedded activity, the parent can do whatever it wants
5915 * with it.
5916 */
5917 public void setTitle(int titleId) {
5918 setTitle(getText(titleId));
5919 }
5920
Alan Viverette2525d9c2013-11-15 14:42:19 -08005921 /**
5922 * Change the color of the title associated with this activity.
5923 * <p>
5924 * This method is deprecated starting in API Level 11 and replaced by action
5925 * bar styles. For information on styling the Action Bar, read the <a
5926 * href="{@docRoot} guide/topics/ui/actionbar.html">Action Bar</a> developer
5927 * guide.
5928 *
5929 * @deprecated Use action bar styles instead.
5930 */
5931 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005932 public void setTitleColor(int textColor) {
5933 mTitleColor = textColor;
5934 onTitleChanged(mTitle, textColor);
5935 }
5936
5937 public final CharSequence getTitle() {
5938 return mTitle;
5939 }
5940
5941 public final int getTitleColor() {
5942 return mTitleColor;
5943 }
5944
5945 protected void onTitleChanged(CharSequence title, int color) {
5946 if (mTitleReady) {
5947 final Window win = getWindow();
5948 if (win != null) {
5949 win.setTitle(title);
5950 if (color != 0) {
5951 win.setTitleColor(color);
5952 }
5953 }
Adam Powellaf2d8592014-08-26 18:06:40 -07005954 if (mActionBar != null) {
5955 mActionBar.setWindowTitle(title);
5956 }
Adam Powella557fdc2014-08-21 18:05:53 -07005957 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005958 }
5959
5960 protected void onChildTitleChanged(Activity childActivity, CharSequence title) {
5961 }
5962
5963 /**
Winson Chunga449dc02014-05-16 11:15:04 -07005964 * Sets information describing the task with this activity for presentation inside the Recents
5965 * System UI. When {@link ActivityManager#getRecentTasks} is called, the activities of each task
5966 * are traversed in order from the topmost activity to the bottommost. The traversal continues
5967 * for each property until a suitable value is found. For each task the taskDescription will be
5968 * returned in {@link android.app.ActivityManager.TaskDescription}.
Craig Mautner2fbd7542014-03-21 09:34:07 -07005969 *
5970 * @see ActivityManager#getRecentTasks
Winson Chunga449dc02014-05-16 11:15:04 -07005971 * @see android.app.ActivityManager.TaskDescription
Craig Mautner2fbd7542014-03-21 09:34:07 -07005972 *
Winson Chunga449dc02014-05-16 11:15:04 -07005973 * @param taskDescription The TaskDescription properties that describe the task with this activity
Craig Mautner2fbd7542014-03-21 09:34:07 -07005974 */
Winson Chunga449dc02014-05-16 11:15:04 -07005975 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
Winsonb6403152016-02-23 13:32:09 -08005976 if (mTaskDescription != taskDescription) {
Jorim Jaggif4156ea2017-04-10 18:39:04 -07005977 mTaskDescription.copyFromPreserveHiddenFields(taskDescription);
Winsonb6403152016-02-23 13:32:09 -08005978 // Scale the icon down to something reasonable if it is provided
5979 if (taskDescription.getIconFilename() == null && taskDescription.getIcon() != null) {
5980 final int size = ActivityManager.getLauncherLargeIconSizeInner(this);
5981 final Bitmap icon = Bitmap.createScaledBitmap(taskDescription.getIcon(), size, size,
5982 true);
5983 mTaskDescription.setIcon(icon);
5984 }
Craig Mautner2fbd7542014-03-21 09:34:07 -07005985 }
5986 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005987 ActivityManager.getService().setTaskDescription(mToken, mTaskDescription);
Craig Mautner2fbd7542014-03-21 09:34:07 -07005988 } catch (RemoteException e) {
5989 }
5990 }
5991
5992 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005993 * Sets the visibility of the progress bar in the title.
5994 * <p>
5995 * In order for the progress bar to be shown, the feature must be requested
5996 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005997 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005998 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04005999 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006000 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006001 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006002 public final void setProgressBarVisibility(boolean visible) {
6003 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, visible ? Window.PROGRESS_VISIBILITY_ON :
6004 Window.PROGRESS_VISIBILITY_OFF);
6005 }
6006
6007 /**
6008 * Sets the visibility of the indeterminate progress bar in the title.
6009 * <p>
6010 * In order for the progress bar to be shown, the feature must be requested
6011 * via {@link #requestWindowFeature(int)}.
6012 *
6013 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006014 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006015 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006016 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006017 public final void setProgressBarIndeterminateVisibility(boolean visible) {
6018 getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,
6019 visible ? Window.PROGRESS_VISIBILITY_ON : Window.PROGRESS_VISIBILITY_OFF);
6020 }
RoboErik55011652014-07-09 15:05:53 -07006021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006022 /**
6023 * Sets whether the horizontal progress bar in the title should be indeterminate (the circular
6024 * is always indeterminate).
6025 * <p>
6026 * In order for the progress bar to be shown, the feature must be requested
6027 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006028 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006029 * @param indeterminate Whether the horizontal progress bar should be indeterminate.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006030 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006031 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006032 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006033 public final void setProgressBarIndeterminate(boolean indeterminate) {
6034 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
George Mounte1803372014-02-26 19:00:52 +00006035 indeterminate ? Window.PROGRESS_INDETERMINATE_ON
6036 : Window.PROGRESS_INDETERMINATE_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006037 }
RoboErik55011652014-07-09 15:05:53 -07006038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006039 /**
6040 * Sets the progress for the progress bars in the title.
6041 * <p>
6042 * In order for the progress bar to be shown, the feature must be requested
6043 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006044 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006045 * @param progress The progress for the progress bar. Valid ranges are from
6046 * 0 to 10000 (both inclusive). If 10000 is given, the progress
6047 * bar will be completely filled and will fade out.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006048 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006049 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006050 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006051 public final void setProgress(int progress) {
6052 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress + Window.PROGRESS_START);
6053 }
RoboErik55011652014-07-09 15:05:53 -07006054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006055 /**
6056 * Sets the secondary progress for the progress bar in the title. This
6057 * progress is drawn between the primary progress (set via
6058 * {@link #setProgress(int)} and the background. It can be ideal for media
6059 * scenarios such as showing the buffering progress while the default
6060 * progress shows the play progress.
6061 * <p>
6062 * In order for the progress bar to be shown, the feature must be requested
6063 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006064 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006065 * @param secondaryProgress The secondary progress for the progress bar. Valid ranges are from
6066 * 0 to 10000 (both inclusive).
Alan Viverette4aef7c82015-09-04 14:14:50 -04006067 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006068 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006069 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006070 public final void setSecondaryProgress(int secondaryProgress) {
6071 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
6072 secondaryProgress + Window.PROGRESS_SECONDARY_START);
6073 }
6074
6075 /**
6076 * Suggests an audio stream whose volume should be changed by the hardware
6077 * volume controls.
6078 * <p>
6079 * The suggested audio stream will be tied to the window of this Activity.
RoboErik55011652014-07-09 15:05:53 -07006080 * Volume requests which are received while the Activity is in the
6081 * foreground will affect this stream.
6082 * <p>
6083 * It is not guaranteed that the hardware volume controls will always change
6084 * this stream's volume (for example, if a call is in progress, its stream's
6085 * volume may be changed instead). To reset back to the default, use
6086 * {@link AudioManager#USE_DEFAULT_STREAM_TYPE}.
6087 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006088 * @param streamType The type of the audio stream whose volume should be
RoboErik55011652014-07-09 15:05:53 -07006089 * changed by the hardware volume controls.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006090 */
6091 public final void setVolumeControlStream(int streamType) {
6092 getWindow().setVolumeControlStream(streamType);
6093 }
6094
6095 /**
6096 * Gets the suggested audio stream whose volume should be changed by the
Tor Norbyed9273d62013-05-30 15:59:53 -07006097 * hardware volume controls.
RoboErik55011652014-07-09 15:05:53 -07006098 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006099 * @return The suggested audio stream type whose volume should be changed by
6100 * the hardware volume controls.
6101 * @see #setVolumeControlStream(int)
6102 */
6103 public final int getVolumeControlStream() {
6104 return getWindow().getVolumeControlStream();
6105 }
RoboErik55011652014-07-09 15:05:53 -07006106
6107 /**
6108 * Sets a {@link MediaController} to send media keys and volume changes to.
6109 * <p>
6110 * The controller will be tied to the window of this Activity. Media key and
6111 * volume events which are received while the Activity is in the foreground
6112 * will be forwarded to the controller and used to invoke transport controls
6113 * or adjust the volume. This may be used instead of or in addition to
6114 * {@link #setVolumeControlStream} to affect a specific session instead of a
6115 * specific stream.
6116 * <p>
6117 * It is not guaranteed that the hardware volume controls will always change
6118 * this session's volume (for example, if a call is in progress, its
6119 * stream's volume may be changed instead). To reset back to the default use
6120 * null as the controller.
6121 *
6122 * @param controller The controller for the session which should receive
6123 * media keys and volume changes.
6124 */
6125 public final void setMediaController(MediaController controller) {
6126 getWindow().setMediaController(controller);
6127 }
6128
6129 /**
6130 * Gets the controller which should be receiving media key and volume events
6131 * while this activity is in the foreground.
6132 *
6133 * @return The controller which should receive events.
6134 * @see #setMediaController(android.media.session.MediaController)
6135 */
6136 public final MediaController getMediaController() {
6137 return getWindow().getMediaController();
6138 }
6139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006140 /**
6141 * Runs the specified action on the UI thread. If the current thread is the UI
6142 * thread, then the action is executed immediately. If the current thread is
6143 * not the UI thread, the action is posted to the event queue of the UI thread.
6144 *
6145 * @param action the action to run on the UI thread
6146 */
Felipe Leme9876a6f2017-05-30 15:47:28 -07006147 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006148 public final void runOnUiThread(Runnable action) {
6149 if (Thread.currentThread() != mUiThread) {
6150 mHandler.post(action);
6151 } else {
6152 action.run();
6153 }
6154 }
6155
6156 /**
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006157 * Standard implementation of
6158 * {@link android.view.LayoutInflater.Factory#onCreateView} used when
6159 * inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackborn625ac272010-09-17 18:29:22 -07006160 * This implementation does nothing and is for
6161 * pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} apps. Newer apps
6162 * should use {@link #onCreateView(View, String, Context, AttributeSet)}.
6163 *
6164 * @see android.view.LayoutInflater#createView
6165 * @see android.view.Window#getLayoutInflater
6166 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006167 @Nullable
Dianne Hackborn625ac272010-09-17 18:29:22 -07006168 public View onCreateView(String name, Context context, AttributeSet attrs) {
6169 return null;
6170 }
6171
6172 /**
6173 * Standard implementation of
6174 * {@link android.view.LayoutInflater.Factory2#onCreateView(View, String, Context, AttributeSet)}
6175 * used when inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006176 * This implementation handles <fragment> tags to embed fragments inside
6177 * of the activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006178 *
6179 * @see android.view.LayoutInflater#createView
6180 * @see android.view.Window#getLayoutInflater
6181 */
Dianne Hackborn625ac272010-09-17 18:29:22 -07006182 public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006183 if (!"fragment".equals(name)) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07006184 return onCreateView(name, context, attrs);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006185 }
RoboErik55011652014-07-09 15:05:53 -07006186
Adam Powell371a8092014-06-20 12:51:12 -07006187 return mFragments.onCreateView(parent, name, context, attrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006188 }
6189
Daniel Sandler69a48172010-06-23 16:29:36 -04006190 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -07006191 * Print the Activity's state into the given stream. This gets invoked if
Jeff Sharkey5554b702012-04-11 18:30:51 -07006192 * you run "adb shell dumpsys activity &lt;activity_component_name&gt;".
Dianne Hackborn625ac272010-09-17 18:29:22 -07006193 *
Dianne Hackborn30d71892010-12-11 10:37:55 -08006194 * @param prefix Desired prefix to prepend at each line of output.
Dianne Hackborn625ac272010-09-17 18:29:22 -07006195 * @param fd The raw file descriptor that the dump is being sent to.
6196 * @param writer The PrintWriter to which you should dump your state. This will be
6197 * closed for you after you return.
6198 * @param args additional arguments to the dump request.
6199 */
Dianne Hackborn30d71892010-12-11 10:37:55 -08006200 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07006201 dumpInner(prefix, fd, writer, args);
6202 }
6203
6204 void dumpInner(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn30d71892010-12-11 10:37:55 -08006205 writer.print(prefix); writer.print("Local Activity ");
6206 writer.print(Integer.toHexString(System.identityHashCode(this)));
6207 writer.println(" State:");
6208 String innerPrefix = prefix + " ";
6209 writer.print(innerPrefix); writer.print("mResumed=");
6210 writer.print(mResumed); writer.print(" mStopped=");
6211 writer.print(mStopped); writer.print(" mFinished=");
6212 writer.println(mFinished);
Dianne Hackborn30d71892010-12-11 10:37:55 -08006213 writer.print(innerPrefix); writer.print("mChangingConfigurations=");
6214 writer.println(mChangingConfigurations);
6215 writer.print(innerPrefix); writer.print("mCurrentConfig=");
6216 writer.println(mCurrentConfig);
Jeff Brown5182c782013-10-15 20:31:52 -07006217
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006218 mFragments.dumpLoaders(innerPrefix, fd, writer, args);
6219 mFragments.getFragmentManager().dump(innerPrefix, fd, writer, args);
Dianne Hackborn57dd7372015-07-27 18:11:14 -07006220 if (mVoiceInteractor != null) {
6221 mVoiceInteractor.dump(innerPrefix, fd, writer, args);
6222 }
Jeff Brown5182c782013-10-15 20:31:52 -07006223
Michael Wright5f48dc72013-11-01 12:42:49 -07006224 if (getWindow() != null &&
6225 getWindow().peekDecorView() != null &&
6226 getWindow().peekDecorView().getViewRootImpl() != null) {
6227 getWindow().peekDecorView().getViewRootImpl().dump(prefix, fd, writer, args);
6228 }
Jeff Brown5182c782013-10-15 20:31:52 -07006229
6230 mHandler.getLooper().dump(new PrintWriterPrinter(writer), prefix);
Dianne Hackborn625ac272010-09-17 18:29:22 -07006231 }
6232
6233 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04006234 * Bit indicating that this activity is "immersive" and should not be
6235 * interrupted by notifications if possible.
6236 *
6237 * This value is initially set by the manifest property
6238 * <code>android:immersive</code> but may be changed at runtime by
6239 * {@link #setImmersive}.
6240 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07006241 * @see #setImmersive(boolean)
Daniel Sandler69a48172010-06-23 16:29:36 -04006242 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
6243 */
6244 public boolean isImmersive() {
6245 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006246 return ActivityManager.getService().isImmersive(mToken);
Daniel Sandler69a48172010-06-23 16:29:36 -04006247 } catch (RemoteException e) {
6248 return false;
6249 }
6250 }
6251
6252 /**
Craig Mautnerd61dc202014-07-07 11:09:11 -07006253 * Indication of whether this is the highest level activity in this task. Can be used to
6254 * determine whether an activity launched by this activity was placed in the same task or
6255 * another task.
6256 *
6257 * @return true if this is the topmost, non-finishing activity in its task.
Craig Mautnerd61dc202014-07-07 11:09:11 -07006258 */
George Mountff243282014-07-07 16:12:07 -07006259 private boolean isTopOfTask() {
George Mountf43ea5d2016-05-26 09:52:53 -07006260 if (mToken == null || mWindow == null) {
George Mountd0ca0e02016-03-29 10:32:56 -07006261 return false;
6262 }
Craig Mautnerd61dc202014-07-07 11:09:11 -07006263 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006264 return ActivityManager.getService().isTopOfTask(getActivityToken());
Craig Mautnerd61dc202014-07-07 11:09:11 -07006265 } catch (RemoteException e) {
6266 return false;
6267 }
6268 }
6269
6270 /**
Craig Mautner4addfc52013-06-25 08:05:45 -07006271 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} to a
6272 * fullscreen opaque Activity.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006273 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07006274 * Call this whenever the background of a translucent Activity has changed to become opaque.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006275 * Doing so will allow the {@link android.view.Surface} of the Activity behind to be released.
6276 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07006277 * This call has no effect on non-translucent activities or on activities with the
6278 * {@link android.R.attr#windowIsFloating} attribute.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006279 *
George Mount62ab9b72014-05-02 13:51:17 -07006280 * @see #convertToTranslucent(android.app.Activity.TranslucentConversionListener,
6281 * ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07006282 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07006283 *
6284 * @hide
Craig Mautner4addfc52013-06-25 08:05:45 -07006285 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006286 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07006287 public void convertFromTranslucent() {
Craig Mautner4addfc52013-06-25 08:05:45 -07006288 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07006289 mTranslucentCallback = null;
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006290 if (ActivityManager.getService().convertFromTranslucent(mToken)) {
Craig Mautnerbc57cd12013-08-19 15:47:42 -07006291 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, true);
6292 }
Craig Mautner4addfc52013-06-25 08:05:45 -07006293 } catch (RemoteException e) {
6294 // pass
6295 }
6296 }
6297
6298 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07006299 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from
6300 * opaque to translucent following a call to {@link #convertFromTranslucent()}.
6301 * <p>
6302 * Calling this allows the Activity behind this one to be seen again. Once all such Activities
6303 * have been redrawn {@link TranslucentConversionListener#onTranslucentConversionComplete} will
6304 * be called indicating that it is safe to make this activity translucent again. Until
6305 * {@link TranslucentConversionListener#onTranslucentConversionComplete} is called the image
6306 * behind the frontmost Activity will be indeterminate.
6307 * <p>
6308 * This call has no effect on non-translucent activities or on activities with the
6309 * {@link android.R.attr#windowIsFloating} attribute.
6310 *
6311 * @param callback the method to call when all visible Activities behind this one have been
6312 * drawn and it is safe to make this Activity translucent again.
Craig Mautner233ceee2014-05-09 17:05:11 -07006313 * @param options activity options delivered to the activity below this one. The options
6314 * are retrieved using {@link #getActivityOptions}.
George Mount3cc716c2014-06-12 16:35:35 -07006315 * @return <code>true</code> if Window was opaque and will become translucent or
6316 * <code>false</code> if window was translucent and no change needed to be made.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006317 *
6318 * @see #convertFromTranslucent()
6319 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07006320 *
6321 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07006322 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006323 @SystemApi
George Mount3cc716c2014-06-12 16:35:35 -07006324 public boolean convertToTranslucent(TranslucentConversionListener callback,
Jose Lima5517ea72014-06-10 12:31:43 -07006325 ActivityOptions options) {
Craig Mautner233ceee2014-05-09 17:05:11 -07006326 boolean drawComplete;
Craig Mautner5eda9b32013-07-02 11:58:16 -07006327 try {
6328 mTranslucentCallback = callback;
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006329 mChangeCanvasToTranslucent = ActivityManager.getService().convertToTranslucent(
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07006330 mToken, options == null ? null : options.toBundle());
George Mount9e183972014-09-03 12:35:09 -07006331 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
Craig Mautner233ceee2014-05-09 17:05:11 -07006332 drawComplete = true;
Craig Mautner5eda9b32013-07-02 11:58:16 -07006333 } catch (RemoteException e) {
Craig Mautner233ceee2014-05-09 17:05:11 -07006334 // Make callback return as though it timed out.
6335 mChangeCanvasToTranslucent = false;
6336 drawComplete = false;
6337 }
6338 if (!mChangeCanvasToTranslucent && mTranslucentCallback != null) {
6339 // Window is already translucent.
6340 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
Craig Mautner5eda9b32013-07-02 11:58:16 -07006341 }
George Mount3cc716c2014-06-12 16:35:35 -07006342 return mChangeCanvasToTranslucent;
Craig Mautner5eda9b32013-07-02 11:58:16 -07006343 }
6344
6345 /** @hide */
6346 void onTranslucentConversionComplete(boolean drawComplete) {
6347 if (mTranslucentCallback != null) {
6348 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
6349 mTranslucentCallback = null;
6350 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -07006351 if (mChangeCanvasToTranslucent) {
6352 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
6353 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07006354 }
6355
Craig Mautnereb8abf72014-07-02 15:04:09 -07006356 /** @hide */
6357 public void onNewActivityOptions(ActivityOptions options) {
6358 mActivityTransitionState.setEnterActivityOptions(this, options);
6359 if (!mStopped) {
6360 mActivityTransitionState.enterReady(this);
6361 }
6362 }
6363
Craig Mautner5eda9b32013-07-02 11:58:16 -07006364 /**
Craig Mautner233ceee2014-05-09 17:05:11 -07006365 * Retrieve the ActivityOptions passed in from the launching activity or passed back
6366 * from an activity launched by this activity in its call to {@link
6367 * #convertToTranslucent(TranslucentConversionListener, ActivityOptions)}
6368 *
6369 * @return The ActivityOptions passed to {@link #convertToTranslucent}.
6370 * @hide
6371 */
6372 ActivityOptions getActivityOptions() {
6373 try {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07006374 return ActivityOptions.fromBundle(
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006375 ActivityManager.getService().getActivityOptions(mToken));
Craig Mautner233ceee2014-05-09 17:05:11 -07006376 } catch (RemoteException e) {
6377 }
6378 return null;
6379 }
6380
6381 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006382 * Activities that want to remain visible behind a translucent activity above them must call
Craig Mautner64ccb702014-10-01 09:38:40 -07006383 * this method anytime between the start of {@link #onResume()} and the return from
6384 * {@link #onPause()}. If this call is successful then the activity will remain visible after
6385 * {@link #onPause()} is called, and is allowed to continue playing media in the background.
6386 *
6387 * <p>The actions of this call are reset each time that this activity is brought to the
6388 * front. That is, every time {@link #onResume()} is called the activity will be assumed
6389 * to not have requested visible behind. Therefore, if you want this activity to continue to
6390 * be visible in the background you must call this method again.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006391 *
6392 * <p>Only fullscreen opaque activities may make this call. I.e. this call is a nop
6393 * for dialog and translucent activities.
6394 *
Craig Mautner64ccb702014-10-01 09:38:40 -07006395 * <p>Under all circumstances, the activity must stop playing and release resources prior to or
6396 * within a call to {@link #onVisibleBehindCanceled()} or if this call returns false.
6397 *
6398 * <p>False will be returned any time this method is called between the return of onPause and
Craig Mautneree2e45a2014-06-27 12:10:03 -07006399 * the next call to onResume.
6400 *
Bryce Leed59629e2017-04-18 14:35:29 -07006401 * @deprecated This method's functionality is no longer supported as of
6402 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
6403 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006404 * @param visible true to notify the system that the activity wishes to be visible behind other
6405 * translucent activities, false to indicate otherwise. Resources must be
6406 * released when passing false to this method.
Bryce Leed59629e2017-04-18 14:35:29 -07006407 *
Craig Mautner64ccb702014-10-01 09:38:40 -07006408 * @return the resulting visibiity state. If true the activity will remain visible beyond
6409 * {@link #onPause()} if the next activity is translucent or not fullscreen. If false
6410 * then the activity may not count on being visible behind other translucent activities,
6411 * and must stop any media playback and release resources.
6412 * Returning false may occur in lieu of a call to {@link #onVisibleBehindCanceled()} so
6413 * the return value must be checked.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006414 *
Jose Limafcf70832014-08-27 23:09:05 -07006415 * @see #onVisibleBehindCanceled()
Craig Mautneree2e45a2014-06-27 12:10:03 -07006416 */
Bryce Leed59629e2017-04-18 14:35:29 -07006417 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07006418 public boolean requestVisibleBehind(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006419 if (!mResumed) {
Jose Lima4b6c6692014-08-12 17:41:12 -07006420 // Do not permit paused or stopped activities to do this.
6421 visible = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006422 }
6423 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006424 mVisibleBehind = ActivityManager.getService()
Jose Lima4b6c6692014-08-12 17:41:12 -07006425 .requestVisibleBehind(mToken, visible) && visible;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006426 } catch (RemoteException e) {
Jose Lima4b6c6692014-08-12 17:41:12 -07006427 mVisibleBehind = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006428 }
Jose Lima4b6c6692014-08-12 17:41:12 -07006429 return mVisibleBehind;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006430 }
6431
6432 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006433 * Called when a translucent activity over this activity is becoming opaque or another
6434 * activity is being launched. Activities that override this method must call
Jose Limafcf70832014-08-27 23:09:05 -07006435 * <code>super.onVisibleBehindCanceled()</code> or a SuperNotCalledException will be thrown.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006436 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006437 * <p>When this method is called the activity has 500 msec to release any resources it may be
6438 * using while visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006439 * If the activity has not returned from this method in 500 msec the system will destroy
Jose Lima4b6c6692014-08-12 17:41:12 -07006440 * the activity and kill the process in order to recover the resources for another
Craig Mautneree2e45a2014-06-27 12:10:03 -07006441 * process. Otherwise {@link #onStop()} will be called following return.
6442 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006443 * @see #requestVisibleBehind(boolean)
Bryce Leed59629e2017-04-18 14:35:29 -07006444 *
6445 * @deprecated This method's functionality is no longer supported as of
6446 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006447 */
Bryce Leed59629e2017-04-18 14:35:29 -07006448 @Deprecated
Tor Norbyec615c6f2015-03-02 10:11:44 -08006449 @CallSuper
Jose Limafcf70832014-08-27 23:09:05 -07006450 public void onVisibleBehindCanceled() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006451 mCalled = true;
6452 }
6453
6454 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006455 * Translucent activities may call this to determine if there is an activity below them that
6456 * is currently set to be visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006457 *
Bryce Leed59629e2017-04-18 14:35:29 -07006458 * @deprecated This method's functionality is no longer supported as of
6459 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
6460 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006461 * @return true if an activity below is set to visible according to the most recent call to
6462 * {@link #requestVisibleBehind(boolean)}, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006463 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006464 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07006465 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07006466 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07006467 * @hide
6468 */
Bryce Leed59629e2017-04-18 14:35:29 -07006469 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07006470 @SystemApi
6471 public boolean isBackgroundVisibleBehind() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006472 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006473 return ActivityManager.getService().isBackgroundVisibleBehind(mToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07006474 } catch (RemoteException e) {
6475 }
6476 return false;
6477 }
6478
6479 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006480 * The topmost foreground activity will receive this call when the background visibility state
6481 * of the activity below it changes.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006482 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006483 * This call may be a consequence of {@link #requestVisibleBehind(boolean)} or might be
Craig Mautneree2e45a2014-06-27 12:10:03 -07006484 * due to a background activity finishing itself.
6485 *
Bryce Leed59629e2017-04-18 14:35:29 -07006486 * @deprecated This method's functionality is no longer supported as of
6487 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
6488 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006489 * @param visible true if a background activity is visible, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006490 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006491 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07006492 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07006493 * @hide
Craig Mautneree2e45a2014-06-27 12:10:03 -07006494 */
Bryce Leed59629e2017-04-18 14:35:29 -07006495 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07006496 @SystemApi
6497 public void onBackgroundVisibleBehindChanged(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006498 }
6499
6500 /**
Craig Mautner8746a472014-07-24 15:12:54 -07006501 * Activities cannot draw during the period that their windows are animating in. In order
6502 * to know when it is safe to begin drawing they can override this method which will be
6503 * called when the entering animation has completed.
6504 */
6505 public void onEnterAnimationComplete() {
6506 }
6507
6508 /**
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08006509 * @hide
6510 */
6511 public void dispatchEnterAnimationComplete() {
6512 onEnterAnimationComplete();
6513 if (getWindow() != null && getWindow().getDecorView() != null) {
6514 getWindow().getDecorView().getViewTreeObserver().dispatchOnEnterAnimationComplete();
6515 }
6516 }
6517
6518 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04006519 * Adjust the current immersive mode setting.
Christopher Tate73c2aee2012-03-15 16:27:14 -07006520 *
Daniel Sandler69a48172010-06-23 16:29:36 -04006521 * Note that changing this value will have no effect on the activity's
6522 * {@link android.content.pm.ActivityInfo} structure; that is, if
6523 * <code>android:immersive</code> is set to <code>true</code>
6524 * in the application's manifest entry for this activity, the {@link
6525 * android.content.pm.ActivityInfo#flags ActivityInfo.flags} member will
6526 * always have its {@link android.content.pm.ActivityInfo#FLAG_IMMERSIVE
6527 * FLAG_IMMERSIVE} bit set.
6528 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07006529 * @see #isImmersive()
Daniel Sandler69a48172010-06-23 16:29:36 -04006530 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
6531 */
6532 public void setImmersive(boolean i) {
6533 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006534 ActivityManager.getService().setImmersive(mToken, i);
Daniel Sandler69a48172010-06-23 16:29:36 -04006535 } catch (RemoteException e) {
6536 // pass
6537 }
6538 }
6539
Adam Powell6e346362010-07-23 10:18:23 -07006540 /**
Ruben Brunk927d3452016-05-02 19:30:51 -07006541 * Enable or disable virtual reality (VR) mode for this Activity.
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006542 *
Ruben Brunk927d3452016-05-02 19:30:51 -07006543 * <p>VR mode is a hint to Android system to switch to a mode optimized for VR applications
6544 * while this Activity has user focus.</p>
6545 *
6546 * <p>It is recommended that applications additionally declare
6547 * {@link android.R.attr#enableVrMode} in their manifest to allow for smooth activity
6548 * transitions when switching between VR activities.</p>
6549 *
6550 * <p>If the requested {@link android.service.vr.VrListenerService} component is not available,
6551 * VR mode will not be started. Developers can handle this case as follows:</p>
6552 *
6553 * <pre>
6554 * String servicePackage = "com.whatever.app";
6555 * String serviceClass = "com.whatever.app.MyVrListenerService";
6556 *
6557 * // Name of the component of the VrListenerService to start.
6558 * ComponentName serviceComponent = new ComponentName(servicePackage, serviceClass);
6559 *
6560 * try {
6561 * setVrModeEnabled(true, myComponentName);
6562 * } catch (PackageManager.NameNotFoundException e) {
6563 * List&lt;ApplicationInfo> installed = getPackageManager().getInstalledApplications(0);
6564 * boolean isInstalled = false;
6565 * for (ApplicationInfo app : installed) {
6566 * if (app.packageName.equals(servicePackage)) {
6567 * isInstalled = true;
6568 * break;
6569 * }
6570 * }
6571 * if (isInstalled) {
6572 * // Package is installed, but not enabled in Settings. Let user enable it.
6573 * startActivity(new Intent(Settings.ACTION_VR_LISTENER_SETTINGS));
6574 * } else {
6575 * // Package is not installed. Send an intent to download this.
6576 * sentIntentToLaunchAppStore(servicePackage);
6577 * }
6578 * }
6579 * </pre>
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006580 *
6581 * @param enabled {@code true} to enable this mode.
Ruben Brunke24b9a62016-02-16 21:38:24 -08006582 * @param requestedComponent the name of the component to use as a
6583 * {@link android.service.vr.VrListenerService} while VR mode is enabled.
6584 *
Ruben Brunk927d3452016-05-02 19:30:51 -07006585 * @throws android.content.pm.PackageManager.NameNotFoundException if the given component
6586 * to run as a {@link android.service.vr.VrListenerService} is not installed, or has
6587 * not been enabled in user settings.
6588 *
6589 * @see android.content.pm.PackageManager#FEATURE_VR_MODE
6590 * @see android.content.pm.PackageManager#FEATURE_VR_MODE_HIGH_PERFORMANCE
6591 * @see android.service.vr.VrListenerService
6592 * @see android.provider.Settings#ACTION_VR_LISTENER_SETTINGS
6593 * @see android.R.attr#enableVrMode
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006594 */
Ruben Brunke24b9a62016-02-16 21:38:24 -08006595 public void setVrModeEnabled(boolean enabled, @NonNull ComponentName requestedComponent)
6596 throws PackageManager.NameNotFoundException {
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006597 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006598 if (ActivityManager.getService().setVrMode(mToken, enabled, requestedComponent)
Ruben Brunke24b9a62016-02-16 21:38:24 -08006599 != 0) {
6600 throw new PackageManager.NameNotFoundException(
6601 requestedComponent.flattenToString());
6602 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006603 } catch (RemoteException e) {
6604 // pass
6605 }
6606 }
6607
6608 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006609 * Start an action mode of the default type {@link ActionMode#TYPE_PRIMARY}.
Adam Powell6e346362010-07-23 10:18:23 -07006610 *
Clara Bayarri4423d912015-03-02 19:42:48 +00006611 * @param callback Callback that will manage lifecycle events for this action mode
6612 * @return The ActionMode that was started, or null if it was canceled
Adam Powell6e346362010-07-23 10:18:23 -07006613 *
6614 * @see ActionMode
6615 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006616 @Nullable
Adam Powell5d279772010-07-27 16:34:07 -07006617 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell6e346362010-07-23 10:18:23 -07006618 return mWindow.getDecorView().startActionMode(callback);
6619 }
6620
Adam Powelldebf3be2010-11-15 18:58:48 -08006621 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006622 * Start an action mode of the given type.
6623 *
6624 * @param callback Callback that will manage lifecycle events for this action mode
6625 * @param type One of {@link ActionMode#TYPE_PRIMARY} or {@link ActionMode#TYPE_FLOATING}.
6626 * @return The ActionMode that was started, or null if it was canceled
6627 *
6628 * @see ActionMode
6629 */
6630 @Nullable
6631 public ActionMode startActionMode(ActionMode.Callback callback, int type) {
6632 return mWindow.getDecorView().startActionMode(callback, type);
6633 }
6634
6635 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08006636 * Give the Activity a chance to control the UI for an action mode requested
6637 * by the system.
6638 *
6639 * <p>Note: If you are looking for a notification callback that an action mode
6640 * has been started for this activity, see {@link #onActionModeStarted(ActionMode)}.</p>
6641 *
6642 * @param callback The callback that should control the new action mode
6643 * @return The new action mode, or <code>null</code> if the activity does not want to
6644 * provide special handling for this action mode. (It will be handled by the system.)
6645 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006646 @Nullable
Craig Mautner5eda9b32013-07-02 11:58:16 -07006647 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006648 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
Clara Bayarri4423d912015-03-02 19:42:48 +00006649 // Only Primary ActionModes are represented in the ActionBar.
6650 if (mActionModeTypeStarting == ActionMode.TYPE_PRIMARY) {
6651 initWindowDecorActionBar();
6652 if (mActionBar != null) {
6653 return mActionBar.startActionMode(callback);
6654 }
Adam Powell6e346362010-07-23 10:18:23 -07006655 }
6656 return null;
6657 }
6658
Adam Powelldebf3be2010-11-15 18:58:48 -08006659 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006660 * {@inheritDoc}
6661 */
6662 @Nullable
6663 @Override
6664 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) {
6665 try {
6666 mActionModeTypeStarting = type;
6667 return onWindowStartingActionMode(callback);
6668 } finally {
6669 mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
6670 }
6671 }
6672
6673 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08006674 * Notifies the Activity that an action mode has been started.
6675 * Activity subclasses overriding this method should call the superclass implementation.
6676 *
6677 * @param mode The new action mode.
6678 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08006679 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07006680 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006681 public void onActionModeStarted(ActionMode mode) {
6682 }
6683
6684 /**
6685 * Notifies the activity that an action mode has finished.
6686 * Activity subclasses overriding this method should call the superclass implementation.
6687 *
6688 * @param mode The action mode that just finished.
6689 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08006690 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07006691 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006692 public void onActionModeFinished(ActionMode mode) {
6693 }
6694
Adam Powelldd8fab22012-03-22 17:47:27 -07006695 /**
6696 * Returns true if the app should recreate the task when navigating 'up' from this activity
6697 * by using targetIntent.
6698 *
6699 * <p>If this method returns false the app can trivially call
6700 * {@link #navigateUpTo(Intent)} using the same parameters to correctly perform
6701 * up navigation. If this method returns false, the app should synthesize a new task stack
6702 * by using {@link TaskStackBuilder} or another similar mechanism to perform up navigation.</p>
6703 *
6704 * @param targetIntent An intent representing the target destination for up navigation
6705 * @return true if navigating up should recreate a new task stack, false if the same task
6706 * should be used for the destination
6707 */
6708 public boolean shouldUpRecreateTask(Intent targetIntent) {
6709 try {
6710 PackageManager pm = getPackageManager();
6711 ComponentName cn = targetIntent.getComponent();
6712 if (cn == null) {
6713 cn = targetIntent.resolveActivity(pm);
6714 }
6715 ActivityInfo info = pm.getActivityInfo(cn, 0);
6716 if (info.taskAffinity == null) {
6717 return false;
6718 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006719 return ActivityManager.getService()
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07006720 .shouldUpRecreateTask(mToken, info.taskAffinity);
Adam Powelldd8fab22012-03-22 17:47:27 -07006721 } catch (RemoteException e) {
6722 return false;
6723 } catch (NameNotFoundException e) {
6724 return false;
6725 }
6726 }
6727
6728 /**
6729 * Navigate from this activity to the activity specified by upIntent, finishing this activity
6730 * in the process. If the activity indicated by upIntent already exists in the task's history,
6731 * this activity and all others before the indicated activity in the history stack will be
Adam Powell35c064b2012-05-02 11:37:15 -07006732 * finished.
6733 *
6734 * <p>If the indicated activity does not appear in the history stack, this will finish
6735 * each activity in this task until the root activity of the task is reached, resulting in
6736 * an "in-app home" behavior. This can be useful in apps with a complex navigation hierarchy
6737 * when an activity may be reached by a path not passing through a canonical parent
6738 * activity.</p>
Adam Powelldd8fab22012-03-22 17:47:27 -07006739 *
6740 * <p>This method should be used when performing up navigation from within the same task
6741 * as the destination. If up navigation should cross tasks in some cases, see
6742 * {@link #shouldUpRecreateTask(Intent)}.</p>
6743 *
6744 * @param upIntent An intent representing the target destination for up navigation
6745 *
6746 * @return true if up navigation successfully reached the activity indicated by upIntent and
6747 * upIntent was delivered to it. false if an instance of the indicated activity could
6748 * not be found and this activity was simply finished normally.
6749 */
6750 public boolean navigateUpTo(Intent upIntent) {
6751 if (mParent == null) {
6752 ComponentName destInfo = upIntent.getComponent();
6753 if (destInfo == null) {
6754 destInfo = upIntent.resolveActivity(getPackageManager());
6755 if (destInfo == null) {
6756 return false;
6757 }
6758 upIntent = new Intent(upIntent);
6759 upIntent.setComponent(destInfo);
6760 }
6761 int resultCode;
6762 Intent resultData;
6763 synchronized (this) {
6764 resultCode = mResultCode;
6765 resultData = mResultData;
6766 }
6767 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006768 resultData.prepareToLeaveProcess(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07006769 }
6770 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006771 upIntent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006772 return ActivityManager.getService().navigateUpTo(mToken, upIntent,
Adam Powelldd8fab22012-03-22 17:47:27 -07006773 resultCode, resultData);
6774 } catch (RemoteException e) {
6775 return false;
6776 }
6777 } else {
6778 return mParent.navigateUpToFromChild(this, upIntent);
6779 }
6780 }
6781
6782 /**
6783 * This is called when a child activity of this one calls its
6784 * {@link #navigateUpTo} method. The default implementation simply calls
6785 * navigateUpTo(upIntent) on this activity (the parent).
6786 *
6787 * @param child The activity making the call.
6788 * @param upIntent An intent representing the target destination for up navigation
6789 *
6790 * @return true if up navigation successfully reached the activity indicated by upIntent and
6791 * upIntent was delivered to it. false if an instance of the indicated activity could
6792 * not be found and this activity was simply finished normally.
6793 */
6794 public boolean navigateUpToFromChild(Activity child, Intent upIntent) {
6795 return navigateUpTo(upIntent);
6796 }
6797
6798 /**
6799 * Obtain an {@link Intent} that will launch an explicit target activity specified by
6800 * this activity's logical parent. The logical parent is named in the application's manifest
6801 * by the {@link android.R.attr#parentActivityName parentActivityName} attribute.
Adam Powell04d58112012-04-09 10:22:12 -07006802 * Activity subclasses may override this method to modify the Intent returned by
6803 * super.getParentActivityIntent() or to implement a different mechanism of retrieving
6804 * the parent intent entirely.
Adam Powelldd8fab22012-03-22 17:47:27 -07006805 *
Adam Powell04d58112012-04-09 10:22:12 -07006806 * @return a new Intent targeting the defined parent of this activity or null if
6807 * there is no valid parent.
Adam Powelldd8fab22012-03-22 17:47:27 -07006808 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006809 @Nullable
Adam Powelldd8fab22012-03-22 17:47:27 -07006810 public Intent getParentActivityIntent() {
Adam Powell04d58112012-04-09 10:22:12 -07006811 final String parentName = mActivityInfo.parentActivityName;
6812 if (TextUtils.isEmpty(parentName)) {
6813 return null;
6814 }
Adam Powell5a4010c2012-09-16 15:14:05 -07006815
6816 // If the parent itself has no parent, generate a main activity intent.
6817 final ComponentName target = new ComponentName(this, parentName);
6818 try {
6819 final ActivityInfo parentInfo = getPackageManager().getActivityInfo(target, 0);
6820 final String parentActivity = parentInfo.parentActivityName;
6821 final Intent parentIntent = parentActivity == null
6822 ? Intent.makeMainActivity(target)
6823 : new Intent().setComponent(target);
6824 return parentIntent;
6825 } catch (NameNotFoundException e) {
6826 Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName +
6827 "' in manifest");
6828 return null;
6829 }
Adam Powelldd8fab22012-03-22 17:47:27 -07006830 }
6831
George Mount31a21722014-03-24 17:44:36 -07006832 /**
George Mount62ab9b72014-05-02 13:51:17 -07006833 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07006834 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07006835 * will be called to handle shared elements on the <i>launched</i> Activity. This requires
George Mount9826f632014-09-11 08:50:09 -07006836 * {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount31a21722014-03-24 17:44:36 -07006837 *
George Mount65580562014-08-29 08:15:48 -07006838 * @param callback Used to manipulate shared element transitions on the launched Activity.
George Mount31a21722014-03-24 17:44:36 -07006839 */
George Mount65580562014-08-29 08:15:48 -07006840 public void setEnterSharedElementCallback(SharedElementCallback callback) {
6841 if (callback == null) {
6842 callback = SharedElementCallback.NULL_CALLBACK;
George Mount31a21722014-03-24 17:44:36 -07006843 }
George Mount65580562014-08-29 08:15:48 -07006844 mEnterTransitionListener = callback;
George Mount800d72b2014-05-19 07:09:00 -07006845 }
6846
6847 /**
6848 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07006849 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07006850 * will be called to handle shared elements on the <i>launching</i> Activity. Most
6851 * calls will only come when returning from the started Activity.
George Mount9826f632014-09-11 08:50:09 -07006852 * This requires {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount800d72b2014-05-19 07:09:00 -07006853 *
George Mount65580562014-08-29 08:15:48 -07006854 * @param callback Used to manipulate shared element transitions on the launching Activity.
George Mount800d72b2014-05-19 07:09:00 -07006855 */
George Mount65580562014-08-29 08:15:48 -07006856 public void setExitSharedElementCallback(SharedElementCallback callback) {
6857 if (callback == null) {
6858 callback = SharedElementCallback.NULL_CALLBACK;
George Mount800d72b2014-05-19 07:09:00 -07006859 }
George Mount65580562014-08-29 08:15:48 -07006860 mExitTransitionListener = callback;
George Mount31a21722014-03-24 17:44:36 -07006861 }
6862
George Mount8c2614c2014-06-10 11:12:01 -07006863 /**
6864 * Postpone the entering activity transition when Activity was started with
6865 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
6866 * android.util.Pair[])}.
6867 * <p>This method gives the Activity the ability to delay starting the entering and
6868 * shared element transitions until all data is loaded. Until then, the Activity won't
6869 * draw into its window, leaving the window transparent. This may also cause the
6870 * returning animation to be delayed until data is ready. This method should be
6871 * called in {@link #onCreate(android.os.Bundle)} or in
6872 * {@link #onActivityReenter(int, android.content.Intent)}.
6873 * {@link #startPostponedEnterTransition()} must be called to allow the Activity to
6874 * start the transitions. If the Activity did not use
6875 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
6876 * android.util.Pair[])}, then this method does nothing.</p>
6877 */
6878 public void postponeEnterTransition() {
6879 mActivityTransitionState.postponeEnterTransition();
6880 }
6881
6882 /**
6883 * Begin postponed transitions after {@link #postponeEnterTransition()} was called.
6884 * If postponeEnterTransition() was called, you must call startPostponedEnterTransition()
6885 * to have your Activity start drawing.
6886 */
6887 public void startPostponedEnterTransition() {
6888 mActivityTransitionState.startPostponedEnterTransition();
6889 }
6890
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08006891 /**
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07006892 * Create {@link DragAndDropPermissions} object bound to this activity and controlling the
6893 * access permissions for content URIs associated with the {@link DragEvent}.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08006894 * @param event Drag event
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07006895 * @return The {@link DragAndDropPermissions} object used to control access to the content URIs.
6896 * Null if no content URIs are associated with the event or if permissions could not be granted.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08006897 */
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07006898 public DragAndDropPermissions requestDragAndDropPermissions(DragEvent event) {
6899 DragAndDropPermissions dragAndDropPermissions = DragAndDropPermissions.obtain(event);
6900 if (dragAndDropPermissions != null && dragAndDropPermissions.take(getActivityToken())) {
6901 return dragAndDropPermissions;
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08006902 }
6903 return null;
6904 }
6905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006906 // ------------------ Internal API ------------------
RoboErik55011652014-07-09 15:05:53 -07006907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006908 final void setParent(Activity parent) {
6909 mParent = parent;
6910 }
6911
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006912 final void attach(Context context, ActivityThread aThread,
6913 Instrumentation instr, IBinder token, int ident,
6914 Application application, Intent intent, ActivityInfo info,
6915 CharSequence title, Activity parent, String id,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006916 NonConfigurationInstances lastNonConfigurationInstances,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07006917 Configuration config, String referrer, IVoiceInteractor voiceInteractor,
Andrii Kulian51c1b672017-04-07 18:39:32 -07006918 Window window, ActivityConfigCallback activityConfigCallback) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006919 attachBaseContext(context);
6920
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006921 mFragments.attachHost(null /*parent*/);
RoboErik55011652014-07-09 15:05:53 -07006922
Andrii Kulian51c1b672017-04-07 18:39:32 -07006923 mWindow = new PhoneWindow(this, window, activityConfigCallback);
Skuhnece2faa52015-08-11 10:36:38 -07006924 mWindow.setWindowControllerCallback(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006925 mWindow.setCallback(this);
Adam Powell117b6952014-05-05 18:14:56 -07006926 mWindow.setOnWindowDismissedCallback(this);
Dianne Hackborn420829e2011-01-28 11:30:35 -08006927 mWindow.getLayoutInflater().setPrivateFactory(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006928 if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
6929 mWindow.setSoftInputMode(info.softInputMode);
6930 }
Adam Powell269248d2011-08-02 10:26:54 -07006931 if (info.uiOptions != 0) {
6932 mWindow.setUiOptions(info.uiOptions);
6933 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006934 mUiThread = Thread.currentThread();
George Mount0a778ed2013-12-13 13:35:36 -08006935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006936 mMainThread = aThread;
6937 mInstrumentation = instr;
6938 mToken = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006939 mIdent = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006940 mApplication = application;
6941 mIntent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08006942 mReferrer = referrer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006943 mComponent = intent.getComponent();
6944 mActivityInfo = info;
6945 mTitle = title;
6946 mParent = parent;
6947 mEmbeddedID = id;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006948 mLastNonConfigurationInstances = lastNonConfigurationInstances;
Dianne Hackborn20d94742014-05-29 18:35:45 -07006949 if (voiceInteractor != null) {
6950 if (lastNonConfigurationInstances != null) {
6951 mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor;
6952 } else {
6953 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
6954 Looper.myLooper());
6955 }
6956 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006957
Jeff Brown98365d72012-08-19 20:30:52 -07006958 mWindow.setWindowManager(
6959 (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
6960 mToken, mComponent.flattenToString(),
Romain Guy529b60a2010-08-03 18:05:47 -07006961 (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006962 if (mParent != null) {
6963 mWindow.setContainer(mParent.getWindow());
6964 }
6965 mWindowManager = mWindow.getWindowManager();
6966 mCurrentConfig = config;
Romain Guy48327452017-01-23 17:03:35 -08006967
6968 mWindow.setColorMode(info.colorMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006969 }
6970
Dianne Hackborn5320eb82012-05-18 12:05:04 -07006971 /** @hide */
6972 public final IBinder getActivityToken() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006973 return mParent != null ? mParent.getActivityToken() : mToken;
6974 }
6975
Narayan Kamathff5659f2017-02-02 13:31:33 +00006976 final void performCreateCommon() {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08006977 mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
6978 com.android.internal.R.styleable.Window_windowNoDisplay, false);
Dianne Hackbornc8017682010-07-06 13:34:38 -07006979 mFragments.dispatchActivityCreated();
Eino-Ville Talvala563df3b2016-06-06 22:04:54 +00006980 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006981 }
George Mount0a778ed2013-12-13 13:35:36 -08006982
Craig Mautnera0026042014-04-23 11:45:37 -07006983 final void performCreate(Bundle icicle) {
Svetoslavffb32b12015-10-15 16:54:00 -07006984 restoreHasCurrentPermissionRequest(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07006985 onCreate(icicle);
Narayan Kamathff5659f2017-02-02 13:31:33 +00006986 mActivityTransitionState.readState(icicle);
6987 performCreateCommon();
Craig Mautnera0026042014-04-23 11:45:37 -07006988 }
6989
6990 final void performCreate(Bundle icicle, PersistableBundle persistentState) {
Svetoslavffb32b12015-10-15 16:54:00 -07006991 restoreHasCurrentPermissionRequest(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07006992 onCreate(icicle, persistentState);
Narayan Kamathff5659f2017-02-02 13:31:33 +00006993 mActivityTransitionState.readState(icicle);
6994 performCreateCommon();
Craig Mautnera0026042014-04-23 11:45:37 -07006995 }
6996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006997 final void performStart() {
George Mount62ab9b72014-05-02 13:51:17 -07006998 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07006999 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007000 mCalled = false;
Dianne Hackborn445646c2010-06-25 15:52:59 -07007001 mFragments.execPendingActions();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007002 mInstrumentation.callActivityOnStart(this);
7003 if (!mCalled) {
7004 throw new SuperNotCalledException(
7005 "Activity " + mComponent.toShortString() +
7006 " did not call through to super.onStart()");
7007 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007008 mFragments.dispatchStart();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007009 mFragments.reportLoaderStart();
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007010
7011 // This property is set for all builds except final release
7012 boolean isDlwarningEnabled = SystemProperties.getInt("ro.bionic.ld.warning", 0) == 1;
7013 boolean isAppDebuggable =
7014 (mApplication.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
7015
7016 if (isAppDebuggable || isDlwarningEnabled) {
7017 String dlwarning = getDlWarning();
7018 if (dlwarning != null) {
Adam Powelld93f3b12016-03-03 08:43:37 -08007019 String appName = getApplicationInfo().loadLabel(getPackageManager())
7020 .toString();
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007021 String warning = "Detected problems with app native libraries\n" +
7022 "(please consult log for detail):\n" + dlwarning;
7023 if (isAppDebuggable) {
7024 new AlertDialog.Builder(this).
7025 setTitle(appName).
7026 setMessage(warning).
7027 setPositiveButton(android.R.string.ok, null).
7028 setCancelable(false).
7029 show();
7030 } else {
7031 Toast.makeText(this, appName + "\n" + warning, Toast.LENGTH_LONG).show();
7032 }
7033 }
7034 }
7035
George Mount62ab9b72014-05-02 13:51:17 -07007036 mActivityTransitionState.enterReady(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007037 }
RoboErik55011652014-07-09 15:05:53 -07007038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007039 final void performRestart() {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07007040 mFragments.noteStateNotSaved();
Dianne Hackborna21e3da2010-09-12 19:27:46 -07007041
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007042 if (mToken != null && mParent == null) {
Chong Zhang7687f252016-02-26 12:03:33 -08007043 // No need to check mStopped, the roots will check if they were actually stopped.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007044 WindowManagerGlobal.getInstance().setStoppedState(mToken, false /* stopped */);
7045 }
7046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007047 if (mStopped) {
7048 mStopped = false;
Dianne Hackborn185e3e22011-06-03 15:26:01 -07007049
7050 synchronized (mManagedCursors) {
7051 final int N = mManagedCursors.size();
7052 for (int i=0; i<N; i++) {
7053 ManagedCursor mc = mManagedCursors.get(i);
7054 if (mc.mReleased || mc.mUpdated) {
7055 if (!mc.mCursor.requery()) {
Dianne Hackborna5445d32011-09-01 14:38:24 -07007056 if (getApplicationInfo().targetSdkVersion
7057 >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
7058 throw new IllegalStateException(
7059 "trying to requery an already closed cursor "
7060 + mc.mCursor);
7061 }
Dianne Hackborn185e3e22011-06-03 15:26:01 -07007062 }
7063 mc.mReleased = false;
7064 mc.mUpdated = false;
7065 }
7066 }
7067 }
7068
7069 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007070 mInstrumentation.callActivityOnRestart(this);
7071 if (!mCalled) {
7072 throw new SuperNotCalledException(
7073 "Activity " + mComponent.toShortString() +
7074 " did not call through to super.onRestart()");
7075 }
7076 performStart();
7077 }
7078 }
RoboErik55011652014-07-09 15:05:53 -07007079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007080 final void performResume() {
7081 performRestart();
RoboErik55011652014-07-09 15:05:53 -07007082
Dianne Hackborn445646c2010-06-25 15:52:59 -07007083 mFragments.execPendingActions();
RoboErik55011652014-07-09 15:05:53 -07007084
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007085 mLastNonConfigurationInstances = null;
RoboErik55011652014-07-09 15:05:53 -07007086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007087 mCalled = false;
Jeff Hamilton52d32032011-01-08 15:31:26 -06007088 // mResumed is set by the instrumentation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007089 mInstrumentation.callActivityOnResume(this);
7090 if (!mCalled) {
7091 throw new SuperNotCalledException(
7092 "Activity " + mComponent.toShortString() +
7093 " did not call through to super.onResume()");
7094 }
7095
Todd Kennedyee8c9c62014-12-10 14:22:59 -08007096 // invisible activities must be finished before onResume() completes
7097 if (!mVisibleFromClient && !mFinished) {
7098 Log.w(TAG, "An activity without a UI must call finish() before onResume() completes");
7099 if (getApplicationInfo().targetSdkVersion
7100 > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
7101 throw new IllegalStateException(
7102 "Activity " + mComponent.toShortString() +
7103 " did not call finish() prior to onResume() completing");
7104 }
7105 }
7106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007107 // Now really resume, and install the current status bar and menu.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007108 mCalled = false;
RoboErik55011652014-07-09 15:05:53 -07007109
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007110 mFragments.dispatchResume();
Dianne Hackborn445646c2010-06-25 15:52:59 -07007111 mFragments.execPendingActions();
Narayan Kamathff5659f2017-02-02 13:31:33 +00007112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007113 onPostResume();
7114 if (!mCalled) {
7115 throw new SuperNotCalledException(
7116 "Activity " + mComponent.toShortString() +
7117 " did not call through to super.onPostResume()");
7118 }
7119 }
7120
7121 final void performPause() {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07007122 mDoReportFullyDrawn = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007123 mFragments.dispatchPause();
Dianne Hackborne794e9f2010-08-24 12:32:10 -07007124 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007125 onPause();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08007126 mResumed = false;
Dianne Hackborne794e9f2010-08-24 12:32:10 -07007127 if (!mCalled && getApplicationInfo().targetSdkVersion
7128 >= android.os.Build.VERSION_CODES.GINGERBREAD) {
7129 throw new SuperNotCalledException(
7130 "Activity " + mComponent.toShortString() +
7131 " did not call through to super.onPause()");
7132 }
Narayan Kamathff5659f2017-02-02 13:31:33 +00007133 mResumed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007134 }
RoboErik55011652014-07-09 15:05:53 -07007135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007136 final void performUserLeaving() {
7137 onUserInteraction();
7138 onUserLeaveHint();
7139 }
RoboErik55011652014-07-09 15:05:53 -07007140
Chong Zhang7687f252016-02-26 12:03:33 -08007141 final void performStop(boolean preserveWindow) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07007142 mDoReportFullyDrawn = false;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007143 mFragments.doLoaderStop(mChangingConfigurations /*retain*/);
Narayan Kamathff5659f2017-02-02 13:31:33 +00007144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007145 if (!mStopped) {
7146 if (mWindow != null) {
7147 mWindow.closeAllPanels();
7148 }
7149
Chong Zhang7687f252016-02-26 12:03:33 -08007150 // If we're preserving the window, don't setStoppedState to true, since we
7151 // need the window started immediately again. Stopping the window will
7152 // destroys hardware resources and causes flicker.
7153 if (!preserveWindow && mToken != null && mParent == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07007154 WindowManagerGlobal.getInstance().setStoppedState(mToken, true);
Dianne Hackbornce418e62011-03-01 14:31:38 -08007155 }
RoboErik55011652014-07-09 15:05:53 -07007156
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007157 mFragments.dispatchStop();
RoboErik55011652014-07-09 15:05:53 -07007158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007159 mCalled = false;
7160 mInstrumentation.callActivityOnStop(this);
7161 if (!mCalled) {
7162 throw new SuperNotCalledException(
7163 "Activity " + mComponent.toShortString() +
7164 " did not call through to super.onStop()");
7165 }
RoboErik55011652014-07-09 15:05:53 -07007166
Makoto Onuki2f6a0182010-02-22 13:26:59 -08007167 synchronized (mManagedCursors) {
7168 final int N = mManagedCursors.size();
7169 for (int i=0; i<N; i++) {
7170 ManagedCursor mc = mManagedCursors.get(i);
7171 if (!mc.mReleased) {
7172 mc.mCursor.deactivate();
7173 mc.mReleased = true;
7174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007175 }
7176 }
George Mount0a778ed2013-12-13 13:35:36 -08007177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007178 mStopped = true;
7179 }
7180 mResumed = false;
7181 }
7182
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007183 final void performDestroy() {
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07007184 mDestroyed = true;
Dianne Hackborn291905e2010-08-17 15:17:15 -07007185 mWindow.destroy();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007186 mFragments.dispatchDestroy();
7187 onDestroy();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007188 mFragments.doLoaderDestroy();
Dianne Hackborn20d94742014-05-29 18:35:45 -07007189 if (mVoiceInteractor != null) {
7190 mVoiceInteractor.detachActivity();
7191 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007192 }
George Mount0a778ed2013-12-13 13:35:36 -08007193
Winson Chung5af42fc2017-03-24 17:11:33 -07007194 final void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode,
7195 Configuration newConfig) {
Adam Powell858cf032016-05-09 15:45:37 -07007196 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Winson Chung5af42fc2017-03-24 17:11:33 -07007197 "dispatchMultiWindowModeChanged " + this + ": " + isInMultiWindowMode
7198 + " " + newConfig);
7199 mFragments.dispatchMultiWindowModeChanged(isInMultiWindowMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07007200 if (mWindow != null) {
7201 mWindow.onMultiWindowModeChanged();
7202 }
Winson Chung5af42fc2017-03-24 17:11:33 -07007203 onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07007204 }
7205
Winson Chung5af42fc2017-03-24 17:11:33 -07007206 final void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode,
7207 Configuration newConfig) {
Adam Powell858cf032016-05-09 15:45:37 -07007208 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Winson Chung5af42fc2017-03-24 17:11:33 -07007209 "dispatchPictureInPictureModeChanged " + this + ": " + isInPictureInPictureMode
7210 + " " + newConfig);
7211 mFragments.dispatchPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
Winson Chung4d8681f2017-05-23 16:22:08 -07007212 if (mWindow != null) {
7213 mWindow.onPictureInPictureModeChanged(isInPictureInPictureMode);
7214 }
Winson Chung5af42fc2017-03-24 17:11:33 -07007215 onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07007216 }
7217
George Mount0a778ed2013-12-13 13:35:36 -08007218 /**
Jeff Hamilton52d32032011-01-08 15:31:26 -06007219 * @hide
7220 */
7221 public final boolean isResumed() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007222 return mResumed;
7223 }
7224
Svetoslavffb32b12015-10-15 16:54:00 -07007225 private void storeHasCurrentPermissionRequest(Bundle bundle) {
7226 if (bundle != null && mHasCurrentPermissionsRequest) {
7227 bundle.putBoolean(HAS_CURENT_PERMISSIONS_REQUEST_KEY, true);
7228 }
7229 }
7230
7231 private void restoreHasCurrentPermissionRequest(Bundle bundle) {
7232 if (bundle != null) {
7233 mHasCurrentPermissionsRequest = bundle.getBoolean(
7234 HAS_CURENT_PERMISSIONS_REQUEST_KEY, false);
7235 }
7236 }
7237
George Mount0a778ed2013-12-13 13:35:36 -08007238 void dispatchActivityResult(String who, int requestCode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007239 int resultCode, Intent data) {
Joe Onorato43a17652011-04-06 19:22:23 -07007240 if (false) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007241 TAG, "Dispatching result: who=" + who + ", reqCode=" + requestCode
7242 + ", resCode=" + resultCode + ", data=" + data);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07007243 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007244 if (who == null) {
Svetoslav970b59c2015-06-09 16:05:21 -07007245 onActivityResult(requestCode, resultCode, data);
7246 } else if (who.startsWith(REQUEST_PERMISSIONS_WHO_PREFIX)) {
7247 who = who.substring(REQUEST_PERMISSIONS_WHO_PREFIX.length());
7248 if (TextUtils.isEmpty(who)) {
Svetoslavc6d1c342015-02-26 14:44:43 -08007249 dispatchRequestPermissionsResult(requestCode, data);
7250 } else {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00007251 Fragment frag = mFragments.findFragmentByWho(who);
7252 if (frag != null) {
Svetoslav970b59c2015-06-09 16:05:21 -07007253 dispatchRequestPermissionsResultToFragment(requestCode, data, frag);
Svetoslavc6d1c342015-02-26 14:44:43 -08007254 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07007255 }
Svetoslav970b59c2015-06-09 16:05:21 -07007256 } else if (who.startsWith("@android:view:")) {
7257 ArrayList<ViewRootImpl> views = WindowManagerGlobal.getInstance().getRootViews(
7258 getActivityToken());
7259 for (ViewRootImpl viewRoot : views) {
7260 if (viewRoot.getView() != null
7261 && viewRoot.getView().dispatchActivityResult(
7262 who, requestCode, resultCode, data)) {
7263 return;
7264 }
7265 }
Svet Ganov782043c2017-02-11 00:52:02 +00007266 } else if (who.startsWith(AUTO_FILL_AUTH_WHO_PREFIX)) {
Svet Ganov00c771dc2017-02-19 00:06:22 -08007267 Intent resultData = (resultCode == Activity.RESULT_OK) ? data : null;
Svetoslav Ganova9379d02017-05-09 17:40:24 -07007268 getAutofillManager().onAuthenticationResult(requestCode, resultData);
Svetoslav970b59c2015-06-09 16:05:21 -07007269 } else {
7270 Fragment frag = mFragments.findFragmentByWho(who);
7271 if (frag != null) {
7272 frag.onActivityResult(requestCode, resultCode, data);
7273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007274 }
7275 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07007276
Jason Monka57e5e02014-05-07 10:06:48 -04007277 /**
Jason Monk62515be2014-05-21 16:06:19 -04007278 * Request to put this Activity in a mode where the user is locked to the
Jason Monka57e5e02014-05-07 10:06:48 -04007279 * current task.
7280 *
Craig Mautner15df08a2015-04-01 12:17:18 -07007281 * This will prevent the user from launching other apps, going to settings, or reaching the
7282 * home screen. This does not include those apps whose {@link android.R.attr#lockTaskMode}
7283 * values permit launching while locked.
Jason Monka57e5e02014-05-07 10:06:48 -04007284 *
Craig Mautner15df08a2015-04-01 12:17:18 -07007285 * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns true or
7286 * lockTaskMode=lockTaskModeAlways for this component then the app will go directly into
7287 * Lock Task mode. The user will not be able to exit this mode until
7288 * {@link Activity#stopLockTask()} is called.
Jason Monk62515be2014-05-21 16:06:19 -04007289 *
7290 * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns false
7291 * then the system will prompt the user with a dialog requesting permission to enter
Jason Monk8863b572014-07-29 13:49:32 -04007292 * this mode. When entered through this method the user can exit at any time through
7293 * an action described by the request dialog. Calling stopLockTask will also exit the
7294 * mode.
Craig Mautner15df08a2015-04-01 12:17:18 -07007295 *
7296 * @see android.R.attr#lockTaskMode
Jason Monka57e5e02014-05-07 10:06:48 -04007297 */
Craig Mautneraea74a52014-03-08 14:23:10 -08007298 public void startLockTask() {
7299 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08007300 ActivityManager.getService().startLockTaskModeByToken(mToken);
Craig Mautneraea74a52014-03-08 14:23:10 -08007301 } catch (RemoteException e) {
7302 }
7303 }
7304
Jason Monka57e5e02014-05-07 10:06:48 -04007305 /**
7306 * Allow the user to switch away from the current task.
7307 *
7308 * Called to end the mode started by {@link Activity#startLockTask}. This
7309 * can only be called by activities that have successfully called
7310 * startLockTask previously.
7311 *
7312 * This will allow the user to exit this app and move onto other activities.
Craig Mautner15df08a2015-04-01 12:17:18 -07007313 * <p>Note: This method should only be called when the activity is user-facing. That is,
7314 * between onResume() and onPause().
7315 * <p>Note: If there are other tasks below this one that are also locked then calling this
7316 * method will immediately finish this task and resume the previous locked one, remaining in
7317 * lockTask mode.
7318 *
7319 * @see android.R.attr#lockTaskMode
7320 * @see ActivityManager#getLockTaskModeState()
Jason Monka57e5e02014-05-07 10:06:48 -04007321 */
Craig Mautneraea74a52014-03-08 14:23:10 -08007322 public void stopLockTask() {
7323 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08007324 ActivityManager.getService().stopLockTaskMode();
Craig Mautneraea74a52014-03-08 14:23:10 -08007325 } catch (RemoteException e) {
7326 }
7327 }
7328
Craig Mautner5eda9b32013-07-02 11:58:16 -07007329 /**
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07007330 * Shows the user the system defined message for telling the user how to exit
7331 * lock task mode. The task containing this activity must be in lock task mode at the time
7332 * of this call for the message to be displayed.
7333 */
7334 public void showLockTaskEscapeMessage() {
7335 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08007336 ActivityManager.getService().showLockTaskEscapeMessage(mToken);
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07007337 } catch (RemoteException e) {
7338 }
7339 }
7340
7341 /**
Andrii Kulian933076d2016-03-29 17:04:42 -07007342 * Check whether the caption on freeform windows is displayed directly on the content.
7343 *
7344 * @return True if caption is displayed on content, false if it pushes the content down.
7345 *
Elliot Waited5ed3bc2017-01-25 15:22:55 -08007346 * @see #setOverlayWithDecorCaptionEnabled(boolean)
Wale Ogunwale724ca142017-04-24 13:40:34 -07007347 * @hide
Andrii Kulian933076d2016-03-29 17:04:42 -07007348 */
7349 public boolean isOverlayWithDecorCaptionEnabled() {
7350 return mWindow.isOverlayWithDecorCaptionEnabled();
7351 }
7352
7353 /**
Filip Gruszczynski63250652015-11-18 14:43:01 -08007354 * Set whether the caption should displayed directly on the content rather than push it down.
7355 *
7356 * This affects only freeform windows since they display the caption and only the main
7357 * window of the activity. The caption is used to drag the window around and also shows
7358 * maximize and close action buttons.
Wale Ogunwale724ca142017-04-24 13:40:34 -07007359 * @hide
Filip Gruszczynski63250652015-11-18 14:43:01 -08007360 */
Andrii Kulian933076d2016-03-29 17:04:42 -07007361 public void setOverlayWithDecorCaptionEnabled(boolean enabled) {
7362 mWindow.setOverlayWithDecorCaptionEnabled(enabled);
Filip Gruszczynski63250652015-11-18 14:43:01 -08007363 }
7364
7365 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07007366 * Interface for informing a translucent {@link Activity} once all visible activities below it
7367 * have completed drawing. This is necessary only after an {@link Activity} has been made
7368 * opaque using {@link Activity#convertFromTranslucent()} and before it has been drawn
7369 * translucent again following a call to {@link
George Mount800d72b2014-05-19 07:09:00 -07007370 * Activity#convertToTranslucent(android.app.Activity.TranslucentConversionListener,
7371 * ActivityOptions)}
Chet Haaseabd3d772013-09-11 14:33:05 -07007372 *
7373 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07007374 */
Jose Lima14914852014-08-14 09:14:12 -07007375 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07007376 public interface TranslucentConversionListener {
7377 /**
7378 * Callback made following {@link Activity#convertToTranslucent} once all visible Activities
7379 * below the top one have been redrawn. Following this callback it is safe to make the top
7380 * Activity translucent because the underlying Activity has been drawn.
7381 *
7382 * @param drawComplete True if the background Activity has drawn itself. False if a timeout
7383 * occurred waiting for the Activity to complete drawing.
7384 *
7385 * @see Activity#convertFromTranslucent()
Craig Mautner233ceee2014-05-09 17:05:11 -07007386 * @see Activity#convertToTranslucent(TranslucentConversionListener, ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07007387 */
7388 public void onTranslucentConversionComplete(boolean drawComplete);
7389 }
Svetoslavc6d1c342015-02-26 14:44:43 -08007390
7391 private void dispatchRequestPermissionsResult(int requestCode, Intent data) {
Svetoslavffb32b12015-10-15 16:54:00 -07007392 mHasCurrentPermissionsRequest = false;
Svet Ganov6a166af2015-06-30 10:15:44 -07007393 // If the package installer crashed we may have not data - best effort.
7394 String[] permissions = (data != null) ? data.getStringArrayExtra(
7395 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
7396 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
7397 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
Svetoslavc6d1c342015-02-26 14:44:43 -08007398 onRequestPermissionsResult(requestCode, permissions, grantResults);
7399 }
7400
7401 private void dispatchRequestPermissionsResultToFragment(int requestCode, Intent data,
Svet Ganov6a166af2015-06-30 10:15:44 -07007402 Fragment fragment) {
7403 // If the package installer crashed we may have not data - best effort.
7404 String[] permissions = (data != null) ? data.getStringArrayExtra(
7405 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
7406 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
7407 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
7408 fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
Svetoslavc6d1c342015-02-26 14:44:43 -08007409 }
7410
Svet Ganov782043c2017-02-11 00:52:02 +00007411 /** @hide */
7412 @Override
Svetoslav Ganova9379d02017-05-09 17:40:24 -07007413 final public void autofillCallbackAuthenticate(int authenticationId, IntentSender intent,
7414 Intent fillInIntent) {
Svet Ganov782043c2017-02-11 00:52:02 +00007415 try {
7416 startIntentSenderForResultInner(intent, AUTO_FILL_AUTH_WHO_PREFIX,
Svetoslav Ganova9379d02017-05-09 17:40:24 -07007417 authenticationId, fillInIntent, 0, 0, null);
Svet Ganov782043c2017-02-11 00:52:02 +00007418 } catch (IntentSender.SendIntentException e) {
7419 Log.e(TAG, "authenticate() failed for intent:" + intent, e);
7420 }
7421 }
7422
Svet Ganov17db9dc2017-02-21 19:54:31 -08007423 /** @hide */
7424 @Override
Felipe Leme4753bb02017-03-22 20:24:00 -07007425 final public void autofillCallbackResetableStateAvailable() {
Svet Ganov17db9dc2017-02-21 19:54:31 -08007426 mAutoFillResetNeeded = true;
7427 }
7428
Felipe Leme4753bb02017-03-22 20:24:00 -07007429 /** @hide */
7430 @Override
7431 final public boolean autofillCallbackRequestShowFillUi(@NonNull View anchor, int width,
7432 int height, @Nullable Rect anchorBounds, IAutofillWindowPresenter presenter) {
Felipe Leme4753bb02017-03-22 20:24:00 -07007433 final boolean wasShowing;
7434
7435 if (mAutofillPopupWindow == null) {
7436 wasShowing = false;
7437 mAutofillPopupWindow = new AutofillPopupWindow(presenter);
7438 } else {
7439 wasShowing = mAutofillPopupWindow.isShowing();
7440 }
Svet Ganov374cae12017-05-10 13:42:33 -07007441 mAutofillPopupWindow.update(anchor, 0, 0, width, height, anchorBounds);
Felipe Leme4753bb02017-03-22 20:24:00 -07007442
7443 return !wasShowing && mAutofillPopupWindow.isShowing();
7444 }
7445
7446 /** @hide */
7447 @Override
7448 final public boolean autofillCallbackRequestHideFillUi() {
7449 if (mAutofillPopupWindow == null) {
7450 return false;
7451 }
7452 mAutofillPopupWindow.dismiss();
7453 mAutofillPopupWindow = null;
7454 return true;
7455 }
7456
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07007457 /** @hide */
Felipe Leme27e20222017-05-18 15:24:11 -07007458 @Override
Phil Weaver846cda932017-06-15 10:10:06 -07007459 @NonNull public View[] findViewsByAutofillIdTraversal(@NonNull int[] viewIds) {
Philip P. Moltmann134cee22017-05-06 11:28:38 -07007460 final View[] views = new View[viewIds.length];
7461 final ArrayList<ViewRootImpl> roots =
7462 WindowManagerGlobal.getInstance().getRootViews(getActivityToken());
7463
7464 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
7465 final View rootView = roots.get(rootNum).getView();
7466
7467 if (rootView != null) {
7468 for (int viewNum = 0; viewNum < viewIds.length; viewNum++) {
7469 if (views[viewNum] == null) {
Phil Weaver846cda932017-06-15 10:10:06 -07007470 views[viewNum] = rootView.findViewByAutofillIdTraversal(
Philip P. Moltmann134cee22017-05-06 11:28:38 -07007471 viewIds[viewNum]);
7472 }
7473 }
7474 }
7475 }
7476
7477 return views;
7478 }
7479
7480 /** @hide */
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07007481 @Override
Phil Weaver846cda932017-06-15 10:10:06 -07007482 @Nullable public View findViewByAutofillIdTraversal(int viewId) {
Felipe Leme27e20222017-05-18 15:24:11 -07007483 final ArrayList<ViewRootImpl> roots =
7484 WindowManagerGlobal.getInstance().getRootViews(getActivityToken());
7485 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
7486 final View rootView = roots.get(rootNum).getView();
7487
7488 if (rootView != null) {
Phil Weaver846cda932017-06-15 10:10:06 -07007489 final View view = rootView.findViewByAutofillIdTraversal(viewId);
Felipe Leme27e20222017-05-18 15:24:11 -07007490 if (view != null) {
7491 return view;
7492 }
7493 }
7494 }
7495
7496 return null;
7497 }
7498
7499 /** @hide */
7500 @Override
Philip P. Moltmann134cee22017-05-06 11:28:38 -07007501 @NonNull public boolean[] getViewVisibility(@NonNull int[] viewIds) {
7502 final boolean[] isVisible = new boolean[viewIds.length];
Phil Weaver846cda932017-06-15 10:10:06 -07007503 final View views[] = findViewsByAutofillIdTraversal(viewIds);
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07007504
Philip P. Moltmann134cee22017-05-06 11:28:38 -07007505 for (int i = 0; i < viewIds.length; i++) {
7506 View view = views[i];
7507 if (view == null) {
7508 isVisible[i] = false;
7509 continue;
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07007510 }
7511
Philip P. Moltmann134cee22017-05-06 11:28:38 -07007512 isVisible[i] = true;
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07007513
Philip P. Moltmann134cee22017-05-06 11:28:38 -07007514 // Check if the view is visible by checking all parents
7515 while (true) {
7516 if (view instanceof DecorView && view.getViewRootImpl() == view.getParent()) {
7517 break;
7518 }
7519
7520 if (view.getVisibility() != View.VISIBLE) {
7521 isVisible[i] = false;
7522 break;
7523 }
7524
7525 if (view.getParent() instanceof View) {
7526 view = (View) view.getParent();
7527 } else {
7528 break;
7529 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07007530 }
7531 }
7532
Philip P. Moltmann134cee22017-05-06 11:28:38 -07007533 return isVisible;
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07007534 }
7535
7536 /** @hide */
7537 @Override
7538 public boolean isVisibleForAutofill() {
7539 return !mStopped;
7540 }
7541
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01007542 /**
7543 * If set to true, this indicates to the system that it should never take a
7544 * screenshot of the activity to be used as a representation while it is not in a started state.
7545 * <p>
7546 * Note that the system may use the window background of the theme instead to represent
7547 * the window when it is not running.
7548 * <p>
7549 * Also note that in comparison to {@link android.view.WindowManager.LayoutParams#FLAG_SECURE},
7550 * this only affects the behavior when the activity's screenshot would be used as a
7551 * representation when the activity is not in a started state, i.e. in Overview. The system may
7552 * still take screenshots of the activity in other contexts; for example, when the user takes a
7553 * screenshot of the entire screen, or when the active
7554 * {@link android.service.voice.VoiceInteractionService} requests a screenshot via
7555 * {@link android.service.voice.VoiceInteractionSession#SHOW_WITH_SCREENSHOT}.
7556 *
7557 * @param disable {@code true} to disable preview screenshots; {@code false} otherwise.
7558 * @hide
7559 */
7560 @SystemApi
7561 public void setDisablePreviewScreenshots(boolean disable) {
7562 try {
7563 ActivityManager.getService().setDisablePreviewScreenshots(mToken, disable);
7564 } catch (RemoteException e) {
7565 Log.e(TAG, "Failed to call setDisablePreviewScreenshots", e);
7566 }
7567 }
7568
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007569 class HostCallbacks extends FragmentHostCallback<Activity> {
7570 public HostCallbacks() {
7571 super(Activity.this /*activity*/);
7572 }
7573
7574 @Override
7575 public void onDump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
7576 Activity.this.dump(prefix, fd, writer, args);
7577 }
7578
7579 @Override
7580 public boolean onShouldSaveFragmentState(Fragment fragment) {
7581 return !isFinishing();
7582 }
7583
7584 @Override
7585 public LayoutInflater onGetLayoutInflater() {
7586 final LayoutInflater result = Activity.this.getLayoutInflater();
7587 if (onUseFragmentManagerInflaterFactory()) {
7588 return result.cloneInContext(Activity.this);
7589 }
7590 return result;
7591 }
7592
7593 @Override
7594 public boolean onUseFragmentManagerInflaterFactory() {
7595 // Newer platform versions use the child fragment manager's LayoutInflaterFactory.
7596 return getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
7597 }
7598
7599 @Override
7600 public Activity onGetHost() {
7601 return Activity.this;
7602 }
7603
7604 @Override
7605 public void onInvalidateOptionsMenu() {
7606 Activity.this.invalidateOptionsMenu();
7607 }
7608
7609 @Override
7610 public void onStartActivityFromFragment(Fragment fragment, Intent intent, int requestCode,
7611 Bundle options) {
7612 Activity.this.startActivityFromFragment(fragment, intent, requestCode, options);
7613 }
7614
7615 @Override
Tony Mak96d26fe2017-04-11 20:05:39 +01007616 public void onStartActivityAsUserFromFragment(
7617 Fragment fragment, Intent intent, int requestCode, Bundle options,
7618 UserHandle user) {
7619 Activity.this.startActivityAsUserFromFragment(
7620 fragment, intent, requestCode, options, user);
7621 }
7622
7623 @Override
Clara Bayarria0c2dc32016-04-12 12:00:15 +01007624 public void onStartIntentSenderFromFragment(Fragment fragment, IntentSender intent,
7625 int requestCode, @Nullable Intent fillInIntent, int flagsMask, int flagsValues,
7626 int extraFlags, Bundle options) throws IntentSender.SendIntentException {
7627 if (mParent == null) {
7628 startIntentSenderForResultInner(intent, fragment.mWho, requestCode, fillInIntent,
7629 flagsMask, flagsValues, options);
7630 } else if (options != null) {
7631 mParent.startIntentSenderFromChildFragment(fragment, intent, requestCode,
7632 fillInIntent, flagsMask, flagsValues, extraFlags, options);
7633 }
7634 }
7635
7636 @Override
Svetoslav970b59c2015-06-09 16:05:21 -07007637 public void onRequestPermissionsFromFragment(Fragment fragment, String[] permissions,
7638 int requestCode) {
7639 String who = REQUEST_PERMISSIONS_WHO_PREFIX + fragment.mWho;
7640 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
7641 startActivityForResult(who, intent, requestCode, null);
7642 }
7643
7644 @Override
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007645 public boolean onHasWindowAnimations() {
7646 return getWindow() != null;
7647 }
7648
7649 @Override
7650 public int onGetWindowAnimations() {
7651 final Window w = getWindow();
7652 return (w == null) ? 0 : w.getAttributes().windowAnimations;
7653 }
7654
Todd Kennedy434bd652015-05-04 12:29:50 -07007655 @Override
7656 public void onAttachFragment(Fragment fragment) {
7657 Activity.this.onAttachFragment(fragment);
7658 }
7659
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007660 @Nullable
7661 @Override
Alan Viverette04fd4702017-04-13 16:37:06 -04007662 public <T extends View> T onFindViewById(int id) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007663 return Activity.this.findViewById(id);
7664 }
7665
7666 @Override
7667 public boolean onHasView() {
7668 final Window w = getWindow();
7669 return (w != null && w.peekDecorView() != null);
7670 }
7671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007672}