The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.app; |
| 18 | |
Winson Chung | 1af8eda | 2016-02-05 17:55:56 +0000 | [diff] [blame] | 19 | import static java.lang.Character.MIN_VALUE; |
| 20 | |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 21 | import android.annotation.CallSuper; |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 22 | import android.annotation.DrawableRes; |
| 23 | import android.annotation.IdRes; |
| 24 | import android.annotation.IntDef; |
| 25 | import android.annotation.LayoutRes; |
Tor Norbye | 83c6896 | 2015-03-10 20:55:31 -0700 | [diff] [blame] | 26 | import android.annotation.MainThread; |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 27 | import android.annotation.NonNull; |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 28 | import android.annotation.Nullable; |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 29 | import android.annotation.RequiresPermission; |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 30 | import android.annotation.StyleRes; |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 31 | import android.annotation.SystemApi; |
Amith Yamasani | eeed06c | 2016-05-03 15:07:03 -0700 | [diff] [blame] | 32 | import android.app.VoiceInteractor.Request; |
Jason Monk | 62515be | 2014-05-21 16:06:19 -0400 | [diff] [blame] | 33 | import android.app.admin.DevicePolicyManager; |
Dianne Hackborn | 69c6adc | 2015-06-02 10:52:59 -0700 | [diff] [blame] | 34 | import android.app.assist.AssistContent; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 35 | import android.content.ComponentCallbacks2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.content.ComponentName; |
| 37 | import android.content.ContentResolver; |
| 38 | import android.content.Context; |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 39 | import android.content.CursorLoader; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 40 | import android.content.IIntentSender; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 41 | import android.content.Intent; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 42 | import android.content.IntentSender; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.content.SharedPreferences; |
| 44 | import android.content.pm.ActivityInfo; |
Dimitry Ivanov | 4449ef5 | 2016-02-25 17:41:13 -0800 | [diff] [blame] | 45 | import android.content.pm.ApplicationInfo; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 46 | import android.content.pm.PackageManager; |
| 47 | import android.content.pm.PackageManager.NameNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import android.content.res.Configuration; |
| 49 | import android.content.res.Resources; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 50 | import android.content.res.TypedArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.database.Cursor; |
| 52 | import android.graphics.Bitmap; |
| 53 | import android.graphics.Canvas; |
Winson | 2d47683 | 2016-02-17 14:53:46 -0800 | [diff] [blame] | 54 | import android.graphics.Color; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import android.graphics.drawable.Drawable; |
Clara Bayarri | fcd7e80 | 2016-03-10 12:58:18 +0000 | [diff] [blame] | 56 | import android.hardware.input.InputManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | import android.media.AudioManager; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 58 | import android.media.session.MediaController; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | import android.net.Uri; |
Adam Powell | d3c63a6 | 2016-06-09 12:36:16 -0700 | [diff] [blame] | 60 | import android.os.BadParcelableException; |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 61 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | import android.os.Handler; |
| 64 | import android.os.IBinder; |
Dianne Hackborn | 30c9bd8 | 2010-12-01 16:07:40 -0800 | [diff] [blame] | 65 | import android.os.Looper; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 66 | import android.os.Parcelable; |
Winson Chung | 1af8eda | 2016-02-05 17:55:56 +0000 | [diff] [blame] | 67 | import android.os.PersistableBundle; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 68 | import android.os.RemoteException; |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 69 | import android.os.ServiceManager.ServiceNotFoundException; |
Brad Fitzpatrick | 7580357 | 2011-01-13 14:21:03 -0800 | [diff] [blame] | 70 | import android.os.StrictMode; |
Dimitry Ivanov | 4449ef5 | 2016-02-25 17:41:13 -0800 | [diff] [blame] | 71 | import android.os.SystemProperties; |
Dianne Hackborn | f1c26e2 | 2012-08-23 13:54:58 -0700 | [diff] [blame] | 72 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | import android.text.Selection; |
| 74 | import android.text.SpannableStringBuilder; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 75 | import android.text.TextUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | import android.text.method.TextKeyListener; |
Winson Chung | 1af8eda | 2016-02-05 17:55:56 +0000 | [diff] [blame] | 77 | import android.transition.Scene; |
| 78 | import android.transition.TransitionManager; |
| 79 | import android.util.ArrayMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | import android.util.AttributeSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | import android.util.EventLog; |
| 82 | import android.util.Log; |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 83 | import android.util.PrintWriterPrinter; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 84 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | import android.util.SparseArray; |
Winson Chung | 1af8eda | 2016-02-05 17:55:56 +0000 | [diff] [blame] | 86 | import android.util.SuperNotCalledException; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 87 | import android.view.ActionMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | import android.view.ContextMenu; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 89 | import android.view.ContextMenu.ContextMenuInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | import android.view.ContextThemeWrapper; |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 91 | import android.view.DragAndDropPermissions; |
Winson Chung | 1af8eda | 2016-02-05 17:55:56 +0000 | [diff] [blame] | 92 | import android.view.DragEvent; |
Clara Bayarri | fcd7e80 | 2016-03-10 12:58:18 +0000 | [diff] [blame] | 93 | import android.view.InputDevice; |
| 94 | import android.view.KeyCharacterMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | import android.view.KeyEvent; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 96 | import android.view.KeyboardShortcutGroup; |
| 97 | import android.view.KeyboardShortcutInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | import android.view.LayoutInflater; |
| 99 | import android.view.Menu; |
| 100 | import android.view.MenuInflater; |
| 101 | import android.view.MenuItem; |
| 102 | import android.view.MotionEvent; |
Tim Kilbourn | 6a975b3 | 2015-04-09 17:14:34 -0700 | [diff] [blame] | 103 | import android.view.SearchEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | import android.view.View; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 105 | import android.view.View.OnCreateContextMenuListener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | import android.view.ViewGroup; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 107 | import android.view.ViewGroup.LayoutParams; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | import android.view.ViewManager; |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 109 | import android.view.ViewRootImpl; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | import android.view.Window; |
Winson Chung | 1af8eda | 2016-02-05 17:55:56 +0000 | [diff] [blame] | 111 | import android.view.Window.WindowControllerCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | import android.view.WindowManager; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 113 | import android.view.WindowManagerGlobal; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 114 | import android.view.accessibility.AccessibilityEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | import android.widget.AdapterView; |
Dimitry Ivanov | 4449ef5 | 2016-02-25 17:41:13 -0800 | [diff] [blame] | 116 | import android.widget.Toast; |
Winson Chung | 1af8eda | 2016-02-05 17:55:56 +0000 | [diff] [blame] | 117 | import android.widget.Toolbar; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 118 | |
Winson Chung | 1af8eda | 2016-02-05 17:55:56 +0000 | [diff] [blame] | 119 | import com.android.internal.app.IVoiceInteractor; |
| 120 | import com.android.internal.app.ToolbarActionBar; |
| 121 | import com.android.internal.app.WindowDecorActionBar; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 122 | import com.android.internal.policy.PhoneWindow; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 124 | import java.io.FileDescriptor; |
| 125 | import java.io.PrintWriter; |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 126 | import java.lang.annotation.Retention; |
| 127 | import java.lang.annotation.RetentionPolicy; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 128 | import java.util.ArrayList; |
| 129 | import java.util.HashMap; |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 130 | import java.util.List; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * An activity is a single, focused thing that the user can do. Almost all |
| 134 | * activities interact with the user, so the Activity class takes care of |
| 135 | * creating a window for you in which you can place your UI with |
| 136 | * {@link #setContentView}. While activities are often presented to the user |
| 137 | * as full-screen windows, they can also be used in other ways: as floating |
| 138 | * windows (via a theme with {@link android.R.attr#windowIsFloating} set) |
| 139 | * or embedded inside of another activity (using {@link ActivityGroup}). |
| 140 | * |
| 141 | * There are two methods almost all subclasses of Activity will implement: |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 142 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | * <ul> |
| 144 | * <li> {@link #onCreate} is where you initialize your activity. Most |
| 145 | * importantly, here you will usually call {@link #setContentView(int)} |
| 146 | * with a layout resource defining your UI, and using {@link #findViewById} |
| 147 | * to retrieve the widgets in that UI that you need to interact with |
| 148 | * programmatically. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 149 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | * <li> {@link #onPause} is where you deal with the user leaving your |
| 151 | * activity. Most importantly, any changes made by the user should at this |
| 152 | * point be committed (usually to the |
| 153 | * {@link android.content.ContentProvider} holding the data). |
| 154 | * </ul> |
| 155 | * |
| 156 | * <p>To be of use with {@link android.content.Context#startActivity Context.startActivity()}, all |
| 157 | * activity classes must have a corresponding |
| 158 | * {@link android.R.styleable#AndroidManifestActivity <activity>} |
| 159 | * declaration in their package's <code>AndroidManifest.xml</code>.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 160 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | * <p>Topics covered here: |
| 162 | * <ol> |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 163 | * <li><a href="#Fragments">Fragments</a> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | * <li><a href="#ActivityLifecycle">Activity Lifecycle</a> |
| 165 | * <li><a href="#ConfigurationChanges">Configuration Changes</a> |
| 166 | * <li><a href="#StartingActivities">Starting Activities and Getting Results</a> |
| 167 | * <li><a href="#SavingPersistentState">Saving Persistent State</a> |
| 168 | * <li><a href="#Permissions">Permissions</a> |
| 169 | * <li><a href="#ProcessLifecycle">Process Lifecycle</a> |
| 170 | * </ol> |
Joe Fernandez | b54e7a3 | 2011-10-03 15:09:50 -0700 | [diff] [blame] | 171 | * |
| 172 | * <div class="special reference"> |
| 173 | * <h3>Developer Guides</h3> |
| 174 | * <p>The Activity class is an important part of an application's overall lifecycle, |
| 175 | * and the way activities are launched and put together is a fundamental |
| 176 | * part of the platform's application model. For a detailed perspective on the structure of an |
| 177 | * Android application and how activities behave, please read the |
| 178 | * <a href="{@docRoot}guide/topics/fundamentals.html">Application Fundamentals</a> and |
Mark Lu | fa43485 | 2016-08-11 17:40:33 -0700 | [diff] [blame] | 179 | * <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back Stack</a> |
Joe Fernandez | b54e7a3 | 2011-10-03 15:09:50 -0700 | [diff] [blame] | 180 | * developer guides.</p> |
| 181 | * |
| 182 | * <p>You can also find a detailed discussion about how to create activities in the |
Mark Lu | fa43485 | 2016-08-11 17:40:33 -0700 | [diff] [blame] | 183 | * <a href="{@docRoot}guide/components/activities.html">Activities</a> |
Joe Fernandez | b54e7a3 | 2011-10-03 15:09:50 -0700 | [diff] [blame] | 184 | * developer guide.</p> |
| 185 | * </div> |
| 186 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 187 | * <a name="Fragments"></a> |
| 188 | * <h3>Fragments</h3> |
| 189 | * |
| 190 | * <p>Starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB}, Activity |
| 191 | * implementations can make use of the {@link Fragment} class to better |
| 192 | * modularize their code, build more sophisticated user interfaces for larger |
| 193 | * screens, and help scale their application between small and large screens. |
| 194 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | * <a name="ActivityLifecycle"></a> |
| 196 | * <h3>Activity Lifecycle</h3> |
| 197 | * |
| 198 | * <p>Activities in the system are managed as an <em>activity stack</em>. |
| 199 | * When a new activity is started, it is placed on the top of the stack |
| 200 | * and becomes the running activity -- the previous activity always remains |
| 201 | * below it in the stack, and will not come to the foreground again until |
| 202 | * the new activity exits.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 203 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | * <p>An activity has essentially four states:</p> |
| 205 | * <ul> |
Trevor Johns | 682c24e | 2016-04-12 10:13:47 -0700 | [diff] [blame] | 206 | * <li> If an activity is in the foreground of the screen (at the top of |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | * the stack), |
| 208 | * it is <em>active</em> or <em>running</em>. </li> |
| 209 | * <li>If an activity has lost focus but is still visible (that is, a new non-full-sized |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 210 | * or transparent activity has focus on top of your activity), it |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | * is <em>paused</em>. A paused activity is completely alive (it |
| 212 | * maintains all state and member information and remains attached to |
| 213 | * the window manager), but can be killed by the system in extreme |
| 214 | * low memory situations. |
| 215 | * <li>If an activity is completely obscured by another activity, |
| 216 | * it is <em>stopped</em>. It still retains all state and member information, |
| 217 | * however, it is no longer visible to the user so its window is hidden |
| 218 | * and it will often be killed by the system when memory is needed |
| 219 | * elsewhere.</li> |
| 220 | * <li>If an activity is paused or stopped, the system can drop the activity |
| 221 | * from memory by either asking it to finish, or simply killing its |
| 222 | * process. When it is displayed again to the user, it must be |
| 223 | * completely restarted and restored to its previous state.</li> |
| 224 | * </ul> |
| 225 | * |
| 226 | * <p>The following diagram shows the important state paths of an Activity. |
| 227 | * The square rectangles represent callback methods you can implement to |
| 228 | * perform operations when the Activity moves between states. The colored |
| 229 | * ovals are major states the Activity can be in.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 230 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | * <p><img src="../../../images/activity_lifecycle.png" |
| 232 | * alt="State diagram for an Android Activity Lifecycle." border="0" /></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 233 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | * <p>There are three key loops you may be interested in monitoring within your |
| 235 | * activity: |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 236 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | * <ul> |
| 238 | * <li>The <b>entire lifetime</b> of an activity happens between the first call |
| 239 | * to {@link android.app.Activity#onCreate} through to a single final call |
| 240 | * to {@link android.app.Activity#onDestroy}. An activity will do all setup |
| 241 | * of "global" state in onCreate(), and release all remaining resources in |
| 242 | * onDestroy(). For example, if it has a thread running in the background |
| 243 | * to download data from the network, it may create that thread in onCreate() |
| 244 | * and then stop the thread in onDestroy(). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 245 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | * <li>The <b>visible lifetime</b> of an activity happens between a call to |
| 247 | * {@link android.app.Activity#onStart} until a corresponding call to |
| 248 | * {@link android.app.Activity#onStop}. During this time the user can see the |
| 249 | * activity on-screen, though it may not be in the foreground and interacting |
| 250 | * with the user. Between these two methods you can maintain resources that |
| 251 | * are needed to show the activity to the user. For example, you can register |
| 252 | * a {@link android.content.BroadcastReceiver} in onStart() to monitor for changes |
Ken Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 253 | * that impact your UI, and unregister it in onStop() when the user no |
| 254 | * longer sees what you are displaying. The onStart() and onStop() methods |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | * can be called multiple times, as the activity becomes visible and hidden |
| 256 | * to the user. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 257 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | * <li>The <b>foreground lifetime</b> of an activity happens between a call to |
| 259 | * {@link android.app.Activity#onResume} until a corresponding call to |
| 260 | * {@link android.app.Activity#onPause}. During this time the activity is |
| 261 | * in front of all other activities and interacting with the user. An activity |
| 262 | * can frequently go between the resumed and paused states -- for example when |
| 263 | * the device goes to sleep, when an activity result is delivered, when a new |
| 264 | * intent is delivered -- so the code in these methods should be fairly |
| 265 | * lightweight. |
| 266 | * </ul> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 267 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | * <p>The entire lifecycle of an activity is defined by the following |
| 269 | * Activity methods. All of these are hooks that you can override |
| 270 | * to do appropriate work when the activity changes state. All |
| 271 | * activities will implement {@link android.app.Activity#onCreate} |
| 272 | * to do their initial setup; many will also implement |
| 273 | * {@link android.app.Activity#onPause} to commit changes to data and |
| 274 | * otherwise prepare to stop interacting with the user. You should always |
| 275 | * call up to your superclass when implementing these methods.</p> |
| 276 | * |
| 277 | * </p> |
| 278 | * <pre class="prettyprint"> |
| 279 | * public class Activity extends ApplicationContext { |
| 280 | * protected void onCreate(Bundle savedInstanceState); |
| 281 | * |
| 282 | * protected void onStart(); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 283 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | * protected void onRestart(); |
| 285 | * |
| 286 | * protected void onResume(); |
| 287 | * |
| 288 | * protected void onPause(); |
| 289 | * |
| 290 | * protected void onStop(); |
| 291 | * |
| 292 | * protected void onDestroy(); |
| 293 | * } |
| 294 | * </pre> |
| 295 | * |
| 296 | * <p>In general the movement through an activity's lifecycle looks like |
| 297 | * this:</p> |
| 298 | * |
| 299 | * <table border="2" width="85%" align="center" frame="hsides" rules="rows"> |
| 300 | * <colgroup align="left" span="3" /> |
| 301 | * <colgroup align="left" /> |
| 302 | * <colgroup align="center" /> |
| 303 | * <colgroup align="center" /> |
| 304 | * |
| 305 | * <thead> |
| 306 | * <tr><th colspan="3">Method</th> <th>Description</th> <th>Killable?</th> <th>Next</th></tr> |
| 307 | * </thead> |
| 308 | * |
| 309 | * <tbody> |
smain@google.com | 10e29b8 | 2016-06-23 12:09:30 -0700 | [diff] [blame] | 310 | * <tr><td colspan="3" align="left" border="0">{@link android.app.Activity#onCreate onCreate()}</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | * <td>Called when the activity is first created. |
| 312 | * This is where you should do all of your normal static set up: |
| 313 | * create views, bind data to lists, etc. This method also |
| 314 | * provides you with a Bundle containing the activity's previously |
| 315 | * frozen state, if there was one. |
| 316 | * <p>Always followed by <code>onStart()</code>.</td> |
| 317 | * <td align="center">No</td> |
| 318 | * <td align="center"><code>onStart()</code></td> |
| 319 | * </tr> |
| 320 | * |
| 321 | * <tr><td rowspan="5" style="border-left: none; border-right: none;"> </td> |
smain@google.com | 10e29b8 | 2016-06-23 12:09:30 -0700 | [diff] [blame] | 322 | * <td colspan="2" align="left" border="0">{@link android.app.Activity#onRestart onRestart()}</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | * <td>Called after your activity has been stopped, prior to it being |
| 324 | * started again. |
| 325 | * <p>Always followed by <code>onStart()</code></td> |
| 326 | * <td align="center">No</td> |
| 327 | * <td align="center"><code>onStart()</code></td> |
| 328 | * </tr> |
| 329 | * |
smain@google.com | 10e29b8 | 2016-06-23 12:09:30 -0700 | [diff] [blame] | 330 | * <tr><td colspan="2" align="left" border="0">{@link android.app.Activity#onStart onStart()}</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | * <td>Called when the activity is becoming visible to the user. |
| 332 | * <p>Followed by <code>onResume()</code> if the activity comes |
| 333 | * to the foreground, or <code>onStop()</code> if it becomes hidden.</td> |
| 334 | * <td align="center">No</td> |
| 335 | * <td align="center"><code>onResume()</code> or <code>onStop()</code></td> |
| 336 | * </tr> |
| 337 | * |
| 338 | * <tr><td rowspan="2" style="border-left: none;"> </td> |
smain@google.com | 10e29b8 | 2016-06-23 12:09:30 -0700 | [diff] [blame] | 339 | * <td align="left" border="0">{@link android.app.Activity#onResume onResume()}</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | * <td>Called when the activity will start |
| 341 | * interacting with the user. At this point your activity is at |
| 342 | * the top of the activity stack, with user input going to it. |
| 343 | * <p>Always followed by <code>onPause()</code>.</td> |
| 344 | * <td align="center">No</td> |
| 345 | * <td align="center"><code>onPause()</code></td> |
| 346 | * </tr> |
| 347 | * |
smain@google.com | 10e29b8 | 2016-06-23 12:09:30 -0700 | [diff] [blame] | 348 | * <tr><td align="left" border="0">{@link android.app.Activity#onPause onPause()}</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | * <td>Called when the system is about to start resuming a previous |
| 350 | * activity. This is typically used to commit unsaved changes to |
| 351 | * persistent data, stop animations and other things that may be consuming |
| 352 | * CPU, etc. Implementations of this method must be very quick because |
| 353 | * the next activity will not be resumed until this method returns. |
| 354 | * <p>Followed by either <code>onResume()</code> if the activity |
| 355 | * returns back to the front, or <code>onStop()</code> if it becomes |
| 356 | * invisible to the user.</td> |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 357 | * <td align="center"><font color="#800000"><strong>Pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB}</strong></font></td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | * <td align="center"><code>onResume()</code> or<br> |
| 359 | * <code>onStop()</code></td> |
| 360 | * </tr> |
| 361 | * |
smain@google.com | 10e29b8 | 2016-06-23 12:09:30 -0700 | [diff] [blame] | 362 | * <tr><td colspan="2" align="left" border="0">{@link android.app.Activity#onStop onStop()}</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | * <td>Called when the activity is no longer visible to the user, because |
| 364 | * another activity has been resumed and is covering this one. This |
| 365 | * may happen either because a new activity is being started, an existing |
| 366 | * one is being brought in front of this one, or this one is being |
| 367 | * destroyed. |
| 368 | * <p>Followed by either <code>onRestart()</code> if |
| 369 | * this activity is coming back to interact with the user, or |
| 370 | * <code>onDestroy()</code> if this activity is going away.</td> |
| 371 | * <td align="center"><font color="#800000"><strong>Yes</strong></font></td> |
| 372 | * <td align="center"><code>onRestart()</code> or<br> |
| 373 | * <code>onDestroy()</code></td> |
| 374 | * </tr> |
| 375 | * |
smain@google.com | 10e29b8 | 2016-06-23 12:09:30 -0700 | [diff] [blame] | 376 | * <tr><td colspan="3" align="left" border="0">{@link android.app.Activity#onDestroy onDestroy()}</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | * <td>The final call you receive before your |
| 378 | * activity is destroyed. This can happen either because the |
| 379 | * activity is finishing (someone called {@link Activity#finish} on |
| 380 | * it, or because the system is temporarily destroying this |
| 381 | * instance of the activity to save space. You can distinguish |
| 382 | * between these two scenarios with the {@link |
| 383 | * Activity#isFinishing} method.</td> |
| 384 | * <td align="center"><font color="#800000"><strong>Yes</strong></font></td> |
| 385 | * <td align="center"><em>nothing</em></td> |
| 386 | * </tr> |
| 387 | * </tbody> |
| 388 | * </table> |
| 389 | * |
| 390 | * <p>Note the "Killable" column in the above table -- for those methods that |
| 391 | * are marked as being killable, after that method returns the process hosting the |
Kris Giesing | 8b6216d | 2014-12-16 17:23:15 -0800 | [diff] [blame] | 392 | * activity may be killed by the system <em>at any time</em> without another line |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | * of its code being executed. Because of this, you should use the |
| 394 | * {@link #onPause} method to write any persistent data (such as user edits) |
| 395 | * to storage. In addition, the method |
| 396 | * {@link #onSaveInstanceState(Bundle)} is called before placing the activity |
| 397 | * in such a background state, allowing you to save away any dynamic instance |
| 398 | * state in your activity into the given Bundle, to be later received in |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 399 | * {@link #onCreate} if the activity needs to be re-created. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | * See the <a href="#ProcessLifecycle">Process Lifecycle</a> |
| 401 | * section for more information on how the lifecycle of a process is tied |
| 402 | * to the activities it is hosting. Note that it is important to save |
| 403 | * persistent data in {@link #onPause} instead of {@link #onSaveInstanceState} |
Daisuke Miyakawa | 5c40f3f | 2011-02-15 13:24:36 -0800 | [diff] [blame] | 404 | * because the latter is not part of the lifecycle callbacks, so will not |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | * be called in every situation as described in its documentation.</p> |
| 406 | * |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 407 | * <p class="note">Be aware that these semantics will change slightly between |
| 408 | * applications targeting platforms starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 409 | * vs. those targeting prior platforms. Starting with Honeycomb, an application |
| 410 | * is not in the killable state until its {@link #onStop} has returned. This |
| 411 | * impacts when {@link #onSaveInstanceState(Bundle)} may be called (it may be |
| 412 | * safely called after {@link #onPause()} and allows and application to safely |
| 413 | * wait until {@link #onStop()} to save persistent state.</p> |
| 414 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 415 | * <p>For those methods that are not marked as being killable, the activity's |
| 416 | * process will not be killed by the system starting from the time the method |
| 417 | * is called and continuing after it returns. Thus an activity is in the killable |
| 418 | * state, for example, between after <code>onPause()</code> to the start of |
| 419 | * <code>onResume()</code>.</p> |
| 420 | * |
| 421 | * <a name="ConfigurationChanges"></a> |
| 422 | * <h3>Configuration Changes</h3> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 423 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | * <p>If the configuration of the device (as defined by the |
| 425 | * {@link Configuration Resources.Configuration} class) changes, |
| 426 | * then anything displaying a user interface will need to update to match that |
| 427 | * configuration. Because Activity is the primary mechanism for interacting |
| 428 | * with the user, it includes special support for handling configuration |
| 429 | * changes.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 430 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | * <p>Unless you specify otherwise, a configuration change (such as a change |
| 432 | * in screen orientation, language, input devices, etc) will cause your |
| 433 | * current activity to be <em>destroyed</em>, going through the normal activity |
| 434 | * lifecycle process of {@link #onPause}, |
| 435 | * {@link #onStop}, and {@link #onDestroy} as appropriate. If the activity |
| 436 | * had been in the foreground or visible to the user, once {@link #onDestroy} is |
| 437 | * called in that instance then a new instance of the activity will be |
| 438 | * created, with whatever savedInstanceState the previous instance had generated |
| 439 | * from {@link #onSaveInstanceState}.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 440 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | * <p>This is done because any application resource, |
| 442 | * including layout files, can change based on any configuration value. Thus |
| 443 | * the only safe way to handle a configuration change is to re-retrieve all |
| 444 | * resources, including layouts, drawables, and strings. Because activities |
| 445 | * must already know how to save their state and re-create themselves from |
| 446 | * that state, this is a convenient way to have an activity restart itself |
| 447 | * with a new configuration.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 448 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 449 | * <p>In some special cases, you may want to bypass restarting of your |
| 450 | * activity based on one or more types of configuration changes. This is |
| 451 | * done with the {@link android.R.attr#configChanges android:configChanges} |
| 452 | * attribute in its manifest. For any types of configuration changes you say |
| 453 | * that you handle there, you will receive a call to your current activity's |
| 454 | * {@link #onConfigurationChanged} method instead of being restarted. If |
| 455 | * a configuration change involves any that you do not handle, however, the |
| 456 | * activity will still be restarted and {@link #onConfigurationChanged} |
| 457 | * will not be called.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 458 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 459 | * <a name="StartingActivities"></a> |
| 460 | * <h3>Starting Activities and Getting Results</h3> |
| 461 | * |
| 462 | * <p>The {@link android.app.Activity#startActivity} |
| 463 | * method is used to start a |
| 464 | * new activity, which will be placed at the top of the activity stack. It |
| 465 | * takes a single argument, an {@link android.content.Intent Intent}, |
| 466 | * which describes the activity |
| 467 | * to be executed.</p> |
| 468 | * |
| 469 | * <p>Sometimes you want to get a result back from an activity when it |
| 470 | * ends. For example, you may start an activity that lets the user pick |
| 471 | * a person in a list of contacts; when it ends, it returns the person |
| 472 | * that was selected. To do this, you call the |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 473 | * {@link android.app.Activity#startActivityForResult(Intent, int)} |
| 474 | * version with a second integer parameter identifying the call. The result |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | * will come back through your {@link android.app.Activity#onActivityResult} |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 476 | * method.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | * |
| 478 | * <p>When an activity exits, it can call |
| 479 | * {@link android.app.Activity#setResult(int)} |
| 480 | * to return data back to its parent. It must always supply a result code, |
| 481 | * which can be the standard results RESULT_CANCELED, RESULT_OK, or any |
| 482 | * custom values starting at RESULT_FIRST_USER. In addition, it can optionally |
| 483 | * return back an Intent containing any additional data it wants. All of this |
| 484 | * information appears back on the |
| 485 | * parent's <code>Activity.onActivityResult()</code>, along with the integer |
| 486 | * identifier it originally supplied.</p> |
| 487 | * |
| 488 | * <p>If a child activity fails for any reason (such as crashing), the parent |
| 489 | * activity will receive a result with the code RESULT_CANCELED.</p> |
| 490 | * |
| 491 | * <pre class="prettyprint"> |
| 492 | * public class MyActivity extends Activity { |
| 493 | * ... |
| 494 | * |
| 495 | * static final int PICK_CONTACT_REQUEST = 0; |
| 496 | * |
Michael Wright | 5438e4e | 2014-07-25 14:05:42 -0700 | [diff] [blame] | 497 | * public boolean onKeyDown(int keyCode, KeyEvent event) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | * if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) { |
| 499 | * // When the user center presses, let them pick a contact. |
| 500 | * startActivityForResult( |
| 501 | * new Intent(Intent.ACTION_PICK, |
| 502 | * new Uri("content://contacts")), |
| 503 | * PICK_CONTACT_REQUEST); |
| 504 | * return true; |
| 505 | * } |
| 506 | * return false; |
| 507 | * } |
| 508 | * |
| 509 | * protected void onActivityResult(int requestCode, int resultCode, |
| 510 | * Intent data) { |
| 511 | * if (requestCode == PICK_CONTACT_REQUEST) { |
| 512 | * if (resultCode == RESULT_OK) { |
| 513 | * // A contact was picked. Here we will just display it |
| 514 | * // to the user. |
| 515 | * startActivity(new Intent(Intent.ACTION_VIEW, data)); |
| 516 | * } |
| 517 | * } |
| 518 | * } |
| 519 | * } |
| 520 | * </pre> |
| 521 | * |
| 522 | * <a name="SavingPersistentState"></a> |
| 523 | * <h3>Saving Persistent State</h3> |
| 524 | * |
| 525 | * <p>There are generally two kinds of persistent state than an activity |
| 526 | * will deal with: shared document-like data (typically stored in a SQLite |
| 527 | * database using a {@linkplain android.content.ContentProvider content provider}) |
| 528 | * and internal state such as user preferences.</p> |
| 529 | * |
| 530 | * <p>For content provider data, we suggest that activities use a |
| 531 | * "edit in place" user model. That is, any edits a user makes are effectively |
| 532 | * made immediately without requiring an additional confirmation step. |
| 533 | * Supporting this model is generally a simple matter of following two rules:</p> |
| 534 | * |
| 535 | * <ul> |
| 536 | * <li> <p>When creating a new document, the backing database entry or file for |
| 537 | * it is created immediately. For example, if the user chooses to write |
| 538 | * a new e-mail, a new entry for that e-mail is created as soon as they |
| 539 | * start entering data, so that if they go to any other activity after |
| 540 | * that point this e-mail will now appear in the list of drafts.</p> |
| 541 | * <li> <p>When an activity's <code>onPause()</code> method is called, it should |
| 542 | * commit to the backing content provider or file any changes the user |
| 543 | * has made. This ensures that those changes will be seen by any other |
| 544 | * activity that is about to run. You will probably want to commit |
| 545 | * your data even more aggressively at key times during your |
| 546 | * activity's lifecycle: for example before starting a new |
| 547 | * activity, before finishing your own activity, when the user |
| 548 | * switches between input fields, etc.</p> |
| 549 | * </ul> |
| 550 | * |
| 551 | * <p>This model is designed to prevent data loss when a user is navigating |
| 552 | * between activities, and allows the system to safely kill an activity (because |
| 553 | * system resources are needed somewhere else) at any time after it has been |
| 554 | * paused. Note this implies |
| 555 | * that the user pressing BACK from your activity does <em>not</em> |
| 556 | * mean "cancel" -- it means to leave the activity with its current contents |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 557 | * saved away. Canceling edits in an activity must be provided through |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | * some other mechanism, such as an explicit "revert" or "undo" option.</p> |
| 559 | * |
| 560 | * <p>See the {@linkplain android.content.ContentProvider content package} for |
| 561 | * more information about content providers. These are a key aspect of how |
| 562 | * different activities invoke and propagate data between themselves.</p> |
| 563 | * |
| 564 | * <p>The Activity class also provides an API for managing internal persistent state |
| 565 | * associated with an activity. This can be used, for example, to remember |
| 566 | * the user's preferred initial display in a calendar (day view or week view) |
| 567 | * or the user's default home page in a web browser.</p> |
| 568 | * |
| 569 | * <p>Activity persistent state is managed |
| 570 | * with the method {@link #getPreferences}, |
| 571 | * allowing you to retrieve and |
| 572 | * modify a set of name/value pairs associated with the activity. To use |
| 573 | * preferences that are shared across multiple application components |
| 574 | * (activities, receivers, services, providers), you can use the underlying |
| 575 | * {@link Context#getSharedPreferences Context.getSharedPreferences()} method |
| 576 | * to retrieve a preferences |
| 577 | * object stored under a specific name. |
| 578 | * (Note that it is not possible to share settings data across application |
| 579 | * packages -- for that you will need a content provider.)</p> |
| 580 | * |
| 581 | * <p>Here is an excerpt from a calendar activity that stores the user's |
| 582 | * preferred view mode in its persistent settings:</p> |
| 583 | * |
| 584 | * <pre class="prettyprint"> |
| 585 | * public class CalendarActivity extends Activity { |
| 586 | * ... |
| 587 | * |
| 588 | * static final int DAY_VIEW_MODE = 0; |
| 589 | * static final int WEEK_VIEW_MODE = 1; |
| 590 | * |
| 591 | * private SharedPreferences mPrefs; |
| 592 | * private int mCurViewMode; |
| 593 | * |
| 594 | * protected void onCreate(Bundle savedInstanceState) { |
| 595 | * super.onCreate(savedInstanceState); |
| 596 | * |
| 597 | * SharedPreferences mPrefs = getSharedPreferences(); |
Quddus Chong | 3fa98cb | 2012-04-13 11:17:24 -0700 | [diff] [blame] | 598 | * mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 599 | * } |
| 600 | * |
| 601 | * protected void onPause() { |
| 602 | * super.onPause(); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 603 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 604 | * SharedPreferences.Editor ed = mPrefs.edit(); |
| 605 | * ed.putInt("view_mode", mCurViewMode); |
| 606 | * ed.commit(); |
| 607 | * } |
| 608 | * } |
| 609 | * </pre> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 610 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | * <a name="Permissions"></a> |
| 612 | * <h3>Permissions</h3> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 613 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | * <p>The ability to start a particular Activity can be enforced when it is |
| 615 | * declared in its |
| 616 | * manifest's {@link android.R.styleable#AndroidManifestActivity <activity>} |
| 617 | * tag. By doing so, other applications will need to declare a corresponding |
| 618 | * {@link android.R.styleable#AndroidManifestUsesPermission <uses-permission>} |
| 619 | * element in their own manifest to be able to start that activity. |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 620 | * |
| 621 | * <p>When starting an Activity you can set {@link Intent#FLAG_GRANT_READ_URI_PERMISSION |
| 622 | * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION |
| 623 | * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} on the Intent. This will grant the |
| 624 | * Activity access to the specific URIs in the Intent. Access will remain |
| 625 | * until the Activity has finished (it will remain across the hosting |
| 626 | * process being killed and other temporary destruction). As of |
| 627 | * {@link android.os.Build.VERSION_CODES#GINGERBREAD}, if the Activity |
| 628 | * was already created and a new Intent is being delivered to |
| 629 | * {@link #onNewIntent(Intent)}, any newly granted URI permissions will be added |
| 630 | * to the existing ones it holds. |
| 631 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 632 | * <p>See the <a href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a> |
| 633 | * document for more information on permissions and security in general. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 634 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 635 | * <a name="ProcessLifecycle"></a> |
| 636 | * <h3>Process Lifecycle</h3> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 637 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | * <p>The Android system attempts to keep application process around for as |
| 639 | * long as possible, but eventually will need to remove old processes when |
| 640 | * memory runs low. As described in <a href="#ActivityLifecycle">Activity |
| 641 | * Lifecycle</a>, the decision about which process to remove is intimately |
| 642 | * tied to the state of the user's interaction with it. In general, there |
| 643 | * are four states a process can be in based on the activities running in it, |
| 644 | * listed here in order of importance. The system will kill less important |
| 645 | * processes (the last ones) before it resorts to killing more important |
| 646 | * processes (the first ones). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 647 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 648 | * <ol> |
| 649 | * <li> <p>The <b>foreground activity</b> (the activity at the top of the screen |
| 650 | * that the user is currently interacting with) is considered the most important. |
| 651 | * Its process will only be killed as a last resort, if it uses more memory |
| 652 | * than is available on the device. Generally at this point the device has |
| 653 | * reached a memory paging state, so this is required in order to keep the user |
| 654 | * interface responsive. |
| 655 | * <li> <p>A <b>visible activity</b> (an activity that is visible to the user |
| 656 | * but not in the foreground, such as one sitting behind a foreground dialog) |
| 657 | * is considered extremely important and will not be killed unless that is |
| 658 | * required to keep the foreground activity running. |
| 659 | * <li> <p>A <b>background activity</b> (an activity that is not visible to |
| 660 | * the user and has been paused) is no longer critical, so the system may |
| 661 | * safely kill its process to reclaim memory for other foreground or |
| 662 | * visible processes. If its process needs to be killed, when the user navigates |
| 663 | * back to the activity (making it visible on the screen again), its |
| 664 | * {@link #onCreate} method will be called with the savedInstanceState it had previously |
| 665 | * supplied in {@link #onSaveInstanceState} so that it can restart itself in the same |
| 666 | * state as the user last left it. |
| 667 | * <li> <p>An <b>empty process</b> is one hosting no activities or other |
| 668 | * application components (such as {@link Service} or |
| 669 | * {@link android.content.BroadcastReceiver} classes). These are killed very |
| 670 | * quickly by the system as memory becomes low. For this reason, any |
| 671 | * background operation you do outside of an activity must be executed in the |
| 672 | * context of an activity BroadcastReceiver or Service to ensure that the system |
| 673 | * knows it needs to keep your process around. |
| 674 | * </ol> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 675 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 676 | * <p>Sometimes an Activity may need to do a long-running operation that exists |
| 677 | * independently of the activity lifecycle itself. An example may be a camera |
| 678 | * application that allows you to upload a picture to a web site. The upload |
| 679 | * may take a long time, and the application should allow the user to leave |
Trevor Johns | 682c24e | 2016-04-12 10:13:47 -0700 | [diff] [blame] | 680 | * the application while it is executing. To accomplish this, your Activity |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 681 | * should start a {@link Service} in which the upload takes place. This allows |
| 682 | * the system to properly prioritize your process (considering it to be more |
| 683 | * important than other non-visible applications) for the duration of the |
| 684 | * upload, independent of whether the original activity is paused, stopped, |
| 685 | * or finished. |
| 686 | */ |
| 687 | public class Activity extends ContextThemeWrapper |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 688 | implements LayoutInflater.Factory2, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 689 | Window.Callback, KeyEvent.Callback, |
Adam Powell | 117b695 | 2014-05-05 18:14:56 -0700 | [diff] [blame] | 690 | OnCreateContextMenuListener, ComponentCallbacks2, |
Skuhne | ce2faa5 | 2015-08-11 10:36:38 -0700 | [diff] [blame] | 691 | Window.OnWindowDismissedCallback, WindowControllerCallback { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | private static final String TAG = "Activity"; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 693 | private static final boolean DEBUG_LIFECYCLE = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 694 | |
| 695 | /** Standard activity result: operation canceled. */ |
| 696 | public static final int RESULT_CANCELED = 0; |
| 697 | /** Standard activity result: operation succeeded. */ |
| 698 | public static final int RESULT_OK = -1; |
| 699 | /** Start of user-defined activity results. */ |
| 700 | public static final int RESULT_FIRST_USER = 1; |
| 701 | |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 702 | /** @hide Task isn't finished when activity is finished */ |
| 703 | public static final int DONT_FINISH_TASK_WITH_ACTIVITY = 0; |
Filip Gruszczynski | c29ecc8 | 2015-10-08 11:08:44 -0700 | [diff] [blame] | 704 | /** |
| 705 | * @hide Task is finished if the finishing activity is the root of the task. To preserve the |
| 706 | * past behavior the task is also removed from recents. |
| 707 | */ |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 708 | public static final int FINISH_TASK_WITH_ROOT_ACTIVITY = 1; |
Filip Gruszczynski | c29ecc8 | 2015-10-08 11:08:44 -0700 | [diff] [blame] | 709 | /** |
| 710 | * @hide Task is finished along with the finishing activity, but it is not removed from |
| 711 | * recents. |
| 712 | */ |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 713 | public static final int FINISH_TASK_WITH_ACTIVITY = 2; |
| 714 | |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 715 | static final String FRAGMENTS_TAG = "android:fragments"; |
| 716 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 717 | private static final String WINDOW_HIERARCHY_TAG = "android:viewHierarchyState"; |
| 718 | private static final String SAVED_DIALOG_IDS_KEY = "android:savedDialogIds"; |
| 719 | private static final String SAVED_DIALOGS_TAG = "android:savedDialogs"; |
| 720 | private static final String SAVED_DIALOG_KEY_PREFIX = "android:dialog_"; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 721 | private static final String SAVED_DIALOG_ARGS_KEY_PREFIX = "android:dialog_args_"; |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 722 | private static final String HAS_CURENT_PERMISSIONS_REQUEST_KEY = |
| 723 | "android:hasCurrentPermissionsRequest"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 724 | |
Svetoslav | 970b59c | 2015-06-09 16:05:21 -0700 | [diff] [blame] | 725 | private static final String REQUEST_PERMISSIONS_WHO_PREFIX = "@android:requestPermissions:"; |
| 726 | |
Andrei Stingaceanu | 0bf096f | 2016-04-14 18:11:57 +0100 | [diff] [blame] | 727 | private static final String KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME = "com.android.systemui"; |
Andrei Stingaceanu | 0bf096f | 2016-04-14 18:11:57 +0100 | [diff] [blame] | 728 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 729 | private static class ManagedDialog { |
| 730 | Dialog mDialog; |
| 731 | Bundle mArgs; |
| 732 | } |
| 733 | private SparseArray<ManagedDialog> mManagedDialogs; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 734 | |
| 735 | // set by the thread after the constructor and before onCreate(Bundle savedInstanceState) is called. |
| 736 | private Instrumentation mInstrumentation; |
| 737 | private IBinder mToken; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 738 | private int mIdent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 739 | /*package*/ String mEmbeddedID; |
| 740 | private Application mApplication; |
Christopher Tate | b70f3df | 2009-04-07 16:07:59 -0700 | [diff] [blame] | 741 | /*package*/ Intent mIntent; |
Dianne Hackborn | 85d558c | 2014-11-04 10:31:54 -0800 | [diff] [blame] | 742 | /*package*/ String mReferrer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 743 | private ComponentName mComponent; |
| 744 | /*package*/ ActivityInfo mActivityInfo; |
| 745 | /*package*/ ActivityThread mMainThread; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 746 | Activity mParent; |
| 747 | boolean mCalled; |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 748 | /*package*/ boolean mResumed; |
Andrii Kulian | 58178f2 | 2016-03-16 13:44:56 -0700 | [diff] [blame] | 749 | /*package*/ boolean mStopped; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 750 | boolean mFinished; |
| 751 | boolean mStartedActivity; |
Dianne Hackborn | 6d9dcbc | 2012-10-02 17:51:13 -0700 | [diff] [blame] | 752 | private boolean mDestroyed; |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 753 | private boolean mDoReportFullyDrawn = true; |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 754 | /** true if the activity is going through a transient pause */ |
| 755 | /*package*/ boolean mTemporaryPause = false; |
Jeff Hamilton | 3d32f6e | 2010-04-01 00:04:16 -0500 | [diff] [blame] | 756 | /** true if the activity is being destroyed in order to recreate it with a new configuration */ |
| 757 | /*package*/ boolean mChangingConfigurations = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 758 | /*package*/ int mConfigChangeFlags; |
| 759 | /*package*/ Configuration mCurrentConfig; |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 760 | private SearchManager mSearchManager; |
Adam Powell | 88ab697 | 2011-07-28 11:25:01 -0700 | [diff] [blame] | 761 | private MenuInflater mMenuInflater; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 762 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 763 | static final class NonConfigurationInstances { |
| 764 | Object activity; |
| 765 | HashMap<String, Object> children; |
Adam Powell | 44ba79e | 2016-02-04 16:20:37 -0800 | [diff] [blame] | 766 | FragmentManagerNonConfig fragments; |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 767 | ArrayMap<String, LoaderManager> loaders; |
Dianne Hackborn | 20d9474 | 2014-05-29 18:35:45 -0700 | [diff] [blame] | 768 | VoiceInteractor voiceInteractor; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 769 | } |
| 770 | /* package */ NonConfigurationInstances mLastNonConfigurationInstances; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 771 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 772 | private Window mWindow; |
| 773 | |
| 774 | private WindowManager mWindowManager; |
| 775 | /*package*/ View mDecor = null; |
| 776 | /*package*/ boolean mWindowAdded = false; |
| 777 | /*package*/ boolean mVisibleFromServer = false; |
| 778 | /*package*/ boolean mVisibleFromClient = true; |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 779 | /*package*/ ActionBar mActionBar = null; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 780 | private boolean mEnableDefaultActionBarUp; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 781 | |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 782 | private VoiceInteractor mVoiceInteractor; |
| 783 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 784 | private CharSequence mTitle; |
| 785 | private int mTitleColor = 0; |
| 786 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 787 | // we must have a handler before the FragmentController is constructed |
| 788 | final Handler mHandler = new Handler(); |
| 789 | final FragmentController mFragments = FragmentController.createController(new HostCallbacks()); |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 790 | |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 791 | // Most recent call to requestVisibleBehind(). |
| 792 | boolean mVisibleBehind; |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 793 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 794 | private static final class ManagedCursor { |
| 795 | ManagedCursor(Cursor cursor) { |
| 796 | mCursor = cursor; |
| 797 | mReleased = false; |
| 798 | mUpdated = false; |
| 799 | } |
| 800 | |
| 801 | private final Cursor mCursor; |
| 802 | private boolean mReleased; |
| 803 | private boolean mUpdated; |
| 804 | } |
| 805 | private final ArrayList<ManagedCursor> mManagedCursors = |
| 806 | new ArrayList<ManagedCursor>(); |
| 807 | |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 808 | // protected by synchronized (this) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 809 | int mResultCode = RESULT_CANCELED; |
| 810 | Intent mResultData = null; |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 811 | |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 812 | private TranslucentConversionListener mTranslucentCallback; |
Craig Mautner | bc57cd1 | 2013-08-19 15:47:42 -0700 | [diff] [blame] | 813 | private boolean mChangeCanvasToTranslucent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 814 | |
Tim Kilbourn | 6a975b3 | 2015-04-09 17:14:34 -0700 | [diff] [blame] | 815 | private SearchEvent mSearchEvent; |
| 816 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 817 | private boolean mTitleReady = false; |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 818 | private int mActionModeTypeStarting = ActionMode.TYPE_PRIMARY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 819 | |
| 820 | private int mDefaultKeyMode = DEFAULT_KEYS_DISABLE; |
| 821 | private SpannableStringBuilder mDefaultKeySsb = null; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 822 | |
Winson | b640315 | 2016-02-23 13:32:09 -0800 | [diff] [blame] | 823 | private ActivityManager.TaskDescription mTaskDescription = |
| 824 | new ActivityManager.TaskDescription(); |
| 825 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 826 | protected static final int[] FOCUSED_STATE_SET = {com.android.internal.R.attr.state_focused}; |
| 827 | |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 828 | @SuppressWarnings("unused") |
Jeff Brown | 7e44283 | 2011-06-10 18:00:16 -0700 | [diff] [blame] | 829 | private final Object mInstanceTracker = StrictMode.trackActivity(this); |
| 830 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 831 | private Thread mUiThread; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 832 | |
George Mount | 1fecfb2 | 2014-06-18 14:55:55 -0700 | [diff] [blame] | 833 | ActivityTransitionState mActivityTransitionState = new ActivityTransitionState(); |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 834 | SharedElementCallback mEnterTransitionListener = SharedElementCallback.NULL_CALLBACK; |
| 835 | SharedElementCallback mExitTransitionListener = SharedElementCallback.NULL_CALLBACK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 836 | |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 837 | private boolean mHasCurrentPermissionsRequest; |
George Mount | 5beb2617 | 2015-12-15 13:36:01 -0800 | [diff] [blame] | 838 | private boolean mEatKeyUpEvent; |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 839 | |
Dimitry Ivanov | 4449ef5 | 2016-02-25 17:41:13 -0800 | [diff] [blame] | 840 | private static native String getDlWarning(); |
| 841 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 842 | /** Return the intent that started this activity. */ |
| 843 | public Intent getIntent() { |
| 844 | return mIntent; |
| 845 | } |
| 846 | |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 847 | /** |
| 848 | * Change the intent returned by {@link #getIntent}. This holds a |
| 849 | * reference to the given intent; it does not copy it. Often used in |
| 850 | * conjunction with {@link #onNewIntent}. |
| 851 | * |
| 852 | * @param newIntent The new Intent object to return from getIntent |
| 853 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 854 | * @see #getIntent |
| 855 | * @see #onNewIntent |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 856 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 857 | public void setIntent(Intent newIntent) { |
| 858 | mIntent = newIntent; |
| 859 | } |
| 860 | |
| 861 | /** Return the application that owns this activity. */ |
| 862 | public final Application getApplication() { |
| 863 | return mApplication; |
| 864 | } |
| 865 | |
| 866 | /** Is this activity embedded inside of another activity? */ |
| 867 | public final boolean isChild() { |
| 868 | return mParent != null; |
| 869 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 870 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 871 | /** Return the parent activity if this view is an embedded child. */ |
| 872 | public final Activity getParent() { |
| 873 | return mParent; |
| 874 | } |
| 875 | |
| 876 | /** Retrieve the window manager for showing custom windows. */ |
| 877 | public WindowManager getWindowManager() { |
| 878 | return mWindowManager; |
| 879 | } |
| 880 | |
| 881 | /** |
| 882 | * Retrieve the current {@link android.view.Window} for the activity. |
| 883 | * This can be used to directly access parts of the Window API that |
| 884 | * are not available through Activity/Screen. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 885 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 886 | * @return Window The current window, or null if the activity is not |
| 887 | * visual. |
| 888 | */ |
| 889 | public Window getWindow() { |
| 890 | return mWindow; |
| 891 | } |
| 892 | |
| 893 | /** |
Mark Doliner | 9525f2a | 2014-01-02 11:17:47 -0800 | [diff] [blame] | 894 | * Return the LoaderManager for this activity, creating it if needed. |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 895 | */ |
| 896 | public LoaderManager getLoaderManager() { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 897 | return mFragments.getLoaderManager(); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 898 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 899 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 900 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 901 | * Calls {@link android.view.Window#getCurrentFocus} on the |
| 902 | * Window of this Activity to return the currently focused view. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 903 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 904 | * @return View The current View with focus or null. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 905 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 906 | * @see #getWindow |
| 907 | * @see android.view.Window#getCurrentFocus |
| 908 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 909 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 910 | public View getCurrentFocus() { |
| 911 | return mWindow != null ? mWindow.getCurrentFocus() : null; |
| 912 | } |
| 913 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 914 | /** |
| 915 | * Called when the activity is starting. This is where most initialization |
| 916 | * should go: calling {@link #setContentView(int)} to inflate the |
| 917 | * activity's UI, using {@link #findViewById} to programmatically interact |
| 918 | * with widgets in the UI, calling |
| 919 | * {@link #managedQuery(android.net.Uri , String[], String, String[], String)} to retrieve |
| 920 | * cursors for data being displayed, etc. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 921 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 922 | * <p>You can call {@link #finish} from within this function, in |
| 923 | * which case onDestroy() will be immediately called without any of the rest |
| 924 | * of the activity lifecycle ({@link #onStart}, {@link #onResume}, |
| 925 | * {@link #onPause}, etc) executing. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 926 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 927 | * <p><em>Derived classes must call through to the super class's |
| 928 | * implementation of this method. If they do not, an exception will be |
| 929 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 930 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 931 | * @param savedInstanceState If the activity is being re-initialized after |
| 932 | * previously being shut down then this Bundle contains the data it most |
| 933 | * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 934 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 935 | * @see #onStart |
| 936 | * @see #onSaveInstanceState |
| 937 | * @see #onRestoreInstanceState |
| 938 | * @see #onPostCreate |
| 939 | */ |
Tor Norbye | 83c6896 | 2015-03-10 20:55:31 -0700 | [diff] [blame] | 940 | @MainThread |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 941 | @CallSuper |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 942 | protected void onCreate(@Nullable Bundle savedInstanceState) { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 943 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 944 | if (mLastNonConfigurationInstances != null) { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 945 | mFragments.restoreLoaderNonConfig(mLastNonConfigurationInstances.loaders); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 946 | } |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 947 | if (mActivityInfo.parentActivityName != null) { |
| 948 | if (mActionBar == null) { |
| 949 | mEnableDefaultActionBarUp = true; |
| 950 | } else { |
| 951 | mActionBar.setDefaultDisplayHomeAsUpEnabled(true); |
| 952 | } |
| 953 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 954 | if (savedInstanceState != null) { |
| 955 | Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG); |
| 956 | mFragments.restoreAllState(p, mLastNonConfigurationInstances != null |
| 957 | ? mLastNonConfigurationInstances.fragments : null); |
| 958 | } |
| 959 | mFragments.dispatchCreate(); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 960 | getApplication().dispatchActivityCreated(this, savedInstanceState); |
Dianne Hackborn | 20d9474 | 2014-05-29 18:35:45 -0700 | [diff] [blame] | 961 | if (mVoiceInteractor != null) { |
| 962 | mVoiceInteractor.attachActivity(this); |
| 963 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 964 | mCalled = true; |
| 965 | } |
| 966 | |
| 967 | /** |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 968 | * Same as {@link #onCreate(android.os.Bundle)} but called for those activities created with |
Craig Mautner | 43e52ed | 2014-06-16 17:18:52 -0700 | [diff] [blame] | 969 | * the attribute {@link android.R.attr#persistableMode} set to |
| 970 | * <code>persistAcrossReboots</code>. |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 971 | * |
| 972 | * @param savedInstanceState if the activity is being re-initialized after |
| 973 | * previously being shut down then this Bundle contains the data it most |
| 974 | * recently supplied in {@link #onSaveInstanceState}. |
| 975 | * <b><i>Note: Otherwise it is null.</i></b> |
| 976 | * @param persistentState if the activity is being re-initialized after |
| 977 | * previously being shut down or powered off then this Bundle contains the data it most |
| 978 | * recently supplied to outPersistentState in {@link #onSaveInstanceState}. |
| 979 | * <b><i>Note: Otherwise it is null.</i></b> |
| 980 | * |
| 981 | * @see #onCreate(android.os.Bundle) |
| 982 | * @see #onStart |
| 983 | * @see #onSaveInstanceState |
| 984 | * @see #onRestoreInstanceState |
| 985 | * @see #onPostCreate |
| 986 | */ |
Craig Mautner | 7a629c2 | 2014-09-04 14:57:04 -0700 | [diff] [blame] | 987 | public void onCreate(@Nullable Bundle savedInstanceState, |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 988 | @Nullable PersistableBundle persistentState) { |
| 989 | onCreate(savedInstanceState); |
| 990 | } |
| 991 | |
| 992 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 993 | * The hook for {@link ActivityThread} to restore the state of this activity. |
| 994 | * |
| 995 | * Calls {@link #onSaveInstanceState(android.os.Bundle)} and |
| 996 | * {@link #restoreManagedDialogs(android.os.Bundle)}. |
| 997 | * |
| 998 | * @param savedInstanceState contains the saved state |
| 999 | */ |
| 1000 | final void performRestoreInstanceState(Bundle savedInstanceState) { |
| 1001 | onRestoreInstanceState(savedInstanceState); |
| 1002 | restoreManagedDialogs(savedInstanceState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | /** |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1006 | * The hook for {@link ActivityThread} to restore the state of this activity. |
| 1007 | * |
| 1008 | * Calls {@link #onSaveInstanceState(android.os.Bundle)} and |
| 1009 | * {@link #restoreManagedDialogs(android.os.Bundle)}. |
| 1010 | * |
| 1011 | * @param savedInstanceState contains the saved state |
| 1012 | * @param persistentState contains the persistable saved state |
| 1013 | */ |
| 1014 | final void performRestoreInstanceState(Bundle savedInstanceState, |
| 1015 | PersistableBundle persistentState) { |
| 1016 | onRestoreInstanceState(savedInstanceState, persistentState); |
| 1017 | if (savedInstanceState != null) { |
| 1018 | restoreManagedDialogs(savedInstanceState); |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1023 | * This method is called after {@link #onStart} when the activity is |
| 1024 | * being re-initialized from a previously saved state, given here in |
Mike LeBeau | 305de9d | 2010-03-11 09:21:08 -0800 | [diff] [blame] | 1025 | * <var>savedInstanceState</var>. Most implementations will simply use {@link #onCreate} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1026 | * to restore their state, but it is sometimes convenient to do it here |
| 1027 | * after all of the initialization has been done or to allow subclasses to |
| 1028 | * decide whether to use your default implementation. The default |
| 1029 | * implementation of this method performs a restore of any view state that |
| 1030 | * had previously been frozen by {@link #onSaveInstanceState}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1031 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1032 | * <p>This method is called between {@link #onStart} and |
| 1033 | * {@link #onPostCreate}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1034 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1035 | * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1036 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1037 | * @see #onCreate |
| 1038 | * @see #onPostCreate |
| 1039 | * @see #onResume |
| 1040 | * @see #onSaveInstanceState |
| 1041 | */ |
| 1042 | protected void onRestoreInstanceState(Bundle savedInstanceState) { |
| 1043 | if (mWindow != null) { |
| 1044 | Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG); |
| 1045 | if (windowState != null) { |
| 1046 | mWindow.restoreHierarchyState(windowState); |
| 1047 | } |
| 1048 | } |
| 1049 | } |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1050 | |
| 1051 | /** |
| 1052 | * This is the same as {@link #onRestoreInstanceState(Bundle)} but is called for activities |
Craig Mautner | 43e52ed | 2014-06-16 17:18:52 -0700 | [diff] [blame] | 1053 | * created with the attribute {@link android.R.attr#persistableMode} set to |
| 1054 | * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed |
| 1055 | * came from the restored PersistableBundle first |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1056 | * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}. |
| 1057 | * |
| 1058 | * <p>This method is called between {@link #onStart} and |
| 1059 | * {@link #onPostCreate}. |
| 1060 | * |
| 1061 | * <p>If this method is called {@link #onRestoreInstanceState(Bundle)} will not be called. |
| 1062 | * |
| 1063 | * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}. |
| 1064 | * @param persistentState the data most recently supplied in {@link #onSaveInstanceState}. |
| 1065 | * |
| 1066 | * @see #onRestoreInstanceState(Bundle) |
| 1067 | * @see #onCreate |
| 1068 | * @see #onPostCreate |
| 1069 | * @see #onResume |
| 1070 | * @see #onSaveInstanceState |
| 1071 | */ |
Craig Mautner | 7a629c2 | 2014-09-04 14:57:04 -0700 | [diff] [blame] | 1072 | public void onRestoreInstanceState(Bundle savedInstanceState, |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1073 | PersistableBundle persistentState) { |
| 1074 | if (savedInstanceState != null) { |
| 1075 | onRestoreInstanceState(savedInstanceState); |
| 1076 | } |
| 1077 | } |
| 1078 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1079 | /** |
| 1080 | * Restore the state of any saved managed dialogs. |
| 1081 | * |
| 1082 | * @param savedInstanceState The bundle to restore from. |
| 1083 | */ |
| 1084 | private void restoreManagedDialogs(Bundle savedInstanceState) { |
| 1085 | final Bundle b = savedInstanceState.getBundle(SAVED_DIALOGS_TAG); |
| 1086 | if (b == null) { |
| 1087 | return; |
| 1088 | } |
| 1089 | |
| 1090 | final int[] ids = b.getIntArray(SAVED_DIALOG_IDS_KEY); |
| 1091 | final int numDialogs = ids.length; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1092 | mManagedDialogs = new SparseArray<ManagedDialog>(numDialogs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1093 | for (int i = 0; i < numDialogs; i++) { |
| 1094 | final Integer dialogId = ids[i]; |
| 1095 | Bundle dialogState = b.getBundle(savedDialogKeyFor(dialogId)); |
| 1096 | if (dialogState != null) { |
Romain Guy | e35c235 | 2009-06-19 13:18:12 -0700 | [diff] [blame] | 1097 | // Calling onRestoreInstanceState() below will invoke dispatchOnCreate |
| 1098 | // so tell createDialog() not to do it, otherwise we get an exception |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1099 | final ManagedDialog md = new ManagedDialog(); |
| 1100 | md.mArgs = b.getBundle(savedDialogArgsKeyFor(dialogId)); |
| 1101 | md.mDialog = createDialog(dialogId, dialogState, md.mArgs); |
| 1102 | if (md.mDialog != null) { |
| 1103 | mManagedDialogs.put(dialogId, md); |
| 1104 | onPrepareDialog(dialogId, md.mDialog, md.mArgs); |
| 1105 | md.mDialog.onRestoreInstanceState(dialogState); |
| 1106 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1107 | } |
| 1108 | } |
| 1109 | } |
| 1110 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1111 | private Dialog createDialog(Integer dialogId, Bundle state, Bundle args) { |
| 1112 | final Dialog dialog = onCreateDialog(dialogId, args); |
Romain Guy | 764d533 | 2009-06-17 16:52:22 -0700 | [diff] [blame] | 1113 | if (dialog == null) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1114 | return null; |
Romain Guy | 764d533 | 2009-06-17 16:52:22 -0700 | [diff] [blame] | 1115 | } |
Romain Guy | 6de4aed | 2009-07-08 10:54:45 -0700 | [diff] [blame] | 1116 | dialog.dispatchOnCreate(state); |
Romain Guy | 764d533 | 2009-06-17 16:52:22 -0700 | [diff] [blame] | 1117 | return dialog; |
| 1118 | } |
| 1119 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1120 | private static String savedDialogKeyFor(int key) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1121 | return SAVED_DIALOG_KEY_PREFIX + key; |
| 1122 | } |
| 1123 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1124 | private static String savedDialogArgsKeyFor(int key) { |
| 1125 | return SAVED_DIALOG_ARGS_KEY_PREFIX + key; |
| 1126 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1127 | |
| 1128 | /** |
| 1129 | * Called when activity start-up is complete (after {@link #onStart} |
| 1130 | * and {@link #onRestoreInstanceState} have been called). Applications will |
| 1131 | * generally not implement this method; it is intended for system |
| 1132 | * classes to do final initialization after application code has run. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1133 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1134 | * <p><em>Derived classes must call through to the super class's |
| 1135 | * implementation of this method. If they do not, an exception will be |
| 1136 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1137 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1138 | * @param savedInstanceState If the activity is being re-initialized after |
| 1139 | * previously being shut down then this Bundle contains the data it most |
| 1140 | * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b> |
| 1141 | * @see #onCreate |
| 1142 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1143 | @CallSuper |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 1144 | protected void onPostCreate(@Nullable Bundle savedInstanceState) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1145 | if (!isChild()) { |
| 1146 | mTitleReady = true; |
| 1147 | onTitleChanged(getTitle(), getTitleColor()); |
| 1148 | } |
Winson | b640315 | 2016-02-23 13:32:09 -0800 | [diff] [blame] | 1149 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1150 | mCalled = true; |
| 1151 | } |
| 1152 | |
| 1153 | /** |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1154 | * This is the same as {@link #onPostCreate(Bundle)} but is called for activities |
Craig Mautner | 43e52ed | 2014-06-16 17:18:52 -0700 | [diff] [blame] | 1155 | * created with the attribute {@link android.R.attr#persistableMode} set to |
| 1156 | * <code>persistAcrossReboots</code>. |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1157 | * |
| 1158 | * @param savedInstanceState The data most recently supplied in {@link #onSaveInstanceState} |
| 1159 | * @param persistentState The data caming from the PersistableBundle first |
| 1160 | * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}. |
| 1161 | * |
| 1162 | * @see #onCreate |
| 1163 | */ |
Craig Mautner | 7a629c2 | 2014-09-04 14:57:04 -0700 | [diff] [blame] | 1164 | public void onPostCreate(@Nullable Bundle savedInstanceState, |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1165 | @Nullable PersistableBundle persistentState) { |
| 1166 | onPostCreate(savedInstanceState); |
| 1167 | } |
| 1168 | |
| 1169 | /** |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1170 | * Called after {@link #onCreate} — or after {@link #onRestart} when |
| 1171 | * the activity had been stopped, but is now again being displayed to the |
John Spurlock | 8a985d2 | 2014-02-25 09:40:05 -0500 | [diff] [blame] | 1172 | * user. It will be followed by {@link #onResume}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1173 | * |
| 1174 | * <p><em>Derived classes must call through to the super class's |
| 1175 | * implementation of this method. If they do not, an exception will be |
| 1176 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1177 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1178 | * @see #onCreate |
| 1179 | * @see #onStop |
| 1180 | * @see #onResume |
| 1181 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1182 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1183 | protected void onStart() { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1184 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStart " + this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1185 | mCalled = true; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1186 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1187 | mFragments.doLoaderStart(); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1188 | |
| 1189 | getApplication().dispatchActivityStarted(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | /** |
| 1193 | * Called after {@link #onStop} when the current activity is being |
| 1194 | * re-displayed to the user (the user has navigated back to it). It will |
| 1195 | * be followed by {@link #onStart} and then {@link #onResume}. |
| 1196 | * |
| 1197 | * <p>For activities that are using raw {@link Cursor} objects (instead of |
| 1198 | * creating them through |
| 1199 | * {@link #managedQuery(android.net.Uri , String[], String, String[], String)}, |
| 1200 | * this is usually the place |
| 1201 | * where the cursor should be requeried (because you had deactivated it in |
| 1202 | * {@link #onStop}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1203 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1204 | * <p><em>Derived classes must call through to the super class's |
| 1205 | * implementation of this method. If they do not, an exception will be |
| 1206 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1207 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1208 | * @see #onStop |
| 1209 | * @see #onStart |
| 1210 | * @see #onResume |
| 1211 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1212 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1213 | protected void onRestart() { |
| 1214 | mCalled = true; |
| 1215 | } |
| 1216 | |
| 1217 | /** |
Dianne Hackborn | 6bdd3a1 | 2015-08-05 15:01:28 -0700 | [diff] [blame] | 1218 | * Called when an {@link #onResume} is coming up, prior to other pre-resume callbacks |
| 1219 | * such as {@link #onNewIntent} and {@link #onActivityResult}. This is primarily intended |
| 1220 | * to give the activity a hint that its state is no longer saved -- it will generally |
| 1221 | * be called after {@link #onSaveInstanceState} and prior to the activity being |
| 1222 | * resumed/started again. |
| 1223 | */ |
| 1224 | public void onStateNotSaved() { |
| 1225 | } |
| 1226 | |
| 1227 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1228 | * Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or |
| 1229 | * {@link #onPause}, for your activity to start interacting with the user. |
| 1230 | * This is a good place to begin animations, open exclusive-access devices |
| 1231 | * (such as the camera), etc. |
| 1232 | * |
| 1233 | * <p>Keep in mind that onResume is not the best indicator that your activity |
| 1234 | * is visible to the user; a system window such as the keyguard may be in |
| 1235 | * front. Use {@link #onWindowFocusChanged} to know for certain that your |
| 1236 | * activity is visible to the user (for example, to resume a game). |
| 1237 | * |
| 1238 | * <p><em>Derived classes must call through to the super class's |
| 1239 | * implementation of this method. If they do not, an exception will be |
| 1240 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1241 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1242 | * @see #onRestoreInstanceState |
| 1243 | * @see #onRestart |
| 1244 | * @see #onPostResume |
| 1245 | * @see #onPause |
| 1246 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1247 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1248 | protected void onResume() { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1249 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " + this); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1250 | getApplication().dispatchActivityResumed(this); |
George Mount | bdc4d8d | 2016-03-23 13:10:13 -0700 | [diff] [blame] | 1251 | mActivityTransitionState.onResume(this, isTopOfTask()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1252 | mCalled = true; |
| 1253 | } |
| 1254 | |
| 1255 | /** |
| 1256 | * Called when activity resume is complete (after {@link #onResume} has |
| 1257 | * been called). Applications will generally not implement this method; |
| 1258 | * it is intended for system classes to do final setup after application |
| 1259 | * resume code has run. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1260 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1261 | * <p><em>Derived classes must call through to the super class's |
| 1262 | * implementation of this method. If they do not, an exception will be |
| 1263 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1264 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1265 | * @see #onResume |
| 1266 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1267 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1268 | protected void onPostResume() { |
| 1269 | final Window win = getWindow(); |
| 1270 | if (win != null) win.makeActive(); |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 1271 | if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1272 | mCalled = true; |
| 1273 | } |
| 1274 | |
Amith Yamasani | 0af6fa7 | 2016-01-17 15:36:19 -0800 | [diff] [blame] | 1275 | void setVoiceInteractor(IVoiceInteractor voiceInteractor) { |
Amith Yamasani | eeed06c | 2016-05-03 15:07:03 -0700 | [diff] [blame] | 1276 | if (mVoiceInteractor != null) { |
| 1277 | for (Request activeRequest: mVoiceInteractor.getActiveRequests()) { |
| 1278 | activeRequest.cancel(); |
| 1279 | activeRequest.clear(); |
| 1280 | } |
| 1281 | } |
Amith Yamasani | 0af6fa7 | 2016-01-17 15:36:19 -0800 | [diff] [blame] | 1282 | if (voiceInteractor == null) { |
| 1283 | mVoiceInteractor = null; |
| 1284 | } else { |
| 1285 | mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this, |
| 1286 | Looper.myLooper()); |
| 1287 | } |
| 1288 | } |
| 1289 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1290 | /** |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 1291 | * Check whether this activity is running as part of a voice interaction with the user. |
| 1292 | * If true, it should perform its interaction with the user through the |
| 1293 | * {@link VoiceInteractor} returned by {@link #getVoiceInteractor}. |
| 1294 | */ |
| 1295 | public boolean isVoiceInteraction() { |
| 1296 | return mVoiceInteractor != null; |
| 1297 | } |
| 1298 | |
| 1299 | /** |
Dianne Hackborn | fb81d09 | 2015-08-03 17:14:46 -0700 | [diff] [blame] | 1300 | * Like {@link #isVoiceInteraction}, but only returns true if this is also the root |
| 1301 | * of a voice interaction. That is, returns true if this activity was directly |
| 1302 | * started by the voice interaction service as the initiation of a voice interaction. |
| 1303 | * Otherwise, for example if it was started by another activity while under voice |
| 1304 | * interaction, returns false. |
| 1305 | */ |
| 1306 | public boolean isVoiceInteractionRoot() { |
| 1307 | try { |
| 1308 | return mVoiceInteractor != null |
| 1309 | && ActivityManagerNative.getDefault().isRootVoiceInteraction(mToken); |
| 1310 | } catch (RemoteException e) { |
| 1311 | } |
| 1312 | return false; |
| 1313 | } |
| 1314 | |
| 1315 | /** |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 1316 | * Retrieve the active {@link VoiceInteractor} that the user is going through to |
| 1317 | * interact with this activity. |
| 1318 | */ |
| 1319 | public VoiceInteractor getVoiceInteractor() { |
| 1320 | return mVoiceInteractor; |
| 1321 | } |
| 1322 | |
| 1323 | /** |
Amith Yamasani | 0af6fa7 | 2016-01-17 15:36:19 -0800 | [diff] [blame] | 1324 | * Queries whether the currently enabled voice interaction service supports returning |
| 1325 | * a voice interactor for use by the activity. This is valid only for the duration of the |
| 1326 | * activity. |
| 1327 | * |
| 1328 | * @return whether the current voice interaction service supports local voice interaction |
| 1329 | */ |
| 1330 | public boolean isLocalVoiceInteractionSupported() { |
| 1331 | try { |
| 1332 | return ActivityManagerNative.getDefault().supportsLocalVoiceInteraction(); |
| 1333 | } catch (RemoteException re) { |
| 1334 | } |
| 1335 | return false; |
| 1336 | } |
| 1337 | |
| 1338 | /** |
| 1339 | * Starts a local voice interaction session. When ready, |
| 1340 | * {@link #onLocalVoiceInteractionStarted()} is called. You can pass a bundle of private options |
| 1341 | * to the registered voice interaction service. |
| 1342 | * @param privateOptions a Bundle of private arguments to the current voice interaction service |
| 1343 | */ |
| 1344 | public void startLocalVoiceInteraction(Bundle privateOptions) { |
| 1345 | try { |
| 1346 | ActivityManagerNative.getDefault().startLocalVoiceInteraction(mToken, privateOptions); |
| 1347 | } catch (RemoteException re) { |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | /** |
| 1352 | * Callback to indicate that {@link #startLocalVoiceInteraction(Bundle)} has resulted in a |
| 1353 | * voice interaction session being started. You can now retrieve a voice interactor using |
| 1354 | * {@link #getVoiceInteractor()}. |
| 1355 | */ |
| 1356 | public void onLocalVoiceInteractionStarted() { |
Amith Yamasani | 0af6fa7 | 2016-01-17 15:36:19 -0800 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | /** |
Amith Yamasani | d8d967e | 2016-04-08 09:57:01 -0700 | [diff] [blame] | 1360 | * Callback to indicate that the local voice interaction has stopped either |
| 1361 | * because it was requested through a call to {@link #stopLocalVoiceInteraction()} |
| 1362 | * or because it was canceled by the user. The previously acquired {@link VoiceInteractor} |
| 1363 | * is no longer valid after this. |
Amith Yamasani | 0af6fa7 | 2016-01-17 15:36:19 -0800 | [diff] [blame] | 1364 | */ |
| 1365 | public void onLocalVoiceInteractionStopped() { |
Amith Yamasani | 0af6fa7 | 2016-01-17 15:36:19 -0800 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | /** |
| 1369 | * Request to terminate the current voice interaction that was previously started |
Amith Yamasani | d8d967e | 2016-04-08 09:57:01 -0700 | [diff] [blame] | 1370 | * using {@link #startLocalVoiceInteraction(Bundle)}. When the interaction is |
| 1371 | * terminated, {@link #onLocalVoiceInteractionStopped()} will be called. |
Amith Yamasani | 0af6fa7 | 2016-01-17 15:36:19 -0800 | [diff] [blame] | 1372 | */ |
| 1373 | public void stopLocalVoiceInteraction() { |
| 1374 | try { |
| 1375 | ActivityManagerNative.getDefault().stopLocalVoiceInteraction(mToken); |
| 1376 | } catch (RemoteException re) { |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1381 | * This is called for activities that set launchMode to "singleTop" in |
| 1382 | * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} |
| 1383 | * flag when calling {@link #startActivity}. In either case, when the |
| 1384 | * activity is re-launched while at the top of the activity stack instead |
| 1385 | * of a new instance of the activity being started, onNewIntent() will be |
| 1386 | * called on the existing instance with the Intent that was used to |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1387 | * re-launch it. |
| 1388 | * |
| 1389 | * <p>An activity will always be paused before receiving a new intent, so |
| 1390 | * you can count on {@link #onResume} being called after this method. |
| 1391 | * |
| 1392 | * <p>Note that {@link #getIntent} still returns the original Intent. You |
| 1393 | * can use {@link #setIntent} to update it to this new Intent. |
| 1394 | * |
| 1395 | * @param intent The new intent that was started for the activity. |
| 1396 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1397 | * @see #getIntent |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1398 | * @see #setIntent |
| 1399 | * @see #onResume |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1400 | */ |
| 1401 | protected void onNewIntent(Intent intent) { |
| 1402 | } |
| 1403 | |
| 1404 | /** |
| 1405 | * The hook for {@link ActivityThread} to save the state of this activity. |
| 1406 | * |
| 1407 | * Calls {@link #onSaveInstanceState(android.os.Bundle)} |
| 1408 | * and {@link #saveManagedDialogs(android.os.Bundle)}. |
| 1409 | * |
| 1410 | * @param outState The bundle to save the state to. |
| 1411 | */ |
| 1412 | final void performSaveInstanceState(Bundle outState) { |
| 1413 | onSaveInstanceState(outState); |
| 1414 | saveManagedDialogs(outState); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 1415 | mActivityTransitionState.saveState(outState); |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 1416 | storeHasCurrentPermissionRequest(outState); |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1417 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | /** |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1421 | * The hook for {@link ActivityThread} to save the state of this activity. |
| 1422 | * |
| 1423 | * Calls {@link #onSaveInstanceState(android.os.Bundle)} |
| 1424 | * and {@link #saveManagedDialogs(android.os.Bundle)}. |
| 1425 | * |
| 1426 | * @param outState The bundle to save the state to. |
| 1427 | * @param outPersistentState The bundle to save persistent state to. |
| 1428 | */ |
| 1429 | final void performSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { |
| 1430 | onSaveInstanceState(outState, outPersistentState); |
| 1431 | saveManagedDialogs(outState); |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 1432 | storeHasCurrentPermissionRequest(outState); |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1433 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState + |
| 1434 | ", " + outPersistentState); |
| 1435 | } |
| 1436 | |
| 1437 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1438 | * Called to retrieve per-instance state from an activity before being killed |
| 1439 | * so that the state can be restored in {@link #onCreate} or |
| 1440 | * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method |
| 1441 | * will be passed to both). |
| 1442 | * |
| 1443 | * <p>This method is called before an activity may be killed so that when it |
| 1444 | * comes back some time in the future it can restore its state. For example, |
| 1445 | * if activity B is launched in front of activity A, and at some point activity |
| 1446 | * A is killed to reclaim resources, activity A will have a chance to save the |
| 1447 | * current state of its user interface via this method so that when the user |
| 1448 | * returns to activity A, the state of the user interface can be restored |
| 1449 | * via {@link #onCreate} or {@link #onRestoreInstanceState}. |
| 1450 | * |
| 1451 | * <p>Do not confuse this method with activity lifecycle callbacks such as |
| 1452 | * {@link #onPause}, which is always called when an activity is being placed |
| 1453 | * in the background or on its way to destruction, or {@link #onStop} which |
| 1454 | * is called before destruction. One example of when {@link #onPause} and |
| 1455 | * {@link #onStop} is called and not this method is when a user navigates back |
| 1456 | * from activity B to activity A: there is no need to call {@link #onSaveInstanceState} |
| 1457 | * on B because that particular instance will never be restored, so the |
| 1458 | * system avoids calling it. An example when {@link #onPause} is called and |
| 1459 | * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A: |
| 1460 | * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't |
| 1461 | * killed during the lifetime of B since the state of the user interface of |
| 1462 | * A will stay intact. |
| 1463 | * |
| 1464 | * <p>The default implementation takes care of most of the UI per-instance |
| 1465 | * state for you by calling {@link android.view.View#onSaveInstanceState()} on each |
| 1466 | * view in the hierarchy that has an id, and by saving the id of the currently |
| 1467 | * focused view (all of which is restored by the default implementation of |
| 1468 | * {@link #onRestoreInstanceState}). If you override this method to save additional |
| 1469 | * information not captured by each individual view, you will likely want to |
| 1470 | * call through to the default implementation, otherwise be prepared to save |
| 1471 | * all of the state of each view yourself. |
| 1472 | * |
| 1473 | * <p>If called, this method will occur before {@link #onStop}. There are |
| 1474 | * no guarantees about whether it will occur before or after {@link #onPause}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1475 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1476 | * @param outState Bundle in which to place your saved state. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1477 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1478 | * @see #onCreate |
| 1479 | * @see #onRestoreInstanceState |
| 1480 | * @see #onPause |
| 1481 | */ |
| 1482 | protected void onSaveInstanceState(Bundle outState) { |
| 1483 | outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState()); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1484 | Parcelable p = mFragments.saveAllState(); |
| 1485 | if (p != null) { |
| 1486 | outState.putParcelable(FRAGMENTS_TAG, p); |
| 1487 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1488 | getApplication().dispatchActivitySaveInstanceState(this, outState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | /** |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1492 | * This is the same as {@link #onSaveInstanceState} but is called for activities |
Craig Mautner | 43e52ed | 2014-06-16 17:18:52 -0700 | [diff] [blame] | 1493 | * created with the attribute {@link android.R.attr#persistableMode} set to |
| 1494 | * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed |
| 1495 | * in will be saved and presented in {@link #onCreate(Bundle, PersistableBundle)} |
| 1496 | * the first time that this activity is restarted following the next device reboot. |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1497 | * |
| 1498 | * @param outState Bundle in which to place your saved state. |
| 1499 | * @param outPersistentState State which will be saved across reboots. |
| 1500 | * |
| 1501 | * @see #onSaveInstanceState(Bundle) |
| 1502 | * @see #onCreate |
| 1503 | * @see #onRestoreInstanceState(Bundle, PersistableBundle) |
| 1504 | * @see #onPause |
| 1505 | */ |
Craig Mautner | 7a629c2 | 2014-09-04 14:57:04 -0700 | [diff] [blame] | 1506 | public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1507 | onSaveInstanceState(outState); |
| 1508 | } |
| 1509 | |
| 1510 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1511 | * Save the state of any managed dialogs. |
| 1512 | * |
| 1513 | * @param outState place to store the saved state. |
| 1514 | */ |
| 1515 | private void saveManagedDialogs(Bundle outState) { |
| 1516 | if (mManagedDialogs == null) { |
| 1517 | return; |
| 1518 | } |
| 1519 | |
| 1520 | final int numDialogs = mManagedDialogs.size(); |
| 1521 | if (numDialogs == 0) { |
| 1522 | return; |
| 1523 | } |
| 1524 | |
| 1525 | Bundle dialogState = new Bundle(); |
| 1526 | |
| 1527 | int[] ids = new int[mManagedDialogs.size()]; |
| 1528 | |
| 1529 | // save each dialog's bundle, gather the ids |
| 1530 | for (int i = 0; i < numDialogs; i++) { |
| 1531 | final int key = mManagedDialogs.keyAt(i); |
| 1532 | ids[i] = key; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1533 | final ManagedDialog md = mManagedDialogs.valueAt(i); |
| 1534 | dialogState.putBundle(savedDialogKeyFor(key), md.mDialog.onSaveInstanceState()); |
| 1535 | if (md.mArgs != null) { |
| 1536 | dialogState.putBundle(savedDialogArgsKeyFor(key), md.mArgs); |
| 1537 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | dialogState.putIntArray(SAVED_DIALOG_IDS_KEY, ids); |
| 1541 | outState.putBundle(SAVED_DIALOGS_TAG, dialogState); |
| 1542 | } |
| 1543 | |
| 1544 | |
| 1545 | /** |
| 1546 | * Called as part of the activity lifecycle when an activity is going into |
| 1547 | * the background, but has not (yet) been killed. The counterpart to |
| 1548 | * {@link #onResume}. |
| 1549 | * |
| 1550 | * <p>When activity B is launched in front of activity A, this callback will |
| 1551 | * be invoked on A. B will not be created until A's {@link #onPause} returns, |
| 1552 | * so be sure to not do anything lengthy here. |
| 1553 | * |
| 1554 | * <p>This callback is mostly used for saving any persistent state the |
| 1555 | * activity is editing, to present a "edit in place" model to the user and |
| 1556 | * making sure nothing is lost if there are not enough resources to start |
| 1557 | * the new activity without first killing this one. This is also a good |
| 1558 | * place to do things like stop animations and other things that consume a |
Pin Ting | 14a9310 | 2012-04-25 11:27:03 +0800 | [diff] [blame] | 1559 | * noticeable amount of CPU in order to make the switch to the next activity |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1560 | * as fast as possible, or to close resources that are exclusive access |
| 1561 | * such as the camera. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1562 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1563 | * <p>In situations where the system needs more memory it may kill paused |
| 1564 | * processes to reclaim resources. Because of this, you should be sure |
| 1565 | * that all of your state is saved by the time you return from |
| 1566 | * this function. In general {@link #onSaveInstanceState} is used to save |
| 1567 | * per-instance state in the activity and this method is used to store |
| 1568 | * global persistent data (in content providers, files, etc.) |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1569 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1570 | * <p>After receiving this call you will usually receive a following call |
| 1571 | * to {@link #onStop} (after the next activity has been resumed and |
| 1572 | * displayed), however in some cases there will be a direct call back to |
| 1573 | * {@link #onResume} without going through the stopped state. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1574 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1575 | * <p><em>Derived classes must call through to the super class's |
| 1576 | * implementation of this method. If they do not, an exception will be |
| 1577 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1578 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1579 | * @see #onResume |
| 1580 | * @see #onSaveInstanceState |
| 1581 | * @see #onStop |
| 1582 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1583 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1584 | protected void onPause() { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1585 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onPause " + this); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1586 | getApplication().dispatchActivityPaused(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1587 | mCalled = true; |
| 1588 | } |
| 1589 | |
| 1590 | /** |
| 1591 | * Called as part of the activity lifecycle when an activity is about to go |
| 1592 | * into the background as the result of user choice. For example, when the |
| 1593 | * user presses the Home key, {@link #onUserLeaveHint} will be called, but |
| 1594 | * when an incoming phone call causes the in-call Activity to be automatically |
| 1595 | * brought to the foreground, {@link #onUserLeaveHint} will not be called on |
| 1596 | * the activity being interrupted. In cases when it is invoked, this method |
| 1597 | * is called right before the activity's {@link #onPause} callback. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1598 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1599 | * <p>This callback and {@link #onUserInteraction} are intended to help |
| 1600 | * activities manage status bar notifications intelligently; specifically, |
| 1601 | * for helping activities determine the proper time to cancel a notfication. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1602 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1603 | * @see #onUserInteraction() |
| 1604 | */ |
| 1605 | protected void onUserLeaveHint() { |
| 1606 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1607 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1608 | /** |
| 1609 | * Generate a new thumbnail for this activity. This method is called before |
| 1610 | * pausing the activity, and should draw into <var>outBitmap</var> the |
| 1611 | * imagery for the desired thumbnail in the dimensions of that bitmap. It |
| 1612 | * can use the given <var>canvas</var>, which is configured to draw into the |
| 1613 | * bitmap, for rendering if desired. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1614 | * |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 1615 | * <p>The default implementation returns fails and does not draw a thumbnail; |
| 1616 | * this will result in the platform creating its own thumbnail if needed. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1617 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1618 | * @param outBitmap The bitmap to contain the thumbnail. |
| 1619 | * @param canvas Can be used to render into the bitmap. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1620 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1621 | * @return Return true if you have drawn into the bitmap; otherwise after |
| 1622 | * you return it will be filled with a default thumbnail. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1623 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1624 | * @see #onCreateDescription |
| 1625 | * @see #onSaveInstanceState |
| 1626 | * @see #onPause |
| 1627 | */ |
| 1628 | public boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) { |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 1629 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | /** |
| 1633 | * Generate a new description for this activity. This method is called |
| 1634 | * before pausing the activity and can, if desired, return some textual |
| 1635 | * description of its current state to be displayed to the user. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1636 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1637 | * <p>The default implementation returns null, which will cause you to |
| 1638 | * inherit the description from the previous activity. If all activities |
| 1639 | * return null, generally the label of the top activity will be used as the |
| 1640 | * description. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1641 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1642 | * @return A description of what the user is doing. It should be short and |
| 1643 | * sweet (only a few words). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1644 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1645 | * @see #onCreateThumbnail |
| 1646 | * @see #onSaveInstanceState |
| 1647 | * @see #onPause |
| 1648 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 1649 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1650 | public CharSequence onCreateDescription() { |
| 1651 | return null; |
| 1652 | } |
| 1653 | |
| 1654 | /** |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 1655 | * This is called when the user is requesting an assist, to build a full |
| 1656 | * {@link Intent#ACTION_ASSIST} Intent with all of the context of the current |
| 1657 | * application. You can override this method to place into the bundle anything |
| 1658 | * you would like to appear in the {@link Intent#EXTRA_ASSIST_CONTEXT} part |
Dianne Hackborn | a83ce1d | 2015-03-11 15:16:13 -0700 | [diff] [blame] | 1659 | * of the assist Intent. |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 1660 | * |
| 1661 | * <p>This function will be called after any global assist callbacks that had |
Ying Wang | 4e0eb22 | 2013-04-18 20:39:48 -0700 | [diff] [blame] | 1662 | * been registered with {@link Application#registerOnProvideAssistDataListener |
| 1663 | * Application.registerOnProvideAssistDataListener}. |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 1664 | */ |
| 1665 | public void onProvideAssistData(Bundle data) { |
Dianne Hackborn | a83ce1d | 2015-03-11 15:16:13 -0700 | [diff] [blame] | 1666 | } |
| 1667 | |
| 1668 | /** |
| 1669 | * This is called when the user is requesting an assist, to provide references |
| 1670 | * to content related to the current activity. Before being called, the |
| 1671 | * {@code outContent} Intent is filled with the base Intent of the activity (the Intent |
| 1672 | * returned by {@link #getIntent()}). The Intent's extras are stripped of any types |
| 1673 | * that are not valid for {@link PersistableBundle} or non-framework Parcelables, and |
| 1674 | * the flags {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION} and |
| 1675 | * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} are cleared from the Intent. |
| 1676 | * |
| 1677 | * <p>Custom implementation may adjust the content intent to better reflect the top-level |
| 1678 | * context of the activity, and fill in its ClipData with additional content of |
| 1679 | * interest that the user is currently viewing. For example, an image gallery application |
| 1680 | * that has launched in to an activity allowing the user to swipe through pictures should |
| 1681 | * modify the intent to reference the current image they are looking it; such an |
| 1682 | * application when showing a list of pictures should add a ClipData that has |
| 1683 | * references to all of the pictures currently visible on screen.</p> |
| 1684 | * |
| 1685 | * @param outContent The assist content to return. |
| 1686 | */ |
| 1687 | public void onProvideAssistContent(AssistContent outContent) { |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 1688 | } |
| 1689 | |
Clara Bayarri | eb3c2d3 | 2016-04-01 14:37:32 +0100 | [diff] [blame] | 1690 | /** |
Clara Bayarri | ab591ba | 2016-05-16 17:48:16 +0100 | [diff] [blame] | 1691 | * Request the Keyboard Shortcuts screen to show up. This will trigger |
Clara Bayarri | eb3c2d3 | 2016-04-01 14:37:32 +0100 | [diff] [blame] | 1692 | * {@link #onProvideKeyboardShortcuts} to retrieve the shortcuts for the foreground activity. |
| 1693 | */ |
Clara Bayarri | ac6f034 | 2016-05-16 14:15:14 +0100 | [diff] [blame] | 1694 | public final void requestShowKeyboardShortcuts() { |
Clara Bayarri | eb3c2d3 | 2016-04-01 14:37:32 +0100 | [diff] [blame] | 1695 | Intent intent = new Intent(Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS); |
Andrei Stingaceanu | a3d2693 | 2016-07-15 11:12:20 +0100 | [diff] [blame] | 1696 | intent.setPackage(KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME); |
| 1697 | sendBroadcastAsUser(intent, UserHandle.SYSTEM); |
Andrei Stingaceanu | 0bf096f | 2016-04-14 18:11:57 +0100 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | /** |
| 1701 | * Dismiss the Keyboard Shortcuts screen. |
| 1702 | */ |
| 1703 | public final void dismissKeyboardShortcutsHelper() { |
| 1704 | Intent intent = new Intent(Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS); |
Andrei Stingaceanu | a3d2693 | 2016-07-15 11:12:20 +0100 | [diff] [blame] | 1705 | intent.setPackage(KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME); |
| 1706 | sendBroadcastAsUser(intent, UserHandle.SYSTEM); |
Clara Bayarri | eb3c2d3 | 2016-04-01 14:37:32 +0100 | [diff] [blame] | 1707 | } |
| 1708 | |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 1709 | @Override |
Clara Bayarri | fcd7e80 | 2016-03-10 12:58:18 +0000 | [diff] [blame] | 1710 | public void onProvideKeyboardShortcuts( |
| 1711 | List<KeyboardShortcutGroup> data, Menu menu, int deviceId) { |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 1712 | if (menu == null) { |
| 1713 | return; |
| 1714 | } |
| 1715 | KeyboardShortcutGroup group = null; |
| 1716 | int menuSize = menu.size(); |
| 1717 | for (int i = 0; i < menuSize; ++i) { |
| 1718 | final MenuItem item = menu.getItem(i); |
| 1719 | final CharSequence title = item.getTitle(); |
| 1720 | final char alphaShortcut = item.getAlphabeticShortcut(); |
| 1721 | if (title != null && alphaShortcut != MIN_VALUE) { |
| 1722 | if (group == null) { |
Clara Bayarri | ada88b3 | 2016-02-02 18:08:01 +0000 | [diff] [blame] | 1723 | final int resource = mApplication.getApplicationInfo().labelRes; |
| 1724 | group = new KeyboardShortcutGroup(resource != 0 ? getString(resource) : null); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 1725 | } |
| 1726 | group.addItem(new KeyboardShortcutInfo( |
| 1727 | title, alphaShortcut, KeyEvent.META_CTRL_ON)); |
| 1728 | } |
| 1729 | } |
| 1730 | if (group != null) { |
| 1731 | data.add(group); |
| 1732 | } |
| 1733 | } |
| 1734 | |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 1735 | /** |
Dianne Hackborn | 17f6935 | 2015-07-17 18:04:14 -0700 | [diff] [blame] | 1736 | * Ask to have the current assistant shown to the user. This only works if the calling |
| 1737 | * activity is the current foreground activity. It is the same as calling |
| 1738 | * {@link android.service.voice.VoiceInteractionService#showSession |
| 1739 | * VoiceInteractionService.showSession} and requesting all of the possible context. |
| 1740 | * The receiver will always see |
| 1741 | * {@link android.service.voice.VoiceInteractionSession#SHOW_SOURCE_APPLICATION} set. |
| 1742 | * @return Returns true if the assistant was successfully invoked, else false. For example |
| 1743 | * false will be returned if the caller is not the current top activity. |
| 1744 | */ |
| 1745 | public boolean showAssist(Bundle args) { |
| 1746 | try { |
| 1747 | return ActivityManagerNative.getDefault().showAssistFromActivity(mToken, args); |
| 1748 | } catch (RemoteException e) { |
| 1749 | } |
| 1750 | return false; |
| 1751 | } |
| 1752 | |
| 1753 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1754 | * Called when you are no longer visible to the user. You will next |
| 1755 | * receive either {@link #onRestart}, {@link #onDestroy}, or nothing, |
| 1756 | * depending on later user activity. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1757 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1758 | * <p><em>Derived classes must call through to the super class's |
| 1759 | * implementation of this method. If they do not, an exception will be |
| 1760 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1761 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1762 | * @see #onRestart |
| 1763 | * @see #onResume |
| 1764 | * @see #onSaveInstanceState |
| 1765 | * @see #onDestroy |
| 1766 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1767 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1768 | protected void onStop() { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1769 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStop " + this); |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 1770 | if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 1771 | mActivityTransitionState.onStop(); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1772 | getApplication().dispatchActivityStopped(this); |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 1773 | mTranslucentCallback = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1774 | mCalled = true; |
| 1775 | } |
| 1776 | |
| 1777 | /** |
| 1778 | * Perform any final cleanup before an activity is destroyed. This can |
| 1779 | * happen either because the activity is finishing (someone called |
| 1780 | * {@link #finish} on it, or because the system is temporarily destroying |
| 1781 | * this instance of the activity to save space. You can distinguish |
| 1782 | * between these two scenarios with the {@link #isFinishing} method. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1783 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1784 | * <p><em>Note: do not count on this method being called as a place for |
| 1785 | * saving data! For example, if an activity is editing data in a content |
| 1786 | * provider, those edits should be committed in either {@link #onPause} or |
| 1787 | * {@link #onSaveInstanceState}, not here.</em> This method is usually implemented to |
| 1788 | * free resources like threads that are associated with an activity, so |
| 1789 | * that a destroyed activity does not leave such things around while the |
| 1790 | * rest of its application is still running. There are situations where |
| 1791 | * the system will simply kill the activity's hosting process without |
| 1792 | * calling this method (or any others) in it, so it should not be used to |
| 1793 | * do things that are intended to remain around after the process goes |
| 1794 | * away. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1795 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1796 | * <p><em>Derived classes must call through to the super class's |
| 1797 | * implementation of this method. If they do not, an exception will be |
| 1798 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1799 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1800 | * @see #onPause |
| 1801 | * @see #onStop |
| 1802 | * @see #finish |
| 1803 | * @see #isFinishing |
| 1804 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1805 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1806 | protected void onDestroy() { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1807 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1808 | mCalled = true; |
| 1809 | |
| 1810 | // dismiss any dialogs we are managing. |
| 1811 | if (mManagedDialogs != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1812 | final int numDialogs = mManagedDialogs.size(); |
| 1813 | for (int i = 0; i < numDialogs; i++) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1814 | final ManagedDialog md = mManagedDialogs.valueAt(i); |
| 1815 | if (md.mDialog.isShowing()) { |
| 1816 | md.mDialog.dismiss(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1817 | } |
| 1818 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1819 | mManagedDialogs = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1820 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1821 | |
| 1822 | // close any cursors we are managing. |
Makoto Onuki | 2f6a018 | 2010-02-22 13:26:59 -0800 | [diff] [blame] | 1823 | synchronized (mManagedCursors) { |
| 1824 | int numCursors = mManagedCursors.size(); |
| 1825 | for (int i = 0; i < numCursors; i++) { |
| 1826 | ManagedCursor c = mManagedCursors.get(i); |
| 1827 | if (c != null) { |
| 1828 | c.mCursor.close(); |
| 1829 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1830 | } |
Makoto Onuki | 2f6a018 | 2010-02-22 13:26:59 -0800 | [diff] [blame] | 1831 | mManagedCursors.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1832 | } |
Amith Yamasani | 4986044 | 2010-03-17 20:54:10 -0700 | [diff] [blame] | 1833 | |
| 1834 | // Close any open search dialog |
| 1835 | if (mSearchManager != null) { |
| 1836 | mSearchManager.stopSearch(); |
| 1837 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1838 | |
Chris Banes | 21b2577 | 2016-01-04 20:41:59 +0000 | [diff] [blame] | 1839 | if (mActionBar != null) { |
| 1840 | mActionBar.onDestroy(); |
| 1841 | } |
| 1842 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1843 | getApplication().dispatchActivityDestroyed(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1844 | } |
| 1845 | |
| 1846 | /** |
Dianne Hackborn | ebc15ef | 2013-10-09 17:36:57 -0700 | [diff] [blame] | 1847 | * Report to the system that your app is now fully drawn, purely for diagnostic |
| 1848 | * purposes (calling it does not impact the visible behavior of the activity). |
| 1849 | * This is only used to help instrument application launch times, so that the |
| 1850 | * app can report when it is fully in a usable state; without this, the only thing |
| 1851 | * the system itself can determine is the point at which the activity's window |
| 1852 | * is <em>first</em> drawn and displayed. To participate in app launch time |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 1853 | * measurement, you should always call this method after first launch (when |
Dianne Hackborn | ebc15ef | 2013-10-09 17:36:57 -0700 | [diff] [blame] | 1854 | * {@link #onCreate(android.os.Bundle)} is called), at the point where you have |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 1855 | * entirely drawn your UI and populated with all of the significant data. You |
| 1856 | * can safely call this method any time after first launch as well, in which case |
| 1857 | * it will simply be ignored. |
| 1858 | */ |
| 1859 | public void reportFullyDrawn() { |
| 1860 | if (mDoReportFullyDrawn) { |
| 1861 | mDoReportFullyDrawn = false; |
| 1862 | try { |
| 1863 | ActivityManagerNative.getDefault().reportActivityFullyDrawn(mToken); |
| 1864 | } catch (RemoteException e) { |
| 1865 | } |
| 1866 | } |
| 1867 | } |
| 1868 | |
| 1869 | /** |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1870 | * Called by the system when the activity changes from fullscreen mode to multi-window mode and |
| 1871 | * visa-versa. |
| 1872 | * @see android.R.attr#resizeableActivity |
| 1873 | * |
Andrii Kulian | 933076d | 2016-03-29 17:04:42 -0700 | [diff] [blame] | 1874 | * @param isInMultiWindowMode True if the activity is in multi-window mode. |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1875 | */ |
Andrii Kulian | 933076d | 2016-03-29 17:04:42 -0700 | [diff] [blame] | 1876 | public void onMultiWindowModeChanged(boolean isInMultiWindowMode) { |
Adam Powell | 858cf03 | 2016-05-09 15:45:37 -0700 | [diff] [blame] | 1877 | // Left deliberately empty. There should be no side effects if a direct |
| 1878 | // subclass of Activity does not call super. |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | /** |
| 1882 | * Returns true if the activity is currently in multi-window mode. |
| 1883 | * @see android.R.attr#resizeableActivity |
| 1884 | * |
| 1885 | * @return True if the activity is in multi-window mode. |
| 1886 | */ |
Andrii Kulian | 933076d | 2016-03-29 17:04:42 -0700 | [diff] [blame] | 1887 | public boolean isInMultiWindowMode() { |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1888 | try { |
Andrii Kulian | 933076d | 2016-03-29 17:04:42 -0700 | [diff] [blame] | 1889 | return ActivityManagerNative.getDefault().isInMultiWindowMode(mToken); |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1890 | } catch (RemoteException e) { |
| 1891 | } |
| 1892 | return false; |
| 1893 | } |
| 1894 | |
| 1895 | /** |
| 1896 | * Called by the system when the activity changes to and from picture-in-picture mode. |
| 1897 | * @see android.R.attr#supportsPictureInPicture |
| 1898 | * |
Andrii Kulian | 933076d | 2016-03-29 17:04:42 -0700 | [diff] [blame] | 1899 | * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode. |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1900 | */ |
Andrii Kulian | 933076d | 2016-03-29 17:04:42 -0700 | [diff] [blame] | 1901 | public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) { |
Adam Powell | 858cf03 | 2016-05-09 15:45:37 -0700 | [diff] [blame] | 1902 | // Left deliberately empty. There should be no side effects if a direct |
| 1903 | // subclass of Activity does not call super. |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1904 | } |
| 1905 | |
| 1906 | /** |
| 1907 | * Returns true if the activity is currently in picture-in-picture mode. |
| 1908 | * @see android.R.attr#supportsPictureInPicture |
| 1909 | * |
| 1910 | * @return True if the activity is in picture-in-picture mode. |
| 1911 | */ |
Andrii Kulian | 933076d | 2016-03-29 17:04:42 -0700 | [diff] [blame] | 1912 | public boolean isInPictureInPictureMode() { |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1913 | try { |
Andrii Kulian | 933076d | 2016-03-29 17:04:42 -0700 | [diff] [blame] | 1914 | return ActivityManagerNative.getDefault().isInPictureInPictureMode(mToken); |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1915 | } catch (RemoteException e) { |
| 1916 | } |
| 1917 | return false; |
| 1918 | } |
| 1919 | |
| 1920 | /** |
Wale Ogunwale | 9c604c7 | 2015-12-06 18:42:57 -0800 | [diff] [blame] | 1921 | * Puts the activity in picture-in-picture mode. |
| 1922 | * @see android.R.attr#supportsPictureInPicture |
| 1923 | */ |
Andrii Kulian | 933076d | 2016-03-29 17:04:42 -0700 | [diff] [blame] | 1924 | public void enterPictureInPictureMode() { |
Wale Ogunwale | 9c604c7 | 2015-12-06 18:42:57 -0800 | [diff] [blame] | 1925 | try { |
Andrii Kulian | 933076d | 2016-03-29 17:04:42 -0700 | [diff] [blame] | 1926 | ActivityManagerNative.getDefault().enterPictureInPictureMode(mToken); |
Wale Ogunwale | 9c604c7 | 2015-12-06 18:42:57 -0800 | [diff] [blame] | 1927 | } catch (RemoteException e) { |
| 1928 | } |
| 1929 | } |
| 1930 | |
| 1931 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1932 | * Called by the system when the device configuration changes while your |
| 1933 | * activity is running. Note that this will <em>only</em> be called if |
| 1934 | * you have selected configurations you would like to handle with the |
| 1935 | * {@link android.R.attr#configChanges} attribute in your manifest. If |
| 1936 | * any configuration change occurs that is not selected to be reported |
| 1937 | * by that attribute, then instead of reporting it the system will stop |
| 1938 | * and restart the activity (to have it launched with the new |
| 1939 | * configuration). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1940 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1941 | * <p>At the time that this function has been called, your Resources |
| 1942 | * object will have been updated to return resource values matching the |
| 1943 | * new configuration. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1944 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1945 | * @param newConfig The new device configuration. |
| 1946 | */ |
| 1947 | public void onConfigurationChanged(Configuration newConfig) { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1948 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onConfigurationChanged " + this + ": " + newConfig); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1949 | mCalled = true; |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 1950 | |
Dianne Hackborn | 9d07180 | 2010-12-08 14:49:15 -0800 | [diff] [blame] | 1951 | mFragments.dispatchConfigurationChanged(newConfig); |
| 1952 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1953 | if (mWindow != null) { |
| 1954 | // Pass the configuration changed event to the window |
| 1955 | mWindow.onConfigurationChanged(newConfig); |
| 1956 | } |
Adam Powell | 45c0b19 | 2011-07-28 15:11:57 -0700 | [diff] [blame] | 1957 | |
| 1958 | if (mActionBar != null) { |
| 1959 | // Do this last; the action bar will need to access |
| 1960 | // view changes from above. |
| 1961 | mActionBar.onConfigurationChanged(newConfig); |
| 1962 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1963 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1964 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1965 | /** |
| 1966 | * If this activity is being destroyed because it can not handle a |
| 1967 | * configuration parameter being changed (and thus its |
| 1968 | * {@link #onConfigurationChanged(Configuration)} method is |
| 1969 | * <em>not</em> being called), then you can use this method to discover |
| 1970 | * the set of changes that have occurred while in the process of being |
| 1971 | * destroyed. Note that there is no guarantee that these will be |
| 1972 | * accurate (other changes could have happened at any time), so you should |
| 1973 | * only use this as an optimization hint. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1974 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1975 | * @return Returns a bit field of the configuration parameters that are |
| 1976 | * changing, as defined by the {@link android.content.res.Configuration} |
| 1977 | * class. |
| 1978 | */ |
| 1979 | public int getChangingConfigurations() { |
| 1980 | return mConfigChangeFlags; |
| 1981 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1982 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1983 | /** |
| 1984 | * Retrieve the non-configuration instance data that was previously |
| 1985 | * returned by {@link #onRetainNonConfigurationInstance()}. This will |
| 1986 | * be available from the initial {@link #onCreate} and |
| 1987 | * {@link #onStart} calls to the new instance, allowing you to extract |
| 1988 | * any useful dynamic state from the previous instance. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1989 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1990 | * <p>Note that the data you retrieve here should <em>only</em> be used |
| 1991 | * as an optimization for handling configuration changes. You should always |
| 1992 | * be able to handle getting a null pointer back, and an activity must |
| 1993 | * still be able to restore itself to its previous state (through the |
| 1994 | * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this |
| 1995 | * function returns null. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1996 | * |
Adam Powell | c4df6bc | 2016-03-09 15:11:37 -0800 | [diff] [blame] | 1997 | * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 1998 | * {@link Fragment#setRetainInstance(boolean)} instead; this is also |
Adam Powell | c4df6bc | 2016-03-09 15:11:37 -0800 | [diff] [blame] | 1999 | * available on older platforms through the Android support libraries. |
| 2000 | * |
| 2001 | * @return the object previously returned by {@link #onRetainNonConfigurationInstance()} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2002 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 2003 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2004 | public Object getLastNonConfigurationInstance() { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2005 | return mLastNonConfigurationInstances != null |
| 2006 | ? mLastNonConfigurationInstances.activity : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2007 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2008 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2009 | /** |
| 2010 | * Called by the system, as part of destroying an |
| 2011 | * activity due to a configuration change, when it is known that a new |
| 2012 | * instance will immediately be created for the new configuration. You |
| 2013 | * can return any object you like here, including the activity instance |
| 2014 | * itself, which can later be retrieved by calling |
| 2015 | * {@link #getLastNonConfigurationInstance()} in the new activity |
| 2016 | * instance. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2017 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 2018 | * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 2019 | * or later, consider instead using a {@link Fragment} with |
| 2020 | * {@link Fragment#setRetainInstance(boolean) |
| 2021 | * Fragment.setRetainInstance(boolean}.</em> |
| 2022 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2023 | * <p>This function is called purely as an optimization, and you must |
| 2024 | * not rely on it being called. When it is called, a number of guarantees |
| 2025 | * will be made to help optimize configuration switching: |
| 2026 | * <ul> |
| 2027 | * <li> The function will be called between {@link #onStop} and |
| 2028 | * {@link #onDestroy}. |
| 2029 | * <li> A new instance of the activity will <em>always</em> be immediately |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 2030 | * created after this one's {@link #onDestroy()} is called. In particular, |
| 2031 | * <em>no</em> messages will be dispatched during this time (when the returned |
| 2032 | * object does not have an activity to be associated with). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2033 | * <li> The object you return here will <em>always</em> be available from |
| 2034 | * the {@link #getLastNonConfigurationInstance()} method of the following |
| 2035 | * activity instance as described there. |
| 2036 | * </ul> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2037 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2038 | * <p>These guarantees are designed so that an activity can use this API |
| 2039 | * to propagate extensive state from the old to new activity instance, from |
| 2040 | * loaded bitmaps, to network connections, to evenly actively running |
| 2041 | * threads. Note that you should <em>not</em> propagate any data that |
| 2042 | * may change based on the configuration, including any data loaded from |
| 2043 | * resources such as strings, layouts, or drawables. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2044 | * |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 2045 | * <p>The guarantee of no message handling during the switch to the next |
| 2046 | * activity simplifies use with active objects. For example if your retained |
| 2047 | * state is an {@link android.os.AsyncTask} you are guaranteed that its |
| 2048 | * call back functions (like {@link android.os.AsyncTask#onPostExecute}) will |
| 2049 | * not be called from the call here until you execute the next instance's |
| 2050 | * {@link #onCreate(Bundle)}. (Note however that there is of course no such |
| 2051 | * guarantee for {@link android.os.AsyncTask#doInBackground} since that is |
| 2052 | * running in a separate thread.) |
| 2053 | * |
Adam Powell | c4df6bc | 2016-03-09 15:11:37 -0800 | [diff] [blame] | 2054 | * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 2055 | * {@link Fragment#setRetainInstance(boolean)} instead; this is also |
Adam Powell | c4df6bc | 2016-03-09 15:11:37 -0800 | [diff] [blame] | 2056 | * available on older platforms through the Android support libraries. |
| 2057 | * |
| 2058 | * @return any Object holding the desired state to propagate to the |
| 2059 | * next activity instance |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2060 | */ |
| 2061 | public Object onRetainNonConfigurationInstance() { |
| 2062 | return null; |
| 2063 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2064 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2065 | /** |
| 2066 | * Retrieve the non-configuration instance data that was previously |
| 2067 | * returned by {@link #onRetainNonConfigurationChildInstances()}. This will |
| 2068 | * be available from the initial {@link #onCreate} and |
| 2069 | * {@link #onStart} calls to the new instance, allowing you to extract |
| 2070 | * any useful dynamic state from the previous instance. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2071 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2072 | * <p>Note that the data you retrieve here should <em>only</em> be used |
| 2073 | * as an optimization for handling configuration changes. You should always |
| 2074 | * be able to handle getting a null pointer back, and an activity must |
| 2075 | * still be able to restore itself to its previous state (through the |
| 2076 | * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this |
| 2077 | * function returns null. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2078 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2079 | * @return Returns the object previously returned by |
| 2080 | * {@link #onRetainNonConfigurationChildInstances()} |
| 2081 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 2082 | @Nullable |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2083 | HashMap<String, Object> getLastNonConfigurationChildInstances() { |
| 2084 | return mLastNonConfigurationInstances != null |
| 2085 | ? mLastNonConfigurationInstances.children : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2086 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2087 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2088 | /** |
| 2089 | * This method is similar to {@link #onRetainNonConfigurationInstance()} except that |
| 2090 | * it should return either a mapping from child activity id strings to arbitrary objects, |
| 2091 | * or null. This method is intended to be used by Activity framework subclasses that control a |
| 2092 | * set of child activities, such as ActivityGroup. The same guarantees and restrictions apply |
| 2093 | * as for {@link #onRetainNonConfigurationInstance()}. The default implementation returns null. |
| 2094 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 2095 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2096 | HashMap<String,Object> onRetainNonConfigurationChildInstances() { |
| 2097 | return null; |
| 2098 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2099 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2100 | NonConfigurationInstances retainNonConfigurationInstances() { |
| 2101 | Object activity = onRetainNonConfigurationInstance(); |
| 2102 | HashMap<String, Object> children = onRetainNonConfigurationChildInstances(); |
Adam Powell | 44ba79e | 2016-02-04 16:20:37 -0800 | [diff] [blame] | 2103 | FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig(); |
Adam Powell | cbade7f | 2016-04-15 11:14:37 -0700 | [diff] [blame] | 2104 | |
| 2105 | // We're already stopped but we've been asked to retain. |
| 2106 | // Our fragments are taken care of but we need to mark the loaders for retention. |
| 2107 | // In order to do this correctly we need to restart the loaders first before |
| 2108 | // handing them off to the next activity. |
| 2109 | mFragments.doLoaderStart(); |
| 2110 | mFragments.doLoaderStop(true); |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 2111 | ArrayMap<String, LoaderManager> loaders = mFragments.retainLoaderNonConfig(); |
Adam Powell | cbade7f | 2016-04-15 11:14:37 -0700 | [diff] [blame] | 2112 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 2113 | if (activity == null && children == null && fragments == null && loaders == null |
Dianne Hackborn | 20d9474 | 2014-05-29 18:35:45 -0700 | [diff] [blame] | 2114 | && mVoiceInteractor == null) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2115 | return null; |
| 2116 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2117 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2118 | NonConfigurationInstances nci = new NonConfigurationInstances(); |
| 2119 | nci.activity = activity; |
| 2120 | nci.children = children; |
| 2121 | nci.fragments = fragments; |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 2122 | nci.loaders = loaders; |
Dianne Hackborn | 57dd737 | 2015-07-27 18:11:14 -0700 | [diff] [blame] | 2123 | if (mVoiceInteractor != null) { |
| 2124 | mVoiceInteractor.retainInstance(); |
| 2125 | nci.voiceInteractor = mVoiceInteractor; |
| 2126 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2127 | return nci; |
| 2128 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 2129 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2130 | public void onLowMemory() { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 2131 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onLowMemory " + this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2132 | mCalled = true; |
Dianne Hackborn | 9d07180 | 2010-12-08 14:49:15 -0800 | [diff] [blame] | 2133 | mFragments.dispatchLowMemory(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2134 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 2135 | |
| 2136 | public void onTrimMemory(int level) { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 2137 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onTrimMemory " + this + ": " + level); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 2138 | mCalled = true; |
| 2139 | mFragments.dispatchTrimMemory(level); |
| 2140 | } |
| 2141 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2142 | /** |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 2143 | * Return the FragmentManager for interacting with fragments associated |
| 2144 | * with this activity. |
| 2145 | */ |
| 2146 | public FragmentManager getFragmentManager() { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 2147 | return mFragments.getFragmentManager(); |
Dianne Hackborn | 9e14e9f3 | 2010-07-14 11:07:38 -0700 | [diff] [blame] | 2148 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2149 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 2150 | /** |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 2151 | * Called when a Fragment is being attached to this activity, immediately |
| 2152 | * after the call to its {@link Fragment#onAttach Fragment.onAttach()} |
| 2153 | * method and before {@link Fragment#onCreate Fragment.onCreate()}. |
| 2154 | */ |
| 2155 | public void onAttachFragment(Fragment fragment) { |
| 2156 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2157 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 2158 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2159 | * Wrapper around |
| 2160 | * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)} |
| 2161 | * that gives the resulting {@link Cursor} to call |
| 2162 | * {@link #startManagingCursor} so that the activity will manage its |
| 2163 | * lifecycle for you. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2164 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 2165 | * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 2166 | * or later, consider instead using {@link LoaderManager} instead, available |
| 2167 | * via {@link #getLoaderManager()}.</em> |
| 2168 | * |
Joe Fernandez | 8ef6970 | 2011-09-02 14:32:22 -0700 | [diff] [blame] | 2169 | * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using |
| 2170 | * this method, because the activity will do that for you at the appropriate time. However, if |
| 2171 | * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will |
| 2172 | * not</em> automatically close the cursor and, in that case, you must call |
| 2173 | * {@link Cursor#close()}.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2174 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2175 | * @param uri The URI of the content provider to query. |
| 2176 | * @param projection List of columns to return. |
| 2177 | * @param selection SQL WHERE clause. |
| 2178 | * @param sortOrder SQL ORDER BY clause. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2179 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2180 | * @return The Cursor that was returned by query(). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2181 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2182 | * @see ContentResolver#query(android.net.Uri , String[], String, String[], String) |
| 2183 | * @see #startManagingCursor |
| 2184 | * @hide |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 2185 | * |
| 2186 | * @deprecated Use {@link CursorLoader} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2187 | */ |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 2188 | @Deprecated |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 2189 | public final Cursor managedQuery(Uri uri, String[] projection, String selection, |
| 2190 | String sortOrder) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2191 | Cursor c = getContentResolver().query(uri, projection, selection, null, sortOrder); |
| 2192 | if (c != null) { |
| 2193 | startManagingCursor(c); |
| 2194 | } |
| 2195 | return c; |
| 2196 | } |
| 2197 | |
| 2198 | /** |
| 2199 | * Wrapper around |
| 2200 | * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)} |
| 2201 | * that gives the resulting {@link Cursor} to call |
| 2202 | * {@link #startManagingCursor} so that the activity will manage its |
| 2203 | * lifecycle for you. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2204 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 2205 | * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 2206 | * or later, consider instead using {@link LoaderManager} instead, available |
| 2207 | * via {@link #getLoaderManager()}.</em> |
| 2208 | * |
Joe Fernandez | 8ef6970 | 2011-09-02 14:32:22 -0700 | [diff] [blame] | 2209 | * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using |
| 2210 | * this method, because the activity will do that for you at the appropriate time. However, if |
| 2211 | * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will |
| 2212 | * not</em> automatically close the cursor and, in that case, you must call |
| 2213 | * {@link Cursor#close()}.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2214 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2215 | * @param uri The URI of the content provider to query. |
| 2216 | * @param projection List of columns to return. |
| 2217 | * @param selection SQL WHERE clause. |
| 2218 | * @param selectionArgs The arguments to selection, if any ?s are pesent |
| 2219 | * @param sortOrder SQL ORDER BY clause. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2220 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2221 | * @return The Cursor that was returned by query(). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2222 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2223 | * @see ContentResolver#query(android.net.Uri , String[], String, String[], String) |
| 2224 | * @see #startManagingCursor |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 2225 | * |
| 2226 | * @deprecated Use {@link CursorLoader} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2227 | */ |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 2228 | @Deprecated |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 2229 | public final Cursor managedQuery(Uri uri, String[] projection, String selection, |
| 2230 | String[] selectionArgs, String sortOrder) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2231 | Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder); |
| 2232 | if (c != null) { |
| 2233 | startManagingCursor(c); |
| 2234 | } |
| 2235 | return c; |
| 2236 | } |
| 2237 | |
| 2238 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2239 | * This method allows the activity to take care of managing the given |
| 2240 | * {@link Cursor}'s lifecycle for you based on the activity's lifecycle. |
| 2241 | * That is, when the activity is stopped it will automatically call |
| 2242 | * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted |
| 2243 | * it will call {@link Cursor#requery} for you. When the activity is |
| 2244 | * destroyed, all managed Cursors will be closed automatically. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2245 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 2246 | * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 2247 | * or later, consider instead using {@link LoaderManager} instead, available |
| 2248 | * via {@link #getLoaderManager()}.</em> |
| 2249 | * |
Joe Fernandez | 8ef6970 | 2011-09-02 14:32:22 -0700 | [diff] [blame] | 2250 | * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on cursor obtained from |
| 2251 | * {@link #managedQuery}, because the activity will do that for you at the appropriate time. |
| 2252 | * However, if you call {@link #stopManagingCursor} on a cursor from a managed query, the system |
| 2253 | * <em>will not</em> automatically close the cursor and, in that case, you must call |
| 2254 | * {@link Cursor#close()}.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2255 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2256 | * @param c The Cursor to be managed. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2257 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2258 | * @see #managedQuery(android.net.Uri , String[], String, String[], String) |
| 2259 | * @see #stopManagingCursor |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 2260 | * |
| 2261 | * @deprecated Use the new {@link android.content.CursorLoader} class with |
| 2262 | * {@link LoaderManager} instead; this is also |
| 2263 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2264 | */ |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 2265 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2266 | public void startManagingCursor(Cursor c) { |
| 2267 | synchronized (mManagedCursors) { |
| 2268 | mManagedCursors.add(new ManagedCursor(c)); |
| 2269 | } |
| 2270 | } |
| 2271 | |
| 2272 | /** |
| 2273 | * Given a Cursor that was previously given to |
| 2274 | * {@link #startManagingCursor}, stop the activity's management of that |
| 2275 | * cursor. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2276 | * |
Joe Fernandez | 8ef6970 | 2011-09-02 14:32:22 -0700 | [diff] [blame] | 2277 | * <p><strong>Warning:</strong> After calling this method on a cursor from a managed query, |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2278 | * the system <em>will not</em> automatically close the cursor and you must call |
Joe Fernandez | 8ef6970 | 2011-09-02 14:32:22 -0700 | [diff] [blame] | 2279 | * {@link Cursor#close()}.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2280 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2281 | * @param c The Cursor that was being managed. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2282 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2283 | * @see #startManagingCursor |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 2284 | * |
| 2285 | * @deprecated Use the new {@link android.content.CursorLoader} class with |
| 2286 | * {@link LoaderManager} instead; this is also |
| 2287 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2288 | */ |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 2289 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2290 | public void stopManagingCursor(Cursor c) { |
| 2291 | synchronized (mManagedCursors) { |
| 2292 | final int N = mManagedCursors.size(); |
| 2293 | for (int i=0; i<N; i++) { |
| 2294 | ManagedCursor mc = mManagedCursors.get(i); |
| 2295 | if (mc.mCursor == c) { |
| 2296 | mManagedCursors.remove(i); |
| 2297 | break; |
| 2298 | } |
| 2299 | } |
| 2300 | } |
| 2301 | } |
| 2302 | |
| 2303 | /** |
Dianne Hackborn | 3c4c2b7 | 2010-10-05 18:07:54 -0700 | [diff] [blame] | 2304 | * @deprecated As of {@link android.os.Build.VERSION_CODES#GINGERBREAD} |
| 2305 | * this is a no-op. |
Dianne Hackborn | 4f3867e | 2010-12-14 22:09:51 -0800 | [diff] [blame] | 2306 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2307 | */ |
Dianne Hackborn | d3efa39 | 2010-09-01 17:34:12 -0700 | [diff] [blame] | 2308 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2309 | public void setPersistent(boolean isPersistent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2310 | } |
| 2311 | |
| 2312 | /** |
| 2313 | * Finds a view that was identified by the id attribute from the XML that |
| 2314 | * was processed in {@link #onCreate}. |
| 2315 | * |
| 2316 | * @return The view if found or null otherwise. |
| 2317 | */ |
Scott Kennedy | c051955 | 2015-02-11 15:33:10 -0800 | [diff] [blame] | 2318 | @Nullable |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 2319 | public View findViewById(@IdRes int id) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2320 | return getWindow().findViewById(id); |
| 2321 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2322 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2323 | /** |
| 2324 | * Retrieve a reference to this activity's ActionBar. |
Adam Powell | 42c0fe8 | 2010-08-10 16:36:56 -0700 | [diff] [blame] | 2325 | * |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2326 | * @return The Activity's ActionBar, or null if it does not have one. |
| 2327 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 2328 | @Nullable |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2329 | public ActionBar getActionBar() { |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2330 | initWindowDecorActionBar(); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2331 | return mActionBar; |
| 2332 | } |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2333 | |
| 2334 | /** |
| 2335 | * Set a {@link android.widget.Toolbar Toolbar} to act as the {@link ActionBar} for this |
| 2336 | * Activity window. |
| 2337 | * |
| 2338 | * <p>When set to a non-null value the {@link #getActionBar()} method will return |
| 2339 | * an {@link ActionBar} object that can be used to control the given toolbar as if it were |
| 2340 | * a traditional window decor action bar. The toolbar's menu will be populated with the |
| 2341 | * Activity's options menu and the navigation button will be wired through the standard |
| 2342 | * {@link android.R.id#home home} menu select action.</p> |
| 2343 | * |
| 2344 | * <p>In order to use a Toolbar within the Activity's window content the application |
| 2345 | * must not request the window feature {@link Window#FEATURE_ACTION_BAR FEATURE_ACTION_BAR}.</p> |
| 2346 | * |
Chris Banes | c7d6c32 | 2016-01-27 14:09:16 +0000 | [diff] [blame] | 2347 | * @param toolbar Toolbar to set as the Activity's action bar, or {@code null} to clear it |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2348 | */ |
Adam Powell | 3778014 | 2014-06-01 13:31:00 -0700 | [diff] [blame] | 2349 | public void setActionBar(@Nullable Toolbar toolbar) { |
Chris Banes | 21b2577 | 2016-01-04 20:41:59 +0000 | [diff] [blame] | 2350 | final ActionBar ab = getActionBar(); |
| 2351 | if (ab instanceof WindowDecorActionBar) { |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2352 | throw new IllegalStateException("This Activity already has an action bar supplied " + |
| 2353 | "by the window decor. Do not request Window.FEATURE_ACTION_BAR and set " + |
| 2354 | "android:windowActionBar to false in your theme to use a Toolbar instead."); |
| 2355 | } |
Chris Banes | 21b2577 | 2016-01-04 20:41:59 +0000 | [diff] [blame] | 2356 | |
| 2357 | // If we reach here then we're setting a new action bar |
| 2358 | // First clear out the MenuInflater to make sure that it is valid for the new Action Bar |
Chris Banes | 601bb99 | 2015-06-22 10:05:38 +0100 | [diff] [blame] | 2359 | mMenuInflater = null; |
| 2360 | |
Chris Banes | 21b2577 | 2016-01-04 20:41:59 +0000 | [diff] [blame] | 2361 | // If we have an action bar currently, destroy it |
| 2362 | if (ab != null) { |
| 2363 | ab.onDestroy(); |
| 2364 | } |
| 2365 | |
Chris Banes | c7d6c32 | 2016-01-27 14:09:16 +0000 | [diff] [blame] | 2366 | if (toolbar != null) { |
| 2367 | final ToolbarActionBar tbab = new ToolbarActionBar(toolbar, getTitle(), this); |
| 2368 | mActionBar = tbab; |
| 2369 | mWindow.setCallback(tbab.getWrappedWindowCallback()); |
| 2370 | } else { |
| 2371 | mActionBar = null; |
| 2372 | // Re-set the original window callback since we may have already set a Toolbar wrapper |
| 2373 | mWindow.setCallback(this); |
| 2374 | } |
| 2375 | |
| 2376 | invalidateOptionsMenu(); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2377 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2378 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2379 | /** |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2380 | * Creates a new ActionBar, locates the inflated ActionBarView, |
| 2381 | * initializes the ActionBar with the view, and sets mActionBar. |
| 2382 | */ |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2383 | private void initWindowDecorActionBar() { |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 2384 | Window window = getWindow(); |
Adam Powell | a593d98 | 2011-05-13 14:09:54 -0700 | [diff] [blame] | 2385 | |
| 2386 | // Initializing the window decor can change window feature flags. |
| 2387 | // Make sure that we have the correct set before performing the test below. |
| 2388 | window.getDecorView(); |
| 2389 | |
Adam Powell | 9b4c804 | 2010-08-10 15:36:44 -0700 | [diff] [blame] | 2390 | if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) { |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2391 | return; |
| 2392 | } |
Adam Powell | 04fe6eb | 2013-05-31 14:39:48 -0700 | [diff] [blame] | 2393 | |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2394 | mActionBar = new WindowDecorActionBar(this); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 2395 | mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp); |
Adam Powell | 04fe6eb | 2013-05-31 14:39:48 -0700 | [diff] [blame] | 2396 | |
| 2397 | mWindow.setDefaultIcon(mActivityInfo.getIconResource()); |
| 2398 | mWindow.setDefaultLogo(mActivityInfo.getLogoResource()); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2399 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2400 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2401 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2402 | * Set the activity content from a layout resource. The resource will be |
| 2403 | * inflated, adding all top-level views to the activity. |
Romain Guy | 482b34a6 | 2011-01-20 10:59:28 -0800 | [diff] [blame] | 2404 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2405 | * @param layoutResID Resource ID to be inflated. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2406 | * |
Romain Guy | 482b34a6 | 2011-01-20 10:59:28 -0800 | [diff] [blame] | 2407 | * @see #setContentView(android.view.View) |
| 2408 | * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2409 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 2410 | public void setContentView(@LayoutRes int layoutResID) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2411 | getWindow().setContentView(layoutResID); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2412 | initWindowDecorActionBar(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2413 | } |
| 2414 | |
| 2415 | /** |
| 2416 | * Set the activity content to an explicit view. This view is placed |
| 2417 | * directly into the activity's view hierarchy. It can itself be a complex |
Romain Guy | 482b34a6 | 2011-01-20 10:59:28 -0800 | [diff] [blame] | 2418 | * view hierarchy. When calling this method, the layout parameters of the |
| 2419 | * specified view are ignored. Both the width and the height of the view are |
| 2420 | * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use |
| 2421 | * your own layout parameters, invoke |
| 2422 | * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)} |
| 2423 | * instead. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2424 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2425 | * @param view The desired content to display. |
Romain Guy | 482b34a6 | 2011-01-20 10:59:28 -0800 | [diff] [blame] | 2426 | * |
| 2427 | * @see #setContentView(int) |
| 2428 | * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2429 | */ |
| 2430 | public void setContentView(View view) { |
| 2431 | getWindow().setContentView(view); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2432 | initWindowDecorActionBar(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2433 | } |
| 2434 | |
| 2435 | /** |
| 2436 | * Set the activity content to an explicit view. This view is placed |
| 2437 | * directly into the activity's view hierarchy. It can itself be a complex |
Romain Guy | 482b34a6 | 2011-01-20 10:59:28 -0800 | [diff] [blame] | 2438 | * view hierarchy. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2439 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2440 | * @param view The desired content to display. |
| 2441 | * @param params Layout parameters for the view. |
Romain Guy | 482b34a6 | 2011-01-20 10:59:28 -0800 | [diff] [blame] | 2442 | * |
| 2443 | * @see #setContentView(android.view.View) |
| 2444 | * @see #setContentView(int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2445 | */ |
| 2446 | public void setContentView(View view, ViewGroup.LayoutParams params) { |
| 2447 | getWindow().setContentView(view, params); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2448 | initWindowDecorActionBar(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2449 | } |
| 2450 | |
| 2451 | /** |
| 2452 | * Add an additional content view to the activity. Added after any existing |
| 2453 | * ones in the activity -- existing views are NOT removed. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2454 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2455 | * @param view The desired content to display. |
| 2456 | * @param params Layout parameters for the view. |
| 2457 | */ |
| 2458 | public void addContentView(View view, ViewGroup.LayoutParams params) { |
| 2459 | getWindow().addContentView(view, params); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2460 | initWindowDecorActionBar(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2461 | } |
| 2462 | |
| 2463 | /** |
Adam Powell | cfbe9be | 2013-11-06 14:58:58 -0800 | [diff] [blame] | 2464 | * Retrieve the {@link TransitionManager} responsible for default transitions in this window. |
| 2465 | * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}. |
| 2466 | * |
| 2467 | * <p>This method will return non-null after content has been initialized (e.g. by using |
| 2468 | * {@link #setContentView}) if {@link Window#FEATURE_CONTENT_TRANSITIONS} has been granted.</p> |
| 2469 | * |
| 2470 | * @return This window's content TransitionManager or null if none is set. |
| 2471 | */ |
| 2472 | public TransitionManager getContentTransitionManager() { |
| 2473 | return getWindow().getTransitionManager(); |
| 2474 | } |
| 2475 | |
| 2476 | /** |
| 2477 | * Set the {@link TransitionManager} to use for default transitions in this window. |
| 2478 | * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}. |
| 2479 | * |
| 2480 | * @param tm The TransitionManager to use for scene changes. |
| 2481 | */ |
| 2482 | public void setContentTransitionManager(TransitionManager tm) { |
| 2483 | getWindow().setTransitionManager(tm); |
| 2484 | } |
| 2485 | |
| 2486 | /** |
| 2487 | * Retrieve the {@link Scene} representing this window's current content. |
| 2488 | * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}. |
| 2489 | * |
| 2490 | * <p>This method will return null if the current content is not represented by a Scene.</p> |
| 2491 | * |
| 2492 | * @return Current Scene being shown or null |
| 2493 | */ |
| 2494 | public Scene getContentScene() { |
| 2495 | return getWindow().getContentScene(); |
| 2496 | } |
| 2497 | |
| 2498 | /** |
Dianne Hackborn | cfaf887 | 2011-01-18 13:57:54 -0800 | [diff] [blame] | 2499 | * Sets whether this activity is finished when touched outside its window's |
| 2500 | * bounds. |
| 2501 | */ |
| 2502 | public void setFinishOnTouchOutside(boolean finish) { |
| 2503 | mWindow.setCloseOnTouchOutside(finish); |
| 2504 | } |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 2505 | |
| 2506 | /** @hide */ |
| 2507 | @IntDef({ |
| 2508 | DEFAULT_KEYS_DISABLE, |
| 2509 | DEFAULT_KEYS_DIALER, |
| 2510 | DEFAULT_KEYS_SHORTCUT, |
| 2511 | DEFAULT_KEYS_SEARCH_LOCAL, |
| 2512 | DEFAULT_KEYS_SEARCH_GLOBAL}) |
| 2513 | @Retention(RetentionPolicy.SOURCE) |
| 2514 | @interface DefaultKeyMode {} |
| 2515 | |
Dianne Hackborn | cfaf887 | 2011-01-18 13:57:54 -0800 | [diff] [blame] | 2516 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2517 | * Use with {@link #setDefaultKeyMode} to turn off default handling of |
| 2518 | * keys. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2519 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2520 | * @see #setDefaultKeyMode |
| 2521 | */ |
| 2522 | static public final int DEFAULT_KEYS_DISABLE = 0; |
| 2523 | /** |
| 2524 | * Use with {@link #setDefaultKeyMode} to launch the dialer during default |
| 2525 | * key handling. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2526 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2527 | * @see #setDefaultKeyMode |
| 2528 | */ |
| 2529 | static public final int DEFAULT_KEYS_DIALER = 1; |
| 2530 | /** |
| 2531 | * Use with {@link #setDefaultKeyMode} to execute a menu shortcut in |
| 2532 | * default key handling. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2533 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2534 | * <p>That is, the user does not need to hold down the menu key to execute menu shortcuts. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2535 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2536 | * @see #setDefaultKeyMode |
| 2537 | */ |
| 2538 | static public final int DEFAULT_KEYS_SHORTCUT = 2; |
| 2539 | /** |
| 2540 | * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes |
| 2541 | * will start an application-defined search. (If the application or activity does not |
| 2542 | * actually define a search, the the keys will be ignored.) |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2543 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2544 | * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2545 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2546 | * @see #setDefaultKeyMode |
| 2547 | */ |
| 2548 | static public final int DEFAULT_KEYS_SEARCH_LOCAL = 3; |
| 2549 | |
| 2550 | /** |
| 2551 | * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes |
| 2552 | * will start a global search (typically web search, but some platforms may define alternate |
| 2553 | * methods for global search) |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2554 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2555 | * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2556 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2557 | * @see #setDefaultKeyMode |
| 2558 | */ |
| 2559 | static public final int DEFAULT_KEYS_SEARCH_GLOBAL = 4; |
| 2560 | |
| 2561 | /** |
| 2562 | * Select the default key handling for this activity. This controls what |
| 2563 | * will happen to key events that are not otherwise handled. The default |
| 2564 | * mode ({@link #DEFAULT_KEYS_DISABLE}) will simply drop them on the |
| 2565 | * floor. Other modes allow you to launch the dialer |
| 2566 | * ({@link #DEFAULT_KEYS_DIALER}), execute a shortcut in your options |
| 2567 | * menu without requiring the menu key be held down |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2568 | * ({@link #DEFAULT_KEYS_SHORTCUT}), or launch a search ({@link #DEFAULT_KEYS_SEARCH_LOCAL} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2569 | * and {@link #DEFAULT_KEYS_SEARCH_GLOBAL}). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2570 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2571 | * <p>Note that the mode selected here does not impact the default |
| 2572 | * handling of system keys, such as the "back" and "menu" keys, and your |
| 2573 | * activity and its views always get a first chance to receive and handle |
| 2574 | * all application keys. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2575 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2576 | * @param mode The desired default key mode constant. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2577 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2578 | * @see #DEFAULT_KEYS_DISABLE |
| 2579 | * @see #DEFAULT_KEYS_DIALER |
| 2580 | * @see #DEFAULT_KEYS_SHORTCUT |
| 2581 | * @see #DEFAULT_KEYS_SEARCH_LOCAL |
| 2582 | * @see #DEFAULT_KEYS_SEARCH_GLOBAL |
| 2583 | * @see #onKeyDown |
| 2584 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 2585 | public final void setDefaultKeyMode(@DefaultKeyMode int mode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2586 | mDefaultKeyMode = mode; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2587 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2588 | // Some modes use a SpannableStringBuilder to track & dispatch input events |
| 2589 | // This list must remain in sync with the switch in onKeyDown() |
| 2590 | switch (mode) { |
| 2591 | case DEFAULT_KEYS_DISABLE: |
| 2592 | case DEFAULT_KEYS_SHORTCUT: |
| 2593 | mDefaultKeySsb = null; // not used in these modes |
| 2594 | break; |
| 2595 | case DEFAULT_KEYS_DIALER: |
| 2596 | case DEFAULT_KEYS_SEARCH_LOCAL: |
| 2597 | case DEFAULT_KEYS_SEARCH_GLOBAL: |
| 2598 | mDefaultKeySsb = new SpannableStringBuilder(); |
| 2599 | Selection.setSelection(mDefaultKeySsb,0); |
| 2600 | break; |
| 2601 | default: |
| 2602 | throw new IllegalArgumentException(); |
| 2603 | } |
| 2604 | } |
| 2605 | |
| 2606 | /** |
| 2607 | * Called when a key was pressed down and not handled by any of the views |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2608 | * inside of the activity. So, for example, key presses while the cursor |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2609 | * is inside a TextView will not trigger the event (unless it is a navigation |
| 2610 | * to another object) because TextView handles its own key presses. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2611 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2612 | * <p>If the focused view didn't want this event, this method is called. |
| 2613 | * |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 2614 | * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK} |
| 2615 | * by calling {@link #onBackPressed()}, though the behavior varies based |
| 2616 | * on the application compatibility mode: for |
| 2617 | * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications, |
| 2618 | * it will set up the dispatch to call {@link #onKeyUp} where the action |
| 2619 | * will be performed; for earlier applications, it will perform the |
| 2620 | * action immediately in on-down, as those versions of the platform |
| 2621 | * behaved. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2622 | * |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 2623 | * <p>Other additional default key handling may be performed |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2624 | * if configured with {@link #setDefaultKeyMode}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2625 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2626 | * @return Return <code>true</code> to prevent this event from being propagated |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2627 | * further, or <code>false</code> to indicate that you have not handled |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2628 | * this event and it should continue to be propagated. |
| 2629 | * @see #onKeyUp |
| 2630 | * @see android.view.KeyEvent |
| 2631 | */ |
| 2632 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2633 | if (keyCode == KeyEvent.KEYCODE_BACK) { |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 2634 | if (getApplicationInfo().targetSdkVersion |
| 2635 | >= Build.VERSION_CODES.ECLAIR) { |
| 2636 | event.startTracking(); |
| 2637 | } else { |
| 2638 | onBackPressed(); |
| 2639 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2640 | return true; |
| 2641 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2642 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2643 | if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) { |
| 2644 | return false; |
| 2645 | } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) { |
Jose Lima | 7a22fc6 | 2015-01-23 17:24:22 -0800 | [diff] [blame] | 2646 | Window w = getWindow(); |
| 2647 | if (w.hasFeature(Window.FEATURE_OPTIONS_PANEL) && |
| 2648 | w.performPanelShortcut(Window.FEATURE_OPTIONS_PANEL, keyCode, event, |
| 2649 | Menu.FLAG_ALWAYS_PERFORM_CLOSE)) { |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2650 | return true; |
| 2651 | } |
| 2652 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2653 | } else { |
| 2654 | // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_* |
| 2655 | boolean clearSpannable = false; |
| 2656 | boolean handled; |
| 2657 | if ((event.getRepeatCount() != 0) || event.isSystem()) { |
| 2658 | clearSpannable = true; |
| 2659 | handled = false; |
| 2660 | } else { |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2661 | handled = TextKeyListener.getInstance().onKeyDown( |
| 2662 | null, mDefaultKeySsb, keyCode, event); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2663 | if (handled && mDefaultKeySsb.length() > 0) { |
| 2664 | // something useable has been typed - dispatch it now. |
| 2665 | |
| 2666 | final String str = mDefaultKeySsb.toString(); |
| 2667 | clearSpannable = true; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2668 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2669 | switch (mDefaultKeyMode) { |
| 2670 | case DEFAULT_KEYS_DIALER: |
| 2671 | Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str)); |
| 2672 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2673 | startActivity(intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2674 | break; |
| 2675 | case DEFAULT_KEYS_SEARCH_LOCAL: |
| 2676 | startSearch(str, false, null, false); |
| 2677 | break; |
| 2678 | case DEFAULT_KEYS_SEARCH_GLOBAL: |
| 2679 | startSearch(str, false, null, true); |
| 2680 | break; |
| 2681 | } |
| 2682 | } |
| 2683 | } |
| 2684 | if (clearSpannable) { |
| 2685 | mDefaultKeySsb.clear(); |
| 2686 | mDefaultKeySsb.clearSpans(); |
| 2687 | Selection.setSelection(mDefaultKeySsb,0); |
| 2688 | } |
| 2689 | return handled; |
| 2690 | } |
| 2691 | } |
| 2692 | |
| 2693 | /** |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2694 | * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent) |
| 2695 | * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle |
| 2696 | * the event). |
| 2697 | */ |
| 2698 | public boolean onKeyLongPress(int keyCode, KeyEvent event) { |
| 2699 | return false; |
| 2700 | } |
| 2701 | |
| 2702 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2703 | * Called when a key was released and not handled by any of the views |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2704 | * inside of the activity. So, for example, key presses while the cursor |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2705 | * is inside a TextView will not trigger the event (unless it is a navigation |
| 2706 | * to another object) because TextView handles its own key presses. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2707 | * |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2708 | * <p>The default implementation handles KEYCODE_BACK to stop the activity |
| 2709 | * and go back. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2710 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2711 | * @return Return <code>true</code> to prevent this event from being propagated |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2712 | * further, or <code>false</code> to indicate that you have not handled |
| 2713 | * this event and it should continue to be propagated. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2714 | * @see #onKeyDown |
| 2715 | * @see KeyEvent |
| 2716 | */ |
| 2717 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 2718 | if (getApplicationInfo().targetSdkVersion |
| 2719 | >= Build.VERSION_CODES.ECLAIR) { |
| 2720 | if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking() |
| 2721 | && !event.isCanceled()) { |
| 2722 | onBackPressed(); |
| 2723 | return true; |
| 2724 | } |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2725 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2726 | return false; |
| 2727 | } |
| 2728 | |
| 2729 | /** |
| 2730 | * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent) |
| 2731 | * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle |
| 2732 | * the event). |
| 2733 | */ |
| 2734 | public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) { |
| 2735 | return false; |
| 2736 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2737 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2738 | /** |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2739 | * Called when the activity has detected the user's press of the back |
| 2740 | * key. The default implementation simply finishes the current activity, |
| 2741 | * but you can override this to do whatever you want. |
| 2742 | */ |
| 2743 | public void onBackPressed() { |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 2744 | if (mActionBar != null && mActionBar.collapseActionView()) { |
| 2745 | return; |
| 2746 | } |
| 2747 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 2748 | if (!mFragments.getFragmentManager().popBackStackImmediate()) { |
Craig Mautner | 73f843d | 2014-05-19 09:42:28 -0700 | [diff] [blame] | 2749 | finishAfterTransition(); |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 2750 | } |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2751 | } |
Jeff Brown | 64da12a | 2011-01-04 19:57:47 -0800 | [diff] [blame] | 2752 | |
| 2753 | /** |
| 2754 | * Called when a key shortcut event is not handled by any of the views in the Activity. |
| 2755 | * Override this method to implement global key shortcuts for the Activity. |
| 2756 | * Key shortcuts can also be implemented by setting the |
| 2757 | * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items. |
| 2758 | * |
| 2759 | * @param keyCode The value in event.getKeyCode(). |
| 2760 | * @param event Description of the key event. |
| 2761 | * @return True if the key shortcut was handled. |
| 2762 | */ |
| 2763 | public boolean onKeyShortcut(int keyCode, KeyEvent event) { |
Abodunrinwa Toki | a04b7ad | 2015-06-30 17:44:04 -0700 | [diff] [blame] | 2764 | // Let the Action Bar have a chance at handling the shortcut. |
| 2765 | ActionBar actionBar = getActionBar(); |
| 2766 | return (actionBar != null && actionBar.onKeyShortcut(keyCode, event)); |
Jeff Brown | 64da12a | 2011-01-04 19:57:47 -0800 | [diff] [blame] | 2767 | } |
| 2768 | |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2769 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2770 | * Called when a touch screen event was not handled by any of the views |
| 2771 | * under it. This is most useful to process touch events that happen |
| 2772 | * outside of your window bounds, where there is no view to receive it. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2773 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2774 | * @param event The touch screen event being processed. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2775 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2776 | * @return Return true if you have consumed the event, false if you haven't. |
| 2777 | * The default implementation always returns false. |
| 2778 | */ |
| 2779 | public boolean onTouchEvent(MotionEvent event) { |
Dianne Hackborn | cfaf887 | 2011-01-18 13:57:54 -0800 | [diff] [blame] | 2780 | if (mWindow.shouldCloseOnTouch(this, event)) { |
| 2781 | finish(); |
| 2782 | return true; |
| 2783 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2784 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2785 | return false; |
| 2786 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2787 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2788 | /** |
| 2789 | * Called when the trackball was moved and not handled by any of the |
| 2790 | * views inside of the activity. So, for example, if the trackball moves |
| 2791 | * while focus is on a button, you will receive a call here because |
| 2792 | * buttons do not normally do anything with trackball events. The call |
| 2793 | * here happens <em>before</em> trackball movements are converted to |
| 2794 | * DPAD key events, which then get sent back to the view hierarchy, and |
| 2795 | * will be processed at the point for things like focus navigation. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2796 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2797 | * @param event The trackball event being processed. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2798 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2799 | * @return Return true if you have consumed the event, false if you haven't. |
| 2800 | * The default implementation always returns false. |
| 2801 | */ |
| 2802 | public boolean onTrackballEvent(MotionEvent event) { |
| 2803 | return false; |
| 2804 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2805 | |
| 2806 | /** |
| 2807 | * Called when a generic motion event was not handled by any of the |
| 2808 | * views inside of the activity. |
| 2809 | * <p> |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2810 | * Generic motion events describe joystick movements, mouse hovers, track pad |
| 2811 | * touches, scroll wheel movements and other input events. The |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2812 | * {@link MotionEvent#getSource() source} of the motion event specifies |
| 2813 | * the class of input that was received. Implementations of this method |
| 2814 | * must examine the bits in the source before processing the event. |
| 2815 | * The following code example shows how this is done. |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2816 | * </p><p> |
| 2817 | * Generic motion events with source class |
| 2818 | * {@link android.view.InputDevice#SOURCE_CLASS_POINTER} |
| 2819 | * are delivered to the view under the pointer. All other generic motion events are |
| 2820 | * delivered to the focused view. |
| 2821 | * </p><p> |
| 2822 | * See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to |
| 2823 | * handle this event. |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2824 | * </p> |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2825 | * |
| 2826 | * @param event The generic motion event being processed. |
| 2827 | * |
| 2828 | * @return Return true if you have consumed the event, false if you haven't. |
| 2829 | * The default implementation always returns false. |
| 2830 | */ |
| 2831 | public boolean onGenericMotionEvent(MotionEvent event) { |
| 2832 | return false; |
| 2833 | } |
| 2834 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2835 | /** |
| 2836 | * Called whenever a key, touch, or trackball event is dispatched to the |
| 2837 | * activity. Implement this method if you wish to know that the user has |
| 2838 | * interacted with the device in some way while your activity is running. |
| 2839 | * This callback and {@link #onUserLeaveHint} are intended to help |
| 2840 | * activities manage status bar notifications intelligently; specifically, |
| 2841 | * for helping activities determine the proper time to cancel a notfication. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2842 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2843 | * <p>All calls to your activity's {@link #onUserLeaveHint} callback will |
| 2844 | * be accompanied by calls to {@link #onUserInteraction}. This |
| 2845 | * ensures that your activity will be told of relevant user activity such |
| 2846 | * as pulling down the notification pane and touching an item there. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2847 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2848 | * <p>Note that this callback will be invoked for the touch down action |
| 2849 | * that begins a touch gesture, but may not be invoked for the touch-moved |
| 2850 | * and touch-up actions that follow. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2851 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2852 | * @see #onUserLeaveHint() |
| 2853 | */ |
| 2854 | public void onUserInteraction() { |
| 2855 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2856 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2857 | public void onWindowAttributesChanged(WindowManager.LayoutParams params) { |
| 2858 | // Update window manager if: we have a view, that view is |
| 2859 | // attached to its parent (which will be a RootView), and |
| 2860 | // this activity is not embedded. |
| 2861 | if (mParent == null) { |
| 2862 | View decor = mDecor; |
| 2863 | if (decor != null && decor.getParent() != null) { |
| 2864 | getWindowManager().updateViewLayout(decor, params); |
| 2865 | } |
| 2866 | } |
| 2867 | } |
| 2868 | |
| 2869 | public void onContentChanged() { |
| 2870 | } |
| 2871 | |
| 2872 | /** |
| 2873 | * Called when the current {@link Window} of the activity gains or loses |
| 2874 | * focus. This is the best indicator of whether this activity is visible |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2875 | * to the user. The default implementation clears the key tracking |
| 2876 | * state, so should always be called. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2877 | * |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2878 | * <p>Note that this provides information about global focus state, which |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2879 | * is managed independently of activity lifecycles. As such, while focus |
| 2880 | * changes will generally have some relation to lifecycle changes (an |
| 2881 | * activity that is stopped will not generally get window focus), you |
| 2882 | * should not rely on any particular order between the callbacks here and |
| 2883 | * those in the other lifecycle methods such as {@link #onResume}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2884 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2885 | * <p>As a general rule, however, a resumed activity will have window |
| 2886 | * focus... unless it has displayed other dialogs or popups that take |
| 2887 | * input focus, in which case the activity itself will not have focus |
| 2888 | * when the other windows have it. Likewise, the system may display |
| 2889 | * system-level windows (such as the status bar notification panel or |
| 2890 | * a system alert) which will temporarily take window input focus without |
| 2891 | * pausing the foreground activity. |
| 2892 | * |
| 2893 | * @param hasFocus Whether the window of this activity has focus. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2894 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2895 | * @see #hasWindowFocus() |
| 2896 | * @see #onResume |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 2897 | * @see View#onWindowFocusChanged(boolean) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2898 | */ |
| 2899 | public void onWindowFocusChanged(boolean hasFocus) { |
| 2900 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2901 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2902 | /** |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 2903 | * Called when the main window associated with the activity has been |
| 2904 | * attached to the window manager. |
| 2905 | * See {@link View#onAttachedToWindow() View.onAttachedToWindow()} |
| 2906 | * for more information. |
| 2907 | * @see View#onAttachedToWindow |
| 2908 | */ |
| 2909 | public void onAttachedToWindow() { |
| 2910 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2911 | |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 2912 | /** |
| 2913 | * Called when the main window associated with the activity has been |
| 2914 | * detached from the window manager. |
| 2915 | * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()} |
| 2916 | * for more information. |
| 2917 | * @see View#onDetachedFromWindow |
| 2918 | */ |
| 2919 | public void onDetachedFromWindow() { |
| 2920 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2921 | |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 2922 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2923 | * Returns true if this activity's <em>main</em> window currently has window focus. |
| 2924 | * Note that this is not the same as the view itself having focus. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2925 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2926 | * @return True if this activity's main window currently has window focus. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2927 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2928 | * @see #onWindowAttributesChanged(android.view.WindowManager.LayoutParams) |
| 2929 | */ |
| 2930 | public boolean hasWindowFocus() { |
| 2931 | Window w = getWindow(); |
| 2932 | if (w != null) { |
| 2933 | View d = w.getDecorView(); |
| 2934 | if (d != null) { |
| 2935 | return d.hasWindowFocus(); |
| 2936 | } |
| 2937 | } |
| 2938 | return false; |
| 2939 | } |
Will Haldean Brown | ca6234e | 2014-02-12 10:23:41 -0800 | [diff] [blame] | 2940 | |
| 2941 | /** |
| 2942 | * Called when the main window associated with the activity has been dismissed. |
Adam Powell | 117b695 | 2014-05-05 18:14:56 -0700 | [diff] [blame] | 2943 | * @hide |
Will Haldean Brown | ca6234e | 2014-02-12 10:23:41 -0800 | [diff] [blame] | 2944 | */ |
Adam Powell | 117b695 | 2014-05-05 18:14:56 -0700 | [diff] [blame] | 2945 | @Override |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 2946 | public void onWindowDismissed(boolean finishTask) { |
| 2947 | finish(finishTask ? FINISH_TASK_WITH_ACTIVITY : DONT_FINISH_TASK_WITH_ACTIVITY); |
Will Haldean Brown | ca6234e | 2014-02-12 10:23:41 -0800 | [diff] [blame] | 2948 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2949 | |
Wale Ogunwale | 868a5e1 | 2015-08-02 16:19:20 -0700 | [diff] [blame] | 2950 | |
Skuhne | ce2faa5 | 2015-08-11 10:36:38 -0700 | [diff] [blame] | 2951 | /** |
Filip Gruszczynski | 411c06f | 2016-01-07 09:44:44 -0800 | [diff] [blame] | 2952 | * Moves the activity from |
| 2953 | * {@link android.app.ActivityManager.StackId#FREEFORM_WORKSPACE_STACK_ID} to |
| 2954 | * {@link android.app.ActivityManager.StackId#FULLSCREEN_WORKSPACE_STACK_ID} stack. |
Wale Ogunwale | 868a5e1 | 2015-08-02 16:19:20 -0700 | [diff] [blame] | 2955 | * |
Wale Ogunwale | 868a5e1 | 2015-08-02 16:19:20 -0700 | [diff] [blame] | 2956 | * @hide |
| 2957 | */ |
| 2958 | @Override |
Filip Gruszczynski | 411c06f | 2016-01-07 09:44:44 -0800 | [diff] [blame] | 2959 | public void exitFreeformMode() throws RemoteException { |
| 2960 | ActivityManagerNative.getDefault().exitFreeformMode(mToken); |
Wale Ogunwale | 868a5e1 | 2015-08-02 16:19:20 -0700 | [diff] [blame] | 2961 | } |
| 2962 | |
| 2963 | /** Returns the current stack Id for the window. |
| 2964 | * @hide |
| 2965 | */ |
| 2966 | @Override |
| 2967 | public int getWindowStackId() throws RemoteException { |
| 2968 | return ActivityManagerNative.getDefault().getActivityStackId(mToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2969 | } |
| 2970 | |
| 2971 | /** |
Jaewan Kim | d98dcab | 2016-05-03 02:52:18 +0900 | [diff] [blame] | 2972 | * Puts the activity in picture-in-picture mode if the activity supports. |
| 2973 | * @see android.R.attr#supportsPictureInPicture |
| 2974 | * @hide |
| 2975 | */ |
| 2976 | @Override |
| 2977 | public void enterPictureInPictureModeIfPossible() { |
| 2978 | if (mActivityInfo.resizeMode == ActivityInfo.RESIZE_MODE_RESIZEABLE_AND_PIPABLE) { |
| 2979 | enterPictureInPictureMode(); |
| 2980 | } |
| 2981 | } |
| 2982 | |
| 2983 | /** |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2984 | * Called to process key events. You can override this to intercept all |
| 2985 | * key events before they are dispatched to the window. Be sure to call |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2986 | * this implementation for key events that should be handled normally. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2987 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2988 | * @param event The key event. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2989 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2990 | * @return boolean Return true if this event was consumed. |
| 2991 | */ |
| 2992 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 2993 | onUserInteraction(); |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 2994 | |
| 2995 | // Let action bars open menus in response to the menu key prioritized over |
| 2996 | // the window handling it |
George Mount | 5beb2617 | 2015-12-15 13:36:01 -0800 | [diff] [blame] | 2997 | final int keyCode = event.getKeyCode(); |
| 2998 | if (keyCode == KeyEvent.KEYCODE_MENU && |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 2999 | mActionBar != null && mActionBar.onMenuKeyEvent(event)) { |
| 3000 | return true; |
George Mount | cbe2835 | 2016-01-06 16:24:26 -0800 | [diff] [blame] | 3001 | } else if (event.isCtrlPressed() && |
| 3002 | event.getUnicodeChar(event.getMetaState() & ~KeyEvent.META_CTRL_MASK) == '<') { |
| 3003 | // Capture the Control-< and send focus to the ActionBar |
George Mount | 5beb2617 | 2015-12-15 13:36:01 -0800 | [diff] [blame] | 3004 | final int action = event.getAction(); |
| 3005 | if (action == KeyEvent.ACTION_DOWN) { |
George Mount | cbe2835 | 2016-01-06 16:24:26 -0800 | [diff] [blame] | 3006 | final ActionBar actionBar = getActionBar(); |
| 3007 | if (actionBar != null && actionBar.isShowing() && actionBar.requestFocus()) { |
| 3008 | mEatKeyUpEvent = true; |
| 3009 | return true; |
George Mount | 5beb2617 | 2015-12-15 13:36:01 -0800 | [diff] [blame] | 3010 | } |
| 3011 | } else if (action == KeyEvent.ACTION_UP && mEatKeyUpEvent) { |
| 3012 | mEatKeyUpEvent = false; |
| 3013 | return true; |
| 3014 | } |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 3015 | } |
| 3016 | |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 3017 | Window win = getWindow(); |
| 3018 | if (win.superDispatchKeyEvent(event)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3019 | return true; |
| 3020 | } |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 3021 | View decor = mDecor; |
| 3022 | if (decor == null) decor = win.getDecorView(); |
| 3023 | return event.dispatch(this, decor != null |
| 3024 | ? decor.getKeyDispatcherState() : null, this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3025 | } |
| 3026 | |
| 3027 | /** |
Jeff Brown | 64da12a | 2011-01-04 19:57:47 -0800 | [diff] [blame] | 3028 | * Called to process a key shortcut event. |
| 3029 | * You can override this to intercept all key shortcut events before they are |
| 3030 | * dispatched to the window. Be sure to call this implementation for key shortcut |
| 3031 | * events that should be handled normally. |
| 3032 | * |
| 3033 | * @param event The key shortcut event. |
| 3034 | * @return True if this event was consumed. |
| 3035 | */ |
| 3036 | public boolean dispatchKeyShortcutEvent(KeyEvent event) { |
| 3037 | onUserInteraction(); |
| 3038 | if (getWindow().superDispatchKeyShortcutEvent(event)) { |
| 3039 | return true; |
| 3040 | } |
| 3041 | return onKeyShortcut(event.getKeyCode(), event); |
| 3042 | } |
| 3043 | |
| 3044 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3045 | * Called to process touch screen events. You can override this to |
| 3046 | * intercept all touch screen events before they are dispatched to the |
| 3047 | * window. Be sure to call this implementation for touch screen events |
| 3048 | * that should be handled normally. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3049 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3050 | * @param ev The touch screen event. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3051 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3052 | * @return boolean Return true if this event was consumed. |
| 3053 | */ |
| 3054 | public boolean dispatchTouchEvent(MotionEvent ev) { |
| 3055 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { |
| 3056 | onUserInteraction(); |
| 3057 | } |
| 3058 | if (getWindow().superDispatchTouchEvent(ev)) { |
| 3059 | return true; |
| 3060 | } |
| 3061 | return onTouchEvent(ev); |
| 3062 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3063 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3064 | /** |
| 3065 | * Called to process trackball events. You can override this to |
| 3066 | * intercept all trackball events before they are dispatched to the |
| 3067 | * window. Be sure to call this implementation for trackball events |
| 3068 | * that should be handled normally. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3069 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3070 | * @param ev The trackball event. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3071 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3072 | * @return boolean Return true if this event was consumed. |
| 3073 | */ |
| 3074 | public boolean dispatchTrackballEvent(MotionEvent ev) { |
| 3075 | onUserInteraction(); |
| 3076 | if (getWindow().superDispatchTrackballEvent(ev)) { |
| 3077 | return true; |
| 3078 | } |
| 3079 | return onTrackballEvent(ev); |
| 3080 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 3081 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 3082 | /** |
| 3083 | * Called to process generic motion events. You can override this to |
| 3084 | * intercept all generic motion events before they are dispatched to the |
| 3085 | * window. Be sure to call this implementation for generic motion events |
| 3086 | * that should be handled normally. |
| 3087 | * |
| 3088 | * @param ev The generic motion event. |
| 3089 | * |
| 3090 | * @return boolean Return true if this event was consumed. |
| 3091 | */ |
| 3092 | public boolean dispatchGenericMotionEvent(MotionEvent ev) { |
| 3093 | onUserInteraction(); |
| 3094 | if (getWindow().superDispatchGenericMotionEvent(ev)) { |
| 3095 | return true; |
| 3096 | } |
| 3097 | return onGenericMotionEvent(ev); |
| 3098 | } |
| 3099 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 3100 | public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 3101 | event.setClassName(getClass().getName()); |
| 3102 | event.setPackageName(getPackageName()); |
| 3103 | |
| 3104 | LayoutParams params = getWindow().getAttributes(); |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 3105 | boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) && |
| 3106 | (params.height == LayoutParams.MATCH_PARENT); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 3107 | event.setFullScreen(isFullScreen); |
| 3108 | |
| 3109 | CharSequence title = getTitle(); |
| 3110 | if (!TextUtils.isEmpty(title)) { |
| 3111 | event.getText().add(title); |
| 3112 | } |
| 3113 | |
| 3114 | return true; |
| 3115 | } |
| 3116 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3117 | /** |
| 3118 | * Default implementation of |
| 3119 | * {@link android.view.Window.Callback#onCreatePanelView} |
| 3120 | * for activities. This |
| 3121 | * simply returns null so that all panel sub-windows will have the default |
| 3122 | * menu behavior. |
| 3123 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3124 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3125 | public View onCreatePanelView(int featureId) { |
| 3126 | return null; |
| 3127 | } |
| 3128 | |
| 3129 | /** |
| 3130 | * Default implementation of |
| 3131 | * {@link android.view.Window.Callback#onCreatePanelMenu} |
| 3132 | * for activities. This calls through to the new |
| 3133 | * {@link #onCreateOptionsMenu} method for the |
| 3134 | * {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel, |
| 3135 | * so that subclasses of Activity don't need to deal with feature codes. |
| 3136 | */ |
| 3137 | public boolean onCreatePanelMenu(int featureId, Menu menu) { |
| 3138 | if (featureId == Window.FEATURE_OPTIONS_PANEL) { |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 3139 | boolean show = onCreateOptionsMenu(menu); |
| 3140 | show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater()); |
| 3141 | return show; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3142 | } |
| 3143 | return false; |
| 3144 | } |
| 3145 | |
| 3146 | /** |
| 3147 | * Default implementation of |
| 3148 | * {@link android.view.Window.Callback#onPreparePanel} |
| 3149 | * for activities. This |
| 3150 | * calls through to the new {@link #onPrepareOptionsMenu} method for the |
| 3151 | * {@link android.view.Window#FEATURE_OPTIONS_PANEL} |
| 3152 | * panel, so that subclasses of |
| 3153 | * Activity don't need to deal with feature codes. |
| 3154 | */ |
| 3155 | public boolean onPreparePanel(int featureId, View view, Menu menu) { |
| 3156 | if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) { |
| 3157 | boolean goforit = onPrepareOptionsMenu(menu); |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 3158 | goforit |= mFragments.dispatchPrepareOptionsMenu(menu); |
Adam Powell | ef31e7c | 2012-05-08 09:59:40 -0700 | [diff] [blame] | 3159 | return goforit; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3160 | } |
| 3161 | return true; |
| 3162 | } |
| 3163 | |
| 3164 | /** |
| 3165 | * {@inheritDoc} |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3166 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3167 | * @return The default implementation returns true. |
| 3168 | */ |
| 3169 | public boolean onMenuOpened(int featureId, Menu menu) { |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 3170 | if (featureId == Window.FEATURE_ACTION_BAR) { |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 3171 | initWindowDecorActionBar(); |
Adam Powell | 049dd3d | 2010-12-02 13:43:59 -0800 | [diff] [blame] | 3172 | if (mActionBar != null) { |
| 3173 | mActionBar.dispatchMenuVisibilityChanged(true); |
| 3174 | } else { |
| 3175 | Log.e(TAG, "Tried to open action bar menu with no action bar"); |
| 3176 | } |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 3177 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3178 | return true; |
| 3179 | } |
| 3180 | |
| 3181 | /** |
| 3182 | * Default implementation of |
| 3183 | * {@link android.view.Window.Callback#onMenuItemSelected} |
| 3184 | * for activities. This calls through to the new |
| 3185 | * {@link #onOptionsItemSelected} method for the |
| 3186 | * {@link android.view.Window#FEATURE_OPTIONS_PANEL} |
| 3187 | * panel, so that subclasses of |
| 3188 | * Activity don't need to deal with feature codes. |
| 3189 | */ |
| 3190 | public boolean onMenuItemSelected(int featureId, MenuItem item) { |
zobject | 686a805 | 2012-12-14 21:11:08 +0900 | [diff] [blame] | 3191 | CharSequence titleCondensed = item.getTitleCondensed(); |
| 3192 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3193 | switch (featureId) { |
| 3194 | case Window.FEATURE_OPTIONS_PANEL: |
| 3195 | // Put event logging here so it gets called even if subclass |
| 3196 | // doesn't call through to superclass's implmeentation of each |
| 3197 | // of these methods below |
zobject | 686a805 | 2012-12-14 21:11:08 +0900 | [diff] [blame] | 3198 | if(titleCondensed != null) { |
| 3199 | EventLog.writeEvent(50000, 0, titleCondensed.toString()); |
| 3200 | } |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 3201 | if (onOptionsItemSelected(item)) { |
| 3202 | return true; |
| 3203 | } |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 3204 | if (mFragments.dispatchOptionsItemSelected(item)) { |
| 3205 | return true; |
| 3206 | } |
| 3207 | if (item.getItemId() == android.R.id.home && mActionBar != null && |
| 3208 | (mActionBar.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) { |
| 3209 | if (mParent == null) { |
Adam Powell | 07304f529 | 2012-05-07 17:32:44 -0700 | [diff] [blame] | 3210 | return onNavigateUp(); |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 3211 | } else { |
Adam Powell | 07304f529 | 2012-05-07 17:32:44 -0700 | [diff] [blame] | 3212 | return mParent.onNavigateUpFromChild(this); |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 3213 | } |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 3214 | } |
| 3215 | return false; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3216 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3217 | case Window.FEATURE_CONTEXT_MENU: |
zobject | 686a805 | 2012-12-14 21:11:08 +0900 | [diff] [blame] | 3218 | if(titleCondensed != null) { |
| 3219 | EventLog.writeEvent(50000, 1, titleCondensed.toString()); |
| 3220 | } |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 3221 | if (onContextItemSelected(item)) { |
| 3222 | return true; |
| 3223 | } |
| 3224 | return mFragments.dispatchContextItemSelected(item); |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 3225 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3226 | default: |
| 3227 | return false; |
| 3228 | } |
| 3229 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3230 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3231 | /** |
| 3232 | * Default implementation of |
| 3233 | * {@link android.view.Window.Callback#onPanelClosed(int, Menu)} for |
| 3234 | * activities. This calls through to {@link #onOptionsMenuClosed(Menu)} |
| 3235 | * method for the {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel, |
| 3236 | * so that subclasses of Activity don't need to deal with feature codes. |
| 3237 | * For context menus ({@link Window#FEATURE_CONTEXT_MENU}), the |
| 3238 | * {@link #onContextMenuClosed(Menu)} will be called. |
| 3239 | */ |
| 3240 | public void onPanelClosed(int featureId, Menu menu) { |
| 3241 | switch (featureId) { |
| 3242 | case Window.FEATURE_OPTIONS_PANEL: |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 3243 | mFragments.dispatchOptionsMenuClosed(menu); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3244 | onOptionsMenuClosed(menu); |
| 3245 | break; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3246 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3247 | case Window.FEATURE_CONTEXT_MENU: |
| 3248 | onContextMenuClosed(menu); |
| 3249 | break; |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 3250 | |
| 3251 | case Window.FEATURE_ACTION_BAR: |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 3252 | initWindowDecorActionBar(); |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 3253 | mActionBar.dispatchMenuVisibilityChanged(false); |
| 3254 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3255 | } |
| 3256 | } |
| 3257 | |
| 3258 | /** |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 3259 | * Declare that the options menu has changed, so should be recreated. |
| 3260 | * The {@link #onCreateOptionsMenu(Menu)} method will be called the next |
| 3261 | * time it needs to be displayed. |
| 3262 | */ |
| 3263 | public void invalidateOptionsMenu() { |
Jose Lima | 7a22fc6 | 2015-01-23 17:24:22 -0800 | [diff] [blame] | 3264 | if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) && |
| 3265 | (mActionBar == null || !mActionBar.invalidateOptionsMenu())) { |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 3266 | mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL); |
| 3267 | } |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 3268 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3269 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 3270 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3271 | * Initialize the contents of the Activity's standard options menu. You |
| 3272 | * should place your menu items in to <var>menu</var>. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3273 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3274 | * <p>This is only called once, the first time the options menu is |
| 3275 | * displayed. To update the menu every time it is displayed, see |
| 3276 | * {@link #onPrepareOptionsMenu}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3277 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3278 | * <p>The default implementation populates the menu with standard system |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3279 | * menu items. These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that |
| 3280 | * they will be correctly ordered with application-defined menu items. |
| 3281 | * Deriving classes should always call through to the base implementation. |
| 3282 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3283 | * <p>You can safely hold on to <var>menu</var> (and any items created |
| 3284 | * from it), making modifications to it as desired, until the next |
| 3285 | * time onCreateOptionsMenu() is called. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3286 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3287 | * <p>When you add items to the menu, you can implement the Activity's |
| 3288 | * {@link #onOptionsItemSelected} method to handle them there. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3289 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3290 | * @param menu The options menu in which you place your items. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3291 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3292 | * @return You must return true for the menu to be displayed; |
| 3293 | * if you return false it will not be shown. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3294 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3295 | * @see #onPrepareOptionsMenu |
| 3296 | * @see #onOptionsItemSelected |
| 3297 | */ |
| 3298 | public boolean onCreateOptionsMenu(Menu menu) { |
| 3299 | if (mParent != null) { |
| 3300 | return mParent.onCreateOptionsMenu(menu); |
| 3301 | } |
| 3302 | return true; |
| 3303 | } |
| 3304 | |
| 3305 | /** |
| 3306 | * Prepare the Screen's standard options menu to be displayed. This is |
| 3307 | * called right before the menu is shown, every time it is shown. You can |
| 3308 | * use this method to efficiently enable/disable items or otherwise |
| 3309 | * dynamically modify the contents. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3310 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3311 | * <p>The default implementation updates the system menu items based on the |
| 3312 | * activity's state. Deriving classes should always call through to the |
| 3313 | * base class implementation. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3314 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3315 | * @param menu The options menu as last shown or first initialized by |
| 3316 | * onCreateOptionsMenu(). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3317 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3318 | * @return You must return true for the menu to be displayed; |
| 3319 | * if you return false it will not be shown. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3320 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3321 | * @see #onCreateOptionsMenu |
| 3322 | */ |
| 3323 | public boolean onPrepareOptionsMenu(Menu menu) { |
| 3324 | if (mParent != null) { |
| 3325 | return mParent.onPrepareOptionsMenu(menu); |
| 3326 | } |
| 3327 | return true; |
| 3328 | } |
| 3329 | |
| 3330 | /** |
| 3331 | * This hook is called whenever an item in your options menu is selected. |
| 3332 | * The default implementation simply returns false to have the normal |
| 3333 | * processing happen (calling the item's Runnable or sending a message to |
| 3334 | * its Handler as appropriate). You can use this method for any items |
| 3335 | * for which you would like to do processing without those other |
| 3336 | * facilities. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3337 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3338 | * <p>Derived classes should call through to the base class for it to |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 3339 | * perform the default menu handling.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3340 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3341 | * @param item The menu item that was selected. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3342 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3343 | * @return boolean Return false to allow normal menu processing to |
| 3344 | * proceed, true to consume it here. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3345 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3346 | * @see #onCreateOptionsMenu |
| 3347 | */ |
| 3348 | public boolean onOptionsItemSelected(MenuItem item) { |
| 3349 | if (mParent != null) { |
| 3350 | return mParent.onOptionsItemSelected(item); |
| 3351 | } |
| 3352 | return false; |
| 3353 | } |
| 3354 | |
| 3355 | /** |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 3356 | * This method is called whenever the user chooses to navigate Up within your application's |
| 3357 | * activity hierarchy from the action bar. |
| 3358 | * |
| 3359 | * <p>If the attribute {@link android.R.attr#parentActivityName parentActivityName} |
| 3360 | * was specified in the manifest for this activity or an activity-alias to it, |
| 3361 | * default Up navigation will be handled automatically. If any activity |
| 3362 | * along the parent chain requires extra Intent arguments, the Activity subclass |
| 3363 | * should override the method {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)} |
| 3364 | * to supply those arguments.</p> |
| 3365 | * |
Mark Lu | fa43485 | 2016-08-11 17:40:33 -0700 | [diff] [blame] | 3366 | * <p>See <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back Stack</a> |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 3367 | * from the developer guide and <a href="{@docRoot}design/patterns/navigation.html">Navigation</a> |
| 3368 | * from the design guide for more information about navigating within your app.</p> |
| 3369 | * |
| 3370 | * <p>See the {@link TaskStackBuilder} class and the Activity methods |
| 3371 | * {@link #getParentActivityIntent()}, {@link #shouldUpRecreateTask(Intent)}, and |
| 3372 | * {@link #navigateUpTo(Intent)} for help implementing custom Up navigation. |
| 3373 | * The AppNavigation sample application in the Android SDK is also available for reference.</p> |
| 3374 | * |
| 3375 | * @return true if Up navigation completed successfully and this Activity was finished, |
| 3376 | * false otherwise. |
| 3377 | */ |
| 3378 | public boolean onNavigateUp() { |
| 3379 | // Automatically handle hierarchical Up navigation if the proper |
| 3380 | // metadata is available. |
| 3381 | Intent upIntent = getParentActivityIntent(); |
| 3382 | if (upIntent != null) { |
Adam Powell | 0fc5b2b | 2012-07-18 18:20:29 -0700 | [diff] [blame] | 3383 | if (mActivityInfo.taskAffinity == null) { |
| 3384 | // Activities with a null affinity are special; they really shouldn't |
| 3385 | // specify a parent activity intent in the first place. Just finish |
| 3386 | // the current activity and call it a day. |
| 3387 | finish(); |
| 3388 | } else if (shouldUpRecreateTask(upIntent)) { |
Adam Powell | f78a844 | 2012-05-01 18:09:32 -0700 | [diff] [blame] | 3389 | TaskStackBuilder b = TaskStackBuilder.create(this); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 3390 | onCreateNavigateUpTaskStack(b); |
| 3391 | onPrepareNavigateUpTaskStack(b); |
| 3392 | b.startActivities(); |
Adam Powell | 3d193d9 | 2012-05-15 16:51:55 -0700 | [diff] [blame] | 3393 | |
| 3394 | // We can't finishAffinity if we have a result. |
| 3395 | // Fall back and simply finish the current activity instead. |
| 3396 | if (mResultCode != RESULT_CANCELED || mResultData != null) { |
| 3397 | // Tell the developer what's going on to avoid hair-pulling. |
| 3398 | Log.i(TAG, "onNavigateUp only finishing topmost activity to return a result"); |
| 3399 | finish(); |
| 3400 | } else { |
| 3401 | finishAffinity(); |
| 3402 | } |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 3403 | } else { |
| 3404 | navigateUpTo(upIntent); |
| 3405 | } |
| 3406 | return true; |
| 3407 | } |
| 3408 | return false; |
| 3409 | } |
| 3410 | |
| 3411 | /** |
| 3412 | * This is called when a child activity of this one attempts to navigate up. |
| 3413 | * The default implementation simply calls onNavigateUp() on this activity (the parent). |
| 3414 | * |
| 3415 | * @param child The activity making the call. |
| 3416 | */ |
| 3417 | public boolean onNavigateUpFromChild(Activity child) { |
| 3418 | return onNavigateUp(); |
| 3419 | } |
| 3420 | |
| 3421 | /** |
| 3422 | * Define the synthetic task stack that will be generated during Up navigation from |
| 3423 | * a different task. |
| 3424 | * |
| 3425 | * <p>The default implementation of this method adds the parent chain of this activity |
| 3426 | * as specified in the manifest to the supplied {@link TaskStackBuilder}. Applications |
| 3427 | * may choose to override this method to construct the desired task stack in a different |
| 3428 | * way.</p> |
| 3429 | * |
Adam Powell | f019595 | 2012-05-02 21:38:54 -0700 | [diff] [blame] | 3430 | * <p>This method will be invoked by the default implementation of {@link #onNavigateUp()} |
| 3431 | * if {@link #shouldUpRecreateTask(Intent)} returns true when supplied with the intent |
| 3432 | * returned by {@link #getParentActivityIntent()}.</p> |
| 3433 | * |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 3434 | * <p>Applications that wish to supply extra Intent parameters to the parent stack defined |
| 3435 | * by the manifest should override {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}.</p> |
| 3436 | * |
| 3437 | * @param builder An empty TaskStackBuilder - the application should add intents representing |
| 3438 | * the desired task stack |
| 3439 | */ |
| 3440 | public void onCreateNavigateUpTaskStack(TaskStackBuilder builder) { |
| 3441 | builder.addParentStack(this); |
| 3442 | } |
| 3443 | |
| 3444 | /** |
| 3445 | * Prepare the synthetic task stack that will be generated during Up navigation |
| 3446 | * from a different task. |
| 3447 | * |
| 3448 | * <p>This method receives the {@link TaskStackBuilder} with the constructed series of |
| 3449 | * Intents as generated by {@link #onCreateNavigateUpTaskStack(TaskStackBuilder)}. |
| 3450 | * If any extra data should be added to these intents before launching the new task, |
| 3451 | * the application should override this method and add that data here.</p> |
| 3452 | * |
| 3453 | * @param builder A TaskStackBuilder that has been populated with Intents by |
| 3454 | * onCreateNavigateUpTaskStack. |
| 3455 | */ |
| 3456 | public void onPrepareNavigateUpTaskStack(TaskStackBuilder builder) { |
| 3457 | } |
| 3458 | |
| 3459 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3460 | * This hook is called whenever the options menu is being closed (either by the user canceling |
| 3461 | * the menu with the back/menu button, or when an item is selected). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3462 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3463 | * @param menu The options menu as last shown or first initialized by |
| 3464 | * onCreateOptionsMenu(). |
| 3465 | */ |
| 3466 | public void onOptionsMenuClosed(Menu menu) { |
| 3467 | if (mParent != null) { |
| 3468 | mParent.onOptionsMenuClosed(menu); |
| 3469 | } |
| 3470 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3471 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3472 | /** |
| 3473 | * Programmatically opens the options menu. If the options menu is already |
| 3474 | * open, this method does nothing. |
| 3475 | */ |
| 3476 | public void openOptionsMenu() { |
Jose Lima | 7a22fc6 | 2015-01-23 17:24:22 -0800 | [diff] [blame] | 3477 | if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) && |
| 3478 | (mActionBar == null || !mActionBar.openOptionsMenu())) { |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 3479 | mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null); |
| 3480 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3481 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3482 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3483 | /** |
| 3484 | * Progammatically closes the options menu. If the options menu is already |
| 3485 | * closed, this method does nothing. |
| 3486 | */ |
| 3487 | public void closeOptionsMenu() { |
Jose Lima | 7a22fc6 | 2015-01-23 17:24:22 -0800 | [diff] [blame] | 3488 | if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL)) { |
| 3489 | mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL); |
| 3490 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | /** |
| 3494 | * Called when a context menu for the {@code view} is about to be shown. |
| 3495 | * Unlike {@link #onCreateOptionsMenu(Menu)}, this will be called every |
| 3496 | * time the context menu is about to be shown and should be populated for |
| 3497 | * the view (or item inside the view for {@link AdapterView} subclasses, |
| 3498 | * this can be found in the {@code menuInfo})). |
| 3499 | * <p> |
| 3500 | * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an |
| 3501 | * item has been selected. |
| 3502 | * <p> |
| 3503 | * It is not safe to hold onto the context menu after this method returns. |
Scott Main | cdf5106 | 2013-01-08 20:03:05 -0800 | [diff] [blame] | 3504 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3505 | */ |
| 3506 | public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { |
| 3507 | } |
| 3508 | |
| 3509 | /** |
| 3510 | * Registers a context menu to be shown for the given view (multiple views |
| 3511 | * can show the context menu). This method will set the |
| 3512 | * {@link OnCreateContextMenuListener} on the view to this activity, so |
| 3513 | * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be |
| 3514 | * called when it is time to show the context menu. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3515 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3516 | * @see #unregisterForContextMenu(View) |
| 3517 | * @param view The view that should show a context menu. |
| 3518 | */ |
| 3519 | public void registerForContextMenu(View view) { |
| 3520 | view.setOnCreateContextMenuListener(this); |
| 3521 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3522 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3523 | /** |
| 3524 | * Prevents a context menu to be shown for the given view. This method will remove the |
| 3525 | * {@link OnCreateContextMenuListener} on the view. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3526 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3527 | * @see #registerForContextMenu(View) |
| 3528 | * @param view The view that should stop showing a context menu. |
| 3529 | */ |
| 3530 | public void unregisterForContextMenu(View view) { |
| 3531 | view.setOnCreateContextMenuListener(null); |
| 3532 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3533 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3534 | /** |
| 3535 | * Programmatically opens the context menu for a particular {@code view}. |
| 3536 | * The {@code view} should have been added via |
| 3537 | * {@link #registerForContextMenu(View)}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3538 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3539 | * @param view The view to show the context menu for. |
| 3540 | */ |
| 3541 | public void openContextMenu(View view) { |
| 3542 | view.showContextMenu(); |
| 3543 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3544 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3545 | /** |
| 3546 | * Programmatically closes the most recently opened context menu, if showing. |
| 3547 | */ |
| 3548 | public void closeContextMenu() { |
Jose Lima | bd769a1 | 2015-01-28 10:43:15 -0800 | [diff] [blame] | 3549 | if (mWindow.hasFeature(Window.FEATURE_CONTEXT_MENU)) { |
| 3550 | mWindow.closePanel(Window.FEATURE_CONTEXT_MENU); |
| 3551 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3552 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3553 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3554 | /** |
| 3555 | * This hook is called whenever an item in a context menu is selected. The |
| 3556 | * default implementation simply returns false to have the normal processing |
| 3557 | * happen (calling the item's Runnable or sending a message to its Handler |
| 3558 | * as appropriate). You can use this method for any items for which you |
| 3559 | * would like to do processing without those other facilities. |
| 3560 | * <p> |
| 3561 | * Use {@link MenuItem#getMenuInfo()} to get extra information set by the |
| 3562 | * View that added this menu item. |
| 3563 | * <p> |
| 3564 | * Derived classes should call through to the base class for it to perform |
| 3565 | * the default menu handling. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3566 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3567 | * @param item The context menu item that was selected. |
| 3568 | * @return boolean Return false to allow normal context menu processing to |
| 3569 | * proceed, true to consume it here. |
| 3570 | */ |
| 3571 | public boolean onContextItemSelected(MenuItem item) { |
| 3572 | if (mParent != null) { |
| 3573 | return mParent.onContextItemSelected(item); |
| 3574 | } |
| 3575 | return false; |
| 3576 | } |
| 3577 | |
| 3578 | /** |
| 3579 | * This hook is called whenever the context menu is being closed (either by |
| 3580 | * the user canceling the menu with the back/menu button, or when an item is |
| 3581 | * selected). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3582 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3583 | * @param menu The context menu that is being closed. |
| 3584 | */ |
| 3585 | public void onContextMenuClosed(Menu menu) { |
| 3586 | if (mParent != null) { |
| 3587 | mParent.onContextMenuClosed(menu); |
| 3588 | } |
| 3589 | } |
| 3590 | |
| 3591 | /** |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3592 | * @deprecated Old no-arguments version of {@link #onCreateDialog(int, Bundle)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3593 | */ |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3594 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3595 | protected Dialog onCreateDialog(int id) { |
| 3596 | return null; |
| 3597 | } |
| 3598 | |
| 3599 | /** |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3600 | * Callback for creating dialogs that are managed (saved and restored) for you |
| 3601 | * by the activity. The default implementation calls through to |
| 3602 | * {@link #onCreateDialog(int)} for compatibility. |
| 3603 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 3604 | * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 3605 | * or later, consider instead using a {@link DialogFragment} instead.</em> |
| 3606 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3607 | * <p>If you use {@link #showDialog(int)}, the activity will call through to |
| 3608 | * this method the first time, and hang onto it thereafter. Any dialog |
| 3609 | * that is created by this method will automatically be saved and restored |
| 3610 | * for you, including whether it is showing. |
| 3611 | * |
| 3612 | * <p>If you would like the activity to manage saving and restoring dialogs |
| 3613 | * for you, you should override this method and handle any ids that are |
| 3614 | * passed to {@link #showDialog}. |
| 3615 | * |
| 3616 | * <p>If you would like an opportunity to prepare your dialog before it is shown, |
| 3617 | * override {@link #onPrepareDialog(int, Dialog, Bundle)}. |
| 3618 | * |
| 3619 | * @param id The id of the dialog. |
| 3620 | * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}. |
| 3621 | * @return The dialog. If you return null, the dialog will not be created. |
| 3622 | * |
| 3623 | * @see #onPrepareDialog(int, Dialog, Bundle) |
| 3624 | * @see #showDialog(int, Bundle) |
| 3625 | * @see #dismissDialog(int) |
| 3626 | * @see #removeDialog(int) |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 3627 | * |
| 3628 | * @deprecated Use the new {@link DialogFragment} class with |
| 3629 | * {@link FragmentManager} instead; this is also |
| 3630 | * available on older platforms through the Android compatibility package. |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3631 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3632 | @Nullable |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 3633 | @Deprecated |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3634 | protected Dialog onCreateDialog(int id, Bundle args) { |
| 3635 | return onCreateDialog(id); |
| 3636 | } |
| 3637 | |
| 3638 | /** |
| 3639 | * @deprecated Old no-arguments version of |
| 3640 | * {@link #onPrepareDialog(int, Dialog, Bundle)}. |
| 3641 | */ |
| 3642 | @Deprecated |
| 3643 | protected void onPrepareDialog(int id, Dialog dialog) { |
| 3644 | dialog.setOwnerActivity(this); |
| 3645 | } |
| 3646 | |
| 3647 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3648 | * Provides an opportunity to prepare a managed dialog before it is being |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3649 | * shown. The default implementation calls through to |
| 3650 | * {@link #onPrepareDialog(int, Dialog)} for compatibility. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3651 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3652 | * <p> |
| 3653 | * Override this if you need to update a managed dialog based on the state |
| 3654 | * of the application each time it is shown. For example, a time picker |
| 3655 | * dialog might want to be updated with the current time. You should call |
| 3656 | * through to the superclass's implementation. The default implementation |
| 3657 | * will set this Activity as the owner activity on the Dialog. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3658 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3659 | * @param id The id of the managed dialog. |
| 3660 | * @param dialog The dialog. |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3661 | * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}. |
| 3662 | * @see #onCreateDialog(int, Bundle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3663 | * @see #showDialog(int) |
| 3664 | * @see #dismissDialog(int) |
| 3665 | * @see #removeDialog(int) |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 3666 | * |
| 3667 | * @deprecated Use the new {@link DialogFragment} class with |
| 3668 | * {@link FragmentManager} instead; this is also |
| 3669 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3670 | */ |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 3671 | @Deprecated |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3672 | protected void onPrepareDialog(int id, Dialog dialog, Bundle args) { |
| 3673 | onPrepareDialog(id, dialog); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3674 | } |
| 3675 | |
| 3676 | /** |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3677 | * Simple version of {@link #showDialog(int, Bundle)} that does not |
| 3678 | * take any arguments. Simply calls {@link #showDialog(int, Bundle)} |
| 3679 | * with null arguments. |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 3680 | * |
| 3681 | * @deprecated Use the new {@link DialogFragment} class with |
| 3682 | * {@link FragmentManager} instead; this is also |
| 3683 | * available on older platforms through the Android compatibility package. |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3684 | */ |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 3685 | @Deprecated |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3686 | public final void showDialog(int id) { |
| 3687 | showDialog(id, null); |
| 3688 | } |
| 3689 | |
| 3690 | /** |
| 3691 | * Show a dialog managed by this activity. A call to {@link #onCreateDialog(int, Bundle)} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3692 | * will be made with the same id the first time this is called for a given |
| 3693 | * id. From thereafter, the dialog will be automatically saved and restored. |
| 3694 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 3695 | * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 3696 | * or later, consider instead using a {@link DialogFragment} instead.</em> |
| 3697 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3698 | * <p>Each time a dialog is shown, {@link #onPrepareDialog(int, Dialog, Bundle)} will |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3699 | * be made to provide an opportunity to do any timely preparation. |
| 3700 | * |
| 3701 | * @param id The id of the managed dialog. |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3702 | * @param args Arguments to pass through to the dialog. These will be saved |
| 3703 | * and restored for you. Note that if the dialog is already created, |
| 3704 | * {@link #onCreateDialog(int, Bundle)} will not be called with the new |
| 3705 | * arguments but {@link #onPrepareDialog(int, Dialog, Bundle)} will be. |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 3706 | * If you need to rebuild the dialog, call {@link #removeDialog(int)} first. |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3707 | * @return Returns true if the Dialog was created; false is returned if |
| 3708 | * it is not created because {@link #onCreateDialog(int, Bundle)} returns false. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3709 | * |
Joe Onorato | 37296dc | 2009-07-31 17:58:55 -0700 | [diff] [blame] | 3710 | * @see Dialog |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3711 | * @see #onCreateDialog(int, Bundle) |
| 3712 | * @see #onPrepareDialog(int, Dialog, Bundle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3713 | * @see #dismissDialog(int) |
| 3714 | * @see #removeDialog(int) |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 3715 | * |
| 3716 | * @deprecated Use the new {@link DialogFragment} class with |
| 3717 | * {@link FragmentManager} instead; this is also |
| 3718 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3719 | */ |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 3720 | @Deprecated |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3721 | public final boolean showDialog(int id, Bundle args) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3722 | if (mManagedDialogs == null) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3723 | mManagedDialogs = new SparseArray<ManagedDialog>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3724 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3725 | ManagedDialog md = mManagedDialogs.get(id); |
| 3726 | if (md == null) { |
| 3727 | md = new ManagedDialog(); |
| 3728 | md.mDialog = createDialog(id, null, args); |
| 3729 | if (md.mDialog == null) { |
| 3730 | return false; |
| 3731 | } |
| 3732 | mManagedDialogs.put(id, md); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3733 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3734 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3735 | md.mArgs = args; |
| 3736 | onPrepareDialog(id, md.mDialog, args); |
| 3737 | md.mDialog.show(); |
| 3738 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3739 | } |
| 3740 | |
| 3741 | /** |
| 3742 | * Dismiss a dialog that was previously shown via {@link #showDialog(int)}. |
| 3743 | * |
| 3744 | * @param id The id of the managed dialog. |
| 3745 | * |
| 3746 | * @throws IllegalArgumentException if the id was not previously shown via |
| 3747 | * {@link #showDialog(int)}. |
| 3748 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3749 | * @see #onCreateDialog(int, Bundle) |
| 3750 | * @see #onPrepareDialog(int, Dialog, Bundle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3751 | * @see #showDialog(int) |
| 3752 | * @see #removeDialog(int) |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 3753 | * |
| 3754 | * @deprecated Use the new {@link DialogFragment} class with |
| 3755 | * {@link FragmentManager} instead; this is also |
| 3756 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3757 | */ |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 3758 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3759 | public final void dismissDialog(int id) { |
| 3760 | if (mManagedDialogs == null) { |
| 3761 | throw missingDialog(id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3762 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3763 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3764 | final ManagedDialog md = mManagedDialogs.get(id); |
| 3765 | if (md == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3766 | throw missingDialog(id); |
| 3767 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3768 | md.mDialog.dismiss(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3769 | } |
| 3770 | |
| 3771 | /** |
| 3772 | * Creates an exception to throw if a user passed in a dialog id that is |
| 3773 | * unexpected. |
| 3774 | */ |
| 3775 | private IllegalArgumentException missingDialog(int id) { |
| 3776 | return new IllegalArgumentException("no dialog with id " + id + " was ever " |
| 3777 | + "shown via Activity#showDialog"); |
| 3778 | } |
| 3779 | |
| 3780 | /** |
| 3781 | * Removes any internal references to a dialog managed by this Activity. |
| 3782 | * If the dialog is showing, it will dismiss it as part of the clean up. |
| 3783 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3784 | * <p>This can be useful if you know that you will never show a dialog again and |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3785 | * want to avoid the overhead of saving and restoring it in the future. |
| 3786 | * |
Dianne Hackborn | d2ce8bbb | 2010-10-06 16:46:05 -0700 | [diff] [blame] | 3787 | * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, this function |
| 3788 | * will not throw an exception if you try to remove an ID that does not |
| 3789 | * currently have an associated dialog.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3790 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3791 | * @param id The id of the managed dialog. |
| 3792 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3793 | * @see #onCreateDialog(int, Bundle) |
| 3794 | * @see #onPrepareDialog(int, Dialog, Bundle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3795 | * @see #showDialog(int) |
| 3796 | * @see #dismissDialog(int) |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 3797 | * |
| 3798 | * @deprecated Use the new {@link DialogFragment} class with |
| 3799 | * {@link FragmentManager} instead; this is also |
| 3800 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3801 | */ |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 3802 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3803 | public final void removeDialog(int id) { |
Dianne Hackborn | d2ce8bbb | 2010-10-06 16:46:05 -0700 | [diff] [blame] | 3804 | if (mManagedDialogs != null) { |
| 3805 | final ManagedDialog md = mManagedDialogs.get(id); |
| 3806 | if (md != null) { |
| 3807 | md.mDialog.dismiss(); |
| 3808 | mManagedDialogs.remove(id); |
| 3809 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3810 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3811 | } |
| 3812 | |
| 3813 | /** |
| 3814 | * This hook is called when the user signals the desire to start a search. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3815 | * |
Bjorn Bringert | 6266e40 | 2009-09-25 14:25:41 +0100 | [diff] [blame] | 3816 | * <p>You can use this function as a simple way to launch the search UI, in response to a |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3817 | * menu item, search button, or other widgets within your activity. Unless overidden, |
Bjorn Bringert | 6266e40 | 2009-09-25 14:25:41 +0100 | [diff] [blame] | 3818 | * calling this function is the same as calling |
| 3819 | * {@link #startSearch startSearch(null, false, null, false)}, which launches |
| 3820 | * search for the current activity as specified in its manifest, see {@link SearchManager}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3821 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3822 | * <p>You can override this function to force global search, e.g. in response to a dedicated |
| 3823 | * search key, or to block search entirely (by simply returning false). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3824 | * |
Dianne Hackborn | fdf5b35 | 2014-10-08 17:43:48 -0700 | [diff] [blame] | 3825 | * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_TELEVISION}, the default |
| 3826 | * implementation changes to simply return false and you must supply your own custom |
| 3827 | * implementation if you want to support search.</p> |
| 3828 | * |
Tim Kilbourn | 6a975b3 | 2015-04-09 17:14:34 -0700 | [diff] [blame] | 3829 | * @param searchEvent The {@link SearchEvent} that signaled this search. |
Dianne Hackborn | fdf5b35 | 2014-10-08 17:43:48 -0700 | [diff] [blame] | 3830 | * @return Returns {@code true} if search launched, and {@code false} if the activity does |
| 3831 | * not respond to search. The default implementation always returns {@code true}, except |
| 3832 | * when in {@link Configuration#UI_MODE_TYPE_TELEVISION} mode where it returns false. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3833 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3834 | * @see android.app.SearchManager |
| 3835 | */ |
Tim Kilbourn | 6a975b3 | 2015-04-09 17:14:34 -0700 | [diff] [blame] | 3836 | public boolean onSearchRequested(@Nullable SearchEvent searchEvent) { |
| 3837 | mSearchEvent = searchEvent; |
| 3838 | boolean result = onSearchRequested(); |
| 3839 | mSearchEvent = null; |
| 3840 | return result; |
| 3841 | } |
| 3842 | |
| 3843 | /** |
| 3844 | * @see #onSearchRequested(SearchEvent) |
| 3845 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3846 | public boolean onSearchRequested() { |
Dianne Hackborn | fdf5b35 | 2014-10-08 17:43:48 -0700 | [diff] [blame] | 3847 | if ((getResources().getConfiguration().uiMode&Configuration.UI_MODE_TYPE_MASK) |
| 3848 | != Configuration.UI_MODE_TYPE_TELEVISION) { |
| 3849 | startSearch(null, false, null, false); |
| 3850 | return true; |
| 3851 | } else { |
| 3852 | return false; |
| 3853 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3854 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3855 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3856 | /** |
Tim Kilbourn | 6a975b3 | 2015-04-09 17:14:34 -0700 | [diff] [blame] | 3857 | * During the onSearchRequested() callbacks, this function will return the |
| 3858 | * {@link SearchEvent} that triggered the callback, if it exists. |
| 3859 | * |
| 3860 | * @return SearchEvent The SearchEvent that triggered the {@link |
| 3861 | * #onSearchRequested} callback. |
| 3862 | */ |
| 3863 | public final SearchEvent getSearchEvent() { |
| 3864 | return mSearchEvent; |
| 3865 | } |
| 3866 | |
| 3867 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3868 | * This hook is called to launch the search UI. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3869 | * |
| 3870 | * <p>It is typically called from onSearchRequested(), either directly from |
| 3871 | * Activity.onSearchRequested() or from an overridden version in any given |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3872 | * Activity. If your goal is simply to activate search, it is preferred to call |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3873 | * onSearchRequested(), which may have been overridden elsewhere in your Activity. If your goal |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3874 | * is to inject specific data such as context data, it is preferred to <i>override</i> |
| 3875 | * onSearchRequested(), so that any callers to it will benefit from the override. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3876 | * |
| 3877 | * @param initialQuery Any non-null non-empty string will be inserted as |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3878 | * pre-entered text in the search query box. |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3879 | * @param selectInitialQuery If true, the initial query will be preselected, which means that |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3880 | * any further typing will replace it. This is useful for cases where an entire pre-formed |
| 3881 | * query is being inserted. If false, the selection point will be placed at the end of the |
| 3882 | * inserted query. This is useful when the inserted query is text that the user entered, |
| 3883 | * and the user would expect to be able to keep typing. <i>This parameter is only meaningful |
| 3884 | * if initialQuery is a non-empty string.</i> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3885 | * @param appSearchData An application can insert application-specific |
| 3886 | * context here, in order to improve quality or specificity of its own |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3887 | * searches. This data will be returned with SEARCH intent(s). Null if |
| 3888 | * no extra data is required. |
| 3889 | * @param globalSearch If false, this will only launch the search that has been specifically |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3890 | * defined by the application (which is usually defined as a local search). If no default |
Mike LeBeau | cfa419b | 2009-08-17 10:56:02 -0700 | [diff] [blame] | 3891 | * search is defined in the current application or activity, global search will be launched. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3892 | * If true, this will always launch a platform-global (e.g. web-based) search instead. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3893 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3894 | * @see android.app.SearchManager |
| 3895 | * @see #onSearchRequested |
| 3896 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3897 | public void startSearch(@Nullable String initialQuery, boolean selectInitialQuery, |
| 3898 | @Nullable Bundle appSearchData, boolean globalSearch) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3899 | ensureSearchManager(); |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 3900 | mSearchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(), |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3901 | appSearchData, globalSearch); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3902 | } |
| 3903 | |
| 3904 | /** |
krosaen | d2d6014 | 2009-08-17 08:56:48 -0700 | [diff] [blame] | 3905 | * Similar to {@link #startSearch}, but actually fires off the search query after invoking |
| 3906 | * the search dialog. Made available for testing purposes. |
| 3907 | * |
| 3908 | * @param query The query to trigger. If empty, the request will be ignored. |
| 3909 | * @param appSearchData An application can insert application-specific |
| 3910 | * context here, in order to improve quality or specificity of its own |
| 3911 | * searches. This data will be returned with SEARCH intent(s). Null if |
| 3912 | * no extra data is required. |
krosaen | d2d6014 | 2009-08-17 08:56:48 -0700 | [diff] [blame] | 3913 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3914 | public void triggerSearch(String query, @Nullable Bundle appSearchData) { |
krosaen | d2d6014 | 2009-08-17 08:56:48 -0700 | [diff] [blame] | 3915 | ensureSearchManager(); |
Bjorn Bringert | b782a2f | 2009-10-01 09:57:33 +0100 | [diff] [blame] | 3916 | mSearchManager.triggerSearch(query, getComponentName(), appSearchData); |
krosaen | d2d6014 | 2009-08-17 08:56:48 -0700 | [diff] [blame] | 3917 | } |
| 3918 | |
| 3919 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3920 | * Request that key events come to this activity. Use this if your |
| 3921 | * activity has no views with focus, but the activity still wants |
| 3922 | * a chance to process key events. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3923 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3924 | * @see android.view.Window#takeKeyEvents |
| 3925 | */ |
| 3926 | public void takeKeyEvents(boolean get) { |
| 3927 | getWindow().takeKeyEvents(get); |
| 3928 | } |
| 3929 | |
| 3930 | /** |
| 3931 | * Enable extended window features. This is a convenience for calling |
| 3932 | * {@link android.view.Window#requestFeature getWindow().requestFeature()}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3933 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3934 | * @param featureId The desired feature as defined in |
| 3935 | * {@link android.view.Window}. |
| 3936 | * @return Returns true if the requested feature is supported and now |
| 3937 | * enabled. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3938 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3939 | * @see android.view.Window#requestFeature |
| 3940 | */ |
| 3941 | public final boolean requestWindowFeature(int featureId) { |
| 3942 | return getWindow().requestFeature(featureId); |
| 3943 | } |
| 3944 | |
| 3945 | /** |
| 3946 | * Convenience for calling |
| 3947 | * {@link android.view.Window#setFeatureDrawableResource}. |
| 3948 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 3949 | public final void setFeatureDrawableResource(int featureId, @DrawableRes int resId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3950 | getWindow().setFeatureDrawableResource(featureId, resId); |
| 3951 | } |
| 3952 | |
| 3953 | /** |
| 3954 | * Convenience for calling |
| 3955 | * {@link android.view.Window#setFeatureDrawableUri}. |
| 3956 | */ |
| 3957 | public final void setFeatureDrawableUri(int featureId, Uri uri) { |
| 3958 | getWindow().setFeatureDrawableUri(featureId, uri); |
| 3959 | } |
| 3960 | |
| 3961 | /** |
| 3962 | * Convenience for calling |
| 3963 | * {@link android.view.Window#setFeatureDrawable(int, Drawable)}. |
| 3964 | */ |
| 3965 | public final void setFeatureDrawable(int featureId, Drawable drawable) { |
| 3966 | getWindow().setFeatureDrawable(featureId, drawable); |
| 3967 | } |
| 3968 | |
| 3969 | /** |
| 3970 | * Convenience for calling |
| 3971 | * {@link android.view.Window#setFeatureDrawableAlpha}. |
| 3972 | */ |
| 3973 | public final void setFeatureDrawableAlpha(int featureId, int alpha) { |
| 3974 | getWindow().setFeatureDrawableAlpha(featureId, alpha); |
| 3975 | } |
| 3976 | |
| 3977 | /** |
| 3978 | * Convenience for calling |
| 3979 | * {@link android.view.Window#getLayoutInflater}. |
| 3980 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3981 | @NonNull |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3982 | public LayoutInflater getLayoutInflater() { |
| 3983 | return getWindow().getLayoutInflater(); |
| 3984 | } |
| 3985 | |
| 3986 | /** |
| 3987 | * Returns a {@link MenuInflater} with this context. |
| 3988 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3989 | @NonNull |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3990 | public MenuInflater getMenuInflater() { |
Adam Powell | 88ab697 | 2011-07-28 11:25:01 -0700 | [diff] [blame] | 3991 | // Make sure that action views can get an appropriate theme. |
| 3992 | if (mMenuInflater == null) { |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 3993 | initWindowDecorActionBar(); |
Adam Powell | 88ab697 | 2011-07-28 11:25:01 -0700 | [diff] [blame] | 3994 | if (mActionBar != null) { |
Dianne Hackborn | 9275197 | 2012-05-18 19:22:14 -0700 | [diff] [blame] | 3995 | mMenuInflater = new MenuInflater(mActionBar.getThemedContext(), this); |
Adam Powell | 88ab697 | 2011-07-28 11:25:01 -0700 | [diff] [blame] | 3996 | } else { |
| 3997 | mMenuInflater = new MenuInflater(this); |
| 3998 | } |
| 3999 | } |
| 4000 | return mMenuInflater; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4001 | } |
| 4002 | |
| 4003 | @Override |
Filip Gruszczynski | 0daf210 | 2015-09-29 08:39:07 -0700 | [diff] [blame] | 4004 | public void setTheme(int resid) { |
| 4005 | super.setTheme(resid); |
| 4006 | mWindow.setTheme(resid); |
| 4007 | } |
| 4008 | |
| 4009 | @Override |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 4010 | protected void onApplyThemeResource(Resources.Theme theme, @StyleRes int resid, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4011 | boolean first) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4012 | if (mParent == null) { |
| 4013 | super.onApplyThemeResource(theme, resid, first); |
| 4014 | } else { |
| 4015 | try { |
| 4016 | theme.setTo(mParent.getTheme()); |
| 4017 | } catch (Exception e) { |
| 4018 | // Empty |
| 4019 | } |
| 4020 | theme.applyStyle(resid, false); |
| 4021 | } |
Winson | 073a526 | 2016-02-29 16:04:40 -0800 | [diff] [blame] | 4022 | |
| 4023 | // Get the primary color and update the TaskDescription for this activity |
| 4024 | TypedArray a = theme.obtainStyledAttributes( |
| 4025 | com.android.internal.R.styleable.ActivityTaskDescription); |
| 4026 | if (mTaskDescription.getPrimaryColor() == 0) { |
| 4027 | int colorPrimary = a.getColor( |
| 4028 | com.android.internal.R.styleable.ActivityTaskDescription_colorPrimary, 0); |
| 4029 | if (colorPrimary != 0 && Color.alpha(colorPrimary) == 0xFF) { |
| 4030 | mTaskDescription.setPrimaryColor(colorPrimary); |
| 4031 | } |
| 4032 | } |
| 4033 | // For dev-preview only. |
| 4034 | if (mTaskDescription.getBackgroundColor() == 0) { |
| 4035 | int colorBackground = a.getColor( |
| 4036 | com.android.internal.R.styleable.ActivityTaskDescription_colorBackground, 0); |
| 4037 | if (colorBackground != 0 && Color.alpha(colorBackground) == 0xFF) { |
| 4038 | mTaskDescription.setBackgroundColor(colorBackground); |
| 4039 | } |
| 4040 | } |
| 4041 | a.recycle(); |
| 4042 | setTaskDescription(mTaskDescription); |
Winson Chung | 1af8eda | 2016-02-05 17:55:56 +0000 | [diff] [blame] | 4043 | } |
| 4044 | |
| 4045 | /** |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4046 | * Requests permissions to be granted to this application. These permissions |
| 4047 | * must be requested in your manifest, they should not be granted to your app, |
| 4048 | * and they should have protection level {@link android.content.pm.PermissionInfo |
| 4049 | * #PROTECTION_DANGEROUS dangerous}, regardless whether they are declared by |
| 4050 | * the platform or a third-party app. |
| 4051 | * <p> |
| 4052 | * Normal permissions {@link android.content.pm.PermissionInfo#PROTECTION_NORMAL} |
| 4053 | * are granted at install time if requested in the manifest. Signature permissions |
| 4054 | * {@link android.content.pm.PermissionInfo#PROTECTION_SIGNATURE} are granted at |
| 4055 | * install time if requested in the manifest and the signature of your app matches |
| 4056 | * the signature of the app declaring the permissions. |
| 4057 | * </p> |
| 4058 | * <p> |
| 4059 | * If your app does not have the requested permissions the user will be presented |
| 4060 | * with UI for accepting them. After the user has accepted or rejected the |
| 4061 | * requested permissions you will receive a callback on {@link |
| 4062 | * #onRequestPermissionsResult(int, String[], int[])} reporting whether the |
| 4063 | * permissions were granted or not. |
| 4064 | * </p> |
| 4065 | * <p> |
| 4066 | * Note that requesting a permission does not guarantee it will be granted and |
| 4067 | * your app should be able to run without having this permission. |
| 4068 | * </p> |
| 4069 | * <p> |
| 4070 | * This method may start an activity allowing the user to choose which permissions |
| 4071 | * to grant and which to reject. Hence, you should be prepared that your activity |
| 4072 | * may be paused and resumed. Further, granting some permissions may require |
| 4073 | * a restart of you application. In such a case, the system will recreate the |
| 4074 | * activity stack before delivering the result to {@link |
| 4075 | * #onRequestPermissionsResult(int, String[], int[])}. |
| 4076 | * </p> |
| 4077 | * <p> |
| 4078 | * When checking whether you have a permission you should use {@link |
| 4079 | * #checkSelfPermission(String)}. |
| 4080 | * </p> |
| 4081 | * <p> |
Svet Ganov | d7ad54e | 2015-08-21 06:18:09 -0700 | [diff] [blame] | 4082 | * Calling this API for permissions already granted to your app would show UI |
| 4083 | * to the user to decide whether the app can still hold these permissions. This |
| 4084 | * can be useful if the way your app uses data guarded by the permissions |
| 4085 | * changes significantly. |
| 4086 | * </p> |
| 4087 | * <p> |
Svet Ganov | 255aece | 2015-07-03 16:03:53 -0700 | [diff] [blame] | 4088 | * You cannot request a permission if your activity sets {@link |
| 4089 | * android.R.styleable#AndroidManifestActivity_noHistory noHistory} to |
| 4090 | * <code>true</code> because in this case the activity would not receive |
| 4091 | * result callbacks including {@link #onRequestPermissionsResult(int, String[], int[])}. |
| 4092 | * </p> |
| 4093 | * <p> |
Trevor Johns | 682c24e | 2016-04-12 10:13:47 -0700 | [diff] [blame] | 4094 | * The <a href="http://developer.android.com/samples/RuntimePermissions/index.html"> |
| 4095 | * RuntimePermissions</a> sample app demonstrates how to use this method to |
| 4096 | * request permissions at run time. |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4097 | * </p> |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4098 | * |
Svet Ganov | f66381c | 2016-02-18 20:02:36 -0800 | [diff] [blame] | 4099 | * @param permissions The requested permissions. Must me non-null and not empty. |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4100 | * @param requestCode Application specific request code to match with a result |
| 4101 | * reported to {@link #onRequestPermissionsResult(int, String[], int[])}. |
Svet Ganov | 255aece | 2015-07-03 16:03:53 -0700 | [diff] [blame] | 4102 | * Should be >= 0. |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4103 | * |
Paul Miller | 77a0e13 | 2016-02-11 13:46:49 -0800 | [diff] [blame] | 4104 | * @throws IllegalArgumentException if requestCode is negative. |
| 4105 | * |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4106 | * @see #onRequestPermissionsResult(int, String[], int[]) |
| 4107 | * @see #checkSelfPermission(String) |
Svetoslav | aa048b6 | 2015-06-02 15:08:48 -0700 | [diff] [blame] | 4108 | * @see #shouldShowRequestPermissionRationale(String) |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4109 | */ |
| 4110 | public final void requestPermissions(@NonNull String[] permissions, int requestCode) { |
Paul Miller | 77a0e13 | 2016-02-11 13:46:49 -0800 | [diff] [blame] | 4111 | if (requestCode < 0) { |
| 4112 | throw new IllegalArgumentException("requestCode should be >= 0"); |
| 4113 | } |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 4114 | if (mHasCurrentPermissionsRequest) { |
| 4115 | Log.w(TAG, "Can reqeust only one set of permissions at a time"); |
| 4116 | // Dispatch the callback with empty arrays which means a cancellation. |
| 4117 | onRequestPermissionsResult(requestCode, new String[0], new int[0]); |
| 4118 | return; |
| 4119 | } |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4120 | Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions); |
Svetoslav | 970b59c | 2015-06-09 16:05:21 -0700 | [diff] [blame] | 4121 | startActivityForResult(REQUEST_PERMISSIONS_WHO_PREFIX, intent, requestCode, null); |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 4122 | mHasCurrentPermissionsRequest = true; |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4123 | } |
| 4124 | |
| 4125 | /** |
| 4126 | * Callback for the result from requesting permissions. This method |
| 4127 | * is invoked for every call on {@link #requestPermissions(String[], int)}. |
Svet Ganov | 0f45c7c | 2015-07-16 16:28:51 -0700 | [diff] [blame] | 4128 | * <p> |
| 4129 | * <strong>Note:</strong> It is possible that the permissions request interaction |
| 4130 | * with the user is interrupted. In this case you will receive empty permissions |
| 4131 | * and results arrays which should be treated as a cancellation. |
| 4132 | * </p> |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4133 | * |
| 4134 | * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}. |
| 4135 | * @param permissions The requested permissions. Never null. |
| 4136 | * @param grantResults The grant results for the corresponding permissions |
| 4137 | * which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED} |
| 4138 | * or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null. |
| 4139 | * |
| 4140 | * @see #requestPermissions(String[], int) |
| 4141 | */ |
| 4142 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, |
| 4143 | @NonNull int[] grantResults) { |
| 4144 | /* callback - no nothing */ |
| 4145 | } |
| 4146 | |
| 4147 | /** |
Svetoslav | 20770dd | 2015-05-29 15:43:04 -0700 | [diff] [blame] | 4148 | * Gets whether you should show UI with rationale for requesting a permission. |
| 4149 | * You should do this only if you do not have the permission and the context in |
| 4150 | * which the permission is requested does not clearly communicate to the user |
| 4151 | * what would be the benefit from granting this permission. |
| 4152 | * <p> |
| 4153 | * For example, if you write a camera app, requesting the camera permission |
| 4154 | * would be expected by the user and no rationale for why it is requested is |
| 4155 | * needed. If however, the app needs location for tagging photos then a non-tech |
| 4156 | * savvy user may wonder how location is related to taking photos. In this case |
| 4157 | * you may choose to show UI with rationale of requesting this permission. |
| 4158 | * </p> |
| 4159 | * |
| 4160 | * @param permission A permission your app wants to request. |
| 4161 | * @return Whether you can show permission rationale UI. |
| 4162 | * |
| 4163 | * @see #checkSelfPermission(String) |
| 4164 | * @see #requestPermissions(String[], int) |
| 4165 | * @see #onRequestPermissionsResult(int, String[], int[]) |
| 4166 | */ |
Svetoslav | 9ce9a15 | 2015-06-02 17:59:31 -0700 | [diff] [blame] | 4167 | public boolean shouldShowRequestPermissionRationale(@NonNull String permission) { |
Svetoslav | 20770dd | 2015-05-29 15:43:04 -0700 | [diff] [blame] | 4168 | return getPackageManager().shouldShowRequestPermissionRationale(permission); |
| 4169 | } |
| 4170 | |
| 4171 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4172 | * Same as calling {@link #startActivityForResult(Intent, int, Bundle)} |
| 4173 | * with no options. |
| 4174 | * |
| 4175 | * @param intent The intent to start. |
| 4176 | * @param requestCode If >= 0, this code will be returned in |
| 4177 | * onActivityResult() when the activity exits. |
| 4178 | * |
| 4179 | * @throws android.content.ActivityNotFoundException |
| 4180 | * |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 4181 | * @see #startActivity |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4182 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4183 | public void startActivityForResult(@RequiresPermission Intent intent, int requestCode) { |
George Mount | abb63cb | 2014-06-23 11:17:58 -0700 | [diff] [blame] | 4184 | startActivityForResult(intent, requestCode, null); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4185 | } |
| 4186 | |
| 4187 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4188 | * Launch an activity for which you would like a result when it finished. |
| 4189 | * When this activity exits, your |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 4190 | * onActivityResult() method will be called with the given requestCode. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4191 | * Using a negative requestCode is the same as calling |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4192 | * {@link #startActivity} (the activity is not launched as a sub-activity). |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4193 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4194 | * <p>Note that this method should only be used with Intent protocols |
| 4195 | * that are defined to return a result. In other protocols (such as |
| 4196 | * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may |
| 4197 | * not get the result when you expect. For example, if the activity you |
| 4198 | * are launching uses the singleTask launch mode, it will not run in your |
| 4199 | * task and thus you will immediately receive a cancel result. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4200 | * |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4201 | * <p>As a special case, if you call startActivityForResult() with a requestCode |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4202 | * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 4203 | * activity, then your window will not be displayed until a result is |
| 4204 | * returned back from the started activity. This is to avoid visible |
| 4205 | * flickering when redirecting to another activity. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4206 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4207 | * <p>This method throws {@link android.content.ActivityNotFoundException} |
| 4208 | * if there was no Activity found to run the given Intent. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4209 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4210 | * @param intent The intent to start. |
| 4211 | * @param requestCode If >= 0, this code will be returned in |
| 4212 | * onActivityResult() when the activity exits. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4213 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4214 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4215 | * Context.startActivity(Intent, Bundle)} for more details. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4216 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4217 | * @throws android.content.ActivityNotFoundException |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4218 | * |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 4219 | * @see #startActivity |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4220 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4221 | public void startActivityForResult(@RequiresPermission Intent intent, int requestCode, |
| 4222 | @Nullable Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4223 | if (mParent == null) { |
George Mount | 413739e | 2016-06-08 07:13:37 -0700 | [diff] [blame] | 4224 | options = transferSpringboardActivityOptions(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4225 | Instrumentation.ActivityResult ar = |
| 4226 | mInstrumentation.execStartActivity( |
| 4227 | this, mMainThread.getApplicationThread(), mToken, this, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4228 | intent, requestCode, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4229 | if (ar != null) { |
| 4230 | mMainThread.sendActivityResult( |
| 4231 | mToken, mEmbeddedID, requestCode, ar.getResultCode(), |
| 4232 | ar.getResultData()); |
| 4233 | } |
| 4234 | if (requestCode >= 0) { |
| 4235 | // If this start is requesting a result, we can avoid making |
| 4236 | // the activity visible until the result is received. Setting |
| 4237 | // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the |
| 4238 | // activity hidden during this time, to avoid flickering. |
| 4239 | // This can only be done when a result is requested because |
| 4240 | // that guarantees we will get information back when the |
| 4241 | // activity is finished, no matter what happens to it. |
| 4242 | mStartedActivity = true; |
| 4243 | } |
Adam Powell | 1487466 | 2013-07-18 19:42:41 -0700 | [diff] [blame] | 4244 | |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 4245 | cancelInputsAndStartExitTransition(options); |
Adam Powell | 1487466 | 2013-07-18 19:42:41 -0700 | [diff] [blame] | 4246 | // TODO Consider clearing/flushing other event sources and events for child windows. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4247 | } else { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4248 | if (options != null) { |
| 4249 | mParent.startActivityFromChild(this, intent, requestCode, options); |
| 4250 | } else { |
| 4251 | // Note we want to go through this method for compatibility with |
| 4252 | // existing applications that may have overridden it. |
| 4253 | mParent.startActivityFromChild(this, intent, requestCode); |
| 4254 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4255 | } |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 4256 | } |
| 4257 | |
| 4258 | /** |
| 4259 | * Cancels pending inputs and if an Activity Transition is to be run, starts the transition. |
| 4260 | * |
| 4261 | * @param options The ActivityOptions bundle used to start an Activity. |
| 4262 | */ |
| 4263 | private void cancelInputsAndStartExitTransition(Bundle options) { |
| 4264 | final View decor = mWindow != null ? mWindow.peekDecorView() : null; |
| 4265 | if (decor != null) { |
| 4266 | decor.cancelPendingInputEvents(); |
| 4267 | } |
George Mount | ff24328 | 2014-07-07 16:12:07 -0700 | [diff] [blame] | 4268 | if (options != null && !isTopOfTask()) { |
| 4269 | mActivityTransitionState.startExitOutTransition(this, options); |
| 4270 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4271 | } |
| 4272 | |
George Mount | 413739e | 2016-06-08 07:13:37 -0700 | [diff] [blame] | 4273 | private Bundle transferSpringboardActivityOptions(Bundle options) { |
| 4274 | if (options == null && (mWindow != null && !mWindow.isActive())) { |
| 4275 | final ActivityOptions activityOptions = getActivityOptions(); |
George Mount | 9638378 | 2016-06-08 15:13:52 -0700 | [diff] [blame] | 4276 | if (activityOptions != null && |
| 4277 | activityOptions.getAnimationType() == ActivityOptions.ANIM_SCENE_TRANSITION) { |
George Mount | 413739e | 2016-06-08 07:13:37 -0700 | [diff] [blame] | 4278 | return activityOptions.toBundle(); |
| 4279 | } |
| 4280 | } |
| 4281 | return options; |
| 4282 | } |
| 4283 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4284 | /** |
Dianne Hackborn | f1c26e2 | 2012-08-23 13:54:58 -0700 | [diff] [blame] | 4285 | * @hide Implement to provide correct calling token. |
| 4286 | */ |
Alexandra Gherghina | 8323145 | 2014-07-04 12:05:20 +0100 | [diff] [blame] | 4287 | public void startActivityForResultAsUser(Intent intent, int requestCode, UserHandle user) { |
| 4288 | startActivityForResultAsUser(intent, requestCode, null, user); |
| 4289 | } |
| 4290 | |
| 4291 | /** |
| 4292 | * @hide Implement to provide correct calling token. |
| 4293 | */ |
| 4294 | public void startActivityForResultAsUser(Intent intent, int requestCode, |
| 4295 | @Nullable Bundle options, UserHandle user) { |
Alexandra Gherghina | 8323145 | 2014-07-04 12:05:20 +0100 | [diff] [blame] | 4296 | if (mParent != null) { |
| 4297 | throw new RuntimeException("Can't be called from a child"); |
| 4298 | } |
George Mount | 413739e | 2016-06-08 07:13:37 -0700 | [diff] [blame] | 4299 | options = transferSpringboardActivityOptions(options); |
Alexandra Gherghina | 8323145 | 2014-07-04 12:05:20 +0100 | [diff] [blame] | 4300 | Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity( |
| 4301 | this, mMainThread.getApplicationThread(), mToken, this, intent, requestCode, |
| 4302 | options, user); |
| 4303 | if (ar != null) { |
| 4304 | mMainThread.sendActivityResult( |
| 4305 | mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData()); |
| 4306 | } |
| 4307 | if (requestCode >= 0) { |
| 4308 | // If this start is requesting a result, we can avoid making |
| 4309 | // the activity visible until the result is received. Setting |
| 4310 | // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the |
| 4311 | // activity hidden during this time, to avoid flickering. |
| 4312 | // This can only be done when a result is requested because |
| 4313 | // that guarantees we will get information back when the |
| 4314 | // activity is finished, no matter what happens to it. |
| 4315 | mStartedActivity = true; |
| 4316 | } |
| 4317 | |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 4318 | cancelInputsAndStartExitTransition(options); |
Alexandra Gherghina | 8323145 | 2014-07-04 12:05:20 +0100 | [diff] [blame] | 4319 | } |
| 4320 | |
| 4321 | /** |
| 4322 | * @hide Implement to provide correct calling token. |
| 4323 | */ |
Dianne Hackborn | f1c26e2 | 2012-08-23 13:54:58 -0700 | [diff] [blame] | 4324 | public void startActivityAsUser(Intent intent, UserHandle user) { |
| 4325 | startActivityAsUser(intent, null, user); |
| 4326 | } |
| 4327 | |
| 4328 | /** |
| 4329 | * @hide Implement to provide correct calling token. |
| 4330 | */ |
| 4331 | public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) { |
| 4332 | if (mParent != null) { |
Alexandra Gherghina | 8323145 | 2014-07-04 12:05:20 +0100 | [diff] [blame] | 4333 | throw new RuntimeException("Can't be called from a child"); |
Dianne Hackborn | f1c26e2 | 2012-08-23 13:54:58 -0700 | [diff] [blame] | 4334 | } |
George Mount | 413739e | 2016-06-08 07:13:37 -0700 | [diff] [blame] | 4335 | options = transferSpringboardActivityOptions(options); |
Dianne Hackborn | f1c26e2 | 2012-08-23 13:54:58 -0700 | [diff] [blame] | 4336 | Instrumentation.ActivityResult ar = |
| 4337 | mInstrumentation.execStartActivity( |
| 4338 | this, mMainThread.getApplicationThread(), mToken, this, |
| 4339 | intent, -1, options, user); |
| 4340 | if (ar != null) { |
| 4341 | mMainThread.sendActivityResult( |
| 4342 | mToken, mEmbeddedID, -1, ar.getResultCode(), |
| 4343 | ar.getResultData()); |
| 4344 | } |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 4345 | cancelInputsAndStartExitTransition(options); |
Dianne Hackborn | f1c26e2 | 2012-08-23 13:54:58 -0700 | [diff] [blame] | 4346 | } |
| 4347 | |
| 4348 | /** |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 4349 | * Start a new activity as if it was started by the activity that started our |
| 4350 | * current activity. This is for the resolver and chooser activities, which operate |
| 4351 | * as intermediaries that dispatch their intent to the target the user selects -- to |
| 4352 | * do this, they must perform all security checks including permission grants as if |
| 4353 | * their launch had come from the original activity. |
Dianne Hackborn | a7cfbe0 | 2015-07-16 10:52:52 -0700 | [diff] [blame] | 4354 | * @param intent The Intent to start. |
| 4355 | * @param options ActivityOptions or null. |
| 4356 | * @param ignoreTargetSecurity If true, the activity manager will not check whether the |
| 4357 | * caller it is doing the start is, is actually allowed to start the target activity. |
| 4358 | * If you set this to true, you must set an explicit component in the Intent and do any |
| 4359 | * appropriate security checks yourself. |
| 4360 | * @param userId The user the new activity should run as. |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 4361 | * @hide |
| 4362 | */ |
Dianne Hackborn | a7cfbe0 | 2015-07-16 10:52:52 -0700 | [diff] [blame] | 4363 | public void startActivityAsCaller(Intent intent, @Nullable Bundle options, |
| 4364 | boolean ignoreTargetSecurity, int userId) { |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 4365 | if (mParent != null) { |
| 4366 | throw new RuntimeException("Can't be called from a child"); |
| 4367 | } |
George Mount | 413739e | 2016-06-08 07:13:37 -0700 | [diff] [blame] | 4368 | options = transferSpringboardActivityOptions(options); |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 4369 | Instrumentation.ActivityResult ar = |
| 4370 | mInstrumentation.execStartActivityAsCaller( |
| 4371 | this, mMainThread.getApplicationThread(), mToken, this, |
Dianne Hackborn | a7cfbe0 | 2015-07-16 10:52:52 -0700 | [diff] [blame] | 4372 | intent, -1, options, ignoreTargetSecurity, userId); |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 4373 | if (ar != null) { |
| 4374 | mMainThread.sendActivityResult( |
| 4375 | mToken, mEmbeddedID, -1, ar.getResultCode(), |
| 4376 | ar.getResultData()); |
| 4377 | } |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 4378 | cancelInputsAndStartExitTransition(options); |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 4379 | } |
| 4380 | |
| 4381 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4382 | * Same as calling {@link #startIntentSenderForResult(IntentSender, int, |
| 4383 | * Intent, int, int, int, Bundle)} with no options. |
| 4384 | * |
| 4385 | * @param intent The IntentSender to launch. |
| 4386 | * @param requestCode If >= 0, this code will be returned in |
| 4387 | * onActivityResult() when the activity exits. |
| 4388 | * @param fillInIntent If non-null, this will be provided as the |
| 4389 | * intent parameter to {@link IntentSender#sendIntent}. |
| 4390 | * @param flagsMask Intent flags in the original IntentSender that you |
| 4391 | * would like to change. |
| 4392 | * @param flagsValues Desired values for any bits set in |
| 4393 | * <var>flagsMask</var> |
| 4394 | * @param extraFlags Always set to 0. |
| 4395 | */ |
| 4396 | public void startIntentSenderForResult(IntentSender intent, int requestCode, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4397 | @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4398 | throws IntentSender.SendIntentException { |
| 4399 | startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask, |
| 4400 | flagsValues, extraFlags, null); |
| 4401 | } |
| 4402 | |
| 4403 | /** |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4404 | * Like {@link #startActivityForResult(Intent, int)}, but allowing you |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4405 | * to use a IntentSender to describe the activity to be started. If |
| 4406 | * the IntentSender is for an activity, that activity will be started |
| 4407 | * as if you had called the regular {@link #startActivityForResult(Intent, int)} |
| 4408 | * here; otherwise, its associated action will be executed (such as |
| 4409 | * sending a broadcast) as if you had called |
| 4410 | * {@link IntentSender#sendIntent IntentSender.sendIntent} on it. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4411 | * |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4412 | * @param intent The IntentSender to launch. |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4413 | * @param requestCode If >= 0, this code will be returned in |
| 4414 | * onActivityResult() when the activity exits. |
| 4415 | * @param fillInIntent If non-null, this will be provided as the |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4416 | * intent parameter to {@link IntentSender#sendIntent}. |
| 4417 | * @param flagsMask Intent flags in the original IntentSender that you |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4418 | * would like to change. |
| 4419 | * @param flagsValues Desired values for any bits set in |
| 4420 | * <var>flagsMask</var> |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4421 | * @param extraFlags Always set to 0. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4422 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4423 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4424 | * Context.startActivity(Intent, Bundle)} for more details. If options |
| 4425 | * have also been supplied by the IntentSender, options given here will |
| 4426 | * override any that conflict with those given by the IntentSender. |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4427 | */ |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4428 | public void startIntentSenderForResult(IntentSender intent, int requestCode, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4429 | @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4430 | Bundle options) throws IntentSender.SendIntentException { |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4431 | if (mParent == null) { |
Clara Bayarri | a0c2dc3 | 2016-04-12 12:00:15 +0100 | [diff] [blame] | 4432 | startIntentSenderForResultInner(intent, mEmbeddedID, requestCode, fillInIntent, |
| 4433 | flagsMask, flagsValues, options); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4434 | } else if (options != null) { |
| 4435 | mParent.startIntentSenderFromChild(this, intent, requestCode, |
| 4436 | fillInIntent, flagsMask, flagsValues, extraFlags, options); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4437 | } else { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4438 | // Note we want to go through this call for compatibility with |
| 4439 | // existing applications that may have overridden the method. |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4440 | mParent.startIntentSenderFromChild(this, intent, requestCode, |
| 4441 | fillInIntent, flagsMask, flagsValues, extraFlags); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4442 | } |
| 4443 | } |
| 4444 | |
Clara Bayarri | a0c2dc3 | 2016-04-12 12:00:15 +0100 | [diff] [blame] | 4445 | private void startIntentSenderForResultInner(IntentSender intent, String who, int requestCode, |
| 4446 | Intent fillInIntent, int flagsMask, int flagsValues, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4447 | Bundle options) |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4448 | throws IntentSender.SendIntentException { |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4449 | try { |
| 4450 | String resolvedType = null; |
| 4451 | if (fillInIntent != null) { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 4452 | fillInIntent.migrateExtraStreamToClipData(); |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 4453 | fillInIntent.prepareToLeaveProcess(this); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4454 | resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver()); |
| 4455 | } |
| 4456 | int result = ActivityManagerNative.getDefault() |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4457 | .startActivityIntentSender(mMainThread.getApplicationThread(), intent, |
Clara Bayarri | a0c2dc3 | 2016-04-12 12:00:15 +0100 | [diff] [blame] | 4458 | fillInIntent, resolvedType, mToken, who, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4459 | requestCode, flagsMask, flagsValues, options); |
| 4460 | if (result == ActivityManager.START_CANCELED) { |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4461 | throw new IntentSender.SendIntentException(); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4462 | } |
| 4463 | Instrumentation.checkStartActivityResult(result, null); |
| 4464 | } catch (RemoteException e) { |
| 4465 | } |
| 4466 | if (requestCode >= 0) { |
| 4467 | // If this start is requesting a result, we can avoid making |
| 4468 | // the activity visible until the result is received. Setting |
| 4469 | // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the |
| 4470 | // activity hidden during this time, to avoid flickering. |
| 4471 | // This can only be done when a result is requested because |
| 4472 | // that guarantees we will get information back when the |
| 4473 | // activity is finished, no matter what happens to it. |
| 4474 | mStartedActivity = true; |
| 4475 | } |
| 4476 | } |
| 4477 | |
| 4478 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4479 | * Same as {@link #startActivity(Intent, Bundle)} with no options |
| 4480 | * specified. |
| 4481 | * |
| 4482 | * @param intent The intent to start. |
| 4483 | * |
| 4484 | * @throws android.content.ActivityNotFoundException |
| 4485 | * |
Mark Lu | fa43485 | 2016-08-11 17:40:33 -0700 | [diff] [blame] | 4486 | * @see #startActivity(Intent, Bundle) |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4487 | * @see #startActivityForResult |
| 4488 | */ |
| 4489 | @Override |
| 4490 | public void startActivity(Intent intent) { |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 4491 | this.startActivity(intent, null); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4492 | } |
| 4493 | |
| 4494 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4495 | * Launch a new activity. You will not receive any information about when |
| 4496 | * the activity exits. This implementation overrides the base version, |
| 4497 | * providing information about |
| 4498 | * the activity performing the launch. Because of this additional |
| 4499 | * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not |
| 4500 | * required; if not specified, the new activity will be added to the |
| 4501 | * task of the caller. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4502 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4503 | * <p>This method throws {@link android.content.ActivityNotFoundException} |
| 4504 | * if there was no Activity found to run the given Intent. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4505 | * |
| 4506 | * @param intent The intent to start. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4507 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4508 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4509 | * Context.startActivity(Intent, Bundle)} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4510 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4511 | * @throws android.content.ActivityNotFoundException |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4512 | * |
Mark Lu | fa43485 | 2016-08-11 17:40:33 -0700 | [diff] [blame] | 4513 | * @see #startActivity(Intent) |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4514 | * @see #startActivityForResult |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4515 | */ |
| 4516 | @Override |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4517 | public void startActivity(Intent intent, @Nullable Bundle options) { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4518 | if (options != null) { |
| 4519 | startActivityForResult(intent, -1, options); |
| 4520 | } else { |
| 4521 | // Note we want to go through this call for compatibility with |
| 4522 | // applications that may have overridden the method. |
| 4523 | startActivityForResult(intent, -1); |
| 4524 | } |
| 4525 | } |
| 4526 | |
| 4527 | /** |
| 4528 | * Same as {@link #startActivities(Intent[], Bundle)} with no options |
| 4529 | * specified. |
| 4530 | * |
| 4531 | * @param intents The intents to start. |
| 4532 | * |
| 4533 | * @throws android.content.ActivityNotFoundException |
| 4534 | * |
Mark Lu | fa43485 | 2016-08-11 17:40:33 -0700 | [diff] [blame] | 4535 | * @see #startActivities(Intent[], Bundle) |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4536 | * @see #startActivityForResult |
| 4537 | */ |
| 4538 | @Override |
| 4539 | public void startActivities(Intent[] intents) { |
| 4540 | startActivities(intents, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4541 | } |
| 4542 | |
| 4543 | /** |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4544 | * Launch a new activity. You will not receive any information about when |
| 4545 | * the activity exits. This implementation overrides the base version, |
| 4546 | * providing information about |
| 4547 | * the activity performing the launch. Because of this additional |
| 4548 | * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not |
| 4549 | * required; if not specified, the new activity will be added to the |
| 4550 | * task of the caller. |
| 4551 | * |
| 4552 | * <p>This method throws {@link android.content.ActivityNotFoundException} |
| 4553 | * if there was no Activity found to run the given Intent. |
| 4554 | * |
| 4555 | * @param intents The intents to start. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4556 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4557 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4558 | * Context.startActivity(Intent, Bundle)} for more details. |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4559 | * |
| 4560 | * @throws android.content.ActivityNotFoundException |
| 4561 | * |
Mark Lu | fa43485 | 2016-08-11 17:40:33 -0700 | [diff] [blame] | 4562 | * @see #startActivities(Intent[]) |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4563 | * @see #startActivityForResult |
| 4564 | */ |
| 4565 | @Override |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4566 | public void startActivities(Intent[] intents, @Nullable Bundle options) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4567 | mInstrumentation.execStartActivities(this, mMainThread.getApplicationThread(), |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4568 | mToken, this, intents, options); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4569 | } |
| 4570 | |
| 4571 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4572 | * Same as calling {@link #startIntentSender(IntentSender, Intent, int, int, int, Bundle)} |
| 4573 | * with no options. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4574 | * |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4575 | * @param intent The IntentSender to launch. |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4576 | * @param fillInIntent If non-null, this will be provided as the |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4577 | * intent parameter to {@link IntentSender#sendIntent}. |
| 4578 | * @param flagsMask Intent flags in the original IntentSender that you |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4579 | * would like to change. |
| 4580 | * @param flagsValues Desired values for any bits set in |
| 4581 | * <var>flagsMask</var> |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4582 | * @param extraFlags Always set to 0. |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4583 | */ |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4584 | public void startIntentSender(IntentSender intent, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4585 | @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4586 | throws IntentSender.SendIntentException { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4587 | startIntentSender(intent, fillInIntent, flagsMask, flagsValues, |
| 4588 | extraFlags, null); |
| 4589 | } |
| 4590 | |
| 4591 | /** |
| 4592 | * Like {@link #startActivity(Intent, Bundle)}, but taking a IntentSender |
| 4593 | * to start; see |
| 4594 | * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int, Bundle)} |
| 4595 | * for more information. |
| 4596 | * |
| 4597 | * @param intent The IntentSender to launch. |
| 4598 | * @param fillInIntent If non-null, this will be provided as the |
| 4599 | * intent parameter to {@link IntentSender#sendIntent}. |
| 4600 | * @param flagsMask Intent flags in the original IntentSender that you |
| 4601 | * would like to change. |
| 4602 | * @param flagsValues Desired values for any bits set in |
| 4603 | * <var>flagsMask</var> |
| 4604 | * @param extraFlags Always set to 0. |
| 4605 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4606 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4607 | * Context.startActivity(Intent, Bundle)} for more details. If options |
| 4608 | * have also been supplied by the IntentSender, options given here will |
| 4609 | * override any that conflict with those given by the IntentSender. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4610 | */ |
| 4611 | public void startIntentSender(IntentSender intent, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4612 | @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4613 | Bundle options) throws IntentSender.SendIntentException { |
| 4614 | if (options != null) { |
| 4615 | startIntentSenderForResult(intent, -1, fillInIntent, flagsMask, |
| 4616 | flagsValues, extraFlags, options); |
| 4617 | } else { |
| 4618 | // Note we want to go through this call for compatibility with |
| 4619 | // applications that may have overridden the method. |
| 4620 | startIntentSenderForResult(intent, -1, fillInIntent, flagsMask, |
| 4621 | flagsValues, extraFlags); |
| 4622 | } |
| 4623 | } |
| 4624 | |
| 4625 | /** |
| 4626 | * Same as calling {@link #startActivityIfNeeded(Intent, int, Bundle)} |
| 4627 | * with no options. |
| 4628 | * |
| 4629 | * @param intent The intent to start. |
| 4630 | * @param requestCode If >= 0, this code will be returned in |
| 4631 | * onActivityResult() when the activity exits, as described in |
| 4632 | * {@link #startActivityForResult}. |
| 4633 | * |
| 4634 | * @return If a new activity was launched then true is returned; otherwise |
| 4635 | * false is returned and you must handle the Intent yourself. |
| 4636 | * |
| 4637 | * @see #startActivity |
| 4638 | * @see #startActivityForResult |
| 4639 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4640 | public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent, |
| 4641 | int requestCode) { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4642 | return startActivityIfNeeded(intent, requestCode, null); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4643 | } |
| 4644 | |
| 4645 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4646 | * A special variation to launch an activity only if a new activity |
| 4647 | * instance is needed to handle the given Intent. In other words, this is |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4648 | * just like {@link #startActivityForResult(Intent, int)} except: if you are |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4649 | * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4650 | * singleTask or singleTop |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4651 | * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode}, |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4652 | * and the activity |
| 4653 | * that handles <var>intent</var> is the same as your currently running |
| 4654 | * activity, then a new instance is not needed. In this case, instead of |
| 4655 | * the normal behavior of calling {@link #onNewIntent} this function will |
| 4656 | * return and you can handle the Intent yourself. |
| 4657 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4658 | * <p>This function can only be called from a top-level activity; if it is |
| 4659 | * called from a child activity, a runtime exception will be thrown. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4660 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4661 | * @param intent The intent to start. |
| 4662 | * @param requestCode If >= 0, this code will be returned in |
| 4663 | * onActivityResult() when the activity exits, as described in |
| 4664 | * {@link #startActivityForResult}. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4665 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4666 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4667 | * Context.startActivity(Intent, Bundle)} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4668 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4669 | * @return If a new activity was launched then true is returned; otherwise |
| 4670 | * false is returned and you must handle the Intent yourself. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4671 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4672 | * @see #startActivity |
| 4673 | * @see #startActivityForResult |
| 4674 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4675 | public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent, |
| 4676 | int requestCode, @Nullable Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4677 | if (mParent == null) { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4678 | int result = ActivityManager.START_RETURN_INTENT_TO_CALLER; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4679 | try { |
Dianne Hackborn | a3acdb3 | 2015-06-08 17:07:40 -0700 | [diff] [blame] | 4680 | Uri referrer = onProvideReferrer(); |
| 4681 | if (referrer != null) { |
| 4682 | intent.putExtra(Intent.EXTRA_REFERRER, referrer); |
| 4683 | } |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 4684 | intent.migrateExtraStreamToClipData(); |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 4685 | intent.prepareToLeaveProcess(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4686 | result = ActivityManagerNative.getDefault() |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 4687 | .startActivity(mMainThread.getApplicationThread(), getBasePackageName(), |
Jeff Hao | 1b012d3 | 2014-08-20 10:35:34 -0700 | [diff] [blame] | 4688 | intent, intent.resolveTypeIfNeeded(getContentResolver()), mToken, |
| 4689 | mEmbeddedID, requestCode, ActivityManager.START_FLAG_ONLY_IF_NEEDED, |
| 4690 | null, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4691 | } catch (RemoteException e) { |
| 4692 | // Empty |
| 4693 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 4694 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4695 | Instrumentation.checkStartActivityResult(result, intent); |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 4696 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4697 | if (requestCode >= 0) { |
| 4698 | // If this start is requesting a result, we can avoid making |
| 4699 | // the activity visible until the result is received. Setting |
| 4700 | // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the |
| 4701 | // activity hidden during this time, to avoid flickering. |
| 4702 | // This can only be done when a result is requested because |
| 4703 | // that guarantees we will get information back when the |
| 4704 | // activity is finished, no matter what happens to it. |
| 4705 | mStartedActivity = true; |
| 4706 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4707 | return result != ActivityManager.START_RETURN_INTENT_TO_CALLER; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4708 | } |
| 4709 | |
| 4710 | throw new UnsupportedOperationException( |
| 4711 | "startActivityIfNeeded can only be called from a top-level activity"); |
| 4712 | } |
| 4713 | |
| 4714 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4715 | * Same as calling {@link #startNextMatchingActivity(Intent, Bundle)} with |
| 4716 | * no options. |
| 4717 | * |
| 4718 | * @param intent The intent to dispatch to the next activity. For |
| 4719 | * correct behavior, this must be the same as the Intent that started |
| 4720 | * your own activity; the only changes you can make are to the extras |
| 4721 | * inside of it. |
| 4722 | * |
| 4723 | * @return Returns a boolean indicating whether there was another Activity |
| 4724 | * to start: true if there was a next activity to start, false if there |
| 4725 | * wasn't. In general, if true is returned you will then want to call |
| 4726 | * finish() on yourself. |
| 4727 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4728 | public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent) { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4729 | return startNextMatchingActivity(intent, null); |
| 4730 | } |
| 4731 | |
| 4732 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4733 | * Special version of starting an activity, for use when you are replacing |
| 4734 | * other activity components. You can use this to hand the Intent off |
| 4735 | * to the next Activity that can handle it. You typically call this in |
| 4736 | * {@link #onCreate} with the Intent returned by {@link #getIntent}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4737 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4738 | * @param intent The intent to dispatch to the next activity. For |
| 4739 | * correct behavior, this must be the same as the Intent that started |
| 4740 | * your own activity; the only changes you can make are to the extras |
| 4741 | * inside of it. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4742 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4743 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4744 | * Context.startActivity(Intent, Bundle)} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4745 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4746 | * @return Returns a boolean indicating whether there was another Activity |
| 4747 | * to start: true if there was a next activity to start, false if there |
| 4748 | * wasn't. In general, if true is returned you will then want to call |
| 4749 | * finish() on yourself. |
| 4750 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4751 | public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent, |
| 4752 | @Nullable Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4753 | if (mParent == null) { |
| 4754 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 4755 | intent.migrateExtraStreamToClipData(); |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 4756 | intent.prepareToLeaveProcess(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4757 | return ActivityManagerNative.getDefault() |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4758 | .startNextMatchingActivity(mToken, intent, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4759 | } catch (RemoteException e) { |
| 4760 | // Empty |
| 4761 | } |
| 4762 | return false; |
| 4763 | } |
| 4764 | |
| 4765 | throw new UnsupportedOperationException( |
| 4766 | "startNextMatchingActivity can only be called from a top-level activity"); |
| 4767 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4768 | |
| 4769 | /** |
| 4770 | * Same as calling {@link #startActivityFromChild(Activity, Intent, int, Bundle)} |
| 4771 | * with no options. |
| 4772 | * |
| 4773 | * @param child The activity making the call. |
| 4774 | * @param intent The intent to start. |
| 4775 | * @param requestCode Reply request code. < 0 if reply is not requested. |
| 4776 | * |
| 4777 | * @throws android.content.ActivityNotFoundException |
| 4778 | * |
| 4779 | * @see #startActivity |
| 4780 | * @see #startActivityForResult |
| 4781 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4782 | public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4783 | int requestCode) { |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 4784 | startActivityFromChild(child, intent, requestCode, null); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4785 | } |
| 4786 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4787 | /** |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4788 | * This is called when a child activity of this one calls its |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4789 | * {@link #startActivity} or {@link #startActivityForResult} method. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4790 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4791 | * <p>This method throws {@link android.content.ActivityNotFoundException} |
| 4792 | * if there was no Activity found to run the given Intent. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4793 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4794 | * @param child The activity making the call. |
| 4795 | * @param intent The intent to start. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4796 | * @param requestCode Reply request code. < 0 if reply is not requested. |
| 4797 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4798 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4799 | * Context.startActivity(Intent, Bundle)} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4800 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4801 | * @throws android.content.ActivityNotFoundException |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4802 | * |
| 4803 | * @see #startActivity |
| 4804 | * @see #startActivityForResult |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4805 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4806 | public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4807 | int requestCode, @Nullable Bundle options) { |
George Mount | 413739e | 2016-06-08 07:13:37 -0700 | [diff] [blame] | 4808 | options = transferSpringboardActivityOptions(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4809 | Instrumentation.ActivityResult ar = |
| 4810 | mInstrumentation.execStartActivity( |
| 4811 | this, mMainThread.getApplicationThread(), mToken, child, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4812 | intent, requestCode, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4813 | if (ar != null) { |
| 4814 | mMainThread.sendActivityResult( |
| 4815 | mToken, child.mEmbeddedID, requestCode, |
| 4816 | ar.getResultCode(), ar.getResultData()); |
| 4817 | } |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 4818 | cancelInputsAndStartExitTransition(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4819 | } |
| 4820 | |
| 4821 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4822 | * Same as calling {@link #startActivityFromFragment(Fragment, Intent, int, Bundle)} |
| 4823 | * with no options. |
| 4824 | * |
| 4825 | * @param fragment The fragment making the call. |
| 4826 | * @param intent The intent to start. |
| 4827 | * @param requestCode Reply request code. < 0 if reply is not requested. |
| 4828 | * |
| 4829 | * @throws android.content.ActivityNotFoundException |
| 4830 | * |
| 4831 | * @see Fragment#startActivity |
| 4832 | * @see Fragment#startActivityForResult |
| 4833 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4834 | public void startActivityFromFragment(@NonNull Fragment fragment, |
| 4835 | @RequiresPermission Intent intent, int requestCode) { |
George Mount | abb63cb | 2014-06-23 11:17:58 -0700 | [diff] [blame] | 4836 | startActivityFromFragment(fragment, intent, requestCode, null); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4837 | } |
| 4838 | |
| 4839 | /** |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4840 | * This is called when a Fragment in this activity calls its |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4841 | * {@link Fragment#startActivity} or {@link Fragment#startActivityForResult} |
| 4842 | * method. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4843 | * |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4844 | * <p>This method throws {@link android.content.ActivityNotFoundException} |
| 4845 | * if there was no Activity found to run the given Intent. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4846 | * |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4847 | * @param fragment The fragment making the call. |
| 4848 | * @param intent The intent to start. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4849 | * @param requestCode Reply request code. < 0 if reply is not requested. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4850 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4851 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4852 | * Context.startActivity(Intent, Bundle)} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4853 | * |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4854 | * @throws android.content.ActivityNotFoundException |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4855 | * |
| 4856 | * @see Fragment#startActivity |
| 4857 | * @see Fragment#startActivityForResult |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4858 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4859 | public void startActivityFromFragment(@NonNull Fragment fragment, |
| 4860 | @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) { |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 4861 | startActivityForResult(fragment.mWho, intent, requestCode, options); |
| 4862 | } |
| 4863 | |
| 4864 | /** |
| 4865 | * @hide |
| 4866 | */ |
| 4867 | @Override |
| 4868 | public void startActivityForResult( |
| 4869 | String who, Intent intent, int requestCode, @Nullable Bundle options) { |
Dianne Hackborn | a3acdb3 | 2015-06-08 17:07:40 -0700 | [diff] [blame] | 4870 | Uri referrer = onProvideReferrer(); |
| 4871 | if (referrer != null) { |
| 4872 | intent.putExtra(Intent.EXTRA_REFERRER, referrer); |
| 4873 | } |
George Mount | 413739e | 2016-06-08 07:13:37 -0700 | [diff] [blame] | 4874 | options = transferSpringboardActivityOptions(options); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4875 | Instrumentation.ActivityResult ar = |
| 4876 | mInstrumentation.execStartActivity( |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 4877 | this, mMainThread.getApplicationThread(), mToken, who, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4878 | intent, requestCode, options); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4879 | if (ar != null) { |
| 4880 | mMainThread.sendActivityResult( |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 4881 | mToken, who, requestCode, |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4882 | ar.getResultCode(), ar.getResultData()); |
| 4883 | } |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 4884 | cancelInputsAndStartExitTransition(options); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4885 | } |
| 4886 | |
| 4887 | /** |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 4888 | * @hide |
| 4889 | */ |
| 4890 | @Override |
| 4891 | public boolean canStartActivityForResult() { |
| 4892 | return true; |
| 4893 | } |
| 4894 | |
| 4895 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4896 | * Same as calling {@link #startIntentSenderFromChild(Activity, IntentSender, |
| 4897 | * int, Intent, int, int, int, Bundle)} with no options. |
| 4898 | */ |
| 4899 | public void startIntentSenderFromChild(Activity child, IntentSender intent, |
| 4900 | int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, |
| 4901 | int extraFlags) |
| 4902 | throws IntentSender.SendIntentException { |
| 4903 | startIntentSenderFromChild(child, intent, requestCode, fillInIntent, |
| 4904 | flagsMask, flagsValues, extraFlags, null); |
| 4905 | } |
| 4906 | |
| 4907 | /** |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4908 | * Like {@link #startActivityFromChild(Activity, Intent, int)}, but |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4909 | * taking a IntentSender; see |
Dianne Hackborn | ae22c05 | 2009-09-17 18:46:22 -0700 | [diff] [blame] | 4910 | * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)} |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4911 | * for more information. |
| 4912 | */ |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4913 | public void startIntentSenderFromChild(Activity child, IntentSender intent, |
| 4914 | int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4915 | int extraFlags, @Nullable Bundle options) |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4916 | throws IntentSender.SendIntentException { |
Clara Bayarri | a0c2dc3 | 2016-04-12 12:00:15 +0100 | [diff] [blame] | 4917 | startIntentSenderForResultInner(intent, child.mEmbeddedID, requestCode, fillInIntent, |
| 4918 | flagsMask, flagsValues, options); |
| 4919 | } |
| 4920 | |
| 4921 | /** |
| 4922 | * Like {@link #startIntentSenderFromChild}, but taking a Fragment; see |
| 4923 | * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)} |
| 4924 | * for more information. |
| 4925 | * |
| 4926 | * @hide |
| 4927 | */ |
| 4928 | public void startIntentSenderFromChildFragment(Fragment child, IntentSender intent, |
| 4929 | int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, |
| 4930 | int extraFlags, @Nullable Bundle options) |
| 4931 | throws IntentSender.SendIntentException { |
| 4932 | startIntentSenderForResultInner(intent, child.mWho, requestCode, fillInIntent, |
| 4933 | flagsMask, flagsValues, options); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4934 | } |
| 4935 | |
| 4936 | /** |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 4937 | * Call immediately after one of the flavors of {@link #startActivity(Intent)} |
| 4938 | * or {@link #finish} to specify an explicit transition animation to |
| 4939 | * perform next. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4940 | * |
| 4941 | * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative |
| 4942 | * to using this with starting activities is to supply the desired animation |
| 4943 | * information through a {@link ActivityOptions} bundle to |
Mark Lu | fa43485 | 2016-08-11 17:40:33 -0700 | [diff] [blame] | 4944 | * {@link #startActivity(Intent, Bundle)} or a related function. This allows |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4945 | * you to specify a custom animation even when starting an activity from |
| 4946 | * outside the context of the current top activity. |
| 4947 | * |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 4948 | * @param enterAnim A resource ID of the animation resource to use for |
Dianne Hackborn | 8b571a8 | 2009-09-25 16:09:43 -0700 | [diff] [blame] | 4949 | * the incoming activity. Use 0 for no animation. |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 4950 | * @param exitAnim A resource ID of the animation resource to use for |
Dianne Hackborn | 8b571a8 | 2009-09-25 16:09:43 -0700 | [diff] [blame] | 4951 | * the outgoing activity. Use 0 for no animation. |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 4952 | */ |
| 4953 | public void overridePendingTransition(int enterAnim, int exitAnim) { |
| 4954 | try { |
| 4955 | ActivityManagerNative.getDefault().overridePendingTransition( |
| 4956 | mToken, getPackageName(), enterAnim, exitAnim); |
| 4957 | } catch (RemoteException e) { |
| 4958 | } |
| 4959 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4960 | |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 4961 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4962 | * Call this to set the result that your activity will return to its |
| 4963 | * caller. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4964 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4965 | * @param resultCode The result code to propagate back to the originating |
| 4966 | * activity, often RESULT_CANCELED or RESULT_OK |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4967 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4968 | * @see #RESULT_CANCELED |
| 4969 | * @see #RESULT_OK |
| 4970 | * @see #RESULT_FIRST_USER |
| 4971 | * @see #setResult(int, Intent) |
| 4972 | */ |
| 4973 | public final void setResult(int resultCode) { |
| 4974 | synchronized (this) { |
| 4975 | mResultCode = resultCode; |
| 4976 | mResultData = null; |
| 4977 | } |
| 4978 | } |
| 4979 | |
| 4980 | /** |
| 4981 | * Call this to set the result that your activity will return to its |
| 4982 | * caller. |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 4983 | * |
| 4984 | * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, the Intent |
| 4985 | * you supply here can have {@link Intent#FLAG_GRANT_READ_URI_PERMISSION |
| 4986 | * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION |
| 4987 | * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} set. This will grant the |
| 4988 | * Activity receiving the result access to the specific URIs in the Intent. |
| 4989 | * Access will remain until the Activity has finished (it will remain across the hosting |
| 4990 | * process being killed and other temporary destruction) and will be added |
| 4991 | * to any existing set of URI permissions it already holds. |
| 4992 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4993 | * @param resultCode The result code to propagate back to the originating |
| 4994 | * activity, often RESULT_CANCELED or RESULT_OK |
| 4995 | * @param data The data to propagate back to the originating activity. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4996 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4997 | * @see #RESULT_CANCELED |
| 4998 | * @see #RESULT_OK |
| 4999 | * @see #RESULT_FIRST_USER |
| 5000 | * @see #setResult(int) |
| 5001 | */ |
| 5002 | public final void setResult(int resultCode, Intent data) { |
| 5003 | synchronized (this) { |
| 5004 | mResultCode = resultCode; |
| 5005 | mResultData = data; |
| 5006 | } |
| 5007 | } |
| 5008 | |
| 5009 | /** |
Dianne Hackborn | 85d558c | 2014-11-04 10:31:54 -0800 | [diff] [blame] | 5010 | * Return information about who launched this activity. If the launching Intent |
| 5011 | * contains an {@link android.content.Intent#EXTRA_REFERRER Intent.EXTRA_REFERRER}, |
| 5012 | * that will be returned as-is; otherwise, if known, an |
| 5013 | * {@link Intent#URI_ANDROID_APP_SCHEME android-app:} referrer URI containing the |
| 5014 | * package name that started the Intent will be returned. This may return null if no |
| 5015 | * referrer can be identified -- it is neither explicitly specified, nor is it known which |
| 5016 | * application package was involved. |
| 5017 | * |
| 5018 | * <p>If called while inside the handling of {@link #onNewIntent}, this function will |
| 5019 | * return the referrer that submitted that new intent to the activity. Otherwise, it |
| 5020 | * always returns the referrer of the original Intent.</p> |
| 5021 | * |
| 5022 | * <p>Note that this is <em>not</em> a security feature -- you can not trust the |
| 5023 | * referrer information, applications can spoof it.</p> |
| 5024 | */ |
| 5025 | @Nullable |
| 5026 | public Uri getReferrer() { |
| 5027 | Intent intent = getIntent(); |
Adam Powell | d3c63a6 | 2016-06-09 12:36:16 -0700 | [diff] [blame] | 5028 | try { |
| 5029 | Uri referrer = intent.getParcelableExtra(Intent.EXTRA_REFERRER); |
| 5030 | if (referrer != null) { |
| 5031 | return referrer; |
| 5032 | } |
| 5033 | String referrerName = intent.getStringExtra(Intent.EXTRA_REFERRER_NAME); |
| 5034 | if (referrerName != null) { |
| 5035 | return Uri.parse(referrerName); |
| 5036 | } |
| 5037 | } catch (BadParcelableException e) { |
| 5038 | Log.w(TAG, "Cannot read referrer from intent;" |
| 5039 | + " intent extras contain unknown custom Parcelable objects"); |
Dianne Hackborn | 85d558c | 2014-11-04 10:31:54 -0800 | [diff] [blame] | 5040 | } |
| 5041 | if (mReferrer != null) { |
| 5042 | return new Uri.Builder().scheme("android-app").authority(mReferrer).build(); |
| 5043 | } |
| 5044 | return null; |
| 5045 | } |
| 5046 | |
| 5047 | /** |
Dianne Hackborn | a3acdb3 | 2015-06-08 17:07:40 -0700 | [diff] [blame] | 5048 | * Override to generate the desired referrer for the content currently being shown |
| 5049 | * by the app. The default implementation returns null, meaning the referrer will simply |
| 5050 | * be the android-app: of the package name of this activity. Return a non-null Uri to |
| 5051 | * have that supplied as the {@link Intent#EXTRA_REFERRER} of any activities started from it. |
| 5052 | */ |
| 5053 | public Uri onProvideReferrer() { |
| 5054 | return null; |
| 5055 | } |
| 5056 | |
| 5057 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5058 | * Return the name of the package that invoked this activity. This is who |
| 5059 | * the data in {@link #setResult setResult()} will be sent to. You can |
| 5060 | * use this information to validate that the recipient is allowed to |
| 5061 | * receive the data. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5062 | * |
Dianne Hackborn | 7b92467 | 2013-04-11 18:08:42 -0700 | [diff] [blame] | 5063 | * <p class="note">Note: if the calling activity is not expecting a result (that is it |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5064 | * did not use the {@link #startActivityForResult} |
| 5065 | * form that includes a request code), then the calling package will be |
Dianne Hackborn | 7b92467 | 2013-04-11 18:08:42 -0700 | [diff] [blame] | 5066 | * null.</p> |
| 5067 | * |
| 5068 | * <p class="note">Note: prior to {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
| 5069 | * the result from this method was unstable. If the process hosting the calling |
| 5070 | * package was no longer running, it would return null instead of the proper package |
| 5071 | * name. You can use {@link #getCallingActivity()} and retrieve the package name |
| 5072 | * from that instead.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5073 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5074 | * @return The package of the activity that will receive your |
| 5075 | * reply, or null if none. |
| 5076 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5077 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5078 | public String getCallingPackage() { |
| 5079 | try { |
| 5080 | return ActivityManagerNative.getDefault().getCallingPackage(mToken); |
| 5081 | } catch (RemoteException e) { |
| 5082 | return null; |
| 5083 | } |
| 5084 | } |
| 5085 | |
| 5086 | /** |
| 5087 | * Return the name of the activity that invoked this activity. This is |
| 5088 | * who the data in {@link #setResult setResult()} will be sent to. You |
| 5089 | * can use this information to validate that the recipient is allowed to |
| 5090 | * receive the data. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5091 | * |
Dianne Hackborn | 7b92467 | 2013-04-11 18:08:42 -0700 | [diff] [blame] | 5092 | * <p class="note">Note: if the calling activity is not expecting a result (that is it |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5093 | * did not use the {@link #startActivityForResult} |
| 5094 | * form that includes a request code), then the calling package will be |
| 5095 | * null. |
| 5096 | * |
Dianne Hackborn | 7b92467 | 2013-04-11 18:08:42 -0700 | [diff] [blame] | 5097 | * @return The ComponentName of the activity that will receive your |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5098 | * reply, or null if none. |
| 5099 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5100 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5101 | public ComponentName getCallingActivity() { |
| 5102 | try { |
| 5103 | return ActivityManagerNative.getDefault().getCallingActivity(mToken); |
| 5104 | } catch (RemoteException e) { |
| 5105 | return null; |
| 5106 | } |
| 5107 | } |
| 5108 | |
| 5109 | /** |
| 5110 | * Control whether this activity's main window is visible. This is intended |
| 5111 | * only for the special case of an activity that is not going to show a |
| 5112 | * UI itself, but can't just finish prior to onResume() because it needs |
| 5113 | * to wait for a service binding or such. Setting this to false allows |
| 5114 | * you to prevent your UI from being shown during that time. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5115 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5116 | * <p>The default value for this is taken from the |
| 5117 | * {@link android.R.attr#windowNoDisplay} attribute of the activity's theme. |
| 5118 | */ |
| 5119 | public void setVisible(boolean visible) { |
| 5120 | if (mVisibleFromClient != visible) { |
| 5121 | mVisibleFromClient = visible; |
| 5122 | if (mVisibleFromServer) { |
| 5123 | if (visible) makeVisible(); |
| 5124 | else mDecor.setVisibility(View.INVISIBLE); |
| 5125 | } |
| 5126 | } |
| 5127 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5128 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5129 | void makeVisible() { |
| 5130 | if (!mWindowAdded) { |
| 5131 | ViewManager wm = getWindowManager(); |
| 5132 | wm.addView(mDecor, getWindow().getAttributes()); |
| 5133 | mWindowAdded = true; |
| 5134 | } |
| 5135 | mDecor.setVisibility(View.VISIBLE); |
| 5136 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5137 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5138 | /** |
| 5139 | * Check to see whether this activity is in the process of finishing, |
| 5140 | * either because you called {@link #finish} on it or someone else |
| 5141 | * has requested that it finished. This is often used in |
| 5142 | * {@link #onPause} to determine whether the activity is simply pausing or |
| 5143 | * completely finishing. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5144 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5145 | * @return If the activity is finishing, returns true; else returns false. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5146 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5147 | * @see #finish |
| 5148 | */ |
| 5149 | public boolean isFinishing() { |
| 5150 | return mFinished; |
| 5151 | } |
| 5152 | |
| 5153 | /** |
Dianne Hackborn | 6d9dcbc | 2012-10-02 17:51:13 -0700 | [diff] [blame] | 5154 | * Returns true if the final {@link #onDestroy()} call has been made |
| 5155 | * on the Activity, so this instance is now dead. |
| 5156 | */ |
| 5157 | public boolean isDestroyed() { |
| 5158 | return mDestroyed; |
| 5159 | } |
| 5160 | |
| 5161 | /** |
Jeff Hamilton | 3d32f6e | 2010-04-01 00:04:16 -0500 | [diff] [blame] | 5162 | * Check to see whether this activity is in the process of being destroyed in order to be |
| 5163 | * recreated with a new configuration. This is often used in |
| 5164 | * {@link #onStop} to determine whether the state needs to be cleaned up or will be passed |
| 5165 | * on to the next instance of the activity via {@link #onRetainNonConfigurationInstance()}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5166 | * |
Jeff Hamilton | 3d32f6e | 2010-04-01 00:04:16 -0500 | [diff] [blame] | 5167 | * @return If the activity is being torn down in order to be recreated with a new configuration, |
| 5168 | * returns true; else returns false. |
| 5169 | */ |
| 5170 | public boolean isChangingConfigurations() { |
| 5171 | return mChangingConfigurations; |
| 5172 | } |
| 5173 | |
| 5174 | /** |
Dianne Hackborn | 30c9bd8 | 2010-12-01 16:07:40 -0800 | [diff] [blame] | 5175 | * Cause this Activity to be recreated with a new instance. This results |
| 5176 | * in essentially the same flow as when the Activity is created due to |
| 5177 | * a configuration change -- the current instance will go through its |
| 5178 | * lifecycle to {@link #onDestroy} and a new instance then created after it. |
| 5179 | */ |
| 5180 | public void recreate() { |
| 5181 | if (mParent != null) { |
| 5182 | throw new IllegalStateException("Can only be called on top-level activity"); |
| 5183 | } |
| 5184 | if (Looper.myLooper() != mMainThread.getLooper()) { |
| 5185 | throw new IllegalStateException("Must be called from main thread"); |
| 5186 | } |
Chong Zhang | fec694e | 2016-08-09 12:57:38 -0700 | [diff] [blame] | 5187 | try { |
| 5188 | ActivityManagerNative.getDefault().requestActivityRelaunch(mToken); |
| 5189 | } catch (RemoteException e) { |
| 5190 | } |
Dianne Hackborn | 30c9bd8 | 2010-12-01 16:07:40 -0800 | [diff] [blame] | 5191 | } |
| 5192 | |
| 5193 | /** |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 5194 | * Finishes the current activity and specifies whether to remove the task associated with this |
| 5195 | * activity. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5196 | */ |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 5197 | private void finish(int finishTask) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5198 | if (mParent == null) { |
| 5199 | int resultCode; |
| 5200 | Intent resultData; |
| 5201 | synchronized (this) { |
| 5202 | resultCode = mResultCode; |
| 5203 | resultData = mResultData; |
| 5204 | } |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 5205 | if (false) Log.v(TAG, "Finishing self: token=" + mToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5206 | try { |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 5207 | if (resultData != null) { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 5208 | resultData.prepareToLeaveProcess(this); |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 5209 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5210 | if (ActivityManagerNative.getDefault() |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 5211 | .finishActivity(mToken, resultCode, resultData, finishTask)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5212 | mFinished = true; |
| 5213 | } |
| 5214 | } catch (RemoteException e) { |
| 5215 | // Empty |
| 5216 | } |
| 5217 | } else { |
| 5218 | mParent.finishFromChild(this); |
| 5219 | } |
| 5220 | } |
| 5221 | |
| 5222 | /** |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 5223 | * Call this when your activity is done and should be closed. The |
| 5224 | * ActivityResult is propagated back to whoever launched you via |
| 5225 | * onActivityResult(). |
| 5226 | */ |
| 5227 | public void finish() { |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 5228 | finish(DONT_FINISH_TASK_WITH_ACTIVITY); |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 5229 | } |
| 5230 | |
| 5231 | /** |
Dianne Hackborn | ecc5a9c | 2012-04-26 18:56:09 -0700 | [diff] [blame] | 5232 | * Finish this activity as well as all activities immediately below it |
| 5233 | * in the current task that have the same affinity. This is typically |
| 5234 | * used when an application can be launched on to another task (such as |
| 5235 | * from an ACTION_VIEW of a content type it understands) and the user |
| 5236 | * has used the up navigation to switch out of the current task and in |
| 5237 | * to its own task. In this case, if the user has navigated down into |
| 5238 | * any other activities of the second application, all of those should |
| 5239 | * be removed from the original task as part of the task switch. |
| 5240 | * |
| 5241 | * <p>Note that this finish does <em>not</em> allow you to deliver results |
| 5242 | * to the previous activity, and an exception will be thrown if you are trying |
| 5243 | * to do so.</p> |
| 5244 | */ |
| 5245 | public void finishAffinity() { |
| 5246 | if (mParent != null) { |
| 5247 | throw new IllegalStateException("Can not be called from an embedded activity"); |
| 5248 | } |
| 5249 | if (mResultCode != RESULT_CANCELED || mResultData != null) { |
| 5250 | throw new IllegalStateException("Can not be called to deliver a result"); |
| 5251 | } |
| 5252 | try { |
| 5253 | if (ActivityManagerNative.getDefault().finishActivityAffinity(mToken)) { |
| 5254 | mFinished = true; |
| 5255 | } |
| 5256 | } catch (RemoteException e) { |
| 5257 | // Empty |
| 5258 | } |
| 5259 | } |
| 5260 | |
| 5261 | /** |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5262 | * This is called when a child activity of this one calls its |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5263 | * {@link #finish} method. The default implementation simply calls |
| 5264 | * finish() on this activity (the parent), finishing the entire group. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5265 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5266 | * @param child The activity making the call. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5267 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5268 | * @see #finish |
| 5269 | */ |
| 5270 | public void finishFromChild(Activity child) { |
| 5271 | finish(); |
| 5272 | } |
| 5273 | |
| 5274 | /** |
George Mount | cb4b7d9 | 2014-02-25 10:47:55 -0800 | [diff] [blame] | 5275 | * Reverses the Activity Scene entry Transition and triggers the calling Activity |
| 5276 | * to reverse its exit Transition. When the exit Transition completes, |
| 5277 | * {@link #finish()} is called. If no entry Transition was used, finish() is called |
| 5278 | * immediately and the Activity exit Transition is run. |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 5279 | * @see android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, android.util.Pair[]) |
George Mount | cb4b7d9 | 2014-02-25 10:47:55 -0800 | [diff] [blame] | 5280 | */ |
Craig Mautner | 73f843d | 2014-05-19 09:42:28 -0700 | [diff] [blame] | 5281 | public void finishAfterTransition() { |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 5282 | if (!mActivityTransitionState.startExitBackTransition(this)) { |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 5283 | finish(); |
| 5284 | } |
George Mount | cb4b7d9 | 2014-02-25 10:47:55 -0800 | [diff] [blame] | 5285 | } |
| 5286 | |
| 5287 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5288 | * Force finish another activity that you had previously started with |
| 5289 | * {@link #startActivityForResult}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5290 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5291 | * @param requestCode The request code of the activity that you had |
| 5292 | * given to startActivityForResult(). If there are multiple |
| 5293 | * activities started with this request code, they |
| 5294 | * will all be finished. |
| 5295 | */ |
| 5296 | public void finishActivity(int requestCode) { |
| 5297 | if (mParent == null) { |
| 5298 | try { |
| 5299 | ActivityManagerNative.getDefault() |
| 5300 | .finishSubActivity(mToken, mEmbeddedID, requestCode); |
| 5301 | } catch (RemoteException e) { |
| 5302 | // Empty |
| 5303 | } |
| 5304 | } else { |
| 5305 | mParent.finishActivityFromChild(this, requestCode); |
| 5306 | } |
| 5307 | } |
| 5308 | |
| 5309 | /** |
| 5310 | * This is called when a child activity of this one calls its |
| 5311 | * finishActivity(). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5312 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5313 | * @param child The activity making the call. |
| 5314 | * @param requestCode Request code that had been used to start the |
| 5315 | * activity. |
| 5316 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5317 | public void finishActivityFromChild(@NonNull Activity child, int requestCode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5318 | try { |
| 5319 | ActivityManagerNative.getDefault() |
| 5320 | .finishSubActivity(mToken, child.mEmbeddedID, requestCode); |
| 5321 | } catch (RemoteException e) { |
| 5322 | // Empty |
| 5323 | } |
| 5324 | } |
| 5325 | |
| 5326 | /** |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 5327 | * Call this when your activity is done and should be closed and the task should be completely |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 5328 | * removed as a part of finishing the root activity of the task. |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 5329 | */ |
| 5330 | public void finishAndRemoveTask() { |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 5331 | finish(FINISH_TASK_WITH_ROOT_ACTIVITY); |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 5332 | } |
| 5333 | |
| 5334 | /** |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 5335 | * Ask that the local app instance of this activity be released to free up its memory. |
| 5336 | * This is asking for the activity to be destroyed, but does <b>not</b> finish the activity -- |
| 5337 | * a new instance of the activity will later be re-created if needed due to the user |
| 5338 | * navigating back to it. |
| 5339 | * |
| 5340 | * @return Returns true if the activity was in a state that it has started the process |
| 5341 | * of destroying its current instance; returns false if for any reason this could not |
| 5342 | * be done: it is currently visible to the user, it is already being destroyed, it is |
| 5343 | * being finished, it hasn't yet saved its state, etc. |
| 5344 | */ |
| 5345 | public boolean releaseInstance() { |
| 5346 | try { |
| 5347 | return ActivityManagerNative.getDefault().releaseActivityInstance(mToken); |
| 5348 | } catch (RemoteException e) { |
| 5349 | // Empty |
| 5350 | } |
| 5351 | return false; |
| 5352 | } |
| 5353 | |
| 5354 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5355 | * Called when an activity you launched exits, giving you the requestCode |
| 5356 | * you started it with, the resultCode it returned, and any additional |
| 5357 | * data from it. The <var>resultCode</var> will be |
| 5358 | * {@link #RESULT_CANCELED} if the activity explicitly returned that, |
| 5359 | * didn't return any result, or crashed during its operation. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5360 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5361 | * <p>You will receive this call immediately before onResume() when your |
| 5362 | * activity is re-starting. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5363 | * |
Ricardo Cervera | 92f6a74 | 2014-04-04 11:17:06 -0700 | [diff] [blame] | 5364 | * <p>This method is never invoked if your activity sets |
| 5365 | * {@link android.R.styleable#AndroidManifestActivity_noHistory noHistory} to |
| 5366 | * <code>true</code>. |
Ricardo Cervera | 93f94c2 | 2015-01-16 09:54:30 -0800 | [diff] [blame] | 5367 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5368 | * @param requestCode The integer request code originally supplied to |
| 5369 | * startActivityForResult(), allowing you to identify who this |
| 5370 | * result came from. |
| 5371 | * @param resultCode The integer result code returned by the child activity |
| 5372 | * through its setResult(). |
| 5373 | * @param data An Intent, which can return result data to the caller |
| 5374 | * (various data can be attached to Intent "extras"). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5375 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5376 | * @see #startActivityForResult |
| 5377 | * @see #createPendingResult |
| 5378 | * @see #setResult(int) |
| 5379 | */ |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 5380 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5381 | } |
| 5382 | |
| 5383 | /** |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 5384 | * Called when an activity you launched with an activity transition exposes this |
| 5385 | * Activity through a returning activity transition, giving you the resultCode |
| 5386 | * and any additional data from it. This method will only be called if the activity |
| 5387 | * set a result code other than {@link #RESULT_CANCELED} and it supports activity |
George Mount | 9826f63 | 2014-09-11 08:50:09 -0700 | [diff] [blame] | 5388 | * transitions with {@link Window#FEATURE_ACTIVITY_TRANSITIONS}. |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 5389 | * |
| 5390 | * <p>The purpose of this function is to let the called Activity send a hint about |
| 5391 | * its state so that this underlying Activity can prepare to be exposed. A call to |
| 5392 | * this method does not guarantee that the called Activity has or will be exiting soon. |
| 5393 | * It only indicates that it will expose this Activity's Window and it has |
| 5394 | * some data to pass to prepare it.</p> |
| 5395 | * |
| 5396 | * @param resultCode The integer result code returned by the child activity |
| 5397 | * through its setResult(). |
| 5398 | * @param data An Intent, which can return result data to the caller |
| 5399 | * (various data can be attached to Intent "extras"). |
| 5400 | */ |
Craig Mautner | 7a629c2 | 2014-09-04 14:57:04 -0700 | [diff] [blame] | 5401 | public void onActivityReenter(int resultCode, Intent data) { |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 5402 | } |
| 5403 | |
| 5404 | /** |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5405 | * Create a new PendingIntent object which you can hand to others |
| 5406 | * for them to use to send result data back to your |
| 5407 | * {@link #onActivityResult} callback. The created object will be either |
| 5408 | * one-shot (becoming invalid after a result is sent back) or multiple |
| 5409 | * (allowing any number of results to be sent through it). |
| 5410 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5411 | * @param requestCode Private request code for the sender that will be |
| 5412 | * associated with the result data when it is returned. The sender can not |
| 5413 | * modify this value, allowing you to identify incoming results. |
| 5414 | * @param data Default data to supply in the result, which may be modified |
| 5415 | * by the sender. |
| 5416 | * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT PendingIntent.FLAG_ONE_SHOT}, |
| 5417 | * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE}, |
| 5418 | * {@link PendingIntent#FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT}, |
| 5419 | * {@link PendingIntent#FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT}, |
| 5420 | * or any of the flags as supported by |
| 5421 | * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts |
| 5422 | * of the intent that can be supplied when the actual send happens. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5423 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5424 | * @return Returns an existing or new PendingIntent matching the given |
| 5425 | * parameters. May return null only if |
| 5426 | * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE} has been |
| 5427 | * supplied. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5428 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5429 | * @see PendingIntent |
| 5430 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5431 | public PendingIntent createPendingResult(int requestCode, @NonNull Intent data, |
| 5432 | @PendingIntent.Flags int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5433 | String packageName = getPackageName(); |
| 5434 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 5435 | data.prepareToLeaveProcess(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5436 | IIntentSender target = |
| 5437 | ActivityManagerNative.getDefault().getIntentSender( |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 5438 | ActivityManager.INTENT_SENDER_ACTIVITY_RESULT, packageName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5439 | mParent == null ? mToken : mParent.mToken, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 5440 | mEmbeddedID, requestCode, new Intent[] { data }, null, flags, null, |
| 5441 | UserHandle.myUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5442 | return target != null ? new PendingIntent(target) : null; |
| 5443 | } catch (RemoteException e) { |
| 5444 | // Empty |
| 5445 | } |
| 5446 | return null; |
| 5447 | } |
| 5448 | |
| 5449 | /** |
| 5450 | * Change the desired orientation of this activity. If the activity |
| 5451 | * is currently in the foreground or otherwise impacting the screen |
| 5452 | * orientation, the screen will immediately be changed (possibly causing |
| 5453 | * the activity to be restarted). Otherwise, this will be used the next |
| 5454 | * time the activity is visible. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5455 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5456 | * @param requestedOrientation An orientation constant as used in |
| 5457 | * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}. |
| 5458 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5459 | public void setRequestedOrientation(@ActivityInfo.ScreenOrientation int requestedOrientation) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5460 | if (mParent == null) { |
| 5461 | try { |
| 5462 | ActivityManagerNative.getDefault().setRequestedOrientation( |
| 5463 | mToken, requestedOrientation); |
| 5464 | } catch (RemoteException e) { |
| 5465 | // Empty |
| 5466 | } |
| 5467 | } else { |
| 5468 | mParent.setRequestedOrientation(requestedOrientation); |
| 5469 | } |
| 5470 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5471 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5472 | /** |
| 5473 | * Return the current requested orientation of the activity. This will |
| 5474 | * either be the orientation requested in its component's manifest, or |
| 5475 | * the last requested orientation given to |
| 5476 | * {@link #setRequestedOrientation(int)}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5477 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5478 | * @return Returns an orientation constant as used in |
| 5479 | * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}. |
| 5480 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5481 | @ActivityInfo.ScreenOrientation |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5482 | public int getRequestedOrientation() { |
| 5483 | if (mParent == null) { |
| 5484 | try { |
| 5485 | return ActivityManagerNative.getDefault() |
| 5486 | .getRequestedOrientation(mToken); |
| 5487 | } catch (RemoteException e) { |
| 5488 | // Empty |
| 5489 | } |
| 5490 | } else { |
| 5491 | return mParent.getRequestedOrientation(); |
| 5492 | } |
| 5493 | return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
| 5494 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5495 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5496 | /** |
| 5497 | * Return the identifier of the task this activity is in. This identifier |
| 5498 | * will remain the same for the lifetime of the activity. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5499 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5500 | * @return Task identifier, an opaque integer. |
| 5501 | */ |
| 5502 | public int getTaskId() { |
| 5503 | try { |
| 5504 | return ActivityManagerNative.getDefault() |
| 5505 | .getTaskForActivity(mToken, false); |
| 5506 | } catch (RemoteException e) { |
| 5507 | return -1; |
| 5508 | } |
| 5509 | } |
| 5510 | |
| 5511 | /** |
| 5512 | * Return whether this activity is the root of a task. The root is the |
| 5513 | * first activity in a task. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5514 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5515 | * @return True if this is the root activity, else false. |
| 5516 | */ |
| 5517 | public boolean isTaskRoot() { |
| 5518 | try { |
Filip Gruszczynski | 3d02671 | 2015-12-16 13:46:38 -0800 | [diff] [blame] | 5519 | return ActivityManagerNative.getDefault().getTaskForActivity(mToken, true) >= 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5520 | } catch (RemoteException e) { |
| 5521 | return false; |
| 5522 | } |
| 5523 | } |
| 5524 | |
| 5525 | /** |
| 5526 | * Move the task containing this activity to the back of the activity |
| 5527 | * stack. The activity's order within the task is unchanged. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5528 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5529 | * @param nonRoot If false then this only works if the activity is the root |
| 5530 | * of a task; if true it will work for any activity in |
| 5531 | * a task. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5532 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5533 | * @return If the task was moved (or it was already at the |
| 5534 | * back) true is returned, else false. |
| 5535 | */ |
| 5536 | public boolean moveTaskToBack(boolean nonRoot) { |
| 5537 | try { |
| 5538 | return ActivityManagerNative.getDefault().moveActivityTaskToBack( |
| 5539 | mToken, nonRoot); |
| 5540 | } catch (RemoteException e) { |
| 5541 | // Empty |
| 5542 | } |
| 5543 | return false; |
| 5544 | } |
| 5545 | |
| 5546 | /** |
| 5547 | * Returns class name for this activity with the package prefix removed. |
| 5548 | * This is the default name used to read and write settings. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5549 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5550 | * @return The local class name. |
| 5551 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5552 | @NonNull |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5553 | public String getLocalClassName() { |
| 5554 | final String pkg = getPackageName(); |
| 5555 | final String cls = mComponent.getClassName(); |
| 5556 | int packageLen = pkg.length(); |
| 5557 | if (!cls.startsWith(pkg) || cls.length() <= packageLen |
| 5558 | || cls.charAt(packageLen) != '.') { |
| 5559 | return cls; |
| 5560 | } |
| 5561 | return cls.substring(packageLen+1); |
| 5562 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5563 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5564 | /** |
| 5565 | * Returns complete component name of this activity. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5566 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5567 | * @return Returns the complete component name for this activity |
| 5568 | */ |
| 5569 | public ComponentName getComponentName() |
| 5570 | { |
| 5571 | return mComponent; |
| 5572 | } |
| 5573 | |
| 5574 | /** |
| 5575 | * Retrieve a {@link SharedPreferences} object for accessing preferences |
| 5576 | * that are private to this activity. This simply calls the underlying |
| 5577 | * {@link #getSharedPreferences(String, int)} method by passing in this activity's |
| 5578 | * class name as the preferences name. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5579 | * |
| 5580 | * @param mode Operating mode. Use {@link #MODE_PRIVATE} for the default |
Jeff Sharkey | 634dc42 | 2016-01-30 17:44:15 -0700 | [diff] [blame] | 5581 | * operation. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5582 | * |
| 5583 | * @return Returns the single SharedPreferences instance that can be used |
| 5584 | * to retrieve and modify the preference values. |
| 5585 | */ |
| 5586 | public SharedPreferences getPreferences(int mode) { |
| 5587 | return getSharedPreferences(getLocalClassName(), mode); |
| 5588 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5589 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 5590 | private void ensureSearchManager() { |
| 5591 | if (mSearchManager != null) { |
| 5592 | return; |
| 5593 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5594 | |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 5595 | try { |
| 5596 | mSearchManager = new SearchManager(this, null); |
| 5597 | } catch (ServiceNotFoundException e) { |
| 5598 | throw new IllegalStateException(e); |
| 5599 | } |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 5600 | } |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5601 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5602 | @Override |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5603 | public Object getSystemService(@ServiceName @NonNull String name) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5604 | if (getBaseContext() == null) { |
| 5605 | throw new IllegalStateException( |
| 5606 | "System services not available to Activities before onCreate()"); |
| 5607 | } |
| 5608 | |
| 5609 | if (WINDOW_SERVICE.equals(name)) { |
| 5610 | return mWindowManager; |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 5611 | } else if (SEARCH_SERVICE.equals(name)) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 5612 | ensureSearchManager(); |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 5613 | return mSearchManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5614 | } |
| 5615 | return super.getSystemService(name); |
| 5616 | } |
| 5617 | |
| 5618 | /** |
| 5619 | * Change the title associated with this activity. If this is a |
| 5620 | * top-level activity, the title for its window will change. If it |
| 5621 | * is an embedded activity, the parent can do whatever it wants |
| 5622 | * with it. |
| 5623 | */ |
| 5624 | public void setTitle(CharSequence title) { |
| 5625 | mTitle = title; |
| 5626 | onTitleChanged(title, mTitleColor); |
| 5627 | |
| 5628 | if (mParent != null) { |
| 5629 | mParent.onChildTitleChanged(this, title); |
| 5630 | } |
| 5631 | } |
| 5632 | |
| 5633 | /** |
| 5634 | * Change the title associated with this activity. If this is a |
| 5635 | * top-level activity, the title for its window will change. If it |
| 5636 | * is an embedded activity, the parent can do whatever it wants |
| 5637 | * with it. |
| 5638 | */ |
| 5639 | public void setTitle(int titleId) { |
| 5640 | setTitle(getText(titleId)); |
| 5641 | } |
| 5642 | |
Alan Viverette | 2525d9c | 2013-11-15 14:42:19 -0800 | [diff] [blame] | 5643 | /** |
| 5644 | * Change the color of the title associated with this activity. |
| 5645 | * <p> |
| 5646 | * This method is deprecated starting in API Level 11 and replaced by action |
| 5647 | * bar styles. For information on styling the Action Bar, read the <a |
| 5648 | * href="{@docRoot} guide/topics/ui/actionbar.html">Action Bar</a> developer |
| 5649 | * guide. |
| 5650 | * |
| 5651 | * @deprecated Use action bar styles instead. |
| 5652 | */ |
| 5653 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5654 | public void setTitleColor(int textColor) { |
| 5655 | mTitleColor = textColor; |
| 5656 | onTitleChanged(mTitle, textColor); |
| 5657 | } |
| 5658 | |
| 5659 | public final CharSequence getTitle() { |
| 5660 | return mTitle; |
| 5661 | } |
| 5662 | |
| 5663 | public final int getTitleColor() { |
| 5664 | return mTitleColor; |
| 5665 | } |
| 5666 | |
| 5667 | protected void onTitleChanged(CharSequence title, int color) { |
| 5668 | if (mTitleReady) { |
| 5669 | final Window win = getWindow(); |
| 5670 | if (win != null) { |
| 5671 | win.setTitle(title); |
| 5672 | if (color != 0) { |
| 5673 | win.setTitleColor(color); |
| 5674 | } |
| 5675 | } |
Adam Powell | af2d859 | 2014-08-26 18:06:40 -0700 | [diff] [blame] | 5676 | if (mActionBar != null) { |
| 5677 | mActionBar.setWindowTitle(title); |
| 5678 | } |
Adam Powell | a557fdc | 2014-08-21 18:05:53 -0700 | [diff] [blame] | 5679 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5680 | } |
| 5681 | |
| 5682 | protected void onChildTitleChanged(Activity childActivity, CharSequence title) { |
| 5683 | } |
| 5684 | |
| 5685 | /** |
Winson Chung | a449dc0 | 2014-05-16 11:15:04 -0700 | [diff] [blame] | 5686 | * Sets information describing the task with this activity for presentation inside the Recents |
| 5687 | * System UI. When {@link ActivityManager#getRecentTasks} is called, the activities of each task |
| 5688 | * are traversed in order from the topmost activity to the bottommost. The traversal continues |
| 5689 | * for each property until a suitable value is found. For each task the taskDescription will be |
| 5690 | * returned in {@link android.app.ActivityManager.TaskDescription}. |
Craig Mautner | 2fbd754 | 2014-03-21 09:34:07 -0700 | [diff] [blame] | 5691 | * |
| 5692 | * @see ActivityManager#getRecentTasks |
Winson Chung | a449dc0 | 2014-05-16 11:15:04 -0700 | [diff] [blame] | 5693 | * @see android.app.ActivityManager.TaskDescription |
Craig Mautner | 2fbd754 | 2014-03-21 09:34:07 -0700 | [diff] [blame] | 5694 | * |
Winson Chung | a449dc0 | 2014-05-16 11:15:04 -0700 | [diff] [blame] | 5695 | * @param taskDescription The TaskDescription properties that describe the task with this activity |
Craig Mautner | 2fbd754 | 2014-03-21 09:34:07 -0700 | [diff] [blame] | 5696 | */ |
Winson Chung | a449dc0 | 2014-05-16 11:15:04 -0700 | [diff] [blame] | 5697 | public void setTaskDescription(ActivityManager.TaskDescription taskDescription) { |
Winson | b640315 | 2016-02-23 13:32:09 -0800 | [diff] [blame] | 5698 | if (mTaskDescription != taskDescription) { |
| 5699 | mTaskDescription.copyFrom(taskDescription); |
| 5700 | // Scale the icon down to something reasonable if it is provided |
| 5701 | if (taskDescription.getIconFilename() == null && taskDescription.getIcon() != null) { |
| 5702 | final int size = ActivityManager.getLauncherLargeIconSizeInner(this); |
| 5703 | final Bitmap icon = Bitmap.createScaledBitmap(taskDescription.getIcon(), size, size, |
| 5704 | true); |
| 5705 | mTaskDescription.setIcon(icon); |
| 5706 | } |
Craig Mautner | 2fbd754 | 2014-03-21 09:34:07 -0700 | [diff] [blame] | 5707 | } |
| 5708 | try { |
Winson | b640315 | 2016-02-23 13:32:09 -0800 | [diff] [blame] | 5709 | ActivityManagerNative.getDefault().setTaskDescription(mToken, mTaskDescription); |
Craig Mautner | 2fbd754 | 2014-03-21 09:34:07 -0700 | [diff] [blame] | 5710 | } catch (RemoteException e) { |
| 5711 | } |
| 5712 | } |
| 5713 | |
| 5714 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5715 | * Sets the visibility of the progress bar in the title. |
| 5716 | * <p> |
| 5717 | * In order for the progress bar to be shown, the feature must be requested |
| 5718 | * via {@link #requestWindowFeature(int)}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5719 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5720 | * @param visible Whether to show the progress bars in the title. |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5721 | * @deprecated No longer supported starting in API 21. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5722 | */ |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5723 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5724 | public final void setProgressBarVisibility(boolean visible) { |
| 5725 | getWindow().setFeatureInt(Window.FEATURE_PROGRESS, visible ? Window.PROGRESS_VISIBILITY_ON : |
| 5726 | Window.PROGRESS_VISIBILITY_OFF); |
| 5727 | } |
| 5728 | |
| 5729 | /** |
| 5730 | * Sets the visibility of the indeterminate progress bar in the title. |
| 5731 | * <p> |
| 5732 | * In order for the progress bar to be shown, the feature must be requested |
| 5733 | * via {@link #requestWindowFeature(int)}. |
| 5734 | * |
| 5735 | * @param visible Whether to show the progress bars in the title. |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5736 | * @deprecated No longer supported starting in API 21. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5737 | */ |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5738 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5739 | public final void setProgressBarIndeterminateVisibility(boolean visible) { |
| 5740 | getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS, |
| 5741 | visible ? Window.PROGRESS_VISIBILITY_ON : Window.PROGRESS_VISIBILITY_OFF); |
| 5742 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5743 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5744 | /** |
| 5745 | * Sets whether the horizontal progress bar in the title should be indeterminate (the circular |
| 5746 | * is always indeterminate). |
| 5747 | * <p> |
| 5748 | * In order for the progress bar to be shown, the feature must be requested |
| 5749 | * via {@link #requestWindowFeature(int)}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5750 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5751 | * @param indeterminate Whether the horizontal progress bar should be indeterminate. |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5752 | * @deprecated No longer supported starting in API 21. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5753 | */ |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5754 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5755 | public final void setProgressBarIndeterminate(boolean indeterminate) { |
| 5756 | getWindow().setFeatureInt(Window.FEATURE_PROGRESS, |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 5757 | indeterminate ? Window.PROGRESS_INDETERMINATE_ON |
| 5758 | : Window.PROGRESS_INDETERMINATE_OFF); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5759 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5760 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5761 | /** |
| 5762 | * Sets the progress for the progress bars in the title. |
| 5763 | * <p> |
| 5764 | * In order for the progress bar to be shown, the feature must be requested |
| 5765 | * via {@link #requestWindowFeature(int)}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5766 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5767 | * @param progress The progress for the progress bar. Valid ranges are from |
| 5768 | * 0 to 10000 (both inclusive). If 10000 is given, the progress |
| 5769 | * bar will be completely filled and will fade out. |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5770 | * @deprecated No longer supported starting in API 21. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5771 | */ |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5772 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5773 | public final void setProgress(int progress) { |
| 5774 | getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress + Window.PROGRESS_START); |
| 5775 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5776 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5777 | /** |
| 5778 | * Sets the secondary progress for the progress bar in the title. This |
| 5779 | * progress is drawn between the primary progress (set via |
| 5780 | * {@link #setProgress(int)} and the background. It can be ideal for media |
| 5781 | * scenarios such as showing the buffering progress while the default |
| 5782 | * progress shows the play progress. |
| 5783 | * <p> |
| 5784 | * In order for the progress bar to be shown, the feature must be requested |
| 5785 | * via {@link #requestWindowFeature(int)}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5786 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5787 | * @param secondaryProgress The secondary progress for the progress bar. Valid ranges are from |
| 5788 | * 0 to 10000 (both inclusive). |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5789 | * @deprecated No longer supported starting in API 21. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5790 | */ |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5791 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5792 | public final void setSecondaryProgress(int secondaryProgress) { |
| 5793 | getWindow().setFeatureInt(Window.FEATURE_PROGRESS, |
| 5794 | secondaryProgress + Window.PROGRESS_SECONDARY_START); |
| 5795 | } |
| 5796 | |
| 5797 | /** |
| 5798 | * Suggests an audio stream whose volume should be changed by the hardware |
| 5799 | * volume controls. |
| 5800 | * <p> |
| 5801 | * The suggested audio stream will be tied to the window of this Activity. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5802 | * Volume requests which are received while the Activity is in the |
| 5803 | * foreground will affect this stream. |
| 5804 | * <p> |
| 5805 | * It is not guaranteed that the hardware volume controls will always change |
| 5806 | * this stream's volume (for example, if a call is in progress, its stream's |
| 5807 | * volume may be changed instead). To reset back to the default, use |
| 5808 | * {@link AudioManager#USE_DEFAULT_STREAM_TYPE}. |
| 5809 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5810 | * @param streamType The type of the audio stream whose volume should be |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5811 | * changed by the hardware volume controls. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5812 | */ |
| 5813 | public final void setVolumeControlStream(int streamType) { |
| 5814 | getWindow().setVolumeControlStream(streamType); |
| 5815 | } |
| 5816 | |
| 5817 | /** |
| 5818 | * Gets the suggested audio stream whose volume should be changed by the |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5819 | * hardware volume controls. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5820 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5821 | * @return The suggested audio stream type whose volume should be changed by |
| 5822 | * the hardware volume controls. |
| 5823 | * @see #setVolumeControlStream(int) |
| 5824 | */ |
| 5825 | public final int getVolumeControlStream() { |
| 5826 | return getWindow().getVolumeControlStream(); |
| 5827 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5828 | |
| 5829 | /** |
| 5830 | * Sets a {@link MediaController} to send media keys and volume changes to. |
| 5831 | * <p> |
| 5832 | * The controller will be tied to the window of this Activity. Media key and |
| 5833 | * volume events which are received while the Activity is in the foreground |
| 5834 | * will be forwarded to the controller and used to invoke transport controls |
| 5835 | * or adjust the volume. This may be used instead of or in addition to |
| 5836 | * {@link #setVolumeControlStream} to affect a specific session instead of a |
| 5837 | * specific stream. |
| 5838 | * <p> |
| 5839 | * It is not guaranteed that the hardware volume controls will always change |
| 5840 | * this session's volume (for example, if a call is in progress, its |
| 5841 | * stream's volume may be changed instead). To reset back to the default use |
| 5842 | * null as the controller. |
| 5843 | * |
| 5844 | * @param controller The controller for the session which should receive |
| 5845 | * media keys and volume changes. |
| 5846 | */ |
| 5847 | public final void setMediaController(MediaController controller) { |
| 5848 | getWindow().setMediaController(controller); |
| 5849 | } |
| 5850 | |
| 5851 | /** |
| 5852 | * Gets the controller which should be receiving media key and volume events |
| 5853 | * while this activity is in the foreground. |
| 5854 | * |
| 5855 | * @return The controller which should receive events. |
| 5856 | * @see #setMediaController(android.media.session.MediaController) |
| 5857 | */ |
| 5858 | public final MediaController getMediaController() { |
| 5859 | return getWindow().getMediaController(); |
| 5860 | } |
| 5861 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5862 | /** |
| 5863 | * Runs the specified action on the UI thread. If the current thread is the UI |
| 5864 | * thread, then the action is executed immediately. If the current thread is |
| 5865 | * not the UI thread, the action is posted to the event queue of the UI thread. |
| 5866 | * |
| 5867 | * @param action the action to run on the UI thread |
| 5868 | */ |
| 5869 | public final void runOnUiThread(Runnable action) { |
| 5870 | if (Thread.currentThread() != mUiThread) { |
| 5871 | mHandler.post(action); |
| 5872 | } else { |
| 5873 | action.run(); |
| 5874 | } |
| 5875 | } |
| 5876 | |
| 5877 | /** |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 5878 | * Standard implementation of |
| 5879 | * {@link android.view.LayoutInflater.Factory#onCreateView} used when |
| 5880 | * inflating with the LayoutInflater returned by {@link #getSystemService}. |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5881 | * This implementation does nothing and is for |
| 5882 | * pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} apps. Newer apps |
| 5883 | * should use {@link #onCreateView(View, String, Context, AttributeSet)}. |
| 5884 | * |
| 5885 | * @see android.view.LayoutInflater#createView |
| 5886 | * @see android.view.Window#getLayoutInflater |
| 5887 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5888 | @Nullable |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5889 | public View onCreateView(String name, Context context, AttributeSet attrs) { |
| 5890 | return null; |
| 5891 | } |
| 5892 | |
| 5893 | /** |
| 5894 | * Standard implementation of |
| 5895 | * {@link android.view.LayoutInflater.Factory2#onCreateView(View, String, Context, AttributeSet)} |
| 5896 | * used when inflating with the LayoutInflater returned by {@link #getSystemService}. |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 5897 | * This implementation handles <fragment> tags to embed fragments inside |
| 5898 | * of the activity. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5899 | * |
| 5900 | * @see android.view.LayoutInflater#createView |
| 5901 | * @see android.view.Window#getLayoutInflater |
| 5902 | */ |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5903 | public View onCreateView(View parent, String name, Context context, AttributeSet attrs) { |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 5904 | if (!"fragment".equals(name)) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5905 | return onCreateView(name, context, attrs); |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 5906 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5907 | |
Adam Powell | 371a809 | 2014-06-20 12:51:12 -0700 | [diff] [blame] | 5908 | return mFragments.onCreateView(parent, name, context, attrs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5909 | } |
| 5910 | |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 5911 | /** |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5912 | * Print the Activity's state into the given stream. This gets invoked if |
Jeff Sharkey | 5554b70 | 2012-04-11 18:30:51 -0700 | [diff] [blame] | 5913 | * you run "adb shell dumpsys activity <activity_component_name>". |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5914 | * |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 5915 | * @param prefix Desired prefix to prepend at each line of output. |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5916 | * @param fd The raw file descriptor that the dump is being sent to. |
| 5917 | * @param writer The PrintWriter to which you should dump your state. This will be |
| 5918 | * closed for you after you return. |
| 5919 | * @param args additional arguments to the dump request. |
| 5920 | */ |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 5921 | public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 5922 | dumpInner(prefix, fd, writer, args); |
| 5923 | } |
| 5924 | |
| 5925 | void dumpInner(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 5926 | writer.print(prefix); writer.print("Local Activity "); |
| 5927 | writer.print(Integer.toHexString(System.identityHashCode(this))); |
| 5928 | writer.println(" State:"); |
| 5929 | String innerPrefix = prefix + " "; |
| 5930 | writer.print(innerPrefix); writer.print("mResumed="); |
| 5931 | writer.print(mResumed); writer.print(" mStopped="); |
| 5932 | writer.print(mStopped); writer.print(" mFinished="); |
| 5933 | writer.println(mFinished); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 5934 | writer.print(innerPrefix); writer.print("mChangingConfigurations="); |
| 5935 | writer.println(mChangingConfigurations); |
| 5936 | writer.print(innerPrefix); writer.print("mCurrentConfig="); |
| 5937 | writer.println(mCurrentConfig); |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 5938 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 5939 | mFragments.dumpLoaders(innerPrefix, fd, writer, args); |
| 5940 | mFragments.getFragmentManager().dump(innerPrefix, fd, writer, args); |
Dianne Hackborn | 57dd737 | 2015-07-27 18:11:14 -0700 | [diff] [blame] | 5941 | if (mVoiceInteractor != null) { |
| 5942 | mVoiceInteractor.dump(innerPrefix, fd, writer, args); |
| 5943 | } |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 5944 | |
Michael Wright | 5f48dc7 | 2013-11-01 12:42:49 -0700 | [diff] [blame] | 5945 | if (getWindow() != null && |
| 5946 | getWindow().peekDecorView() != null && |
| 5947 | getWindow().peekDecorView().getViewRootImpl() != null) { |
| 5948 | getWindow().peekDecorView().getViewRootImpl().dump(prefix, fd, writer, args); |
| 5949 | } |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 5950 | |
| 5951 | mHandler.getLooper().dump(new PrintWriterPrinter(writer), prefix); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5952 | } |
| 5953 | |
| 5954 | /** |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 5955 | * Bit indicating that this activity is "immersive" and should not be |
| 5956 | * interrupted by notifications if possible. |
| 5957 | * |
| 5958 | * This value is initially set by the manifest property |
| 5959 | * <code>android:immersive</code> but may be changed at runtime by |
| 5960 | * {@link #setImmersive}. |
| 5961 | * |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 5962 | * @see #setImmersive(boolean) |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 5963 | * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE |
| 5964 | */ |
| 5965 | public boolean isImmersive() { |
| 5966 | try { |
| 5967 | return ActivityManagerNative.getDefault().isImmersive(mToken); |
| 5968 | } catch (RemoteException e) { |
| 5969 | return false; |
| 5970 | } |
| 5971 | } |
| 5972 | |
| 5973 | /** |
Craig Mautner | d61dc20 | 2014-07-07 11:09:11 -0700 | [diff] [blame] | 5974 | * Indication of whether this is the highest level activity in this task. Can be used to |
| 5975 | * determine whether an activity launched by this activity was placed in the same task or |
| 5976 | * another task. |
| 5977 | * |
| 5978 | * @return true if this is the topmost, non-finishing activity in its task. |
Craig Mautner | d61dc20 | 2014-07-07 11:09:11 -0700 | [diff] [blame] | 5979 | */ |
George Mount | ff24328 | 2014-07-07 16:12:07 -0700 | [diff] [blame] | 5980 | private boolean isTopOfTask() { |
George Mount | f43ea5d | 2016-05-26 09:52:53 -0700 | [diff] [blame] | 5981 | if (mToken == null || mWindow == null) { |
George Mount | d0ca0e0 | 2016-03-29 10:32:56 -0700 | [diff] [blame] | 5982 | return false; |
| 5983 | } |
Craig Mautner | d61dc20 | 2014-07-07 11:09:11 -0700 | [diff] [blame] | 5984 | try { |
Chong Zhang | 6f9ae4f | 2016-06-03 11:30:20 -0700 | [diff] [blame] | 5985 | return ActivityManagerNative.getDefault().isTopOfTask(getActivityToken()); |
Craig Mautner | d61dc20 | 2014-07-07 11:09:11 -0700 | [diff] [blame] | 5986 | } catch (RemoteException e) { |
| 5987 | return false; |
| 5988 | } |
| 5989 | } |
| 5990 | |
| 5991 | /** |
Craig Mautner | 4addfc5 | 2013-06-25 08:05:45 -0700 | [diff] [blame] | 5992 | * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} to a |
| 5993 | * fullscreen opaque Activity. |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5994 | * <p> |
Craig Mautner | 4addfc5 | 2013-06-25 08:05:45 -0700 | [diff] [blame] | 5995 | * Call this whenever the background of a translucent Activity has changed to become opaque. |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5996 | * Doing so will allow the {@link android.view.Surface} of the Activity behind to be released. |
| 5997 | * <p> |
Craig Mautner | 4addfc5 | 2013-06-25 08:05:45 -0700 | [diff] [blame] | 5998 | * This call has no effect on non-translucent activities or on activities with the |
| 5999 | * {@link android.R.attr#windowIsFloating} attribute. |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6000 | * |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 6001 | * @see #convertToTranslucent(android.app.Activity.TranslucentConversionListener, |
| 6002 | * ActivityOptions) |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6003 | * @see TranslucentConversionListener |
Chet Haase | abd3d77 | 2013-09-11 14:33:05 -0700 | [diff] [blame] | 6004 | * |
| 6005 | * @hide |
Craig Mautner | 4addfc5 | 2013-06-25 08:05:45 -0700 | [diff] [blame] | 6006 | */ |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6007 | @SystemApi |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6008 | public void convertFromTranslucent() { |
Craig Mautner | 4addfc5 | 2013-06-25 08:05:45 -0700 | [diff] [blame] | 6009 | try { |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6010 | mTranslucentCallback = null; |
Craig Mautner | bc57cd1 | 2013-08-19 15:47:42 -0700 | [diff] [blame] | 6011 | if (ActivityManagerNative.getDefault().convertFromTranslucent(mToken)) { |
| 6012 | WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, true); |
| 6013 | } |
Craig Mautner | 4addfc5 | 2013-06-25 08:05:45 -0700 | [diff] [blame] | 6014 | } catch (RemoteException e) { |
| 6015 | // pass |
| 6016 | } |
| 6017 | } |
| 6018 | |
| 6019 | /** |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6020 | * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from |
| 6021 | * opaque to translucent following a call to {@link #convertFromTranslucent()}. |
| 6022 | * <p> |
| 6023 | * Calling this allows the Activity behind this one to be seen again. Once all such Activities |
| 6024 | * have been redrawn {@link TranslucentConversionListener#onTranslucentConversionComplete} will |
| 6025 | * be called indicating that it is safe to make this activity translucent again. Until |
| 6026 | * {@link TranslucentConversionListener#onTranslucentConversionComplete} is called the image |
| 6027 | * behind the frontmost Activity will be indeterminate. |
| 6028 | * <p> |
| 6029 | * This call has no effect on non-translucent activities or on activities with the |
| 6030 | * {@link android.R.attr#windowIsFloating} attribute. |
| 6031 | * |
| 6032 | * @param callback the method to call when all visible Activities behind this one have been |
| 6033 | * drawn and it is safe to make this Activity translucent again. |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 6034 | * @param options activity options delivered to the activity below this one. The options |
| 6035 | * are retrieved using {@link #getActivityOptions}. |
George Mount | 3cc716c | 2014-06-12 16:35:35 -0700 | [diff] [blame] | 6036 | * @return <code>true</code> if Window was opaque and will become translucent or |
| 6037 | * <code>false</code> if window was translucent and no change needed to be made. |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6038 | * |
| 6039 | * @see #convertFromTranslucent() |
| 6040 | * @see TranslucentConversionListener |
Chet Haase | abd3d77 | 2013-09-11 14:33:05 -0700 | [diff] [blame] | 6041 | * |
| 6042 | * @hide |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6043 | */ |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6044 | @SystemApi |
George Mount | 3cc716c | 2014-06-12 16:35:35 -0700 | [diff] [blame] | 6045 | public boolean convertToTranslucent(TranslucentConversionListener callback, |
Jose Lima | 5517ea7 | 2014-06-10 12:31:43 -0700 | [diff] [blame] | 6046 | ActivityOptions options) { |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 6047 | boolean drawComplete; |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6048 | try { |
| 6049 | mTranslucentCallback = callback; |
Craig Mautner | bc57cd1 | 2013-08-19 15:47:42 -0700 | [diff] [blame] | 6050 | mChangeCanvasToTranslucent = |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 6051 | ActivityManagerNative.getDefault().convertToTranslucent(mToken, options); |
George Mount | 9e18397 | 2014-09-03 12:35:09 -0700 | [diff] [blame] | 6052 | WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false); |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 6053 | drawComplete = true; |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6054 | } catch (RemoteException e) { |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 6055 | // Make callback return as though it timed out. |
| 6056 | mChangeCanvasToTranslucent = false; |
| 6057 | drawComplete = false; |
| 6058 | } |
| 6059 | if (!mChangeCanvasToTranslucent && mTranslucentCallback != null) { |
| 6060 | // Window is already translucent. |
| 6061 | mTranslucentCallback.onTranslucentConversionComplete(drawComplete); |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6062 | } |
George Mount | 3cc716c | 2014-06-12 16:35:35 -0700 | [diff] [blame] | 6063 | return mChangeCanvasToTranslucent; |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6064 | } |
| 6065 | |
| 6066 | /** @hide */ |
| 6067 | void onTranslucentConversionComplete(boolean drawComplete) { |
| 6068 | if (mTranslucentCallback != null) { |
| 6069 | mTranslucentCallback.onTranslucentConversionComplete(drawComplete); |
| 6070 | mTranslucentCallback = null; |
| 6071 | } |
Craig Mautner | bc57cd1 | 2013-08-19 15:47:42 -0700 | [diff] [blame] | 6072 | if (mChangeCanvasToTranslucent) { |
| 6073 | WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false); |
| 6074 | } |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6075 | } |
| 6076 | |
Craig Mautner | eb8abf7 | 2014-07-02 15:04:09 -0700 | [diff] [blame] | 6077 | /** @hide */ |
| 6078 | public void onNewActivityOptions(ActivityOptions options) { |
| 6079 | mActivityTransitionState.setEnterActivityOptions(this, options); |
| 6080 | if (!mStopped) { |
| 6081 | mActivityTransitionState.enterReady(this); |
| 6082 | } |
| 6083 | } |
| 6084 | |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6085 | /** |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 6086 | * Retrieve the ActivityOptions passed in from the launching activity or passed back |
| 6087 | * from an activity launched by this activity in its call to {@link |
| 6088 | * #convertToTranslucent(TranslucentConversionListener, ActivityOptions)} |
| 6089 | * |
| 6090 | * @return The ActivityOptions passed to {@link #convertToTranslucent}. |
| 6091 | * @hide |
| 6092 | */ |
| 6093 | ActivityOptions getActivityOptions() { |
| 6094 | try { |
| 6095 | return ActivityManagerNative.getDefault().getActivityOptions(mToken); |
| 6096 | } catch (RemoteException e) { |
| 6097 | } |
| 6098 | return null; |
| 6099 | } |
| 6100 | |
| 6101 | /** |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6102 | * Activities that want to remain visible behind a translucent activity above them must call |
Craig Mautner | 64ccb70 | 2014-10-01 09:38:40 -0700 | [diff] [blame] | 6103 | * this method anytime between the start of {@link #onResume()} and the return from |
| 6104 | * {@link #onPause()}. If this call is successful then the activity will remain visible after |
| 6105 | * {@link #onPause()} is called, and is allowed to continue playing media in the background. |
| 6106 | * |
| 6107 | * <p>The actions of this call are reset each time that this activity is brought to the |
| 6108 | * front. That is, every time {@link #onResume()} is called the activity will be assumed |
| 6109 | * to not have requested visible behind. Therefore, if you want this activity to continue to |
| 6110 | * be visible in the background you must call this method again. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6111 | * |
| 6112 | * <p>Only fullscreen opaque activities may make this call. I.e. this call is a nop |
| 6113 | * for dialog and translucent activities. |
| 6114 | * |
Craig Mautner | 64ccb70 | 2014-10-01 09:38:40 -0700 | [diff] [blame] | 6115 | * <p>Under all circumstances, the activity must stop playing and release resources prior to or |
| 6116 | * within a call to {@link #onVisibleBehindCanceled()} or if this call returns false. |
| 6117 | * |
| 6118 | * <p>False will be returned any time this method is called between the return of onPause and |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6119 | * the next call to onResume. |
| 6120 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6121 | * @param visible true to notify the system that the activity wishes to be visible behind other |
| 6122 | * translucent activities, false to indicate otherwise. Resources must be |
| 6123 | * released when passing false to this method. |
Craig Mautner | 64ccb70 | 2014-10-01 09:38:40 -0700 | [diff] [blame] | 6124 | * @return the resulting visibiity state. If true the activity will remain visible beyond |
| 6125 | * {@link #onPause()} if the next activity is translucent or not fullscreen. If false |
| 6126 | * then the activity may not count on being visible behind other translucent activities, |
| 6127 | * and must stop any media playback and release resources. |
| 6128 | * Returning false may occur in lieu of a call to {@link #onVisibleBehindCanceled()} so |
| 6129 | * the return value must be checked. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6130 | * |
Jose Lima | fcf7083 | 2014-08-27 23:09:05 -0700 | [diff] [blame] | 6131 | * @see #onVisibleBehindCanceled() |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6132 | */ |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6133 | public boolean requestVisibleBehind(boolean visible) { |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6134 | if (!mResumed) { |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6135 | // Do not permit paused or stopped activities to do this. |
| 6136 | visible = false; |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6137 | } |
| 6138 | try { |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6139 | mVisibleBehind = ActivityManagerNative.getDefault() |
| 6140 | .requestVisibleBehind(mToken, visible) && visible; |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6141 | } catch (RemoteException e) { |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6142 | mVisibleBehind = false; |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6143 | } |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6144 | return mVisibleBehind; |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6145 | } |
| 6146 | |
| 6147 | /** |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6148 | * Called when a translucent activity over this activity is becoming opaque or another |
| 6149 | * activity is being launched. Activities that override this method must call |
Jose Lima | fcf7083 | 2014-08-27 23:09:05 -0700 | [diff] [blame] | 6150 | * <code>super.onVisibleBehindCanceled()</code> or a SuperNotCalledException will be thrown. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6151 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6152 | * <p>When this method is called the activity has 500 msec to release any resources it may be |
| 6153 | * using while visible in the background. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6154 | * If the activity has not returned from this method in 500 msec the system will destroy |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6155 | * the activity and kill the process in order to recover the resources for another |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6156 | * process. Otherwise {@link #onStop()} will be called following return. |
| 6157 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6158 | * @see #requestVisibleBehind(boolean) |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6159 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 6160 | @CallSuper |
Jose Lima | fcf7083 | 2014-08-27 23:09:05 -0700 | [diff] [blame] | 6161 | public void onVisibleBehindCanceled() { |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6162 | mCalled = true; |
| 6163 | } |
| 6164 | |
| 6165 | /** |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6166 | * Translucent activities may call this to determine if there is an activity below them that |
| 6167 | * is currently set to be visible in the background. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6168 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6169 | * @return true if an activity below is set to visible according to the most recent call to |
| 6170 | * {@link #requestVisibleBehind(boolean)}, false otherwise. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6171 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6172 | * @see #requestVisibleBehind(boolean) |
Jose Lima | fcf7083 | 2014-08-27 23:09:05 -0700 | [diff] [blame] | 6173 | * @see #onVisibleBehindCanceled() |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6174 | * @see #onBackgroundVisibleBehindChanged(boolean) |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6175 | * @hide |
| 6176 | */ |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6177 | @SystemApi |
| 6178 | public boolean isBackgroundVisibleBehind() { |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6179 | try { |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6180 | return ActivityManagerNative.getDefault().isBackgroundVisibleBehind(mToken); |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6181 | } catch (RemoteException e) { |
| 6182 | } |
| 6183 | return false; |
| 6184 | } |
| 6185 | |
| 6186 | /** |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6187 | * The topmost foreground activity will receive this call when the background visibility state |
| 6188 | * of the activity below it changes. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6189 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6190 | * This call may be a consequence of {@link #requestVisibleBehind(boolean)} or might be |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6191 | * due to a background activity finishing itself. |
| 6192 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6193 | * @param visible true if a background activity is visible, false otherwise. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6194 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6195 | * @see #requestVisibleBehind(boolean) |
Jose Lima | fcf7083 | 2014-08-27 23:09:05 -0700 | [diff] [blame] | 6196 | * @see #onVisibleBehindCanceled() |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6197 | * @hide |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6198 | */ |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6199 | @SystemApi |
| 6200 | public void onBackgroundVisibleBehindChanged(boolean visible) { |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6201 | } |
| 6202 | |
| 6203 | /** |
Craig Mautner | 8746a47 | 2014-07-24 15:12:54 -0700 | [diff] [blame] | 6204 | * Activities cannot draw during the period that their windows are animating in. In order |
| 6205 | * to know when it is safe to begin drawing they can override this method which will be |
| 6206 | * called when the entering animation has completed. |
| 6207 | */ |
| 6208 | public void onEnterAnimationComplete() { |
| 6209 | } |
| 6210 | |
| 6211 | /** |
Filip Gruszczynski | 6eafa90 | 2014-11-14 14:24:37 -0800 | [diff] [blame] | 6212 | * @hide |
| 6213 | */ |
| 6214 | public void dispatchEnterAnimationComplete() { |
| 6215 | onEnterAnimationComplete(); |
| 6216 | if (getWindow() != null && getWindow().getDecorView() != null) { |
| 6217 | getWindow().getDecorView().getViewTreeObserver().dispatchOnEnterAnimationComplete(); |
| 6218 | } |
| 6219 | } |
| 6220 | |
| 6221 | /** |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6222 | * Adjust the current immersive mode setting. |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 6223 | * |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6224 | * Note that changing this value will have no effect on the activity's |
| 6225 | * {@link android.content.pm.ActivityInfo} structure; that is, if |
| 6226 | * <code>android:immersive</code> is set to <code>true</code> |
| 6227 | * in the application's manifest entry for this activity, the {@link |
| 6228 | * android.content.pm.ActivityInfo#flags ActivityInfo.flags} member will |
| 6229 | * always have its {@link android.content.pm.ActivityInfo#FLAG_IMMERSIVE |
| 6230 | * FLAG_IMMERSIVE} bit set. |
| 6231 | * |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 6232 | * @see #isImmersive() |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6233 | * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE |
| 6234 | */ |
| 6235 | public void setImmersive(boolean i) { |
| 6236 | try { |
| 6237 | ActivityManagerNative.getDefault().setImmersive(mToken, i); |
| 6238 | } catch (RemoteException e) { |
| 6239 | // pass |
| 6240 | } |
| 6241 | } |
| 6242 | |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 6243 | /** |
Ruben Brunk | 927d345 | 2016-05-02 19:30:51 -0700 | [diff] [blame] | 6244 | * Enable or disable virtual reality (VR) mode for this Activity. |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 6245 | * |
Ruben Brunk | 927d345 | 2016-05-02 19:30:51 -0700 | [diff] [blame] | 6246 | * <p>VR mode is a hint to Android system to switch to a mode optimized for VR applications |
| 6247 | * while this Activity has user focus.</p> |
| 6248 | * |
| 6249 | * <p>It is recommended that applications additionally declare |
| 6250 | * {@link android.R.attr#enableVrMode} in their manifest to allow for smooth activity |
| 6251 | * transitions when switching between VR activities.</p> |
| 6252 | * |
| 6253 | * <p>If the requested {@link android.service.vr.VrListenerService} component is not available, |
| 6254 | * VR mode will not be started. Developers can handle this case as follows:</p> |
| 6255 | * |
| 6256 | * <pre> |
| 6257 | * String servicePackage = "com.whatever.app"; |
| 6258 | * String serviceClass = "com.whatever.app.MyVrListenerService"; |
| 6259 | * |
| 6260 | * // Name of the component of the VrListenerService to start. |
| 6261 | * ComponentName serviceComponent = new ComponentName(servicePackage, serviceClass); |
| 6262 | * |
| 6263 | * try { |
| 6264 | * setVrModeEnabled(true, myComponentName); |
| 6265 | * } catch (PackageManager.NameNotFoundException e) { |
| 6266 | * List<ApplicationInfo> installed = getPackageManager().getInstalledApplications(0); |
| 6267 | * boolean isInstalled = false; |
| 6268 | * for (ApplicationInfo app : installed) { |
| 6269 | * if (app.packageName.equals(servicePackage)) { |
| 6270 | * isInstalled = true; |
| 6271 | * break; |
| 6272 | * } |
| 6273 | * } |
| 6274 | * if (isInstalled) { |
| 6275 | * // Package is installed, but not enabled in Settings. Let user enable it. |
| 6276 | * startActivity(new Intent(Settings.ACTION_VR_LISTENER_SETTINGS)); |
| 6277 | * } else { |
| 6278 | * // Package is not installed. Send an intent to download this. |
| 6279 | * sentIntentToLaunchAppStore(servicePackage); |
| 6280 | * } |
| 6281 | * } |
| 6282 | * </pre> |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 6283 | * |
| 6284 | * @param enabled {@code true} to enable this mode. |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 6285 | * @param requestedComponent the name of the component to use as a |
| 6286 | * {@link android.service.vr.VrListenerService} while VR mode is enabled. |
| 6287 | * |
Ruben Brunk | 927d345 | 2016-05-02 19:30:51 -0700 | [diff] [blame] | 6288 | * @throws android.content.pm.PackageManager.NameNotFoundException if the given component |
| 6289 | * to run as a {@link android.service.vr.VrListenerService} is not installed, or has |
| 6290 | * not been enabled in user settings. |
| 6291 | * |
| 6292 | * @see android.content.pm.PackageManager#FEATURE_VR_MODE |
| 6293 | * @see android.content.pm.PackageManager#FEATURE_VR_MODE_HIGH_PERFORMANCE |
| 6294 | * @see android.service.vr.VrListenerService |
| 6295 | * @see android.provider.Settings#ACTION_VR_LISTENER_SETTINGS |
| 6296 | * @see android.R.attr#enableVrMode |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 6297 | */ |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 6298 | public void setVrModeEnabled(boolean enabled, @NonNull ComponentName requestedComponent) |
| 6299 | throws PackageManager.NameNotFoundException { |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 6300 | try { |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 6301 | if (ActivityManagerNative.getDefault().setVrMode(mToken, enabled, requestedComponent) |
| 6302 | != 0) { |
| 6303 | throw new PackageManager.NameNotFoundException( |
| 6304 | requestedComponent.flattenToString()); |
| 6305 | } |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 6306 | } catch (RemoteException e) { |
| 6307 | // pass |
| 6308 | } |
| 6309 | } |
| 6310 | |
| 6311 | /** |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 6312 | * Start an action mode of the default type {@link ActionMode#TYPE_PRIMARY}. |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 6313 | * |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 6314 | * @param callback Callback that will manage lifecycle events for this action mode |
| 6315 | * @return The ActionMode that was started, or null if it was canceled |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 6316 | * |
| 6317 | * @see ActionMode |
| 6318 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 6319 | @Nullable |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 6320 | public ActionMode startActionMode(ActionMode.Callback callback) { |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 6321 | return mWindow.getDecorView().startActionMode(callback); |
| 6322 | } |
| 6323 | |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 6324 | /** |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 6325 | * Start an action mode of the given type. |
| 6326 | * |
| 6327 | * @param callback Callback that will manage lifecycle events for this action mode |
| 6328 | * @param type One of {@link ActionMode#TYPE_PRIMARY} or {@link ActionMode#TYPE_FLOATING}. |
| 6329 | * @return The ActionMode that was started, or null if it was canceled |
| 6330 | * |
| 6331 | * @see ActionMode |
| 6332 | */ |
| 6333 | @Nullable |
| 6334 | public ActionMode startActionMode(ActionMode.Callback callback, int type) { |
| 6335 | return mWindow.getDecorView().startActionMode(callback, type); |
| 6336 | } |
| 6337 | |
| 6338 | /** |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 6339 | * Give the Activity a chance to control the UI for an action mode requested |
| 6340 | * by the system. |
| 6341 | * |
| 6342 | * <p>Note: If you are looking for a notification callback that an action mode |
| 6343 | * has been started for this activity, see {@link #onActionModeStarted(ActionMode)}.</p> |
| 6344 | * |
| 6345 | * @param callback The callback that should control the new action mode |
| 6346 | * @return The new action mode, or <code>null</code> if the activity does not want to |
| 6347 | * provide special handling for this action mode. (It will be handled by the system.) |
| 6348 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 6349 | @Nullable |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6350 | @Override |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 6351 | public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) { |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 6352 | // Only Primary ActionModes are represented in the ActionBar. |
| 6353 | if (mActionModeTypeStarting == ActionMode.TYPE_PRIMARY) { |
| 6354 | initWindowDecorActionBar(); |
| 6355 | if (mActionBar != null) { |
| 6356 | return mActionBar.startActionMode(callback); |
| 6357 | } |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 6358 | } |
| 6359 | return null; |
| 6360 | } |
| 6361 | |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 6362 | /** |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 6363 | * {@inheritDoc} |
| 6364 | */ |
| 6365 | @Nullable |
| 6366 | @Override |
| 6367 | public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) { |
| 6368 | try { |
| 6369 | mActionModeTypeStarting = type; |
| 6370 | return onWindowStartingActionMode(callback); |
| 6371 | } finally { |
| 6372 | mActionModeTypeStarting = ActionMode.TYPE_PRIMARY; |
| 6373 | } |
| 6374 | } |
| 6375 | |
| 6376 | /** |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 6377 | * Notifies the Activity that an action mode has been started. |
| 6378 | * Activity subclasses overriding this method should call the superclass implementation. |
| 6379 | * |
| 6380 | * @param mode The new action mode. |
| 6381 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 6382 | @CallSuper |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6383 | @Override |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 6384 | public void onActionModeStarted(ActionMode mode) { |
| 6385 | } |
| 6386 | |
| 6387 | /** |
| 6388 | * Notifies the activity that an action mode has finished. |
| 6389 | * Activity subclasses overriding this method should call the superclass implementation. |
| 6390 | * |
| 6391 | * @param mode The action mode that just finished. |
| 6392 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 6393 | @CallSuper |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6394 | @Override |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 6395 | public void onActionModeFinished(ActionMode mode) { |
| 6396 | } |
| 6397 | |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6398 | /** |
| 6399 | * Returns true if the app should recreate the task when navigating 'up' from this activity |
| 6400 | * by using targetIntent. |
| 6401 | * |
| 6402 | * <p>If this method returns false the app can trivially call |
| 6403 | * {@link #navigateUpTo(Intent)} using the same parameters to correctly perform |
| 6404 | * up navigation. If this method returns false, the app should synthesize a new task stack |
| 6405 | * by using {@link TaskStackBuilder} or another similar mechanism to perform up navigation.</p> |
| 6406 | * |
| 6407 | * @param targetIntent An intent representing the target destination for up navigation |
| 6408 | * @return true if navigating up should recreate a new task stack, false if the same task |
| 6409 | * should be used for the destination |
| 6410 | */ |
| 6411 | public boolean shouldUpRecreateTask(Intent targetIntent) { |
| 6412 | try { |
| 6413 | PackageManager pm = getPackageManager(); |
| 6414 | ComponentName cn = targetIntent.getComponent(); |
| 6415 | if (cn == null) { |
| 6416 | cn = targetIntent.resolveActivity(pm); |
| 6417 | } |
| 6418 | ActivityInfo info = pm.getActivityInfo(cn, 0); |
| 6419 | if (info.taskAffinity == null) { |
| 6420 | return false; |
| 6421 | } |
Dianne Hackborn | 6f4d61f | 2014-08-21 17:50:42 -0700 | [diff] [blame] | 6422 | return ActivityManagerNative.getDefault() |
| 6423 | .shouldUpRecreateTask(mToken, info.taskAffinity); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6424 | } catch (RemoteException e) { |
| 6425 | return false; |
| 6426 | } catch (NameNotFoundException e) { |
| 6427 | return false; |
| 6428 | } |
| 6429 | } |
| 6430 | |
| 6431 | /** |
| 6432 | * Navigate from this activity to the activity specified by upIntent, finishing this activity |
| 6433 | * in the process. If the activity indicated by upIntent already exists in the task's history, |
| 6434 | * this activity and all others before the indicated activity in the history stack will be |
Adam Powell | 35c064b | 2012-05-02 11:37:15 -0700 | [diff] [blame] | 6435 | * finished. |
| 6436 | * |
| 6437 | * <p>If the indicated activity does not appear in the history stack, this will finish |
| 6438 | * each activity in this task until the root activity of the task is reached, resulting in |
| 6439 | * an "in-app home" behavior. This can be useful in apps with a complex navigation hierarchy |
| 6440 | * when an activity may be reached by a path not passing through a canonical parent |
| 6441 | * activity.</p> |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6442 | * |
| 6443 | * <p>This method should be used when performing up navigation from within the same task |
| 6444 | * as the destination. If up navigation should cross tasks in some cases, see |
| 6445 | * {@link #shouldUpRecreateTask(Intent)}.</p> |
| 6446 | * |
| 6447 | * @param upIntent An intent representing the target destination for up navigation |
| 6448 | * |
| 6449 | * @return true if up navigation successfully reached the activity indicated by upIntent and |
| 6450 | * upIntent was delivered to it. false if an instance of the indicated activity could |
| 6451 | * not be found and this activity was simply finished normally. |
| 6452 | */ |
| 6453 | public boolean navigateUpTo(Intent upIntent) { |
| 6454 | if (mParent == null) { |
| 6455 | ComponentName destInfo = upIntent.getComponent(); |
| 6456 | if (destInfo == null) { |
| 6457 | destInfo = upIntent.resolveActivity(getPackageManager()); |
| 6458 | if (destInfo == null) { |
| 6459 | return false; |
| 6460 | } |
| 6461 | upIntent = new Intent(upIntent); |
| 6462 | upIntent.setComponent(destInfo); |
| 6463 | } |
| 6464 | int resultCode; |
| 6465 | Intent resultData; |
| 6466 | synchronized (this) { |
| 6467 | resultCode = mResultCode; |
| 6468 | resultData = mResultData; |
| 6469 | } |
| 6470 | if (resultData != null) { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 6471 | resultData.prepareToLeaveProcess(this); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6472 | } |
| 6473 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 6474 | upIntent.prepareToLeaveProcess(this); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6475 | return ActivityManagerNative.getDefault().navigateUpTo(mToken, upIntent, |
| 6476 | resultCode, resultData); |
| 6477 | } catch (RemoteException e) { |
| 6478 | return false; |
| 6479 | } |
| 6480 | } else { |
| 6481 | return mParent.navigateUpToFromChild(this, upIntent); |
| 6482 | } |
| 6483 | } |
| 6484 | |
| 6485 | /** |
| 6486 | * This is called when a child activity of this one calls its |
| 6487 | * {@link #navigateUpTo} method. The default implementation simply calls |
| 6488 | * navigateUpTo(upIntent) on this activity (the parent). |
| 6489 | * |
| 6490 | * @param child The activity making the call. |
| 6491 | * @param upIntent An intent representing the target destination for up navigation |
| 6492 | * |
| 6493 | * @return true if up navigation successfully reached the activity indicated by upIntent and |
| 6494 | * upIntent was delivered to it. false if an instance of the indicated activity could |
| 6495 | * not be found and this activity was simply finished normally. |
| 6496 | */ |
| 6497 | public boolean navigateUpToFromChild(Activity child, Intent upIntent) { |
| 6498 | return navigateUpTo(upIntent); |
| 6499 | } |
| 6500 | |
| 6501 | /** |
| 6502 | * Obtain an {@link Intent} that will launch an explicit target activity specified by |
| 6503 | * this activity's logical parent. The logical parent is named in the application's manifest |
| 6504 | * by the {@link android.R.attr#parentActivityName parentActivityName} attribute. |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 6505 | * Activity subclasses may override this method to modify the Intent returned by |
| 6506 | * super.getParentActivityIntent() or to implement a different mechanism of retrieving |
| 6507 | * the parent intent entirely. |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6508 | * |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 6509 | * @return a new Intent targeting the defined parent of this activity or null if |
| 6510 | * there is no valid parent. |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6511 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 6512 | @Nullable |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6513 | public Intent getParentActivityIntent() { |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 6514 | final String parentName = mActivityInfo.parentActivityName; |
| 6515 | if (TextUtils.isEmpty(parentName)) { |
| 6516 | return null; |
| 6517 | } |
Adam Powell | 5a4010c | 2012-09-16 15:14:05 -0700 | [diff] [blame] | 6518 | |
| 6519 | // If the parent itself has no parent, generate a main activity intent. |
| 6520 | final ComponentName target = new ComponentName(this, parentName); |
| 6521 | try { |
| 6522 | final ActivityInfo parentInfo = getPackageManager().getActivityInfo(target, 0); |
| 6523 | final String parentActivity = parentInfo.parentActivityName; |
| 6524 | final Intent parentIntent = parentActivity == null |
| 6525 | ? Intent.makeMainActivity(target) |
| 6526 | : new Intent().setComponent(target); |
| 6527 | return parentIntent; |
| 6528 | } catch (NameNotFoundException e) { |
| 6529 | Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName + |
| 6530 | "' in manifest"); |
| 6531 | return null; |
| 6532 | } |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6533 | } |
| 6534 | |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 6535 | /** |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 6536 | * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 6537 | * android.view.View, String)} was used to start an Activity, <var>callback</var> |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 6538 | * will be called to handle shared elements on the <i>launched</i> Activity. This requires |
George Mount | 9826f63 | 2014-09-11 08:50:09 -0700 | [diff] [blame] | 6539 | * {@link Window#FEATURE_ACTIVITY_TRANSITIONS}. |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 6540 | * |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 6541 | * @param callback Used to manipulate shared element transitions on the launched Activity. |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 6542 | */ |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 6543 | public void setEnterSharedElementCallback(SharedElementCallback callback) { |
| 6544 | if (callback == null) { |
| 6545 | callback = SharedElementCallback.NULL_CALLBACK; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 6546 | } |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 6547 | mEnterTransitionListener = callback; |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 6548 | } |
| 6549 | |
| 6550 | /** |
| 6551 | * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 6552 | * android.view.View, String)} was used to start an Activity, <var>callback</var> |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 6553 | * will be called to handle shared elements on the <i>launching</i> Activity. Most |
| 6554 | * calls will only come when returning from the started Activity. |
George Mount | 9826f63 | 2014-09-11 08:50:09 -0700 | [diff] [blame] | 6555 | * This requires {@link Window#FEATURE_ACTIVITY_TRANSITIONS}. |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 6556 | * |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 6557 | * @param callback Used to manipulate shared element transitions on the launching Activity. |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 6558 | */ |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 6559 | public void setExitSharedElementCallback(SharedElementCallback callback) { |
| 6560 | if (callback == null) { |
| 6561 | callback = SharedElementCallback.NULL_CALLBACK; |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 6562 | } |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 6563 | mExitTransitionListener = callback; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 6564 | } |
| 6565 | |
George Mount | 8c2614c | 2014-06-10 11:12:01 -0700 | [diff] [blame] | 6566 | /** |
| 6567 | * Postpone the entering activity transition when Activity was started with |
| 6568 | * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, |
| 6569 | * android.util.Pair[])}. |
| 6570 | * <p>This method gives the Activity the ability to delay starting the entering and |
| 6571 | * shared element transitions until all data is loaded. Until then, the Activity won't |
| 6572 | * draw into its window, leaving the window transparent. This may also cause the |
| 6573 | * returning animation to be delayed until data is ready. This method should be |
| 6574 | * called in {@link #onCreate(android.os.Bundle)} or in |
| 6575 | * {@link #onActivityReenter(int, android.content.Intent)}. |
| 6576 | * {@link #startPostponedEnterTransition()} must be called to allow the Activity to |
| 6577 | * start the transitions. If the Activity did not use |
| 6578 | * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, |
| 6579 | * android.util.Pair[])}, then this method does nothing.</p> |
| 6580 | */ |
| 6581 | public void postponeEnterTransition() { |
| 6582 | mActivityTransitionState.postponeEnterTransition(); |
| 6583 | } |
| 6584 | |
| 6585 | /** |
| 6586 | * Begin postponed transitions after {@link #postponeEnterTransition()} was called. |
| 6587 | * If postponeEnterTransition() was called, you must call startPostponedEnterTransition() |
| 6588 | * to have your Activity start drawing. |
| 6589 | */ |
| 6590 | public void startPostponedEnterTransition() { |
| 6591 | mActivityTransitionState.startPostponedEnterTransition(); |
| 6592 | } |
| 6593 | |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 6594 | /** |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 6595 | * Create {@link DragAndDropPermissions} object bound to this activity and controlling the |
| 6596 | * access permissions for content URIs associated with the {@link DragEvent}. |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 6597 | * @param event Drag event |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 6598 | * @return The {@link DragAndDropPermissions} object used to control access to the content URIs. |
| 6599 | * Null if no content URIs are associated with the event or if permissions could not be granted. |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 6600 | */ |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 6601 | public DragAndDropPermissions requestDragAndDropPermissions(DragEvent event) { |
| 6602 | DragAndDropPermissions dragAndDropPermissions = DragAndDropPermissions.obtain(event); |
| 6603 | if (dragAndDropPermissions != null && dragAndDropPermissions.take(getActivityToken())) { |
| 6604 | return dragAndDropPermissions; |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 6605 | } |
| 6606 | return null; |
| 6607 | } |
| 6608 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6609 | // ------------------ Internal API ------------------ |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6610 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6611 | final void setParent(Activity parent) { |
| 6612 | mParent = parent; |
| 6613 | } |
| 6614 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6615 | final void attach(Context context, ActivityThread aThread, |
| 6616 | Instrumentation instr, IBinder token, int ident, |
| 6617 | Application application, Intent intent, ActivityInfo info, |
| 6618 | CharSequence title, Activity parent, String id, |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 6619 | NonConfigurationInstances lastNonConfigurationInstances, |
Filip Gruszczynski | a59ac9c | 2015-09-10 18:28:48 -0700 | [diff] [blame] | 6620 | Configuration config, String referrer, IVoiceInteractor voiceInteractor, |
| 6621 | Window window) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6622 | attachBaseContext(context); |
| 6623 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 6624 | mFragments.attachHost(null /*parent*/); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6625 | |
Filip Gruszczynski | a59ac9c | 2015-09-10 18:28:48 -0700 | [diff] [blame] | 6626 | mWindow = new PhoneWindow(this, window); |
Skuhne | ce2faa5 | 2015-08-11 10:36:38 -0700 | [diff] [blame] | 6627 | mWindow.setWindowControllerCallback(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6628 | mWindow.setCallback(this); |
Adam Powell | 117b695 | 2014-05-05 18:14:56 -0700 | [diff] [blame] | 6629 | mWindow.setOnWindowDismissedCallback(this); |
Dianne Hackborn | 420829e | 2011-01-28 11:30:35 -0800 | [diff] [blame] | 6630 | mWindow.getLayoutInflater().setPrivateFactory(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6631 | if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) { |
| 6632 | mWindow.setSoftInputMode(info.softInputMode); |
| 6633 | } |
Adam Powell | 269248d | 2011-08-02 10:26:54 -0700 | [diff] [blame] | 6634 | if (info.uiOptions != 0) { |
| 6635 | mWindow.setUiOptions(info.uiOptions); |
| 6636 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6637 | mUiThread = Thread.currentThread(); |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 6638 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6639 | mMainThread = aThread; |
| 6640 | mInstrumentation = instr; |
| 6641 | mToken = token; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6642 | mIdent = ident; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6643 | mApplication = application; |
| 6644 | mIntent = intent; |
Dianne Hackborn | 85d558c | 2014-11-04 10:31:54 -0800 | [diff] [blame] | 6645 | mReferrer = referrer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6646 | mComponent = intent.getComponent(); |
| 6647 | mActivityInfo = info; |
| 6648 | mTitle = title; |
| 6649 | mParent = parent; |
| 6650 | mEmbeddedID = id; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 6651 | mLastNonConfigurationInstances = lastNonConfigurationInstances; |
Dianne Hackborn | 20d9474 | 2014-05-29 18:35:45 -0700 | [diff] [blame] | 6652 | if (voiceInteractor != null) { |
| 6653 | if (lastNonConfigurationInstances != null) { |
| 6654 | mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor; |
| 6655 | } else { |
| 6656 | mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this, |
| 6657 | Looper.myLooper()); |
| 6658 | } |
| 6659 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6660 | |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 6661 | mWindow.setWindowManager( |
| 6662 | (WindowManager)context.getSystemService(Context.WINDOW_SERVICE), |
| 6663 | mToken, mComponent.flattenToString(), |
Romain Guy | 529b60a | 2010-08-03 18:05:47 -0700 | [diff] [blame] | 6664 | (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6665 | if (mParent != null) { |
| 6666 | mWindow.setContainer(mParent.getWindow()); |
| 6667 | } |
| 6668 | mWindowManager = mWindow.getWindowManager(); |
| 6669 | mCurrentConfig = config; |
| 6670 | } |
| 6671 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 6672 | /** @hide */ |
| 6673 | public final IBinder getActivityToken() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6674 | return mParent != null ? mParent.getActivityToken() : mToken; |
| 6675 | } |
| 6676 | |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 6677 | final void performCreateCommon() { |
Dianne Hackborn | 30c9bd8 | 2010-12-01 16:07:40 -0800 | [diff] [blame] | 6678 | mVisibleFromClient = !mWindow.getWindowStyle().getBoolean( |
| 6679 | com.android.internal.R.styleable.Window_windowNoDisplay, false); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 6680 | mFragments.dispatchActivityCreated(); |
Eino-Ville Talvala | 563df3b | 2016-06-06 22:04:54 +0000 | [diff] [blame] | 6681 | mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions()); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6682 | } |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 6683 | |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 6684 | final void performCreate(Bundle icicle) { |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 6685 | restoreHasCurrentPermissionRequest(icicle); |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 6686 | onCreate(icicle); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 6687 | mActivityTransitionState.readState(icicle); |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 6688 | performCreateCommon(); |
| 6689 | } |
| 6690 | |
| 6691 | final void performCreate(Bundle icicle, PersistableBundle persistentState) { |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 6692 | restoreHasCurrentPermissionRequest(icicle); |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 6693 | onCreate(icicle, persistentState); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 6694 | mActivityTransitionState.readState(icicle); |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 6695 | performCreateCommon(); |
| 6696 | } |
| 6697 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6698 | final void performStart() { |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 6699 | mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions()); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 6700 | mFragments.noteStateNotSaved(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6701 | mCalled = false; |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 6702 | mFragments.execPendingActions(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6703 | mInstrumentation.callActivityOnStart(this); |
| 6704 | if (!mCalled) { |
| 6705 | throw new SuperNotCalledException( |
| 6706 | "Activity " + mComponent.toShortString() + |
| 6707 | " did not call through to super.onStart()"); |
| 6708 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6709 | mFragments.dispatchStart(); |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 6710 | mFragments.reportLoaderStart(); |
Dimitry Ivanov | 4449ef5 | 2016-02-25 17:41:13 -0800 | [diff] [blame] | 6711 | |
| 6712 | // This property is set for all builds except final release |
| 6713 | boolean isDlwarningEnabled = SystemProperties.getInt("ro.bionic.ld.warning", 0) == 1; |
| 6714 | boolean isAppDebuggable = |
| 6715 | (mApplication.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; |
| 6716 | |
| 6717 | if (isAppDebuggable || isDlwarningEnabled) { |
| 6718 | String dlwarning = getDlWarning(); |
| 6719 | if (dlwarning != null) { |
Adam Powell | d93f3b1 | 2016-03-03 08:43:37 -0800 | [diff] [blame] | 6720 | String appName = getApplicationInfo().loadLabel(getPackageManager()) |
| 6721 | .toString(); |
Dimitry Ivanov | 4449ef5 | 2016-02-25 17:41:13 -0800 | [diff] [blame] | 6722 | String warning = "Detected problems with app native libraries\n" + |
| 6723 | "(please consult log for detail):\n" + dlwarning; |
| 6724 | if (isAppDebuggable) { |
| 6725 | new AlertDialog.Builder(this). |
| 6726 | setTitle(appName). |
| 6727 | setMessage(warning). |
| 6728 | setPositiveButton(android.R.string.ok, null). |
| 6729 | setCancelable(false). |
| 6730 | show(); |
| 6731 | } else { |
| 6732 | Toast.makeText(this, appName + "\n" + warning, Toast.LENGTH_LONG).show(); |
| 6733 | } |
| 6734 | } |
| 6735 | } |
| 6736 | |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 6737 | mActivityTransitionState.enterReady(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6738 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6739 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6740 | final void performRestart() { |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 6741 | mFragments.noteStateNotSaved(); |
Dianne Hackborn | a21e3da | 2010-09-12 19:27:46 -0700 | [diff] [blame] | 6742 | |
Filip Gruszczynski | a59ac9c | 2015-09-10 18:28:48 -0700 | [diff] [blame] | 6743 | if (mToken != null && mParent == null) { |
Chong Zhang | 7687f25 | 2016-02-26 12:03:33 -0800 | [diff] [blame] | 6744 | // No need to check mStopped, the roots will check if they were actually stopped. |
Filip Gruszczynski | a59ac9c | 2015-09-10 18:28:48 -0700 | [diff] [blame] | 6745 | WindowManagerGlobal.getInstance().setStoppedState(mToken, false /* stopped */); |
| 6746 | } |
| 6747 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6748 | if (mStopped) { |
| 6749 | mStopped = false; |
Dianne Hackborn | 185e3e2 | 2011-06-03 15:26:01 -0700 | [diff] [blame] | 6750 | |
| 6751 | synchronized (mManagedCursors) { |
| 6752 | final int N = mManagedCursors.size(); |
| 6753 | for (int i=0; i<N; i++) { |
| 6754 | ManagedCursor mc = mManagedCursors.get(i); |
| 6755 | if (mc.mReleased || mc.mUpdated) { |
| 6756 | if (!mc.mCursor.requery()) { |
Dianne Hackborn | a5445d3 | 2011-09-01 14:38:24 -0700 | [diff] [blame] | 6757 | if (getApplicationInfo().targetSdkVersion |
| 6758 | >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) { |
| 6759 | throw new IllegalStateException( |
| 6760 | "trying to requery an already closed cursor " |
| 6761 | + mc.mCursor); |
| 6762 | } |
Dianne Hackborn | 185e3e2 | 2011-06-03 15:26:01 -0700 | [diff] [blame] | 6763 | } |
| 6764 | mc.mReleased = false; |
| 6765 | mc.mUpdated = false; |
| 6766 | } |
| 6767 | } |
| 6768 | } |
| 6769 | |
| 6770 | mCalled = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6771 | mInstrumentation.callActivityOnRestart(this); |
| 6772 | if (!mCalled) { |
| 6773 | throw new SuperNotCalledException( |
| 6774 | "Activity " + mComponent.toShortString() + |
| 6775 | " did not call through to super.onRestart()"); |
| 6776 | } |
| 6777 | performStart(); |
| 6778 | } |
| 6779 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6780 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6781 | final void performResume() { |
| 6782 | performRestart(); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6783 | |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 6784 | mFragments.execPendingActions(); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6785 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 6786 | mLastNonConfigurationInstances = null; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6787 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6788 | mCalled = false; |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 6789 | // mResumed is set by the instrumentation |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6790 | mInstrumentation.callActivityOnResume(this); |
| 6791 | if (!mCalled) { |
| 6792 | throw new SuperNotCalledException( |
| 6793 | "Activity " + mComponent.toShortString() + |
| 6794 | " did not call through to super.onResume()"); |
| 6795 | } |
| 6796 | |
Todd Kennedy | ee8c9c6 | 2014-12-10 14:22:59 -0800 | [diff] [blame] | 6797 | // invisible activities must be finished before onResume() completes |
| 6798 | if (!mVisibleFromClient && !mFinished) { |
| 6799 | Log.w(TAG, "An activity without a UI must call finish() before onResume() completes"); |
| 6800 | if (getApplicationInfo().targetSdkVersion |
| 6801 | > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) { |
| 6802 | throw new IllegalStateException( |
| 6803 | "Activity " + mComponent.toShortString() + |
| 6804 | " did not call finish() prior to onResume() completing"); |
| 6805 | } |
| 6806 | } |
| 6807 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6808 | // Now really resume, and install the current status bar and menu. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6809 | mCalled = false; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6810 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6811 | mFragments.dispatchResume(); |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 6812 | mFragments.execPendingActions(); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6813 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6814 | onPostResume(); |
| 6815 | if (!mCalled) { |
| 6816 | throw new SuperNotCalledException( |
| 6817 | "Activity " + mComponent.toShortString() + |
| 6818 | " did not call through to super.onPostResume()"); |
| 6819 | } |
| 6820 | } |
| 6821 | |
| 6822 | final void performPause() { |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 6823 | mDoReportFullyDrawn = false; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6824 | mFragments.dispatchPause(); |
Dianne Hackborn | e794e9f | 2010-08-24 12:32:10 -0700 | [diff] [blame] | 6825 | mCalled = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6826 | onPause(); |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6827 | mResumed = false; |
Dianne Hackborn | e794e9f | 2010-08-24 12:32:10 -0700 | [diff] [blame] | 6828 | if (!mCalled && getApplicationInfo().targetSdkVersion |
| 6829 | >= android.os.Build.VERSION_CODES.GINGERBREAD) { |
| 6830 | throw new SuperNotCalledException( |
| 6831 | "Activity " + mComponent.toShortString() + |
| 6832 | " did not call through to super.onPause()"); |
| 6833 | } |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 6834 | mResumed = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6835 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6836 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6837 | final void performUserLeaving() { |
| 6838 | onUserInteraction(); |
| 6839 | onUserLeaveHint(); |
| 6840 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6841 | |
Chong Zhang | 7687f25 | 2016-02-26 12:03:33 -0800 | [diff] [blame] | 6842 | final void performStop(boolean preserveWindow) { |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 6843 | mDoReportFullyDrawn = false; |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 6844 | mFragments.doLoaderStop(mChangingConfigurations /*retain*/); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6845 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6846 | if (!mStopped) { |
| 6847 | if (mWindow != null) { |
| 6848 | mWindow.closeAllPanels(); |
| 6849 | } |
| 6850 | |
Chong Zhang | 7687f25 | 2016-02-26 12:03:33 -0800 | [diff] [blame] | 6851 | // If we're preserving the window, don't setStoppedState to true, since we |
| 6852 | // need the window started immediately again. Stopping the window will |
| 6853 | // destroys hardware resources and causes flicker. |
| 6854 | if (!preserveWindow && mToken != null && mParent == null) { |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 6855 | WindowManagerGlobal.getInstance().setStoppedState(mToken, true); |
Dianne Hackborn | ce418e6 | 2011-03-01 14:31:38 -0800 | [diff] [blame] | 6856 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6857 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6858 | mFragments.dispatchStop(); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6859 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6860 | mCalled = false; |
| 6861 | mInstrumentation.callActivityOnStop(this); |
| 6862 | if (!mCalled) { |
| 6863 | throw new SuperNotCalledException( |
| 6864 | "Activity " + mComponent.toShortString() + |
| 6865 | " did not call through to super.onStop()"); |
| 6866 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6867 | |
Makoto Onuki | 2f6a018 | 2010-02-22 13:26:59 -0800 | [diff] [blame] | 6868 | synchronized (mManagedCursors) { |
| 6869 | final int N = mManagedCursors.size(); |
| 6870 | for (int i=0; i<N; i++) { |
| 6871 | ManagedCursor mc = mManagedCursors.get(i); |
| 6872 | if (!mc.mReleased) { |
| 6873 | mc.mCursor.deactivate(); |
| 6874 | mc.mReleased = true; |
| 6875 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6876 | } |
| 6877 | } |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 6878 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6879 | mStopped = true; |
| 6880 | } |
| 6881 | mResumed = false; |
| 6882 | } |
| 6883 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6884 | final void performDestroy() { |
Dianne Hackborn | 6d9dcbc | 2012-10-02 17:51:13 -0700 | [diff] [blame] | 6885 | mDestroyed = true; |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 6886 | mWindow.destroy(); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6887 | mFragments.dispatchDestroy(); |
| 6888 | onDestroy(); |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 6889 | mFragments.doLoaderDestroy(); |
Dianne Hackborn | 20d9474 | 2014-05-29 18:35:45 -0700 | [diff] [blame] | 6890 | if (mVoiceInteractor != null) { |
| 6891 | mVoiceInteractor.detachActivity(); |
| 6892 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6893 | } |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 6894 | |
Adam Powell | 858cf03 | 2016-05-09 15:45:37 -0700 | [diff] [blame] | 6895 | final void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode) { |
| 6896 | if (DEBUG_LIFECYCLE) Slog.v(TAG, |
| 6897 | "dispatchMultiWindowModeChanged " + this + ": " + isInMultiWindowMode); |
| 6898 | mFragments.dispatchMultiWindowModeChanged(isInMultiWindowMode); |
| 6899 | if (mWindow != null) { |
| 6900 | mWindow.onMultiWindowModeChanged(); |
| 6901 | } |
| 6902 | onMultiWindowModeChanged(isInMultiWindowMode); |
| 6903 | } |
| 6904 | |
| 6905 | final void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode) { |
| 6906 | if (DEBUG_LIFECYCLE) Slog.v(TAG, |
| 6907 | "dispatchPictureInPictureModeChanged " + this + ": " + isInPictureInPictureMode); |
| 6908 | mFragments.dispatchPictureInPictureModeChanged(isInPictureInPictureMode); |
| 6909 | onPictureInPictureModeChanged(isInPictureInPictureMode); |
| 6910 | } |
| 6911 | |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 6912 | /** |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 6913 | * @hide |
| 6914 | */ |
| 6915 | public final boolean isResumed() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6916 | return mResumed; |
| 6917 | } |
| 6918 | |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 6919 | private void storeHasCurrentPermissionRequest(Bundle bundle) { |
| 6920 | if (bundle != null && mHasCurrentPermissionsRequest) { |
| 6921 | bundle.putBoolean(HAS_CURENT_PERMISSIONS_REQUEST_KEY, true); |
| 6922 | } |
| 6923 | } |
| 6924 | |
| 6925 | private void restoreHasCurrentPermissionRequest(Bundle bundle) { |
| 6926 | if (bundle != null) { |
| 6927 | mHasCurrentPermissionsRequest = bundle.getBoolean( |
| 6928 | HAS_CURENT_PERMISSIONS_REQUEST_KEY, false); |
| 6929 | } |
| 6930 | } |
| 6931 | |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 6932 | void dispatchActivityResult(String who, int requestCode, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6933 | int resultCode, Intent data) { |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 6934 | if (false) Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6935 | TAG, "Dispatching result: who=" + who + ", reqCode=" + requestCode |
| 6936 | + ", resCode=" + resultCode + ", data=" + data); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 6937 | mFragments.noteStateNotSaved(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6938 | if (who == null) { |
Svetoslav | 970b59c | 2015-06-09 16:05:21 -0700 | [diff] [blame] | 6939 | onActivityResult(requestCode, resultCode, data); |
| 6940 | } else if (who.startsWith(REQUEST_PERMISSIONS_WHO_PREFIX)) { |
| 6941 | who = who.substring(REQUEST_PERMISSIONS_WHO_PREFIX.length()); |
| 6942 | if (TextUtils.isEmpty(who)) { |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 6943 | dispatchRequestPermissionsResult(requestCode, data); |
| 6944 | } else { |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 6945 | Fragment frag = mFragments.findFragmentByWho(who); |
| 6946 | if (frag != null) { |
Svetoslav | 970b59c | 2015-06-09 16:05:21 -0700 | [diff] [blame] | 6947 | dispatchRequestPermissionsResultToFragment(requestCode, data, frag); |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 6948 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 6949 | } |
Svetoslav | 970b59c | 2015-06-09 16:05:21 -0700 | [diff] [blame] | 6950 | } else if (who.startsWith("@android:view:")) { |
| 6951 | ArrayList<ViewRootImpl> views = WindowManagerGlobal.getInstance().getRootViews( |
| 6952 | getActivityToken()); |
| 6953 | for (ViewRootImpl viewRoot : views) { |
| 6954 | if (viewRoot.getView() != null |
| 6955 | && viewRoot.getView().dispatchActivityResult( |
| 6956 | who, requestCode, resultCode, data)) { |
| 6957 | return; |
| 6958 | } |
| 6959 | } |
| 6960 | } else { |
| 6961 | Fragment frag = mFragments.findFragmentByWho(who); |
| 6962 | if (frag != null) { |
| 6963 | frag.onActivityResult(requestCode, resultCode, data); |
| 6964 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6965 | } |
| 6966 | } |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6967 | |
Jason Monk | a57e5e0 | 2014-05-07 10:06:48 -0400 | [diff] [blame] | 6968 | /** |
Jason Monk | 62515be | 2014-05-21 16:06:19 -0400 | [diff] [blame] | 6969 | * Request to put this Activity in a mode where the user is locked to the |
Jason Monk | a57e5e0 | 2014-05-07 10:06:48 -0400 | [diff] [blame] | 6970 | * current task. |
| 6971 | * |
Craig Mautner | 15df08a | 2015-04-01 12:17:18 -0700 | [diff] [blame] | 6972 | * This will prevent the user from launching other apps, going to settings, or reaching the |
| 6973 | * home screen. This does not include those apps whose {@link android.R.attr#lockTaskMode} |
| 6974 | * values permit launching while locked. |
Jason Monk | a57e5e0 | 2014-05-07 10:06:48 -0400 | [diff] [blame] | 6975 | * |
Craig Mautner | 15df08a | 2015-04-01 12:17:18 -0700 | [diff] [blame] | 6976 | * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns true or |
| 6977 | * lockTaskMode=lockTaskModeAlways for this component then the app will go directly into |
| 6978 | * Lock Task mode. The user will not be able to exit this mode until |
| 6979 | * {@link Activity#stopLockTask()} is called. |
Jason Monk | 62515be | 2014-05-21 16:06:19 -0400 | [diff] [blame] | 6980 | * |
| 6981 | * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns false |
| 6982 | * then the system will prompt the user with a dialog requesting permission to enter |
Jason Monk | 8863b57 | 2014-07-29 13:49:32 -0400 | [diff] [blame] | 6983 | * this mode. When entered through this method the user can exit at any time through |
| 6984 | * an action described by the request dialog. Calling stopLockTask will also exit the |
| 6985 | * mode. |
Craig Mautner | 15df08a | 2015-04-01 12:17:18 -0700 | [diff] [blame] | 6986 | * |
| 6987 | * @see android.R.attr#lockTaskMode |
Jason Monk | a57e5e0 | 2014-05-07 10:06:48 -0400 | [diff] [blame] | 6988 | */ |
Craig Mautner | aea74a5 | 2014-03-08 14:23:10 -0800 | [diff] [blame] | 6989 | public void startLockTask() { |
| 6990 | try { |
| 6991 | ActivityManagerNative.getDefault().startLockTaskMode(mToken); |
| 6992 | } catch (RemoteException e) { |
| 6993 | } |
| 6994 | } |
| 6995 | |
Jason Monk | a57e5e0 | 2014-05-07 10:06:48 -0400 | [diff] [blame] | 6996 | /** |
| 6997 | * Allow the user to switch away from the current task. |
| 6998 | * |
| 6999 | * Called to end the mode started by {@link Activity#startLockTask}. This |
| 7000 | * can only be called by activities that have successfully called |
| 7001 | * startLockTask previously. |
| 7002 | * |
| 7003 | * This will allow the user to exit this app and move onto other activities. |
Craig Mautner | 15df08a | 2015-04-01 12:17:18 -0700 | [diff] [blame] | 7004 | * <p>Note: This method should only be called when the activity is user-facing. That is, |
| 7005 | * between onResume() and onPause(). |
| 7006 | * <p>Note: If there are other tasks below this one that are also locked then calling this |
| 7007 | * method will immediately finish this task and resume the previous locked one, remaining in |
| 7008 | * lockTask mode. |
| 7009 | * |
| 7010 | * @see android.R.attr#lockTaskMode |
| 7011 | * @see ActivityManager#getLockTaskModeState() |
Jason Monk | a57e5e0 | 2014-05-07 10:06:48 -0400 | [diff] [blame] | 7012 | */ |
Craig Mautner | aea74a5 | 2014-03-08 14:23:10 -0800 | [diff] [blame] | 7013 | public void stopLockTask() { |
| 7014 | try { |
| 7015 | ActivityManagerNative.getDefault().stopLockTaskMode(); |
| 7016 | } catch (RemoteException e) { |
| 7017 | } |
| 7018 | } |
| 7019 | |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 7020 | /** |
Craig Mautner | c21ae9e | 2015-04-15 09:45:42 -0700 | [diff] [blame] | 7021 | * Shows the user the system defined message for telling the user how to exit |
| 7022 | * lock task mode. The task containing this activity must be in lock task mode at the time |
| 7023 | * of this call for the message to be displayed. |
| 7024 | */ |
| 7025 | public void showLockTaskEscapeMessage() { |
| 7026 | try { |
| 7027 | ActivityManagerNative.getDefault().showLockTaskEscapeMessage(mToken); |
| 7028 | } catch (RemoteException e) { |
| 7029 | } |
| 7030 | } |
| 7031 | |
| 7032 | /** |
Andrii Kulian | 933076d | 2016-03-29 17:04:42 -0700 | [diff] [blame] | 7033 | * Check whether the caption on freeform windows is displayed directly on the content. |
| 7034 | * |
| 7035 | * @return True if caption is displayed on content, false if it pushes the content down. |
| 7036 | * |
| 7037 | * @see {@link #setOverlayWithDecorCaptionEnabled(boolean)} |
| 7038 | */ |
| 7039 | public boolean isOverlayWithDecorCaptionEnabled() { |
| 7040 | return mWindow.isOverlayWithDecorCaptionEnabled(); |
| 7041 | } |
| 7042 | |
| 7043 | /** |
Filip Gruszczynski | 6325065 | 2015-11-18 14:43:01 -0800 | [diff] [blame] | 7044 | * Set whether the caption should displayed directly on the content rather than push it down. |
| 7045 | * |
| 7046 | * This affects only freeform windows since they display the caption and only the main |
| 7047 | * window of the activity. The caption is used to drag the window around and also shows |
| 7048 | * maximize and close action buttons. |
| 7049 | */ |
Andrii Kulian | 933076d | 2016-03-29 17:04:42 -0700 | [diff] [blame] | 7050 | public void setOverlayWithDecorCaptionEnabled(boolean enabled) { |
| 7051 | mWindow.setOverlayWithDecorCaptionEnabled(enabled); |
Filip Gruszczynski | 6325065 | 2015-11-18 14:43:01 -0800 | [diff] [blame] | 7052 | } |
| 7053 | |
| 7054 | /** |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 7055 | * Interface for informing a translucent {@link Activity} once all visible activities below it |
| 7056 | * have completed drawing. This is necessary only after an {@link Activity} has been made |
| 7057 | * opaque using {@link Activity#convertFromTranslucent()} and before it has been drawn |
| 7058 | * translucent again following a call to {@link |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 7059 | * Activity#convertToTranslucent(android.app.Activity.TranslucentConversionListener, |
| 7060 | * ActivityOptions)} |
Chet Haase | abd3d77 | 2013-09-11 14:33:05 -0700 | [diff] [blame] | 7061 | * |
| 7062 | * @hide |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 7063 | */ |
Jose Lima | 1491485 | 2014-08-14 09:14:12 -0700 | [diff] [blame] | 7064 | @SystemApi |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 7065 | public interface TranslucentConversionListener { |
| 7066 | /** |
| 7067 | * Callback made following {@link Activity#convertToTranslucent} once all visible Activities |
| 7068 | * below the top one have been redrawn. Following this callback it is safe to make the top |
| 7069 | * Activity translucent because the underlying Activity has been drawn. |
| 7070 | * |
| 7071 | * @param drawComplete True if the background Activity has drawn itself. False if a timeout |
| 7072 | * occurred waiting for the Activity to complete drawing. |
| 7073 | * |
| 7074 | * @see Activity#convertFromTranslucent() |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 7075 | * @see Activity#convertToTranslucent(TranslucentConversionListener, ActivityOptions) |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 7076 | */ |
| 7077 | public void onTranslucentConversionComplete(boolean drawComplete); |
| 7078 | } |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 7079 | |
| 7080 | private void dispatchRequestPermissionsResult(int requestCode, Intent data) { |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 7081 | mHasCurrentPermissionsRequest = false; |
Svet Ganov | 6a166af | 2015-06-30 10:15:44 -0700 | [diff] [blame] | 7082 | // If the package installer crashed we may have not data - best effort. |
| 7083 | String[] permissions = (data != null) ? data.getStringArrayExtra( |
| 7084 | PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0]; |
| 7085 | final int[] grantResults = (data != null) ? data.getIntArrayExtra( |
| 7086 | PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0]; |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 7087 | onRequestPermissionsResult(requestCode, permissions, grantResults); |
| 7088 | } |
| 7089 | |
| 7090 | private void dispatchRequestPermissionsResultToFragment(int requestCode, Intent data, |
Svet Ganov | 6a166af | 2015-06-30 10:15:44 -0700 | [diff] [blame] | 7091 | Fragment fragment) { |
| 7092 | // If the package installer crashed we may have not data - best effort. |
| 7093 | String[] permissions = (data != null) ? data.getStringArrayExtra( |
| 7094 | PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0]; |
| 7095 | final int[] grantResults = (data != null) ? data.getIntArrayExtra( |
| 7096 | PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0]; |
| 7097 | fragment.onRequestPermissionsResult(requestCode, permissions, grantResults); |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 7098 | } |
| 7099 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 7100 | class HostCallbacks extends FragmentHostCallback<Activity> { |
| 7101 | public HostCallbacks() { |
| 7102 | super(Activity.this /*activity*/); |
| 7103 | } |
| 7104 | |
| 7105 | @Override |
| 7106 | public void onDump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 7107 | Activity.this.dump(prefix, fd, writer, args); |
| 7108 | } |
| 7109 | |
| 7110 | @Override |
| 7111 | public boolean onShouldSaveFragmentState(Fragment fragment) { |
| 7112 | return !isFinishing(); |
| 7113 | } |
| 7114 | |
| 7115 | @Override |
| 7116 | public LayoutInflater onGetLayoutInflater() { |
| 7117 | final LayoutInflater result = Activity.this.getLayoutInflater(); |
| 7118 | if (onUseFragmentManagerInflaterFactory()) { |
| 7119 | return result.cloneInContext(Activity.this); |
| 7120 | } |
| 7121 | return result; |
| 7122 | } |
| 7123 | |
| 7124 | @Override |
| 7125 | public boolean onUseFragmentManagerInflaterFactory() { |
| 7126 | // Newer platform versions use the child fragment manager's LayoutInflaterFactory. |
| 7127 | return getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP; |
| 7128 | } |
| 7129 | |
| 7130 | @Override |
| 7131 | public Activity onGetHost() { |
| 7132 | return Activity.this; |
| 7133 | } |
| 7134 | |
| 7135 | @Override |
| 7136 | public void onInvalidateOptionsMenu() { |
| 7137 | Activity.this.invalidateOptionsMenu(); |
| 7138 | } |
| 7139 | |
| 7140 | @Override |
| 7141 | public void onStartActivityFromFragment(Fragment fragment, Intent intent, int requestCode, |
| 7142 | Bundle options) { |
| 7143 | Activity.this.startActivityFromFragment(fragment, intent, requestCode, options); |
| 7144 | } |
| 7145 | |
| 7146 | @Override |
Clara Bayarri | a0c2dc3 | 2016-04-12 12:00:15 +0100 | [diff] [blame] | 7147 | public void onStartIntentSenderFromFragment(Fragment fragment, IntentSender intent, |
| 7148 | int requestCode, @Nullable Intent fillInIntent, int flagsMask, int flagsValues, |
| 7149 | int extraFlags, Bundle options) throws IntentSender.SendIntentException { |
| 7150 | if (mParent == null) { |
| 7151 | startIntentSenderForResultInner(intent, fragment.mWho, requestCode, fillInIntent, |
| 7152 | flagsMask, flagsValues, options); |
| 7153 | } else if (options != null) { |
| 7154 | mParent.startIntentSenderFromChildFragment(fragment, intent, requestCode, |
| 7155 | fillInIntent, flagsMask, flagsValues, extraFlags, options); |
| 7156 | } |
| 7157 | } |
| 7158 | |
| 7159 | @Override |
Svetoslav | 970b59c | 2015-06-09 16:05:21 -0700 | [diff] [blame] | 7160 | public void onRequestPermissionsFromFragment(Fragment fragment, String[] permissions, |
| 7161 | int requestCode) { |
| 7162 | String who = REQUEST_PERMISSIONS_WHO_PREFIX + fragment.mWho; |
| 7163 | Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions); |
| 7164 | startActivityForResult(who, intent, requestCode, null); |
| 7165 | } |
| 7166 | |
| 7167 | @Override |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 7168 | public boolean onHasWindowAnimations() { |
| 7169 | return getWindow() != null; |
| 7170 | } |
| 7171 | |
| 7172 | @Override |
| 7173 | public int onGetWindowAnimations() { |
| 7174 | final Window w = getWindow(); |
| 7175 | return (w == null) ? 0 : w.getAttributes().windowAnimations; |
| 7176 | } |
| 7177 | |
Todd Kennedy | 434bd65 | 2015-05-04 12:29:50 -0700 | [diff] [blame] | 7178 | @Override |
| 7179 | public void onAttachFragment(Fragment fragment) { |
| 7180 | Activity.this.onAttachFragment(fragment); |
| 7181 | } |
| 7182 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 7183 | @Nullable |
| 7184 | @Override |
| 7185 | public View onFindViewById(int id) { |
| 7186 | return Activity.this.findViewById(id); |
| 7187 | } |
| 7188 | |
| 7189 | @Override |
| 7190 | public boolean onHasView() { |
| 7191 | final Window w = getWindow(); |
| 7192 | return (w != null && w.peekDecorView() != null); |
| 7193 | } |
| 7194 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7195 | } |