Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 com.android.server.am; |
| 18 | |
Jeff Sharkey | 35be756 | 2012-04-18 19:16:15 -0700 | [diff] [blame] | 19 | import static android.Manifest.permission.START_ANY_ACTIVITY; |
| 20 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
| 21 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 22 | import com.android.internal.os.BatteryStatsImpl; |
Craig Mautner | d74f7d7 | 2013-02-26 13:41:02 -0800 | [diff] [blame] | 23 | import com.android.internal.util.Objects; |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 24 | import com.android.server.am.ActivityManagerService.ItemMatcher; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 25 | import com.android.server.am.ActivityManagerService.PendingActivityLaunch; |
Craig Mautner | 4b71aa1 | 2012-12-27 17:20:01 -0800 | [diff] [blame] | 26 | import com.android.server.wm.AppTransition; |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 27 | import com.android.server.wm.TaskGroup; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 28 | |
| 29 | import android.app.Activity; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 30 | import android.app.ActivityManager; |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 31 | import android.app.ActivityOptions; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 32 | import android.app.AppGlobals; |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 33 | import android.app.IActivityController; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 34 | import android.app.IActivityManager; |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 35 | import android.app.IThumbnailReceiver; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 36 | import android.app.IThumbnailRetriever; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 37 | import android.app.IApplicationThread; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 38 | import android.app.ResultInfo; |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 39 | import android.app.ActivityManager.RunningTaskInfo; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 40 | import android.app.IActivityManager.WaitResult; |
| 41 | import android.content.ComponentName; |
| 42 | import android.content.Context; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 43 | import android.content.Intent; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 44 | import android.content.pm.ActivityInfo; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 45 | import android.content.pm.PackageManager; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 46 | import android.content.res.Configuration; |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 47 | import android.content.res.Resources; |
| 48 | import android.graphics.Bitmap; |
Craig Mautner | b12428a | 2012-12-20 16:07:06 -0800 | [diff] [blame] | 49 | import android.graphics.Bitmap.Config; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 50 | import android.os.Binder; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 51 | import android.os.Bundle; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 52 | import android.os.Handler; |
| 53 | import android.os.IBinder; |
Zoran Marceta | f958b32 | 2012-08-09 20:27:12 +0900 | [diff] [blame] | 54 | import android.os.Looper; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 55 | import android.os.Message; |
| 56 | import android.os.PowerManager; |
| 57 | import android.os.RemoteException; |
| 58 | import android.os.SystemClock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 59 | import android.os.UserHandle; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 60 | import android.util.EventLog; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 61 | import android.util.Slog; |
Craig Mautner | 59c0097 | 2012-07-30 12:10:24 -0700 | [diff] [blame] | 62 | import android.view.Display; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 63 | |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 64 | import java.io.FileDescriptor; |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 65 | import java.io.PrintWriter; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 66 | import java.lang.ref.WeakReference; |
| 67 | import java.util.ArrayList; |
| 68 | import java.util.Iterator; |
| 69 | import java.util.List; |
| 70 | |
| 71 | /** |
| 72 | * State and management of a single stack of activities. |
| 73 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 74 | final class ActivityStack { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 75 | static final String TAG = ActivityManagerService.TAG; |
Dianne Hackborn | b961cd2 | 2011-06-21 12:13:37 -0700 | [diff] [blame] | 76 | static final boolean localLOGV = ActivityManagerService.localLOGV; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 77 | static final boolean DEBUG_SWITCH = ActivityManagerService.DEBUG_SWITCH; |
| 78 | static final boolean DEBUG_PAUSE = ActivityManagerService.DEBUG_PAUSE; |
| 79 | static final boolean DEBUG_VISBILITY = ActivityManagerService.DEBUG_VISBILITY; |
| 80 | static final boolean DEBUG_USER_LEAVING = ActivityManagerService.DEBUG_USER_LEAVING; |
| 81 | static final boolean DEBUG_TRANSITION = ActivityManagerService.DEBUG_TRANSITION; |
| 82 | static final boolean DEBUG_RESULTS = ActivityManagerService.DEBUG_RESULTS; |
| 83 | static final boolean DEBUG_CONFIGURATION = ActivityManagerService.DEBUG_CONFIGURATION; |
| 84 | static final boolean DEBUG_TASKS = ActivityManagerService.DEBUG_TASKS; |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 85 | static final boolean DEBUG_CLEANUP = ActivityManagerService.DEBUG_CLEANUP; |
Craig Mautner | 9658b31 | 2013-02-28 10:55:59 -0800 | [diff] [blame] | 86 | |
Craig Mautner | 2420ead | 2013-04-01 17:13:20 -0700 | [diff] [blame] | 87 | static final boolean DEBUG_STATES = ActivityStackSupervisor.DEBUG_STATES; |
| 88 | static final boolean DEBUG_ADD_REMOVE = ActivityStackSupervisor.DEBUG_ADD_REMOVE; |
| 89 | static final boolean DEBUG_SAVED_STATE = ActivityStackSupervisor.DEBUG_SAVED_STATE; |
| 90 | static final boolean DEBUG_APP = ActivityStackSupervisor.DEBUG_APP; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 91 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 92 | static final boolean VALIDATE_TOKENS = ActivityManagerService.VALIDATE_TOKENS; |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 93 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 94 | // How long we wait until giving up on the last activity telling us it |
| 95 | // is idle. |
| 96 | static final int IDLE_TIMEOUT = 10*1000; |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 97 | |
| 98 | // Ticks during which we check progress while waiting for an app to launch. |
| 99 | static final int LAUNCH_TICK = 500; |
| 100 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 101 | // How long we wait until giving up on the last activity to pause. This |
| 102 | // is short because it directly impacts the responsiveness of starting the |
| 103 | // next activity. |
| 104 | static final int PAUSE_TIMEOUT = 500; |
| 105 | |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 106 | // How long we wait for the activity to tell us it has stopped before |
| 107 | // giving up. This is a good amount of time because we really need this |
| 108 | // from the application in order to get its saved state. |
| 109 | static final int STOP_TIMEOUT = 10*1000; |
| 110 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 111 | // How long we can hold the sleep wake lock before giving up. |
| 112 | static final int SLEEP_TIMEOUT = 5*1000; |
| 113 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 114 | // How long we can hold the launch wake lock before giving up. |
| 115 | static final int LAUNCH_TIMEOUT = 10*1000; |
| 116 | |
| 117 | // How long we wait until giving up on an activity telling us it has |
| 118 | // finished destroying itself. |
| 119 | static final int DESTROY_TIMEOUT = 10*1000; |
Craig Mautner | d74f7d7 | 2013-02-26 13:41:02 -0800 | [diff] [blame] | 120 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 121 | // How long until we reset a task when the user returns to it. Currently |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 122 | // disabled. |
| 123 | static final long ACTIVITY_INACTIVE_RESET_TIME = 0; |
Craig Mautner | d74f7d7 | 2013-02-26 13:41:02 -0800 | [diff] [blame] | 124 | |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 125 | // How long between activity launches that we consider safe to not warn |
| 126 | // the user about an unexpected activity being launched on top. |
| 127 | static final long START_WARN_TIME = 5*1000; |
Craig Mautner | d74f7d7 | 2013-02-26 13:41:02 -0800 | [diff] [blame] | 128 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 129 | // Set to false to disable the preview that is shown while a new activity |
| 130 | // is being started. |
| 131 | static final boolean SHOW_APP_STARTING_PREVIEW = true; |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 132 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 133 | enum ActivityState { |
| 134 | INITIALIZING, |
| 135 | RESUMED, |
| 136 | PAUSING, |
| 137 | PAUSED, |
| 138 | STOPPING, |
| 139 | STOPPED, |
| 140 | FINISHING, |
| 141 | DESTROYING, |
| 142 | DESTROYED |
| 143 | } |
| 144 | |
| 145 | final ActivityManagerService mService; |
Craig Mautner | d74f7d7 | 2013-02-26 13:41:02 -0800 | [diff] [blame] | 146 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 147 | final Context mContext; |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 148 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 149 | /** |
| 150 | * The back history of all previous (and possibly still |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 151 | * running) activities. It contains #TaskRecord objects. |
| 152 | */ |
| 153 | private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>(); |
| 154 | |
| 155 | /** |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 156 | * Used for validating app tokens with window manager. |
| 157 | */ |
Craig Mautner | b1fd65c0 | 2013-02-05 13:34:57 -0800 | [diff] [blame] | 158 | final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>(); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 159 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 160 | /** |
| 161 | * List of running activities, sorted by recent usage. |
| 162 | * The first entry in the list is the least recently used. |
| 163 | * It contains HistoryRecord objects. |
| 164 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 165 | final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 166 | |
| 167 | /** |
| 168 | * List of activities that are waiting for a new activity |
| 169 | * to become visible before completing whatever operation they are |
| 170 | * supposed to do. |
| 171 | */ |
| 172 | final ArrayList<ActivityRecord> mWaitingVisibleActivities |
| 173 | = new ArrayList<ActivityRecord>(); |
| 174 | |
| 175 | /** |
| 176 | * List of activities that are ready to be stopped, but waiting |
| 177 | * for the next activity to settle down before doing so. It contains |
| 178 | * HistoryRecord objects. |
| 179 | */ |
| 180 | final ArrayList<ActivityRecord> mStoppingActivities |
| 181 | = new ArrayList<ActivityRecord>(); |
| 182 | |
| 183 | /** |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 184 | * List of activities that are in the process of going to sleep. |
| 185 | */ |
| 186 | final ArrayList<ActivityRecord> mGoingToSleepActivities |
| 187 | = new ArrayList<ActivityRecord>(); |
| 188 | |
| 189 | /** |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 190 | * Animations that for the current transition have requested not to |
| 191 | * be considered for the transition animation. |
| 192 | */ |
| 193 | final ArrayList<ActivityRecord> mNoAnimActivities |
| 194 | = new ArrayList<ActivityRecord>(); |
| 195 | |
| 196 | /** |
| 197 | * List of activities that are ready to be finished, but waiting |
| 198 | * for the previous activity to settle down before doing so. It contains |
| 199 | * HistoryRecord objects. |
| 200 | */ |
| 201 | final ArrayList<ActivityRecord> mFinishingActivities |
| 202 | = new ArrayList<ActivityRecord>(); |
Craig Mautner | d74f7d7 | 2013-02-26 13:41:02 -0800 | [diff] [blame] | 203 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 204 | /** |
| 205 | * List of people waiting to find out about the next launched activity. |
| 206 | */ |
| 207 | final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched |
| 208 | = new ArrayList<IActivityManager.WaitResult>(); |
Craig Mautner | d74f7d7 | 2013-02-26 13:41:02 -0800 | [diff] [blame] | 209 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 210 | /** |
| 211 | * List of people waiting to find out about the next visible activity. |
| 212 | */ |
| 213 | final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible |
| 214 | = new ArrayList<IActivityManager.WaitResult>(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 215 | |
| 216 | final ArrayList<UserStartedState> mStartingUsers |
| 217 | = new ArrayList<UserStartedState>(); |
| 218 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 219 | /** |
| 220 | * Set when the system is going to sleep, until we have |
| 221 | * successfully paused the current activity and released our wake lock. |
| 222 | * At that point the system is allowed to actually sleep. |
| 223 | */ |
| 224 | final PowerManager.WakeLock mGoingToSleep; |
| 225 | |
| 226 | /** |
| 227 | * We don't want to allow the device to go to sleep while in the process |
| 228 | * of launching an activity. This is primarily to allow alarm intent |
| 229 | * receivers to launch an activity and get that to run before the device |
| 230 | * goes back to sleep. |
| 231 | */ |
| 232 | final PowerManager.WakeLock mLaunchingActivity; |
| 233 | |
| 234 | /** |
| 235 | * When we are in the process of pausing an activity, before starting the |
| 236 | * next one, this variable holds the activity that is currently being paused. |
| 237 | */ |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 238 | ActivityRecord mPausingActivity = null; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 239 | |
| 240 | /** |
| 241 | * This is the last activity that we put into the paused state. This is |
| 242 | * used to determine if we need to do an activity transition while sleeping, |
| 243 | * when we normally hold the top activity paused. |
| 244 | */ |
| 245 | ActivityRecord mLastPausedActivity = null; |
| 246 | |
| 247 | /** |
| 248 | * Current activity that is resumed, or null if there is none. |
| 249 | */ |
| 250 | ActivityRecord mResumedActivity = null; |
Craig Mautner | d74f7d7 | 2013-02-26 13:41:02 -0800 | [diff] [blame] | 251 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 252 | /** |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 253 | * This is the last activity that has been started. It is only used to |
| 254 | * identify when multiple activities are started at once so that the user |
| 255 | * can be warned they may not be in the activity they think they are. |
| 256 | */ |
| 257 | ActivityRecord mLastStartedActivity = null; |
Craig Mautner | d74f7d7 | 2013-02-26 13:41:02 -0800 | [diff] [blame] | 258 | |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 259 | /** |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 260 | * Set when we know we are going to be calling updateConfiguration() |
| 261 | * soon, so want to skip intermediate config checks. |
| 262 | */ |
| 263 | boolean mConfigWillChange; |
| 264 | |
| 265 | /** |
| 266 | * Set to indicate whether to issue an onUserLeaving callback when a |
| 267 | * newly launched activity is being brought in front of us. |
| 268 | */ |
| 269 | boolean mUserLeaving = false; |
Craig Mautner | d74f7d7 | 2013-02-26 13:41:02 -0800 | [diff] [blame] | 270 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 271 | long mInitialStartTime = 0; |
Craig Mautner | d74f7d7 | 2013-02-26 13:41:02 -0800 | [diff] [blame] | 272 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 273 | /** |
| 274 | * Set when we have taken too long waiting to go to sleep. |
| 275 | */ |
| 276 | boolean mSleepTimeout = false; |
| 277 | |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 278 | /** |
Craig Mautner | b12428a | 2012-12-20 16:07:06 -0800 | [diff] [blame] | 279 | * Save the most recent screenshot for reuse. This keeps Recents from taking two identical |
| 280 | * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail. |
| 281 | */ |
| 282 | private ActivityRecord mLastScreenshotActivity = null; |
| 283 | private Bitmap mLastScreenshotBitmap = null; |
| 284 | |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 285 | /** |
| 286 | * List of ActivityRecord objects that have been finished and must |
| 287 | * still report back to a pending thumbnail receiver. |
| 288 | */ |
| 289 | private final ArrayList<ActivityRecord> mCancelledThumbnails = new ArrayList<ActivityRecord>(); |
| 290 | |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 291 | int mThumbnailWidth = -1; |
| 292 | int mThumbnailHeight = -1; |
| 293 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 294 | private int mCurrentUser; |
| 295 | |
Craig Mautner | c00204b | 2013-03-05 15:02:14 -0800 | [diff] [blame] | 296 | final int mStackId; |
| 297 | |
Craig Mautner | 2708430 | 2013-03-25 08:05:25 -0700 | [diff] [blame] | 298 | /** Run all ActivityStacks through this */ |
| 299 | final ActivityStackSupervisor mStackSupervisor; |
| 300 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 301 | static final int SLEEP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG; |
| 302 | static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1; |
| 303 | static final int IDLE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2; |
| 304 | static final int IDLE_NOW_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3; |
| 305 | static final int LAUNCH_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4; |
| 306 | static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5; |
| 307 | static final int RESUME_TOP_ACTIVITY_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6; |
Dianne Hackborn | 29ba7e6 | 2012-03-16 15:03:36 -0700 | [diff] [blame] | 308 | static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 309 | static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 8; |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 310 | static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 9; |
| 311 | |
| 312 | static class ScheduleDestroyArgs { |
| 313 | final ProcessRecord mOwner; |
| 314 | final boolean mOomAdj; |
| 315 | final String mReason; |
| 316 | ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) { |
| 317 | mOwner = owner; |
| 318 | mOomAdj = oomAdj; |
| 319 | mReason = reason; |
| 320 | } |
| 321 | } |
| 322 | |
Zoran Marceta | f958b32 | 2012-08-09 20:27:12 +0900 | [diff] [blame] | 323 | final Handler mHandler; |
| 324 | |
| 325 | final class ActivityStackHandler extends Handler { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 326 | //public Handler() { |
| 327 | // if (localLOGV) Slog.v(TAG, "Handler started!"); |
| 328 | //} |
Zoran Marceta | f958b32 | 2012-08-09 20:27:12 +0900 | [diff] [blame] | 329 | public ActivityStackHandler(Looper looper) { |
| 330 | super(looper); |
| 331 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 332 | |
Zoran Marceta | f958b32 | 2012-08-09 20:27:12 +0900 | [diff] [blame] | 333 | @Override |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 334 | public void handleMessage(Message msg) { |
| 335 | switch (msg.what) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 336 | case SLEEP_TIMEOUT_MSG: { |
Dianne Hackborn | 8e8d65f | 2011-08-11 19:36:18 -0700 | [diff] [blame] | 337 | synchronized (mService) { |
Craig Mautner | e11f2b7 | 2013-04-01 12:37:17 -0700 | [diff] [blame] | 338 | if (mService.isSleepingOrShuttingDown()) { |
Dianne Hackborn | 8e8d65f | 2011-08-11 19:36:18 -0700 | [diff] [blame] | 339 | Slog.w(TAG, "Sleep timeout! Sleeping now."); |
| 340 | mSleepTimeout = true; |
| 341 | checkReadyForSleepLocked(); |
| 342 | } |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 343 | } |
| 344 | } break; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 345 | case PAUSE_TIMEOUT_MSG: { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 346 | ActivityRecord r = (ActivityRecord)msg.obj; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 347 | // We don't at this point know if the activity is fullscreen, |
| 348 | // so we need to be conservative and assume it isn't. |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 349 | Slog.w(TAG, "Activity pause timeout for " + r); |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 350 | synchronized (mService) { |
| 351 | if (r.app != null) { |
| 352 | mService.logAppTooSlow(r.app, r.pauseTime, |
| 353 | "pausing " + r); |
| 354 | } |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 355 | |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 356 | activityPausedLocked(r != null ? r.appToken : null, true); |
| 357 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 358 | } break; |
| 359 | case IDLE_TIMEOUT_MSG: { |
| 360 | if (mService.mDidDexOpt) { |
| 361 | mService.mDidDexOpt = false; |
| 362 | Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG); |
| 363 | nmsg.obj = msg.obj; |
| 364 | mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT); |
| 365 | return; |
| 366 | } |
| 367 | // We don't at this point know if the activity is fullscreen, |
| 368 | // so we need to be conservative and assume it isn't. |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 369 | ActivityRecord r = (ActivityRecord)msg.obj; |
| 370 | Slog.w(TAG, "Activity idle timeout for " + r); |
Craig Mautner | e79d4268 | 2013-04-01 19:01:53 -0700 | [diff] [blame^] | 371 | synchronized (this) { |
| 372 | activityIdleInternalLocked(r != null ? r.appToken : null, true, null); |
| 373 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 374 | } break; |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 375 | case LAUNCH_TICK_MSG: { |
| 376 | ActivityRecord r = (ActivityRecord)msg.obj; |
| 377 | synchronized (mService) { |
| 378 | if (r.continueLaunchTickingLocked()) { |
| 379 | mService.logAppTooSlow(r.app, r.launchTickTime, |
| 380 | "launching " + r); |
| 381 | } |
| 382 | } |
| 383 | } break; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 384 | case DESTROY_TIMEOUT_MSG: { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 385 | ActivityRecord r = (ActivityRecord)msg.obj; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 386 | // We don't at this point know if the activity is fullscreen, |
| 387 | // so we need to be conservative and assume it isn't. |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 388 | Slog.w(TAG, "Activity destroy timeout for " + r); |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 389 | synchronized (mService) { |
| 390 | activityDestroyedLocked(r != null ? r.appToken : null); |
| 391 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 392 | } break; |
| 393 | case IDLE_NOW_MSG: { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 394 | ActivityRecord r = (ActivityRecord)msg.obj; |
Craig Mautner | e79d4268 | 2013-04-01 19:01:53 -0700 | [diff] [blame^] | 395 | synchronized (mService) { |
| 396 | activityIdleInternalLocked(r != null ? r.appToken : null, false, null); |
| 397 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 398 | } break; |
| 399 | case LAUNCH_TIMEOUT_MSG: { |
| 400 | if (mService.mDidDexOpt) { |
| 401 | mService.mDidDexOpt = false; |
| 402 | Message nmsg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG); |
| 403 | mHandler.sendMessageDelayed(nmsg, LAUNCH_TIMEOUT); |
| 404 | return; |
| 405 | } |
| 406 | synchronized (mService) { |
| 407 | if (mLaunchingActivity.isHeld()) { |
| 408 | Slog.w(TAG, "Launch timeout has expired, giving up wake lock!"); |
| 409 | mLaunchingActivity.release(); |
| 410 | } |
| 411 | } |
| 412 | } break; |
| 413 | case RESUME_TOP_ACTIVITY_MSG: { |
| 414 | synchronized (mService) { |
| 415 | resumeTopActivityLocked(null); |
| 416 | } |
| 417 | } break; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 418 | case STOP_TIMEOUT_MSG: { |
| 419 | ActivityRecord r = (ActivityRecord)msg.obj; |
| 420 | // We don't at this point know if the activity is fullscreen, |
| 421 | // so we need to be conservative and assume it isn't. |
| 422 | Slog.w(TAG, "Activity stop timeout for " + r); |
| 423 | synchronized (mService) { |
| 424 | if (r.isInHistory()) { |
| 425 | activityStoppedLocked(r, null, null, null); |
| 426 | } |
| 427 | } |
| 428 | } break; |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 429 | case DESTROY_ACTIVITIES_MSG: { |
| 430 | ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj; |
| 431 | synchronized (mService) { |
| 432 | destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason); |
| 433 | } |
| 434 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 435 | } |
| 436 | } |
Craig Mautner | 4b71aa1 | 2012-12-27 17:20:01 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Craig Mautner | 000f002 | 2013-02-26 15:04:29 -0800 | [diff] [blame] | 439 | private int numActivities() { |
| 440 | int count = 0; |
| 441 | for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { |
| 442 | count += mTaskHistory.get(taskNdx).mActivities.size(); |
| 443 | } |
| 444 | return count; |
| 445 | } |
| 446 | |
Craig Mautner | 2219a1b | 2013-03-25 09:44:30 -0700 | [diff] [blame] | 447 | ActivityStack(ActivityManagerService service, Context context, Looper looper, int stackId, |
Craig Mautner | 2420ead | 2013-04-01 17:13:20 -0700 | [diff] [blame] | 448 | ActivityStackSupervisor supervisor, int userId) { |
Zoran Marceta | f958b32 | 2012-08-09 20:27:12 +0900 | [diff] [blame] | 449 | mHandler = new ActivityStackHandler(looper); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 450 | mService = service; |
| 451 | mContext = context; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 452 | PowerManager pm = |
| 453 | (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
| 454 | mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep"); |
| 455 | mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch"); |
| 456 | mLaunchingActivity.setReferenceCounted(false); |
Craig Mautner | c00204b | 2013-03-05 15:02:14 -0800 | [diff] [blame] | 457 | mStackId = stackId; |
Craig Mautner | 2708430 | 2013-03-25 08:05:25 -0700 | [diff] [blame] | 458 | mStackSupervisor = supervisor; |
Craig Mautner | 2420ead | 2013-04-01 17:13:20 -0700 | [diff] [blame] | 459 | mCurrentUser = userId; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 460 | } |
Craig Mautner | 5962b12 | 2012-10-05 14:45:52 -0700 | [diff] [blame] | 461 | |
| 462 | private boolean okToShow(ActivityRecord r) { |
| 463 | return r.userId == mCurrentUser |
| 464 | || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0; |
| 465 | } |
| 466 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 467 | final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) { |
Craig Mautner | d74f7d7 | 2013-02-26 13:41:02 -0800 | [diff] [blame] | 468 | for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { |
| 469 | final TaskRecord task = mTaskHistory.get(taskNdx); |
Craig Mautner | 11bf9a5 | 2013-02-19 14:08:51 -0800 | [diff] [blame] | 470 | final ArrayList<ActivityRecord> activities = task.mActivities; |
Craig Mautner | d74f7d7 | 2013-02-26 13:41:02 -0800 | [diff] [blame] | 471 | for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) { |
| 472 | ActivityRecord r = activities.get(activityNdx); |
Craig Mautner | 11bf9a5 | 2013-02-19 14:08:51 -0800 | [diff] [blame] | 473 | if (!r.finishing && r != notTop && okToShow(r)) { |
| 474 | return r; |
| 475 | } |
| 476 | } |
| 477 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 478 | return null; |
| 479 | } |
| 480 | |
| 481 | final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) { |
Craig Mautner | d74f7d7 | 2013-02-26 13:41:02 -0800 | [diff] [blame] | 482 | for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { |
| 483 | final TaskRecord task = mTaskHistory.get(taskNdx); |
Craig Mautner | 11bf9a5 | 2013-02-19 14:08:51 -0800 | [diff] [blame] | 484 | final ArrayList<ActivityRecord> activities = task.mActivities; |
Craig Mautner | d74f7d7 | 2013-02-26 13:41:02 -0800 | [diff] [blame] | 485 | for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) { |
| 486 | ActivityRecord r = activities.get(activityNdx); |
Craig Mautner | 11bf9a5 | 2013-02-19 14:08:51 -0800 | [diff] [blame] | 487 | if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) { |
| 488 | return r; |
| 489 | } |
| 490 | } |
| 491 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 492 | return null; |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * This is a simplified version of topRunningActivityLocked that provides a number of |
| 497 | * optional skip-over modes. It is intended for use with the ActivityController hook only. |
Craig Mautner | 9658b31 | 2013-02-28 10:55:59 -0800 | [diff] [blame] | 498 | * |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 499 | * @param token If non-null, any history records matching this token will be skipped. |
| 500 | * @param taskId If non-zero, we'll attempt to skip over records with the same task ID. |
Craig Mautner | 9658b31 | 2013-02-28 10:55:59 -0800 | [diff] [blame] | 501 | * |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 502 | * @return Returns the HistoryRecord of the next activity on the stack. |
| 503 | */ |
| 504 | final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) { |
Craig Mautner | 11bf9a5 | 2013-02-19 14:08:51 -0800 | [diff] [blame] | 505 | for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { |
| 506 | TaskRecord task = mTaskHistory.get(taskNdx); |
| 507 | if (task.taskId == taskId) { |
| 508 | continue; |
| 509 | } |
| 510 | ArrayList<ActivityRecord> activities = task.mActivities; |
| 511 | for (int i = activities.size() - 1; i >= 0; --i) { |
| 512 | final ActivityRecord r = activities.get(i); |
| 513 | // Note: the taskId check depends on real taskId fields being non-zero |
| 514 | if (!r.finishing && (token != r.appToken) && okToShow(r)) { |
| 515 | return r; |
| 516 | } |
| 517 | } |
| 518 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 519 | return null; |
| 520 | } |
| 521 | |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 522 | TaskRecord taskForIdLocked(int id) { |
| 523 | for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { |
| 524 | final TaskRecord task = mTaskHistory.get(taskNdx); |
| 525 | if (task.taskId == id) { |
| 526 | return task; |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 527 | } |
| 528 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 529 | return null; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 532 | ActivityRecord isInStackLocked(IBinder token) { |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 533 | final ActivityRecord r = ActivityRecord.forToken(token); |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 534 | if (r != null) { |
| 535 | final TaskRecord task = r.task; |
| 536 | if (task.mActivities.contains(r) && mTaskHistory.contains(task)) { |
| 537 | if (task.stack != this) Slog.w(TAG, |
| 538 | "Illegal state! task does not point to stack it is in."); |
| 539 | return r; |
| 540 | } |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 541 | } |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 542 | return null; |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 543 | } |
| 544 | |
Craig Mautner | 2420ead | 2013-04-01 17:13:20 -0700 | [diff] [blame] | 545 | final boolean updateLRUListLocked(ActivityRecord r) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 546 | final boolean hadit = mLRUActivities.remove(r); |
| 547 | mLRUActivities.add(r); |
| 548 | return hadit; |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * Returns the top activity in any existing task matching the given |
| 553 | * Intent. Returns null if no such task is found. |
| 554 | */ |
| 555 | private ActivityRecord findTaskLocked(Intent intent, ActivityInfo info) { |
| 556 | ComponentName cls = intent.getComponent(); |
| 557 | if (info.targetActivity != null) { |
| 558 | cls = new ComponentName(info.packageName, info.targetActivity); |
| 559 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 560 | final int userId = UserHandle.getUserId(info.applicationInfo.uid); |
Craig Mautner | 000f002 | 2013-02-26 15:04:29 -0800 | [diff] [blame] | 561 | |
| 562 | for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { |
| 563 | final TaskRecord task = mTaskHistory.get(taskNdx); |
| 564 | final ActivityRecord r = task.getTopActivity(); |
| 565 | if (r == null || r.finishing || r.userId != userId || |
| 566 | r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) { |
| 567 | continue; |
| 568 | } |
| 569 | |
| 570 | //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString() |
| 571 | // + "/aff=" + r.task.affinity + " to new cls=" |
| 572 | // + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity); |
| 573 | if (task.affinity != null) { |
| 574 | if (task.affinity.equals(info.taskAffinity)) { |
| 575 | //Slog.i(TAG, "Found matching affinity!"); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 576 | return r; |
| 577 | } |
Craig Mautner | 000f002 | 2013-02-26 15:04:29 -0800 | [diff] [blame] | 578 | } else if (task.intent != null && task.intent.getComponent().equals(cls)) { |
| 579 | //Slog.i(TAG, "Found matching class!"); |
| 580 | //dump(); |
| 581 | //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent); |
| 582 | return r; |
| 583 | } else if (task.affinityIntent != null |
| 584 | && task.affinityIntent.getComponent().equals(cls)) { |
| 585 | //Slog.i(TAG, "Found matching class!"); |
| 586 | //dump(); |
| 587 | //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent); |
| 588 | return r; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
| 592 | return null; |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * Returns the first activity (starting from the top of the stack) that |
| 597 | * is the same as the given activity. Returns null if no such activity |
| 598 | * is found. |
| 599 | */ |
| 600 | private ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) { |
| 601 | ComponentName cls = intent.getComponent(); |
| 602 | if (info.targetActivity != null) { |
| 603 | cls = new ComponentName(info.packageName, info.targetActivity); |
| 604 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 605 | final int userId = UserHandle.getUserId(info.applicationInfo.uid); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 606 | |
Craig Mautner | 000f002 | 2013-02-26 15:04:29 -0800 | [diff] [blame] | 607 | for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { |
| 608 | final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities; |
| 609 | for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) { |
| 610 | ActivityRecord r = activities.get(activityNdx); |
| 611 | if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 612 | //Slog.i(TAG, "Found matching class!"); |
| 613 | //dump(); |
| 614 | //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent); |
| 615 | return r; |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | return null; |
| 621 | } |
| 622 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 623 | /* |
| 624 | * Move the activities around in the stack to bring a user to the foreground. |
| 625 | * @return whether there are any activities for the specified user. |
| 626 | */ |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 627 | final boolean switchUserLocked(int userId, UserStartedState uss) { |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 628 | if (VALIDATE_TOKENS) { |
| 629 | validateAppTokensLocked(); |
| 630 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 631 | mStartingUsers.add(uss); |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 632 | if (mCurrentUser == userId) { |
| 633 | return true; |
| 634 | } |
| 635 | mCurrentUser = userId; |
| 636 | |
| 637 | // Move userId's tasks to the top. |
| 638 | boolean haveActivities = false; |
| 639 | TaskRecord task = null; |
| 640 | int index = mTaskHistory.size(); |
| 641 | for (int i = 0; i < index; ++i) { |
| 642 | task = mTaskHistory.get(i); |
| 643 | if (task.userId == userId) { |
| 644 | haveActivities = true; |
| 645 | mTaskHistory.remove(i); |
| 646 | mTaskHistory.add(task); |
| 647 | --index; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | // task is now the original topmost TaskRecord. Transition from the old top to the new top. |
| 652 | ActivityRecord top = task != null ? task.getTopActivity() : null; |
Craig Mautner | 000f002 | 2013-02-26 15:04:29 -0800 | [diff] [blame] | 653 | resumeTopActivityLocked(top); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 654 | return haveActivities; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Craig Mautner | 2420ead | 2013-04-01 17:13:20 -0700 | [diff] [blame] | 657 | void minimalResumeActivityLocked(ActivityRecord r) { |
| 658 | r.state = ActivityState.RESUMED; |
| 659 | if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r |
| 660 | + " (starting new instance)"); |
| 661 | r.stopped = false; |
| 662 | mResumedActivity = r; |
| 663 | r.task.touchActiveTime(); |
| 664 | mService.addRecentTaskLocked(r.task); |
| 665 | completeResumeLocked(r); |
| 666 | checkReadyForSleepLocked(); |
| 667 | if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Craig Mautner | e79d4268 | 2013-04-01 19:01:53 -0700 | [diff] [blame^] | 670 | void setLaunchTime(ActivityRecord r) { |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 671 | if (r.launchTime == 0) { |
| 672 | r.launchTime = SystemClock.uptimeMillis(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 673 | if (mInitialStartTime == 0) { |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 674 | mInitialStartTime = r.launchTime; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 675 | } |
| 676 | } else if (mInitialStartTime == 0) { |
| 677 | mInitialStartTime = SystemClock.uptimeMillis(); |
| 678 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 679 | } |
Craig Mautner | aab647e | 2013-02-28 16:31:36 -0800 | [diff] [blame] | 680 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 681 | void stopIfSleepingLocked() { |
Craig Mautner | e11f2b7 | 2013-04-01 12:37:17 -0700 | [diff] [blame] | 682 | if (mService.isSleepingOrShuttingDown()) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 683 | if (!mGoingToSleep.isHeld()) { |
| 684 | mGoingToSleep.acquire(); |
| 685 | if (mLaunchingActivity.isHeld()) { |
| 686 | mLaunchingActivity.release(); |
| 687 | mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG); |
| 688 | } |
| 689 | } |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 690 | mHandler.removeMessages(SLEEP_TIMEOUT_MSG); |
| 691 | Message msg = mHandler.obtainMessage(SLEEP_TIMEOUT_MSG); |
| 692 | mHandler.sendMessageDelayed(msg, SLEEP_TIMEOUT); |
| 693 | checkReadyForSleepLocked(); |
| 694 | } |
| 695 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 696 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 697 | void awakeFromSleepingLocked() { |
| 698 | mHandler.removeMessages(SLEEP_TIMEOUT_MSG); |
| 699 | mSleepTimeout = false; |
| 700 | if (mGoingToSleep.isHeld()) { |
| 701 | mGoingToSleep.release(); |
| 702 | } |
| 703 | // Ensure activities are no longer sleeping. |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 704 | for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { |
| 705 | final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities; |
| 706 | for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) { |
| 707 | activities.get(activityNdx).setSleeping(false); |
| 708 | } |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 709 | } |
| 710 | mGoingToSleepActivities.clear(); |
| 711 | } |
| 712 | |
| 713 | void activitySleptLocked(ActivityRecord r) { |
| 714 | mGoingToSleepActivities.remove(r); |
| 715 | checkReadyForSleepLocked(); |
| 716 | } |
| 717 | |
Craig Mautner | e79d4268 | 2013-04-01 19:01:53 -0700 | [diff] [blame^] | 718 | // Checked. |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 719 | void checkReadyForSleepLocked() { |
Craig Mautner | e11f2b7 | 2013-04-01 12:37:17 -0700 | [diff] [blame] | 720 | if (!mService.isSleepingOrShuttingDown()) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 721 | // Do not care. |
| 722 | return; |
| 723 | } |
| 724 | |
| 725 | if (!mSleepTimeout) { |
| 726 | if (mResumedActivity != null) { |
| 727 | // Still have something resumed; can't sleep until it is paused. |
| 728 | if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 729 | if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false"); |
| 730 | startPausingLocked(false, true); |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 731 | return; |
| 732 | } |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 733 | if (mPausingActivity != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 734 | // Still waiting for something to pause; can't sleep yet. |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 735 | if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity); |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 736 | return; |
| 737 | } |
| 738 | |
| 739 | if (mStoppingActivities.size() > 0) { |
| 740 | // Still need to tell some activities to stop; can't sleep yet. |
| 741 | if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop " |
| 742 | + mStoppingActivities.size() + " activities"); |
Dianne Hackborn | 80a7ac1 | 2011-09-22 18:32:52 -0700 | [diff] [blame] | 743 | scheduleIdleLocked(); |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 744 | return; |
| 745 | } |
| 746 | |
| 747 | ensureActivitiesVisibleLocked(null, 0); |
| 748 | |
| 749 | // Make sure any stopped but visible activities are now sleeping. |
| 750 | // This ensures that the activity's onStop() is called. |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 751 | for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { |
| 752 | final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities; |
| 753 | for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) { |
| 754 | final ActivityRecord r = activities.get(activityNdx); |
| 755 | if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) { |
| 756 | r.setSleeping(true); |
| 757 | } |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | |
| 761 | if (mGoingToSleepActivities.size() > 0) { |
| 762 | // Still need to tell some activities to sleep; can't sleep yet. |
| 763 | if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep " |
| 764 | + mGoingToSleepActivities.size() + " activities"); |
| 765 | return; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 766 | } |
| 767 | } |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 768 | |
| 769 | mHandler.removeMessages(SLEEP_TIMEOUT_MSG); |
| 770 | |
| 771 | if (mGoingToSleep.isHeld()) { |
| 772 | mGoingToSleep.release(); |
| 773 | } |
| 774 | if (mService.mShuttingDown) { |
| 775 | mService.notifyAll(); |
| 776 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 777 | } |
Craig Mautner | 59c0097 | 2012-07-30 12:10:24 -0700 | [diff] [blame] | 778 | |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 779 | public final Bitmap screenshotActivities(ActivityRecord who) { |
Dianne Hackborn | ff801ec | 2011-01-22 18:05:38 -0800 | [diff] [blame] | 780 | if (who.noDisplay) { |
| 781 | return null; |
| 782 | } |
Craig Mautner | aab647e | 2013-02-28 16:31:36 -0800 | [diff] [blame] | 783 | |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 784 | Resources res = mService.mContext.getResources(); |
| 785 | int w = mThumbnailWidth; |
| 786 | int h = mThumbnailHeight; |
| 787 | if (w < 0) { |
| 788 | mThumbnailWidth = w = |
| 789 | res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width); |
| 790 | mThumbnailHeight = h = |
| 791 | res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height); |
| 792 | } |
| 793 | |
| 794 | if (w > 0) { |
Craig Mautner | b12428a | 2012-12-20 16:07:06 -0800 | [diff] [blame] | 795 | if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null |
| 796 | || mLastScreenshotBitmap.getWidth() != w |
| 797 | || mLastScreenshotBitmap.getHeight() != h) { |
| 798 | mLastScreenshotActivity = who; |
| 799 | mLastScreenshotBitmap = mService.mWindowManager.screenshotApplications( |
| 800 | who.appToken, Display.DEFAULT_DISPLAY, w, h); |
| 801 | } |
| 802 | if (mLastScreenshotBitmap != null) { |
| 803 | return mLastScreenshotBitmap.copy(Config.ARGB_8888, true); |
| 804 | } |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 805 | } |
| 806 | return null; |
| 807 | } |
| 808 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 809 | private final void startPausingLocked(boolean userLeaving, boolean uiSleeping) { |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 810 | if (mPausingActivity != null) { |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 811 | Slog.e(TAG, "Trying to pause when pause is already pending for " |
Craig Mautner | e11f2b7 | 2013-04-01 12:37:17 -0700 | [diff] [blame] | 812 | + mPausingActivity, new RuntimeException("here").fillInStackTrace()); |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 813 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 814 | ActivityRecord prev = mResumedActivity; |
| 815 | if (prev == null) { |
Craig Mautner | e11f2b7 | 2013-04-01 12:37:17 -0700 | [diff] [blame] | 816 | Slog.e(TAG, "Trying to pause when nothing is resumed", |
| 817 | new RuntimeException("here").fillInStackTrace()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 818 | resumeTopActivityLocked(null); |
| 819 | return; |
| 820 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 821 | if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev); |
| 822 | else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 823 | mResumedActivity = null; |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 824 | mPausingActivity = prev; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 825 | mLastPausedActivity = prev; |
| 826 | prev.state = ActivityState.PAUSING; |
| 827 | prev.task.touchActiveTime(); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 828 | prev.updateThumbnail(screenshotActivities(prev), null); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 829 | |
| 830 | mService.updateCpuStats(); |
Craig Mautner | aab647e | 2013-02-28 16:31:36 -0800 | [diff] [blame] | 831 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 832 | if (prev.app != null && prev.app.thread != null) { |
| 833 | if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev); |
| 834 | try { |
| 835 | EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 836 | prev.userId, System.identityHashCode(prev), |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 837 | prev.shortComponentName); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 838 | prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing, |
| 839 | userLeaving, prev.configChangeFlags); |
Craig Mautner | 2219a1b | 2013-03-25 09:44:30 -0700 | [diff] [blame] | 840 | if (mStackSupervisor.isMainStack(this)) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 841 | mService.updateUsageStats(prev, false); |
| 842 | } |
| 843 | } catch (Exception e) { |
| 844 | // Ignore exception, if process died other code will cleanup. |
| 845 | Slog.w(TAG, "Exception thrown during pause", e); |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 846 | mPausingActivity = null; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 847 | mLastPausedActivity = null; |
| 848 | } |
| 849 | } else { |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 850 | mPausingActivity = null; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 851 | mLastPausedActivity = null; |
| 852 | } |
| 853 | |
| 854 | // If we are not going to sleep, we want to ensure the device is |
| 855 | // awake until the next activity is started. |
Craig Mautner | e11f2b7 | 2013-04-01 12:37:17 -0700 | [diff] [blame] | 856 | if (!mService.isSleepingOrShuttingDown()) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 857 | mLaunchingActivity.acquire(); |
| 858 | if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) { |
| 859 | // To be safe, don't allow the wake lock to be held for too long. |
| 860 | Message msg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG); |
| 861 | mHandler.sendMessageDelayed(msg, LAUNCH_TIMEOUT); |
| 862 | } |
| 863 | } |
| 864 | |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 865 | if (mPausingActivity != null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 866 | // Have the window manager pause its key dispatching until the new |
| 867 | // activity has started. If we're pausing the activity just because |
| 868 | // the screen is being turned off and the UI is sleeping, don't interrupt |
| 869 | // key dispatch; the same activity will pick it up again on wakeup. |
| 870 | if (!uiSleeping) { |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 871 | prev.pauseKeyDispatchingLocked(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 872 | } else { |
| 873 | if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off"); |
| 874 | } |
| 875 | |
| 876 | // Schedule a pause timeout in case the app doesn't respond. |
| 877 | // We don't give it much time because this directly impacts the |
| 878 | // responsiveness seen by the user. |
| 879 | Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG); |
| 880 | msg.obj = prev; |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 881 | prev.pauseTime = SystemClock.uptimeMillis(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 882 | mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT); |
| 883 | if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete..."); |
| 884 | } else { |
| 885 | // This activity failed to schedule the |
| 886 | // pause, so just treat it as being paused now. |
| 887 | if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next."); |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 888 | resumeTopActivityLocked(null); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 889 | } |
| 890 | } |
Dianne Hackborn | ad9b3211 | 2012-09-17 15:35:01 -0700 | [diff] [blame] | 891 | |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 892 | final void activityResumedLocked(IBinder token) { |
| 893 | final ActivityRecord r = ActivityRecord.forToken(token); |
| 894 | if (DEBUG_SAVED_STATE) Slog.i(TAG, "Resumed activity; dropping state of: " + r); |
| 895 | r.icicle = null; |
| 896 | r.haveState = false; |
Dianne Hackborn | ad9b3211 | 2012-09-17 15:35:01 -0700 | [diff] [blame] | 897 | } |
| 898 | |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 899 | final void activityPausedLocked(IBinder token, boolean timeout) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 900 | if (DEBUG_PAUSE) Slog.v( |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 901 | TAG, "Activity paused: token=" + token + ", timeout=" + timeout); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 902 | |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 903 | final ActivityRecord r = isInStackLocked(token); |
| 904 | if (r != null) { |
| 905 | mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r); |
| 906 | if (mPausingActivity == r) { |
| 907 | if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r |
| 908 | + (timeout ? " (due to timeout)" : " (pause complete)")); |
| 909 | r.state = ActivityState.PAUSED; |
| 910 | completePauseLocked(); |
| 911 | } else { |
| 912 | EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE, |
| 913 | r.userId, System.identityHashCode(r), r.shortComponentName, |
| 914 | mPausingActivity != null |
| 915 | ? mPausingActivity.shortComponentName : "(none)"); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 916 | } |
| 917 | } |
| 918 | } |
| 919 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 920 | final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail, |
| 921 | CharSequence description) { |
Dianne Hackborn | b61a026 | 2012-05-14 17:19:18 -0700 | [diff] [blame] | 922 | if (r.state != ActivityState.STOPPING) { |
| 923 | Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r); |
| 924 | mHandler.removeMessages(STOP_TIMEOUT_MSG, r); |
| 925 | return; |
| 926 | } |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 927 | if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle); |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 928 | if (icicle != null) { |
| 929 | // If icicle is null, this is happening due to a timeout, so we |
| 930 | // haven't really saved the state. |
| 931 | r.icicle = icicle; |
| 932 | r.haveState = true; |
Dianne Hackborn | 0798149 | 2013-01-28 11:36:23 -0800 | [diff] [blame] | 933 | r.launchCount = 0; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 934 | r.updateThumbnail(thumbnail, description); |
| 935 | } |
| 936 | if (!r.stopped) { |
| 937 | if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)"); |
| 938 | mHandler.removeMessages(STOP_TIMEOUT_MSG, r); |
| 939 | r.stopped = true; |
| 940 | r.state = ActivityState.STOPPED; |
Dianne Hackborn | 6e3d6da | 2012-06-15 12:05:27 -0700 | [diff] [blame] | 941 | if (r.finishing) { |
| 942 | r.clearOptionsLocked(); |
| 943 | } else { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 944 | if (r.configDestroy) { |
| 945 | destroyActivityLocked(r, true, false, "stop-config"); |
| 946 | resumeTopActivityLocked(null); |
| 947 | } else { |
| 948 | // Now that this process has stopped, we may want to consider |
| 949 | // it to be the previous app to try to keep around in case |
| 950 | // the user wants to return to it. |
| 951 | ProcessRecord fgApp = null; |
| 952 | if (mResumedActivity != null) { |
| 953 | fgApp = mResumedActivity.app; |
| 954 | } else if (mPausingActivity != null) { |
| 955 | fgApp = mPausingActivity.app; |
| 956 | } |
| 957 | if (r.app != null && fgApp != null && r.app != fgApp |
| 958 | && r.lastVisibleTime > mService.mPreviousProcessVisibleTime |
| 959 | && r.app != mService.mHomeProcess) { |
| 960 | mService.mPreviousProcess = r.app; |
| 961 | mService.mPreviousProcessVisibleTime = r.lastVisibleTime; |
| 962 | } |
Dianne Hackborn | 5068560 | 2011-12-01 12:23:37 -0800 | [diff] [blame] | 963 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 964 | } |
| 965 | } |
| 966 | } |
| 967 | |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 968 | private final void completePauseLocked() { |
| 969 | ActivityRecord prev = mPausingActivity; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 970 | if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev); |
Craig Mautner | aab647e | 2013-02-28 16:31:36 -0800 | [diff] [blame] | 971 | |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 972 | if (prev != null) { |
| 973 | if (prev.finishing) { |
| 974 | if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev); |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 975 | prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false); |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 976 | } else if (prev.app != null) { |
| 977 | if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev); |
| 978 | if (prev.waitingVisible) { |
| 979 | prev.waitingVisible = false; |
| 980 | mWaitingVisibleActivities.remove(prev); |
| 981 | if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v( |
| 982 | TAG, "Complete pause, no longer waiting: " + prev); |
Dianne Hackborn | cbb722e | 2012-02-07 18:33:49 -0800 | [diff] [blame] | 983 | } |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 984 | if (prev.configDestroy) { |
| 985 | // The previous is being paused because the configuration |
| 986 | // is changing, which means it is actually stopping... |
| 987 | // To juggle the fact that we are also starting a new |
| 988 | // instance right now, we need to first completely stop |
| 989 | // the current instance before starting the new one. |
| 990 | if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev); |
| 991 | destroyActivityLocked(prev, true, false, "pause-config"); |
| 992 | } else { |
| 993 | mStoppingActivities.add(prev); |
| 994 | if (mStoppingActivities.size() > 3) { |
| 995 | // If we already have a few activities waiting to stop, |
| 996 | // then give up on things going idle and start clearing |
| 997 | // them out. |
| 998 | if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle"); |
| 999 | scheduleIdleLocked(); |
| 1000 | } else { |
| 1001 | checkReadyForSleepLocked(); |
| 1002 | } |
| 1003 | } |
| 1004 | } else { |
| 1005 | if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev); |
| 1006 | prev = null; |
Dianne Hackborn | cbb722e | 2012-02-07 18:33:49 -0800 | [diff] [blame] | 1007 | } |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 1008 | mPausingActivity = null; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1009 | } |
Dianne Hackborn | cbb722e | 2012-02-07 18:33:49 -0800 | [diff] [blame] | 1010 | |
Craig Mautner | e11f2b7 | 2013-04-01 12:37:17 -0700 | [diff] [blame] | 1011 | if (!mService.isSleepingOrShuttingDown()) { |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 1012 | resumeTopActivityLocked(prev); |
| 1013 | } else { |
Dianne Hackborn | cbb722e | 2012-02-07 18:33:49 -0800 | [diff] [blame] | 1014 | checkReadyForSleepLocked(); |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 1015 | ActivityRecord top = topRunningActivityLocked(null); |
| 1016 | if (top == null || (prev != null && top != prev)) { |
| 1017 | // If there are no more activities available to run, |
| 1018 | // do resume anyway to start something. Also if the top |
| 1019 | // activity on the stack is not the just paused activity, |
| 1020 | // we need to go ahead and resume it to ensure we complete |
| 1021 | // an in-flight app switch. |
Dianne Hackborn | 42e620c | 2012-06-24 13:20:51 -0700 | [diff] [blame] | 1022 | resumeTopActivityLocked(null); |
| 1023 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1024 | } |
Craig Mautner | aab647e | 2013-02-28 16:31:36 -0800 | [diff] [blame] | 1025 | |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 1026 | if (prev != null) { |
| 1027 | prev.resumeKeyDispatchingLocked(); |
| 1028 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1029 | |
| 1030 | if (prev.app != null && prev.cpuTimeAtResume > 0 |
| 1031 | && mService.mBatteryStatsService.isOnBattery()) { |
| 1032 | long diff = 0; |
| 1033 | synchronized (mService.mProcessStatsThread) { |
| 1034 | diff = mService.mProcessStats.getCpuTimeForPid(prev.app.pid) |
| 1035 | - prev.cpuTimeAtResume; |
| 1036 | } |
| 1037 | if (diff > 0) { |
| 1038 | BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics(); |
| 1039 | synchronized (bsi) { |
| 1040 | BatteryStatsImpl.Uid.Proc ps = |
| 1041 | bsi.getProcessStatsLocked(prev.info.applicationInfo.uid, |
| 1042 | prev.info.packageName); |
| 1043 | if (ps != null) { |
| 1044 | ps.addForegroundTimeLocked(diff); |
| 1045 | } |
| 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | prev.cpuTimeAtResume = 0; // reset it |
| 1050 | } |
| 1051 | |
Craig Mautner | e79d4268 | 2013-04-01 19:01:53 -0700 | [diff] [blame^] | 1052 | // Checked. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1053 | /** |
| 1054 | * Once we know that we have asked an application to put an activity in |
| 1055 | * the resumed state (either by launching it or explicitly telling it), |
| 1056 | * this function updates the rest of our state to match that fact. |
| 1057 | */ |
| 1058 | private final void completeResumeLocked(ActivityRecord next) { |
| 1059 | next.idle = false; |
| 1060 | next.results = null; |
| 1061 | next.newIntents = null; |
| 1062 | |
| 1063 | // schedule an idle timeout in case the app doesn't do it for us. |
| 1064 | Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG); |
| 1065 | msg.obj = next; |
| 1066 | mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT); |
| 1067 | |
| 1068 | if (false) { |
| 1069 | // The activity was never told to pause, so just keep |
| 1070 | // things going as-is. To maintain our own state, |
| 1071 | // we need to emulate it coming back and saying it is |
| 1072 | // idle. |
| 1073 | msg = mHandler.obtainMessage(IDLE_NOW_MSG); |
| 1074 | msg.obj = next; |
| 1075 | mHandler.sendMessage(msg); |
| 1076 | } |
| 1077 | |
Craig Mautner | 2219a1b | 2013-03-25 09:44:30 -0700 | [diff] [blame] | 1078 | if (mStackSupervisor.isMainStack(this)) { |
Craig Mautner | e79d4268 | 2013-04-01 19:01:53 -0700 | [diff] [blame^] | 1079 | // Should this be done for all stacks, not just mMainStack? |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1080 | mService.reportResumedActivityLocked(next); |
| 1081 | } |
Dianne Hackborn | 15491c6 | 2012-09-19 10:59:14 -0700 | [diff] [blame] | 1082 | |
Craig Mautner | 2219a1b | 2013-03-25 09:44:30 -0700 | [diff] [blame] | 1083 | if (mStackSupervisor.isMainStack(this)) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1084 | mService.setFocusedActivityLocked(next); |
| 1085 | } |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 1086 | next.resumeKeyDispatchingLocked(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1087 | ensureActivitiesVisibleLocked(null, 0); |
| 1088 | mService.mWindowManager.executeAppTransition(); |
| 1089 | mNoAnimActivities.clear(); |
| 1090 | |
| 1091 | // Mark the point when the activity is resuming |
| 1092 | // TODO: To be more accurate, the mark should be before the onCreate, |
| 1093 | // not after the onResume. But for subsequent starts, onResume is fine. |
| 1094 | if (next.app != null) { |
| 1095 | synchronized (mService.mProcessStatsThread) { |
| 1096 | next.cpuTimeAtResume = mService.mProcessStats.getCpuTimeForPid(next.app.pid); |
| 1097 | } |
| 1098 | } else { |
| 1099 | next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process |
| 1100 | } |
| 1101 | } |
| 1102 | |
Craig Mautner | e79d4268 | 2013-04-01 19:01:53 -0700 | [diff] [blame^] | 1103 | // Checked. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1104 | /** |
| 1105 | * Make sure that all activities that need to be visible (that is, they |
| 1106 | * currently can be seen by the user) actually are. |
| 1107 | */ |
| 1108 | final void ensureActivitiesVisibleLocked(ActivityRecord top, |
| 1109 | ActivityRecord starting, String onlyThisProcess, int configChanges) { |
| 1110 | if (DEBUG_VISBILITY) Slog.v( |
| 1111 | TAG, "ensureActivitiesVisible behind " + top |
| 1112 | + " configChanges=0x" + Integer.toHexString(configChanges)); |
| 1113 | |
| 1114 | // If the top activity is not fullscreen, then we need to |
| 1115 | // make sure any activities under it are now visible. |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 1116 | boolean aboveTop = true; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1117 | boolean behindFullscreen = false; |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 1118 | for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { |
| 1119 | final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities; |
| 1120 | for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) { |
| 1121 | final ActivityRecord r = activities.get(activityNdx); |
| 1122 | if (r.finishing) { |
| 1123 | continue; |
| 1124 | } |
| 1125 | if (aboveTop && r != top) { |
| 1126 | continue; |
| 1127 | } |
| 1128 | aboveTop = false; |
| 1129 | if (!behindFullscreen) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1130 | if (DEBUG_VISBILITY) Slog.v( |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 1131 | TAG, "Make visible? " + r + " finishing=" + r.finishing |
| 1132 | + " state=" + r.state); |
Craig Mautner | 5854780 | 2013-03-05 08:23:53 -0800 | [diff] [blame] | 1133 | |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 1134 | final boolean doThisProcess = onlyThisProcess == null |
| 1135 | || onlyThisProcess.equals(r.processName); |
| 1136 | |
| 1137 | // First: if this is not the current activity being started, make |
| 1138 | // sure it matches the current configuration. |
| 1139 | if (r != starting && doThisProcess) { |
| 1140 | ensureActivityConfigurationLocked(r, 0); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1141 | } |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 1142 | |
| 1143 | if (r.app == null || r.app.thread == null) { |
| 1144 | if (onlyThisProcess == null |
| 1145 | || onlyThisProcess.equals(r.processName)) { |
| 1146 | // This activity needs to be visible, but isn't even |
| 1147 | // running... get it started, but don't resume it |
| 1148 | // at this point. |
| 1149 | if (DEBUG_VISBILITY) Slog.v( |
| 1150 | TAG, "Start and freeze screen for " + r); |
| 1151 | if (r != starting) { |
| 1152 | r.startFreezingScreenLocked(r.app, configChanges); |
| 1153 | } |
| 1154 | if (!r.visible) { |
| 1155 | if (DEBUG_VISBILITY) Slog.v( |
| 1156 | TAG, "Starting and making visible: " + r); |
| 1157 | mService.mWindowManager.setAppVisibility(r.appToken, true); |
| 1158 | } |
| 1159 | if (r != starting) { |
Craig Mautner | e79d4268 | 2013-04-01 19:01:53 -0700 | [diff] [blame^] | 1160 | mStackSupervisor.startSpecificActivityLocked(r, false, false); |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | } else if (r.visible) { |
| 1165 | // If this activity is already visible, then there is nothing |
| 1166 | // else to do here. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1167 | if (DEBUG_VISBILITY) Slog.v( |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 1168 | TAG, "Skipping: already visible at " + r); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1169 | r.stopFreezingScreenLocked(false); |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 1170 | |
| 1171 | } else if (onlyThisProcess == null) { |
| 1172 | // This activity is not currently visible, but is running. |
| 1173 | // Tell it to become visible. |
| 1174 | r.visible = true; |
| 1175 | if (r.state != ActivityState.RESUMED && r != starting) { |
| 1176 | // If this activity is paused, tell it |
| 1177 | // to now show its window. |
| 1178 | if (DEBUG_VISBILITY) Slog.v( |
| 1179 | TAG, "Making visible and scheduling visibility: " + r); |
| 1180 | try { |
| 1181 | mService.mWindowManager.setAppVisibility(r.appToken, true); |
| 1182 | r.sleeping = false; |
| 1183 | r.app.pendingUiClean = true; |
| 1184 | r.app.thread.scheduleWindowVisibility(r.appToken, true); |
| 1185 | r.stopFreezingScreenLocked(false); |
| 1186 | } catch (Exception e) { |
| 1187 | // Just skip on any failure; we'll make it |
| 1188 | // visible when it next restarts. |
| 1189 | Slog.w(TAG, "Exception thrown making visibile: " |
| 1190 | + r.intent.getComponent(), e); |
| 1191 | } |
| 1192 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1193 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1194 | |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 1195 | // Aggregate current change flags. |
| 1196 | configChanges |= r.configChangeFlags; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1197 | |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 1198 | if (r.fullscreen) { |
| 1199 | // At this point, nothing else needs to be shown |
| 1200 | if (DEBUG_VISBILITY) Slog.v( |
| 1201 | TAG, "Stopping: fullscreen at " + r); |
| 1202 | behindFullscreen = true; |
| 1203 | } |
| 1204 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1205 | if (r.visible) { |
| 1206 | if (DEBUG_VISBILITY) Slog.v( |
| 1207 | TAG, "Making invisible: " + r); |
| 1208 | r.visible = false; |
| 1209 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 1210 | mService.mWindowManager.setAppVisibility(r.appToken, false); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1211 | if ((r.state == ActivityState.STOPPING |
| 1212 | || r.state == ActivityState.STOPPED) |
| 1213 | && r.app != null && r.app.thread != null) { |
| 1214 | if (DEBUG_VISBILITY) Slog.v( |
| 1215 | TAG, "Scheduling invisibility: " + r); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 1216 | r.app.thread.scheduleWindowVisibility(r.appToken, false); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1217 | } |
| 1218 | } catch (Exception e) { |
| 1219 | // Just skip on any failure; we'll make it |
| 1220 | // visible when it next restarts. |
| 1221 | Slog.w(TAG, "Exception thrown making hidden: " |
| 1222 | + r.intent.getComponent(), e); |
| 1223 | } |
| 1224 | } else { |
| 1225 | if (DEBUG_VISBILITY) Slog.v( |
| 1226 | TAG, "Already invisible: " + r); |
| 1227 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1228 | } |
| 1229 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1230 | } |
| 1231 | } |
| 1232 | |
Craig Mautner | e79d4268 | 2013-04-01 19:01:53 -0700 | [diff] [blame^] | 1233 | // Checked. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1234 | /** |
| 1235 | * Version of ensureActivitiesVisible that can easily be called anywhere. |
| 1236 | */ |
| 1237 | final void ensureActivitiesVisibleLocked(ActivityRecord starting, |
| 1238 | int configChanges) { |
| 1239 | ActivityRecord r = topRunningActivityLocked(null); |
| 1240 | if (r != null) { |
| 1241 | ensureActivitiesVisibleLocked(r, starting, null, configChanges); |
| 1242 | } |
| 1243 | } |
Craig Mautner | 5854780 | 2013-03-05 08:23:53 -0800 | [diff] [blame] | 1244 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1245 | /** |
| 1246 | * Ensure that the top activity in the stack is resumed. |
| 1247 | * |
| 1248 | * @param prev The previously resumed activity, for when in the process |
| 1249 | * of pausing; can be null to call from elsewhere. |
| 1250 | * |
| 1251 | * @return Returns true if something is being resumed, or false if |
| 1252 | * nothing happened. |
| 1253 | */ |
| 1254 | final boolean resumeTopActivityLocked(ActivityRecord prev) { |
Dianne Hackborn | 8437587 | 2012-06-01 19:03:50 -0700 | [diff] [blame] | 1255 | return resumeTopActivityLocked(prev, null); |
| 1256 | } |
| 1257 | |
| 1258 | final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1259 | // Find the first activity that is not finishing. |
| 1260 | ActivityRecord next = topRunningActivityLocked(null); |
| 1261 | |
| 1262 | // Remember how we'll process this pause/resume situation, and ensure |
| 1263 | // that the state is reset however we wind up proceeding. |
| 1264 | final boolean userLeaving = mUserLeaving; |
| 1265 | mUserLeaving = false; |
| 1266 | |
| 1267 | if (next == null) { |
| 1268 | // There are no more activities! Let's just start up the |
| 1269 | // Launcher... |
Craig Mautner | 2219a1b | 2013-03-25 09:44:30 -0700 | [diff] [blame] | 1270 | if (mStackSupervisor.isMainStack(this)) { |
Dianne Hackborn | 8437587 | 2012-06-01 19:03:50 -0700 | [diff] [blame] | 1271 | ActivityOptions.abort(options); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1272 | return mService.startHomeActivityLocked(mCurrentUser); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | next.delayedResume = false; |
Craig Mautner | 5854780 | 2013-03-05 08:23:53 -0800 | [diff] [blame] | 1277 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1278 | // If the top activity is the resumed one, nothing to do. |
| 1279 | if (mResumedActivity == next && next.state == ActivityState.RESUMED) { |
| 1280 | // Make sure we have executed any pending transitions, since there |
| 1281 | // should be nothing left to do at this point. |
| 1282 | mService.mWindowManager.executeAppTransition(); |
| 1283 | mNoAnimActivities.clear(); |
Dianne Hackborn | 8437587 | 2012-06-01 19:03:50 -0700 | [diff] [blame] | 1284 | ActivityOptions.abort(options); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1285 | return false; |
| 1286 | } |
| 1287 | |
| 1288 | // If we are sleeping, and there is no resumed activity, and the top |
| 1289 | // activity is paused, well that is the state we want. |
| 1290 | if ((mService.mSleeping || mService.mShuttingDown) |
p13451 | dbad287 | 2012-04-18 11:39:23 +0900 | [diff] [blame] | 1291 | && mLastPausedActivity == next |
| 1292 | && (next.state == ActivityState.PAUSED |
| 1293 | || next.state == ActivityState.STOPPED |
| 1294 | || next.state == ActivityState.STOPPING)) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1295 | // Make sure we have executed any pending transitions, since there |
| 1296 | // should be nothing left to do at this point. |
| 1297 | mService.mWindowManager.executeAppTransition(); |
| 1298 | mNoAnimActivities.clear(); |
Dianne Hackborn | 8437587 | 2012-06-01 19:03:50 -0700 | [diff] [blame] | 1299 | ActivityOptions.abort(options); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1300 | return false; |
| 1301 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1302 | |
| 1303 | // Make sure that the user who owns this activity is started. If not, |
| 1304 | // we will just leave it as is because someone should be bringing |
| 1305 | // another user's activities to the top of the stack. |
| 1306 | if (mService.mStartedUsers.get(next.userId) == null) { |
| 1307 | Slog.w(TAG, "Skipping resume of top activity " + next |
| 1308 | + ": user " + next.userId + " is stopped"); |
| 1309 | return false; |
| 1310 | } |
| 1311 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1312 | // The activity may be waiting for stop, but that is no longer |
| 1313 | // appropriate for it. |
| 1314 | mStoppingActivities.remove(next); |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 1315 | mGoingToSleepActivities.remove(next); |
| 1316 | next.sleeping = false; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1317 | mWaitingVisibleActivities.remove(next); |
| 1318 | |
Dianne Hackborn | 8437587 | 2012-06-01 19:03:50 -0700 | [diff] [blame] | 1319 | next.updateOptionsLocked(options); |
| 1320 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1321 | if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next); |
| 1322 | |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 1323 | // If we are currently pausing an activity, then don't do anything |
| 1324 | // until that is done. |
| 1325 | if (mPausingActivity != null) { |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 1326 | if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG, |
| 1327 | "Skip resume: pausing=" + mPausingActivity); |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 1328 | return false; |
| 1329 | } |
| 1330 | |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 1331 | // Okay we are now going to start a switch, to 'next'. We may first |
| 1332 | // have to pause the current activity, but this is an important point |
| 1333 | // where we have decided to go to 'next' so keep track of that. |
Dianne Hackborn | 034093a4 | 2010-09-20 22:24:38 -0700 | [diff] [blame] | 1334 | // XXX "App Redirected" dialog is getting too many false positives |
| 1335 | // at this point, so turn off for now. |
| 1336 | if (false) { |
| 1337 | if (mLastStartedActivity != null && !mLastStartedActivity.finishing) { |
| 1338 | long now = SystemClock.uptimeMillis(); |
| 1339 | final boolean inTime = mLastStartedActivity.startTime != 0 |
| 1340 | && (mLastStartedActivity.startTime + START_WARN_TIME) >= now; |
| 1341 | final int lastUid = mLastStartedActivity.info.applicationInfo.uid; |
| 1342 | final int nextUid = next.info.applicationInfo.uid; |
| 1343 | if (inTime && lastUid != nextUid |
| 1344 | && lastUid != next.launchedFromUid |
| 1345 | && mService.checkPermission( |
| 1346 | android.Manifest.permission.STOP_APP_SWITCHES, |
| 1347 | -1, next.launchedFromUid) |
| 1348 | != PackageManager.PERMISSION_GRANTED) { |
| 1349 | mService.showLaunchWarningLocked(mLastStartedActivity, next); |
| 1350 | } else { |
| 1351 | next.startTime = now; |
| 1352 | mLastStartedActivity = next; |
| 1353 | } |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 1354 | } else { |
Dianne Hackborn | 034093a4 | 2010-09-20 22:24:38 -0700 | [diff] [blame] | 1355 | next.startTime = SystemClock.uptimeMillis(); |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 1356 | mLastStartedActivity = next; |
| 1357 | } |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 1358 | } |
Craig Mautner | 5854780 | 2013-03-05 08:23:53 -0800 | [diff] [blame] | 1359 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1360 | // We need to start pausing the current activity so the top one |
| 1361 | // can be resumed... |
| 1362 | if (mResumedActivity != null) { |
| 1363 | if (DEBUG_SWITCH) Slog.v(TAG, "Skip resume: need to start pausing"); |
Dianne Hackborn | ad9b3211 | 2012-09-17 15:35:01 -0700 | [diff] [blame] | 1364 | // At this point we want to put the upcoming activity's process |
| 1365 | // at the top of the LRU list, since we know we will be needing it |
| 1366 | // very soon and it would be a waste to let it get killed if it |
| 1367 | // happens to be sitting towards the end. |
| 1368 | if (next.app != null && next.app.thread != null) { |
| 1369 | // No reason to do full oom adj update here; we'll let that |
| 1370 | // happen whenever it needs to later. |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1371 | mService.updateLruProcessLocked(next.app, false); |
Dianne Hackborn | ad9b3211 | 2012-09-17 15:35:01 -0700 | [diff] [blame] | 1372 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1373 | startPausingLocked(userLeaving, false); |
| 1374 | return true; |
| 1375 | } |
| 1376 | |
Christopher Tate | d3f175c | 2012-06-14 14:16:54 -0700 | [diff] [blame] | 1377 | // If the most recent activity was noHistory but was only stopped rather |
| 1378 | // than stopped+finished because the device went to sleep, we need to make |
| 1379 | // sure to finish it as we're making a new activity topmost. |
| 1380 | final ActivityRecord last = mLastPausedActivity; |
| 1381 | if (mService.mSleeping && last != null && !last.finishing) { |
| 1382 | if ((last.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0 |
| 1383 | || (last.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) { |
| 1384 | if (DEBUG_STATES) { |
| 1385 | Slog.d(TAG, "no-history finish of " + last + " on new resume"); |
| 1386 | } |
| 1387 | requestFinishActivityLocked(last.appToken, Activity.RESULT_CANCELED, null, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 1388 | "no-history", false); |
Christopher Tate | d3f175c | 2012-06-14 14:16:54 -0700 | [diff] [blame] | 1389 | } |
| 1390 | } |
| 1391 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1392 | if (prev != null && prev != next) { |
| 1393 | if (!prev.waitingVisible && next != null && !next.nowVisible) { |
| 1394 | prev.waitingVisible = true; |
| 1395 | mWaitingVisibleActivities.add(prev); |
| 1396 | if (DEBUG_SWITCH) Slog.v( |
| 1397 | TAG, "Resuming top, waiting visible to hide: " + prev); |
| 1398 | } else { |
| 1399 | // The next activity is already visible, so hide the previous |
| 1400 | // activity's windows right now so we can show the new one ASAP. |
| 1401 | // We only do this if the previous is finishing, which should mean |
| 1402 | // it is on top of the one being resumed so hiding it quickly |
| 1403 | // is good. Otherwise, we want to do the normal route of allowing |
| 1404 | // the resumed activity to be shown so we can decide if the |
| 1405 | // previous should actually be hidden depending on whether the |
| 1406 | // new one is found to be full-screen or not. |
| 1407 | if (prev.finishing) { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 1408 | mService.mWindowManager.setAppVisibility(prev.appToken, false); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1409 | if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: " |
| 1410 | + prev + ", waitingVisible=" |
| 1411 | + (prev != null ? prev.waitingVisible : null) |
| 1412 | + ", nowVisible=" + next.nowVisible); |
| 1413 | } else { |
| 1414 | if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: " |
| 1415 | + prev + ", waitingVisible=" |
| 1416 | + (prev != null ? prev.waitingVisible : null) |
| 1417 | + ", nowVisible=" + next.nowVisible); |
| 1418 | } |
| 1419 | } |
| 1420 | } |
| 1421 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 1422 | // Launching this app's activity, make sure the app is no longer |
| 1423 | // considered stopped. |
| 1424 | try { |
| 1425 | AppGlobals.getPackageManager().setPackageStoppedState( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1426 | next.packageName, false, next.userId); /* TODO: Verify if correct userid */ |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 1427 | } catch (RemoteException e1) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 1428 | } catch (IllegalArgumentException e) { |
| 1429 | Slog.w(TAG, "Failed trying to unstop package " |
| 1430 | + next.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 1431 | } |
| 1432 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1433 | // We are starting up the next activity, so tell the window manager |
| 1434 | // that the previous one will be hidden soon. This way it can know |
| 1435 | // to ignore it when computing the desired screen orientation. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 1436 | boolean noAnim = false; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1437 | if (prev != null) { |
| 1438 | if (prev.finishing) { |
| 1439 | if (DEBUG_TRANSITION) Slog.v(TAG, |
| 1440 | "Prepare close transition: prev=" + prev); |
| 1441 | if (mNoAnimActivities.contains(prev)) { |
Dianne Hackborn | 7da6ac3 | 2010-12-09 19:22:04 -0800 | [diff] [blame] | 1442 | mService.mWindowManager.prepareAppTransition( |
Craig Mautner | 4b71aa1 | 2012-12-27 17:20:01 -0800 | [diff] [blame] | 1443 | AppTransition.TRANSIT_NONE, false); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1444 | } else { |
| 1445 | mService.mWindowManager.prepareAppTransition(prev.task == next.task |
Craig Mautner | 4b71aa1 | 2012-12-27 17:20:01 -0800 | [diff] [blame] | 1446 | ? AppTransition.TRANSIT_ACTIVITY_CLOSE |
| 1447 | : AppTransition.TRANSIT_TASK_CLOSE, false); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1448 | } |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 1449 | mService.mWindowManager.setAppWillBeHidden(prev.appToken); |
| 1450 | mService.mWindowManager.setAppVisibility(prev.appToken, false); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1451 | } else { |
| 1452 | if (DEBUG_TRANSITION) Slog.v(TAG, |
| 1453 | "Prepare open transition: prev=" + prev); |
| 1454 | if (mNoAnimActivities.contains(next)) { |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 1455 | noAnim = true; |
Dianne Hackborn | 7da6ac3 | 2010-12-09 19:22:04 -0800 | [diff] [blame] | 1456 | mService.mWindowManager.prepareAppTransition( |
Craig Mautner | 4b71aa1 | 2012-12-27 17:20:01 -0800 | [diff] [blame] | 1457 | AppTransition.TRANSIT_NONE, false); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1458 | } else { |
| 1459 | mService.mWindowManager.prepareAppTransition(prev.task == next.task |
Craig Mautner | 4b71aa1 | 2012-12-27 17:20:01 -0800 | [diff] [blame] | 1460 | ? AppTransition.TRANSIT_ACTIVITY_OPEN |
| 1461 | : AppTransition.TRANSIT_TASK_OPEN, false); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1462 | } |
| 1463 | } |
| 1464 | if (false) { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 1465 | mService.mWindowManager.setAppWillBeHidden(prev.appToken); |
| 1466 | mService.mWindowManager.setAppVisibility(prev.appToken, false); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1467 | } |
Craig Mautner | 000f002 | 2013-02-26 15:04:29 -0800 | [diff] [blame] | 1468 | } else if (numActivities() > 1) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1469 | if (DEBUG_TRANSITION) Slog.v(TAG, |
| 1470 | "Prepare open transition: no previous"); |
| 1471 | if (mNoAnimActivities.contains(next)) { |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 1472 | noAnim = true; |
Dianne Hackborn | 7da6ac3 | 2010-12-09 19:22:04 -0800 | [diff] [blame] | 1473 | mService.mWindowManager.prepareAppTransition( |
Craig Mautner | 4b71aa1 | 2012-12-27 17:20:01 -0800 | [diff] [blame] | 1474 | AppTransition.TRANSIT_NONE, false); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1475 | } else { |
Dianne Hackborn | 7da6ac3 | 2010-12-09 19:22:04 -0800 | [diff] [blame] | 1476 | mService.mWindowManager.prepareAppTransition( |
Craig Mautner | 4b71aa1 | 2012-12-27 17:20:01 -0800 | [diff] [blame] | 1477 | AppTransition.TRANSIT_ACTIVITY_OPEN, false); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1478 | } |
| 1479 | } |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 1480 | if (!noAnim) { |
| 1481 | next.applyOptionsLocked(); |
| 1482 | } else { |
| 1483 | next.clearOptionsLocked(); |
| 1484 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1485 | |
| 1486 | if (next.app != null && next.app.thread != null) { |
| 1487 | if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next); |
| 1488 | |
| 1489 | // This activity is now becoming visible. |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 1490 | mService.mWindowManager.setAppVisibility(next.appToken, true); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1491 | |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 1492 | // schedule launch ticks to collect information about slow apps. |
| 1493 | next.startLaunchTickingLocked(); |
| 1494 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1495 | ActivityRecord lastResumedActivity = mResumedActivity; |
| 1496 | ActivityState lastState = next.state; |
| 1497 | |
| 1498 | mService.updateCpuStats(); |
Craig Mautner | 5854780 | 2013-03-05 08:23:53 -0800 | [diff] [blame] | 1499 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 1500 | if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)"); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1501 | next.state = ActivityState.RESUMED; |
| 1502 | mResumedActivity = next; |
| 1503 | next.task.touchActiveTime(); |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 1504 | mService.addRecentTaskLocked(next.task); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1505 | mService.updateLruProcessLocked(next.app, true); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1506 | updateLRUListLocked(next); |
| 1507 | |
| 1508 | // Have the window manager re-evaluate the orientation of |
| 1509 | // the screen based on the new activity order. |
| 1510 | boolean updated = false; |
Craig Mautner | 2219a1b | 2013-03-25 09:44:30 -0700 | [diff] [blame] | 1511 | if (mStackSupervisor.isMainStack(this)) { |
Craig Mautner | 8d341ef | 2013-03-26 09:03:27 -0700 | [diff] [blame] | 1512 | Configuration config = mService.mWindowManager.updateOrientationFromAppTokens( |
| 1513 | mService.mConfiguration, |
| 1514 | next.mayFreezeScreenLocked(next.app) ? next.appToken : null); |
| 1515 | if (config != null) { |
| 1516 | next.frozenBeforeDestroy = true; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1517 | } |
Craig Mautner | 8d341ef | 2013-03-26 09:03:27 -0700 | [diff] [blame] | 1518 | updated = mService.updateConfigurationLocked(config, next, false, false); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1519 | } |
Craig Mautner | 8d341ef | 2013-03-26 09:03:27 -0700 | [diff] [blame] | 1520 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1521 | if (!updated) { |
| 1522 | // The configuration update wasn't able to keep the existing |
| 1523 | // instance of the activity, and instead started a new one. |
| 1524 | // We should be all done, but let's just make sure our activity |
| 1525 | // is still at the top and schedule another run if something |
| 1526 | // weird happened. |
| 1527 | ActivityRecord nextNext = topRunningActivityLocked(null); |
| 1528 | if (DEBUG_SWITCH) Slog.i(TAG, |
| 1529 | "Activity config changed during resume: " + next |
| 1530 | + ", new next: " + nextNext); |
| 1531 | if (nextNext != next) { |
| 1532 | // Do over! |
| 1533 | mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG); |
| 1534 | } |
Craig Mautner | 2219a1b | 2013-03-25 09:44:30 -0700 | [diff] [blame] | 1535 | if (mStackSupervisor.isMainStack(this)) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1536 | mService.setFocusedActivityLocked(next); |
| 1537 | } |
| 1538 | ensureActivitiesVisibleLocked(null, 0); |
| 1539 | mService.mWindowManager.executeAppTransition(); |
| 1540 | mNoAnimActivities.clear(); |
| 1541 | return true; |
| 1542 | } |
Craig Mautner | 5854780 | 2013-03-05 08:23:53 -0800 | [diff] [blame] | 1543 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1544 | try { |
| 1545 | // Deliver all pending results. |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 1546 | ArrayList<ResultInfo> a = next.results; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1547 | if (a != null) { |
| 1548 | final int N = a.size(); |
| 1549 | if (!next.finishing && N > 0) { |
| 1550 | if (DEBUG_RESULTS) Slog.v( |
| 1551 | TAG, "Delivering results to " + next |
| 1552 | + ": " + a); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 1553 | next.app.thread.scheduleSendResult(next.appToken, a); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1554 | } |
| 1555 | } |
| 1556 | |
| 1557 | if (next.newIntents != null) { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 1558 | next.app.thread.scheduleNewIntent(next.newIntents, next.appToken); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1562 | next.userId, System.identityHashCode(next), |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1563 | next.task.taskId, next.shortComponentName); |
Craig Mautner | 5854780 | 2013-03-05 08:23:53 -0800 | [diff] [blame] | 1564 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 1565 | next.sleeping = false; |
Craig Mautner | 2420ead | 2013-04-01 17:13:20 -0700 | [diff] [blame] | 1566 | mService.showAskCompatModeDialogLocked(next); |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 1567 | next.app.pendingUiClean = true; |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 1568 | next.app.thread.scheduleResumeActivity(next.appToken, |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1569 | mService.isNextTransitionForward()); |
Craig Mautner | 5854780 | 2013-03-05 08:23:53 -0800 | [diff] [blame] | 1570 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 1571 | checkReadyForSleepLocked(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1572 | |
| 1573 | } catch (Exception e) { |
| 1574 | // Whoops, need to restart this activity! |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 1575 | if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to " |
| 1576 | + lastState + ": " + next); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1577 | next.state = lastState; |
| 1578 | mResumedActivity = lastResumedActivity; |
| 1579 | Slog.i(TAG, "Restarting because process died: " + next); |
| 1580 | if (!next.hasBeenLaunched) { |
| 1581 | next.hasBeenLaunched = true; |
| 1582 | } else { |
Craig Mautner | 2219a1b | 2013-03-25 09:44:30 -0700 | [diff] [blame] | 1583 | if (SHOW_APP_STARTING_PREVIEW && mStackSupervisor.isMainStack(this)) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1584 | mService.mWindowManager.setAppStartingWindow( |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 1585 | next.appToken, next.packageName, next.theme, |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 1586 | mService.compatibilityInfoForPackageLocked( |
| 1587 | next.info.applicationInfo), |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1588 | next.nonLocalizedLabel, |
Dianne Hackborn | 7eec10e | 2010-11-12 18:03:47 -0800 | [diff] [blame] | 1589 | next.labelRes, next.icon, next.windowFlags, |
| 1590 | null, true); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1591 | } |
| 1592 | } |
Craig Mautner | e79d4268 | 2013-04-01 19:01:53 -0700 | [diff] [blame^] | 1593 | mStackSupervisor.startSpecificActivityLocked(next, true, false); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1594 | return true; |
| 1595 | } |
| 1596 | |
| 1597 | // From this point on, if something goes wrong there is no way |
| 1598 | // to recover the activity. |
| 1599 | try { |
| 1600 | next.visible = true; |
| 1601 | completeResumeLocked(next); |
| 1602 | } catch (Exception e) { |
| 1603 | // If any exception gets thrown, toss away this |
| 1604 | // activity and try the next one. |
| 1605 | Slog.w(TAG, "Exception thrown during resume of " + next, e); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 1606 | requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 1607 | "resume-exception", true); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1608 | return true; |
| 1609 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1610 | next.stopped = false; |
| 1611 | |
| 1612 | } else { |
| 1613 | // Whoops, need to restart this activity! |
| 1614 | if (!next.hasBeenLaunched) { |
| 1615 | next.hasBeenLaunched = true; |
| 1616 | } else { |
| 1617 | if (SHOW_APP_STARTING_PREVIEW) { |
| 1618 | mService.mWindowManager.setAppStartingWindow( |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 1619 | next.appToken, next.packageName, next.theme, |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 1620 | mService.compatibilityInfoForPackageLocked( |
| 1621 | next.info.applicationInfo), |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1622 | next.nonLocalizedLabel, |
Dianne Hackborn | 7eec10e | 2010-11-12 18:03:47 -0800 | [diff] [blame] | 1623 | next.labelRes, next.icon, next.windowFlags, |
| 1624 | null, true); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1625 | } |
| 1626 | if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next); |
| 1627 | } |
Craig Mautner | e79d4268 | 2013-04-01 19:01:53 -0700 | [diff] [blame^] | 1628 | mStackSupervisor.startSpecificActivityLocked(next, true, true); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1629 | } |
| 1630 | |
| 1631 | return true; |
| 1632 | } |
| 1633 | |
Craig Mautner | 11bf9a5 | 2013-02-19 14:08:51 -0800 | [diff] [blame] | 1634 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1635 | private final void startActivityLocked(ActivityRecord r, boolean newTask, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 1636 | boolean doResume, boolean keepCurTransition, Bundle options) { |
Craig Mautner | 70a8693 | 2013-02-28 22:37:44 -0800 | [diff] [blame] | 1637 | TaskRecord task = null; |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 1638 | TaskRecord rTask = r.task; |
| 1639 | final int taskId = rTask.taskId; |
| 1640 | if (taskForIdLocked(taskId) == null || newTask) { |
Craig Mautner | 7787877 | 2013-03-04 19:46:24 -0800 | [diff] [blame] | 1641 | // Last activity in task had been removed or ActivityManagerService is reusing task. |
| 1642 | // Insert or replace. |
Craig Mautner | 7787877 | 2013-03-04 19:46:24 -0800 | [diff] [blame] | 1643 | // Might not even be in. |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 1644 | mTaskHistory.remove(rTask); |
Craig Mautner | 7787877 | 2013-03-04 19:46:24 -0800 | [diff] [blame] | 1645 | // Now put task at top. |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 1646 | mTaskHistory.add(rTask); |
| 1647 | mService.mWindowManager.moveTaskToTop(taskId); |
Craig Mautner | 7787877 | 2013-03-04 19:46:24 -0800 | [diff] [blame] | 1648 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1649 | if (!newTask) { |
| 1650 | // If starting in an existing task, find where that is... |
Craig Mautner | 70a8693 | 2013-02-28 22:37:44 -0800 | [diff] [blame] | 1651 | boolean startIt = true; |
| 1652 | for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { |
| 1653 | task = mTaskHistory.get(taskNdx); |
| 1654 | if (task == r.task) { |
| 1655 | // Here it is! Now, if this is not yet visible to the |
| 1656 | // user, then just add it without starting; it will |
| 1657 | // get started when the user navigates back to it. |
Craig Mautner | 70a8693 | 2013-02-28 22:37:44 -0800 | [diff] [blame] | 1658 | if (!startIt) { |
| 1659 | if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task " |
| 1660 | + task, new RuntimeException("here").fillInStackTrace()); |
| 1661 | task.addActivityToTop(r); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 1662 | r.putInHistory(); |
Craig Mautner | 70a8693 | 2013-02-28 22:37:44 -0800 | [diff] [blame] | 1663 | mService.mWindowManager.addAppToken(task.mActivities.indexOf(r), |
Craig Mautner | c00204b | 2013-03-05 15:02:14 -0800 | [diff] [blame] | 1664 | r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, |
| 1665 | r.fullscreen, |
Craig Mautner | 5962b12 | 2012-10-05 14:45:52 -0700 | [diff] [blame] | 1666 | (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1667 | if (VALIDATE_TOKENS) { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 1668 | validateAppTokensLocked(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1669 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 1670 | ActivityOptions.abort(options); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1671 | return; |
| 1672 | } |
| 1673 | break; |
Craig Mautner | 70a8693 | 2013-02-28 22:37:44 -0800 | [diff] [blame] | 1674 | } else if (task.numFullscreen > 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1675 | startIt = false; |
| 1676 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | // Place a new activity at top of stack, so it is next to interact |
| 1681 | // with the user. |
Craig Mautner | 70a8693 | 2013-02-28 22:37:44 -0800 | [diff] [blame] | 1682 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1683 | // If we are not placing the new activity frontmost, we do not want |
| 1684 | // to deliver the onUserLeaving callback to the actual frontmost |
| 1685 | // activity |
Craig Mautner | 70a8693 | 2013-02-28 22:37:44 -0800 | [diff] [blame] | 1686 | if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1687 | mUserLeaving = false; |
Craig Mautner | 70a8693 | 2013-02-28 22:37:44 -0800 | [diff] [blame] | 1688 | if (DEBUG_USER_LEAVING) Slog.v(TAG, |
| 1689 | "startActivity() behind front, mUserLeaving=false"); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1690 | } |
Craig Mautner | 70a8693 | 2013-02-28 22:37:44 -0800 | [diff] [blame] | 1691 | |
| 1692 | task = r.task; |
| 1693 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1694 | // Slot the activity into the history stack and proceed |
Craig Mautner | 70a8693 | 2013-02-28 22:37:44 -0800 | [diff] [blame] | 1695 | if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task, |
Craig Mautner | 56f52db | 2013-02-25 10:03:01 -0800 | [diff] [blame] | 1696 | new RuntimeException("here").fillInStackTrace()); |
Craig Mautner | 70a8693 | 2013-02-28 22:37:44 -0800 | [diff] [blame] | 1697 | task.addActivityToTop(r); |
| 1698 | |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 1699 | r.putInHistory(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1700 | r.frontOfTask = newTask; |
Craig Mautner | 70a8693 | 2013-02-28 22:37:44 -0800 | [diff] [blame] | 1701 | if (numActivities() > 1) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1702 | // We want to show the starting preview window if we are |
| 1703 | // switching to a new task, or the next activity's process is |
| 1704 | // not currently running. |
| 1705 | boolean showStartingIcon = newTask; |
| 1706 | ProcessRecord proc = r.app; |
| 1707 | if (proc == null) { |
| 1708 | proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid); |
| 1709 | } |
| 1710 | if (proc == null || proc.thread == null) { |
| 1711 | showStartingIcon = true; |
| 1712 | } |
| 1713 | if (DEBUG_TRANSITION) Slog.v(TAG, |
| 1714 | "Prepare open transition: starting " + r); |
| 1715 | if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) { |
Dianne Hackborn | 7da6ac3 | 2010-12-09 19:22:04 -0800 | [diff] [blame] | 1716 | mService.mWindowManager.prepareAppTransition( |
Craig Mautner | 4b71aa1 | 2012-12-27 17:20:01 -0800 | [diff] [blame] | 1717 | AppTransition.TRANSIT_NONE, keepCurTransition); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1718 | mNoAnimActivities.add(r); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1719 | } else { |
| 1720 | mService.mWindowManager.prepareAppTransition(newTask |
Craig Mautner | 4b71aa1 | 2012-12-27 17:20:01 -0800 | [diff] [blame] | 1721 | ? AppTransition.TRANSIT_TASK_OPEN |
| 1722 | : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1723 | mNoAnimActivities.remove(r); |
| 1724 | } |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 1725 | r.updateOptionsLocked(options); |
Craig Mautner | 70a8693 | 2013-02-28 22:37:44 -0800 | [diff] [blame] | 1726 | mService.mWindowManager.addAppToken(task.mActivities.indexOf(r), |
Craig Mautner | c00204b | 2013-03-05 15:02:14 -0800 | [diff] [blame] | 1727 | r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen, |
Craig Mautner | 5962b12 | 2012-10-05 14:45:52 -0700 | [diff] [blame] | 1728 | (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1729 | boolean doShow = true; |
| 1730 | if (newTask) { |
| 1731 | // Even though this activity is starting fresh, we still need |
| 1732 | // to reset it to make sure we apply affinities to move any |
| 1733 | // existing activities from other tasks in to it. |
| 1734 | // If the caller has requested that the target task be |
| 1735 | // reset, then do so. |
| 1736 | if ((r.intent.getFlags() |
| 1737 | &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) { |
| 1738 | resetTaskIfNeededLocked(r, r); |
| 1739 | doShow = topRunningNonDelayedActivityLocked(null) == r; |
| 1740 | } |
| 1741 | } |
| 1742 | if (SHOW_APP_STARTING_PREVIEW && doShow) { |
| 1743 | // Figure out if we are transitioning from another activity that is |
| 1744 | // "has the same starting icon" as the next one. This allows the |
| 1745 | // window manager to keep the previous window it had previously |
| 1746 | // created, if it still had one. |
| 1747 | ActivityRecord prev = mResumedActivity; |
| 1748 | if (prev != null) { |
| 1749 | // We don't want to reuse the previous starting preview if: |
| 1750 | // (1) The current activity is in a different task. |
| 1751 | if (prev.task != r.task) prev = null; |
| 1752 | // (2) The current activity is already displayed. |
| 1753 | else if (prev.nowVisible) prev = null; |
| 1754 | } |
| 1755 | mService.mWindowManager.setAppStartingWindow( |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 1756 | r.appToken, r.packageName, r.theme, |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 1757 | mService.compatibilityInfoForPackageLocked( |
| 1758 | r.info.applicationInfo), r.nonLocalizedLabel, |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 1759 | r.labelRes, r.icon, r.windowFlags, |
| 1760 | prev != null ? prev.appToken : null, showStartingIcon); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1761 | } |
| 1762 | } else { |
| 1763 | // If this is the first activity, don't do any fancy animations, |
| 1764 | // because there is nothing for it to animate on top of. |
Craig Mautner | 70a8693 | 2013-02-28 22:37:44 -0800 | [diff] [blame] | 1765 | mService.mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken, |
Craig Mautner | c00204b | 2013-03-05 15:02:14 -0800 | [diff] [blame] | 1766 | r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen, |
Craig Mautner | 5962b12 | 2012-10-05 14:45:52 -0700 | [diff] [blame] | 1767 | (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0); |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 1768 | ActivityOptions.abort(options); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1769 | } |
| 1770 | if (VALIDATE_TOKENS) { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 1771 | validateAppTokensLocked(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | if (doResume) { |
| 1775 | resumeTopActivityLocked(null); |
| 1776 | } |
| 1777 | } |
| 1778 | |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 1779 | final void validateAppTokensLocked() { |
| 1780 | mValidateAppTokens.clear(); |
Craig Mautner | 000f002 | 2013-02-26 15:04:29 -0800 | [diff] [blame] | 1781 | mValidateAppTokens.ensureCapacity(numActivities()); |
| 1782 | final int numTasks = mTaskHistory.size(); |
| 1783 | for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) { |
| 1784 | TaskRecord task = mTaskHistory.get(taskNdx); |
| 1785 | final ArrayList<ActivityRecord> activities = task.mActivities; |
| 1786 | if (activities.size() == 0) { |
| 1787 | continue; |
Craig Mautner | b1fd65c0 | 2013-02-05 13:34:57 -0800 | [diff] [blame] | 1788 | } |
Craig Mautner | 000f002 | 2013-02-26 15:04:29 -0800 | [diff] [blame] | 1789 | TaskGroup group = new TaskGroup(); |
| 1790 | group.taskId = task.taskId; |
| 1791 | mValidateAppTokens.add(group); |
| 1792 | final int numActivities = activities.size(); |
| 1793 | for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) { |
| 1794 | final ActivityRecord r = activities.get(activityNdx); |
| 1795 | group.tokens.add(r.appToken); |
| 1796 | } |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 1797 | } |
Craig Mautner | 00af9fe | 2013-03-25 09:13:41 -0700 | [diff] [blame] | 1798 | mService.mWindowManager.validateAppTokens(mStackId, mValidateAppTokens); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 1799 | } |
| 1800 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1801 | /** |
| 1802 | * Perform a reset of the given task, if needed as part of launching it. |
| 1803 | * Returns the new HistoryRecord at the top of the task. |
| 1804 | */ |
Craig Mautner | e3a74d5 | 2013-02-22 14:14:58 -0800 | [diff] [blame] | 1805 | /** |
| 1806 | * Helper method for #resetTaskIfNeededLocked. |
| 1807 | * We are inside of the task being reset... we'll either finish this activity, push it out |
| 1808 | * for another task, or leave it as-is. |
| 1809 | * @param task The task containing the Activity (taskTop) that might be reset. |
| 1810 | * @param forceReset |
| 1811 | * @return An ActivityOptions that needs to be processed. |
| 1812 | */ |
| 1813 | private final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, |
| 1814 | boolean forceReset) { |
| 1815 | ActivityOptions topOptions = null; |
| 1816 | |
| 1817 | int replyChainEnd = -1; |
| 1818 | boolean canMoveOptions = true; |
| 1819 | |
| 1820 | // We only do this for activities that are not the root of the task (since if we finish |
| 1821 | // the root, we may no longer have the task!). |
| 1822 | final ArrayList<ActivityRecord> activities = task.mActivities; |
| 1823 | final int numActivities = activities.size(); |
| 1824 | for (int i = numActivities - 1; i > 0; --i ) { |
| 1825 | ActivityRecord target = activities.get(i); |
| 1826 | |
| 1827 | final int flags = target.info.flags; |
| 1828 | final boolean finishOnTaskLaunch = |
| 1829 | (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0; |
| 1830 | final boolean allowTaskReparenting = |
| 1831 | (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0; |
| 1832 | final boolean clearWhenTaskReset = |
| 1833 | (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0; |
| 1834 | |
| 1835 | if (!finishOnTaskLaunch |
| 1836 | && !clearWhenTaskReset |
| 1837 | && target.resultTo != null) { |
| 1838 | // If this activity is sending a reply to a previous |
| 1839 | // activity, we can't do anything with it now until |
| 1840 | // we reach the start of the reply chain. |
| 1841 | // XXX note that we are assuming the result is always |
| 1842 | // to the previous activity, which is almost always |
| 1843 | // the case but we really shouldn't count on. |
| 1844 | if (replyChainEnd < 0) { |
| 1845 | replyChainEnd = i; |
| 1846 | } |
| 1847 | } else if (!finishOnTaskLaunch |
| 1848 | && !clearWhenTaskReset |
| 1849 | && allowTaskReparenting |
| 1850 | && target.taskAffinity != null |
| 1851 | && !target.taskAffinity.equals(task.affinity)) { |
| 1852 | // If this activity has an affinity for another |
| 1853 | // task, then we need to move it out of here. We will |
| 1854 | // move it as far out of the way as possible, to the |
| 1855 | // bottom of the activity stack. This also keeps it |
| 1856 | // correctly ordered with any activities we previously |
| 1857 | // moved. |
| 1858 | TaskRecord bottomTask = mTaskHistory.get(0); |
| 1859 | ActivityRecord p = bottomTask.mActivities.get(0); |
| 1860 | if (target.taskAffinity != null |
| 1861 | && target.taskAffinity.equals(p.task.affinity)) { |
| 1862 | // If the activity currently at the bottom has the |
| 1863 | // same task affinity as the one we are moving, |
| 1864 | // then merge it into the same task. |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 1865 | setTask(target, p.task, p.thumbHolder, false); |
Craig Mautner | e3a74d5 | 2013-02-22 14:14:58 -0800 | [diff] [blame] | 1866 | if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target |
| 1867 | + " out to bottom task " + p.task); |
| 1868 | } else { |
Craig Mautner | 8d341ef | 2013-03-26 09:03:27 -0700 | [diff] [blame] | 1869 | setTask(target, createTaskRecord(mStackSupervisor.getNextTaskId(), target.info, |
| 1870 | null, false), null, false); |
Craig Mautner | e3a74d5 | 2013-02-22 14:14:58 -0800 | [diff] [blame] | 1871 | target.task.affinityIntent = target.intent; |
| 1872 | if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target |
| 1873 | + " out to new task " + target.task); |
| 1874 | } |
| 1875 | |
| 1876 | final TaskRecord targetTask = target.task; |
| 1877 | final int targetTaskId = targetTask.taskId; |
| 1878 | mService.mWindowManager.setAppGroupId(target.appToken, targetTaskId); |
| 1879 | |
| 1880 | ThumbnailHolder curThumbHolder = target.thumbHolder; |
| 1881 | boolean gotOptions = !canMoveOptions; |
| 1882 | |
| 1883 | final int start = replyChainEnd < 0 ? i : replyChainEnd; |
| 1884 | for (int srcPos = start; srcPos >= i; --srcPos) { |
| 1885 | p = activities.get(srcPos); |
| 1886 | if (p.finishing) { |
| 1887 | continue; |
| 1888 | } |
| 1889 | |
| 1890 | curThumbHolder = p.thumbHolder; |
| 1891 | canMoveOptions = false; |
| 1892 | if (!gotOptions && topOptions == null) { |
| 1893 | topOptions = p.takeOptionsLocked(); |
| 1894 | if (topOptions != null) { |
| 1895 | gotOptions = true; |
| 1896 | } |
| 1897 | } |
| 1898 | if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task=" |
| 1899 | + task + " adding to task=" + targetTask, |
| 1900 | new RuntimeException("here").fillInStackTrace()); |
| 1901 | if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p |
| 1902 | + " out to target's task " + target.task); |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 1903 | setTask(p, targetTask, curThumbHolder, false); |
Craig Mautner | e3a74d5 | 2013-02-22 14:14:58 -0800 | [diff] [blame] | 1904 | targetTask.addActivityAtBottom(p); |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 1905 | |
Craig Mautner | e3a74d5 | 2013-02-22 14:14:58 -0800 | [diff] [blame] | 1906 | mService.mWindowManager.setAppGroupId(p.appToken, targetTaskId); |
| 1907 | } |
| 1908 | |
| 1909 | mService.mWindowManager.moveTaskToBottom(targetTaskId); |
| 1910 | if (VALIDATE_TOKENS) { |
| 1911 | validateAppTokensLocked(); |
| 1912 | } |
| 1913 | |
| 1914 | replyChainEnd = -1; |
| 1915 | } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) { |
| 1916 | // If the activity should just be removed -- either |
| 1917 | // because it asks for it, or the task should be |
| 1918 | // cleared -- then finish it and anything that is |
| 1919 | // part of its reply chain. |
| 1920 | int end; |
| 1921 | if (clearWhenTaskReset) { |
| 1922 | // In this case, we want to finish this activity |
| 1923 | // and everything above it, so be sneaky and pretend |
| 1924 | // like these are all in the reply chain. |
| 1925 | end = numActivities - 1; |
| 1926 | } else if (replyChainEnd < 0) { |
| 1927 | end = i; |
| 1928 | } else { |
| 1929 | end = replyChainEnd; |
| 1930 | } |
| 1931 | ActivityRecord p = null; |
| 1932 | boolean gotOptions = !canMoveOptions; |
| 1933 | for (int srcPos = i; srcPos <= end; srcPos++) { |
| 1934 | p = activities.get(srcPos); |
| 1935 | if (p.finishing) { |
| 1936 | continue; |
| 1937 | } |
| 1938 | canMoveOptions = false; |
| 1939 | if (!gotOptions && topOptions == null) { |
| 1940 | topOptions = p.takeOptionsLocked(); |
| 1941 | if (topOptions != null) { |
| 1942 | gotOptions = true; |
| 1943 | } |
| 1944 | } |
Craig Mautner | 5854780 | 2013-03-05 08:23:53 -0800 | [diff] [blame] | 1945 | if (DEBUG_TASKS) Slog.w(TAG, |
| 1946 | "resetTaskIntendedTask: calling finishActivity on " + p); |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 1947 | if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) { |
Craig Mautner | e3a74d5 | 2013-02-22 14:14:58 -0800 | [diff] [blame] | 1948 | end--; |
| 1949 | srcPos--; |
| 1950 | } |
| 1951 | } |
| 1952 | replyChainEnd = -1; |
| 1953 | } else { |
| 1954 | // If we were in the middle of a chain, well the |
| 1955 | // activity that started it all doesn't want anything |
| 1956 | // special, so leave it all as-is. |
| 1957 | replyChainEnd = -1; |
| 1958 | } |
| 1959 | } |
| 1960 | |
| 1961 | return topOptions; |
| 1962 | } |
| 1963 | |
| 1964 | /** |
| 1965 | * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given |
| 1966 | * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop. |
| 1967 | * @param affinityTask The task we are looking for an affinity to. |
| 1968 | * @param task Task that resetTaskIfNeededLocked.taskTop belongs to. |
| 1969 | * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked. |
| 1970 | * @param forceReset Flag passed in to resetTaskIfNeededLocked. |
| 1971 | */ |
Craig Mautner | 7787877 | 2013-03-04 19:46:24 -0800 | [diff] [blame] | 1972 | private final int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task, |
| 1973 | boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) { |
Craig Mautner | e3a74d5 | 2013-02-22 14:14:58 -0800 | [diff] [blame] | 1974 | int replyChainEnd = -1; |
| 1975 | final int taskId = task.taskId; |
| 1976 | final String taskAffinity = task.affinity; |
| 1977 | |
| 1978 | final ArrayList<ActivityRecord> activities = affinityTask.mActivities; |
| 1979 | final int numActivities = activities.size(); |
| 1980 | // Do not operate on the root Activity. |
| 1981 | for (int i = numActivities - 1; i > 0; --i) { |
| 1982 | ActivityRecord target = activities.get(i); |
| 1983 | |
| 1984 | final int flags = target.info.flags; |
| 1985 | boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0; |
| 1986 | boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0; |
| 1987 | |
| 1988 | if (target.resultTo != null) { |
| 1989 | // If this activity is sending a reply to a previous |
| 1990 | // activity, we can't do anything with it now until |
| 1991 | // we reach the start of the reply chain. |
| 1992 | // XXX note that we are assuming the result is always |
| 1993 | // to the previous activity, which is almost always |
| 1994 | // the case but we really shouldn't count on. |
| 1995 | if (replyChainEnd < 0) { |
| 1996 | replyChainEnd = i; |
| 1997 | } |
| 1998 | } else if (topTaskIsHigher |
| 1999 | && allowTaskReparenting |
| 2000 | && taskAffinity != null |
| 2001 | && taskAffinity.equals(target.taskAffinity)) { |
| 2002 | // This activity has an affinity for our task. Either remove it if we are |
| 2003 | // clearing or move it over to our task. Note that |
| 2004 | // we currently punt on the case where we are resetting a |
| 2005 | // task that is not at the top but who has activities above |
| 2006 | // with an affinity to it... this is really not a normal |
| 2007 | // case, and we will need to later pull that task to the front |
| 2008 | // and usually at that point we will do the reset and pick |
| 2009 | // up those remaining activities. (This only happens if |
| 2010 | // someone starts an activity in a new task from an activity |
| 2011 | // in a task that is not currently on top.) |
| 2012 | if (forceReset || finishOnTaskLaunch) { |
| 2013 | final int start = replyChainEnd >= 0 ? replyChainEnd : i; |
| 2014 | if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i); |
| 2015 | for (int srcPos = start; srcPos >= i; --srcPos) { |
| 2016 | final ActivityRecord p = activities.get(srcPos); |
| 2017 | if (p.finishing) { |
| 2018 | continue; |
| 2019 | } |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 2020 | finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false); |
Craig Mautner | e3a74d5 | 2013-02-22 14:14:58 -0800 | [diff] [blame] | 2021 | } |
| 2022 | } else { |
Craig Mautner | 7787877 | 2013-03-04 19:46:24 -0800 | [diff] [blame] | 2023 | if (taskInsertionPoint < 0) { |
| 2024 | taskInsertionPoint = task.mActivities.size(); |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 2025 | |
Craig Mautner | 7787877 | 2013-03-04 19:46:24 -0800 | [diff] [blame] | 2026 | } |
Craig Mautner | 7787877 | 2013-03-04 19:46:24 -0800 | [diff] [blame] | 2027 | |
| 2028 | final int start = replyChainEnd >= 0 ? replyChainEnd : i; |
| 2029 | if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":" |
| 2030 | + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint); |
| 2031 | for (int srcPos = start; srcPos >= i; --srcPos) { |
Craig Mautner | e3a74d5 | 2013-02-22 14:14:58 -0800 | [diff] [blame] | 2032 | final ActivityRecord p = activities.get(srcPos); |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 2033 | setTask(p, task, null, false); |
Craig Mautner | 7787877 | 2013-03-04 19:46:24 -0800 | [diff] [blame] | 2034 | task.addActivityAtIndex(taskInsertionPoint, p); |
Craig Mautner | e3a74d5 | 2013-02-22 14:14:58 -0800 | [diff] [blame] | 2035 | |
Craig Mautner | e3a74d5 | 2013-02-22 14:14:58 -0800 | [diff] [blame] | 2036 | if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p |
| 2037 | + " to stack at " + task, |
| 2038 | new RuntimeException("here").fillInStackTrace()); |
| 2039 | if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos |
| 2040 | + " in to resetting task " + task); |
| 2041 | mService.mWindowManager.setAppGroupId(p.appToken, taskId); |
| 2042 | } |
| 2043 | mService.mWindowManager.moveTaskToTop(taskId); |
Craig Mautner | 9658b31 | 2013-02-28 10:55:59 -0800 | [diff] [blame] | 2044 | if (VALIDATE_TOKENS) { |
| 2045 | validateAppTokensLocked(); |
| 2046 | } |
Craig Mautner | e3a74d5 | 2013-02-22 14:14:58 -0800 | [diff] [blame] | 2047 | |
| 2048 | // Now we've moved it in to place... but what if this is |
| 2049 | // a singleTop activity and we have put it on top of another |
| 2050 | // instance of the same activity? Then we drop the instance |
| 2051 | // below so it remains singleTop. |
| 2052 | if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) { |
| 2053 | ArrayList<ActivityRecord> taskActivities = task.mActivities; |
| 2054 | boolean found = false; |
| 2055 | int targetNdx = taskActivities.indexOf(target); |
| 2056 | if (targetNdx > 0) { |
| 2057 | ActivityRecord p = taskActivities.get(targetNdx - 1); |
| 2058 | if (p.intent.getComponent().equals(target.intent.getComponent())) { |
Craig Mautner | 5854780 | 2013-03-05 08:23:53 -0800 | [diff] [blame] | 2059 | finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace", |
| 2060 | false); |
Craig Mautner | e3a74d5 | 2013-02-22 14:14:58 -0800 | [diff] [blame] | 2061 | } |
| 2062 | } |
| 2063 | } |
| 2064 | } |
| 2065 | |
| 2066 | replyChainEnd = -1; |
| 2067 | } |
| 2068 | } |
Craig Mautner | 7787877 | 2013-03-04 19:46:24 -0800 | [diff] [blame] | 2069 | return taskInsertionPoint; |
Craig Mautner | e3a74d5 | 2013-02-22 14:14:58 -0800 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | private final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop, |
| 2073 | ActivityRecord newActivity) { |
| 2074 | boolean forceReset = |
| 2075 | (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0; |
| 2076 | if (ACTIVITY_INACTIVE_RESET_TIME > 0 |
| 2077 | && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) { |
| 2078 | if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) { |
| 2079 | forceReset = true; |
| 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | final TaskRecord task = taskTop.task; |
| 2084 | |
| 2085 | /** False until we evaluate the TaskRecord associated with taskTop. Switches to true |
| 2086 | * for remaining tasks. Used for later tasks to reparent to task. */ |
| 2087 | boolean taskFound = false; |
| 2088 | |
| 2089 | /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */ |
| 2090 | ActivityOptions topOptions = null; |
| 2091 | |
Craig Mautner | 7787877 | 2013-03-04 19:46:24 -0800 | [diff] [blame] | 2092 | // Preserve the location for reparenting in the new task. |
| 2093 | int reparentInsertionPoint = -1; |
| 2094 | |
Craig Mautner | e3a74d5 | 2013-02-22 14:14:58 -0800 | [diff] [blame] | 2095 | for (int i = mTaskHistory.size() - 1; i >= 0; --i) { |
| 2096 | final TaskRecord targetTask = mTaskHistory.get(i); |
| 2097 | |
| 2098 | if (targetTask == task) { |
| 2099 | topOptions = resetTargetTaskIfNeededLocked(task, forceReset); |
| 2100 | taskFound = true; |
| 2101 | } else { |
Craig Mautner | 7787877 | 2013-03-04 19:46:24 -0800 | [diff] [blame] | 2102 | reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task, |
| 2103 | taskFound, forceReset, reparentInsertionPoint); |
Craig Mautner | e3a74d5 | 2013-02-22 14:14:58 -0800 | [diff] [blame] | 2104 | } |
| 2105 | } |
| 2106 | |
Craig Mautner | 70a8693 | 2013-02-28 22:37:44 -0800 | [diff] [blame] | 2107 | int taskNdx = mTaskHistory.indexOf(task); |
| 2108 | do { |
| 2109 | taskTop = mTaskHistory.get(taskNdx--).getTopActivity(); |
| 2110 | } while (taskTop == null && taskNdx >= 0); |
| 2111 | |
Craig Mautner | e3a74d5 | 2013-02-22 14:14:58 -0800 | [diff] [blame] | 2112 | if (topOptions != null) { |
| 2113 | // If we got some ActivityOptions from an activity on top that |
| 2114 | // was removed from the task, propagate them to the new real top. |
| 2115 | if (taskTop != null) { |
Craig Mautner | e3a74d5 | 2013-02-22 14:14:58 -0800 | [diff] [blame] | 2116 | taskTop.updateOptionsLocked(topOptions); |
| 2117 | } else { |
Craig Mautner | e3a74d5 | 2013-02-22 14:14:58 -0800 | [diff] [blame] | 2118 | topOptions.abort(); |
| 2119 | } |
| 2120 | } |
| 2121 | |
| 2122 | return taskTop; |
| 2123 | } |
| 2124 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2125 | /** |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2126 | * Find the activity in the history stack within the given task. Returns |
| 2127 | * the index within the history at which it's found, or < 0 if not found. |
| 2128 | */ |
Craig Mautner | 56f52db | 2013-02-25 10:03:01 -0800 | [diff] [blame] | 2129 | private final ActivityRecord findActivityInHistoryLocked(ActivityRecord r, TaskRecord task) { |
| 2130 | final ComponentName realActivity = r.realActivity; |
| 2131 | ArrayList<ActivityRecord> activities = task.mActivities; |
| 2132 | for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) { |
| 2133 | ActivityRecord candidate = activities.get(activityNdx); |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 2134 | if (candidate.finishing) { |
| 2135 | continue; |
| 2136 | } |
Craig Mautner | 56f52db | 2013-02-25 10:03:01 -0800 | [diff] [blame] | 2137 | if (candidate.realActivity.equals(realActivity)) { |
| 2138 | return candidate; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2139 | } |
| 2140 | } |
Craig Mautner | 56f52db | 2013-02-25 10:03:01 -0800 | [diff] [blame] | 2141 | return null; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2142 | } |
| 2143 | |
| 2144 | /** |
| 2145 | * Reorder the history stack so that the activity at the given index is |
| 2146 | * brought to the front. |
| 2147 | */ |
Craig Mautner | 56f52db | 2013-02-25 10:03:01 -0800 | [diff] [blame] | 2148 | private final void moveActivityToFrontLocked(ActivityRecord newTop) { |
| 2149 | if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + newTop |
| 2150 | + " to stack at top", new RuntimeException("here").fillInStackTrace()); |
| 2151 | |
| 2152 | final TaskRecord task = newTop.task; |
| 2153 | task.getTopActivity().frontOfTask = false; |
| 2154 | task.mActivities.remove(newTop); |
| 2155 | task.mActivities.add(newTop); |
| 2156 | newTop.frontOfTask = true; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2157 | } |
| 2158 | |
| 2159 | final int startActivityLocked(IApplicationThread caller, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2160 | Intent intent, String resolvedType, ActivityInfo aInfo, IBinder resultTo, |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2161 | String resultWho, int requestCode, |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2162 | int callingPid, int callingUid, String callingPackage, int startFlags, Bundle options, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2163 | boolean componentSpecified, ActivityRecord[] outActivity) { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2164 | int err = ActivityManager.START_SUCCESS; |
Dianne Hackborn | efb5810 | 2010-10-14 16:47:34 -0700 | [diff] [blame] | 2165 | |
| 2166 | ProcessRecord callerApp = null; |
| 2167 | if (caller != null) { |
| 2168 | callerApp = mService.getRecordForAppLocked(caller); |
| 2169 | if (callerApp != null) { |
| 2170 | callingPid = callerApp.pid; |
| 2171 | callingUid = callerApp.info.uid; |
| 2172 | } else { |
| 2173 | Slog.w(TAG, "Unable to find app for caller " + caller |
| 2174 | + " (pid=" + callingPid + ") when starting: " |
| 2175 | + intent.toString()); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2176 | err = ActivityManager.START_PERMISSION_DENIED; |
Dianne Hackborn | efb5810 | 2010-10-14 16:47:34 -0700 | [diff] [blame] | 2177 | } |
| 2178 | } |
| 2179 | |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2180 | if (err == ActivityManager.START_SUCCESS) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2181 | final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0; |
Dianne Hackborn | 5fe7e2a | 2012-10-04 11:58:16 -0700 | [diff] [blame] | 2182 | Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false) |
| 2183 | + "} from pid " + (callerApp != null ? callerApp.pid : callingPid)); |
Dianne Hackborn | efb5810 | 2010-10-14 16:47:34 -0700 | [diff] [blame] | 2184 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2185 | |
| 2186 | ActivityRecord sourceRecord = null; |
| 2187 | ActivityRecord resultRecord = null; |
| 2188 | if (resultTo != null) { |
Craig Mautner | 56f52db | 2013-02-25 10:03:01 -0800 | [diff] [blame] | 2189 | sourceRecord = isInStackLocked(resultTo); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2190 | if (DEBUG_RESULTS) Slog.v( |
Craig Mautner | 56f52db | 2013-02-25 10:03:01 -0800 | [diff] [blame] | 2191 | TAG, "Will send result to " + resultTo + " " + sourceRecord); |
| 2192 | if (sourceRecord != null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2193 | if (requestCode >= 0 && !sourceRecord.finishing) { |
| 2194 | resultRecord = sourceRecord; |
| 2195 | } |
| 2196 | } |
| 2197 | } |
| 2198 | |
| 2199 | int launchFlags = intent.getFlags(); |
| 2200 | |
| 2201 | if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 |
| 2202 | && sourceRecord != null) { |
| 2203 | // Transfer the result target from the source activity to the new |
| 2204 | // one being started, including any failures. |
| 2205 | if (requestCode >= 0) { |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2206 | ActivityOptions.abort(options); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2207 | return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2208 | } |
| 2209 | resultRecord = sourceRecord.resultTo; |
| 2210 | resultWho = sourceRecord.resultWho; |
| 2211 | requestCode = sourceRecord.requestCode; |
| 2212 | sourceRecord.resultTo = null; |
| 2213 | if (resultRecord != null) { |
| 2214 | resultRecord.removeResultsLocked( |
| 2215 | sourceRecord, resultWho, requestCode); |
| 2216 | } |
| 2217 | } |
| 2218 | |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2219 | if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2220 | // We couldn't find a class that can handle the given Intent. |
| 2221 | // That's the end of that! |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2222 | err = ActivityManager.START_INTENT_NOT_RESOLVED; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2223 | } |
| 2224 | |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2225 | if (err == ActivityManager.START_SUCCESS && aInfo == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2226 | // We couldn't find the specific class specified in the Intent. |
| 2227 | // Also the end of the line. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2228 | err = ActivityManager.START_CLASS_NOT_FOUND; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2229 | } |
| 2230 | |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2231 | if (err != ActivityManager.START_SUCCESS) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2232 | if (resultRecord != null) { |
| 2233 | sendActivityResultLocked(-1, |
| 2234 | resultRecord, resultWho, requestCode, |
| 2235 | Activity.RESULT_CANCELED, null); |
| 2236 | } |
Craig Mautner | 2708430 | 2013-03-25 08:05:25 -0700 | [diff] [blame] | 2237 | mStackSupervisor.setDismissKeyguard(false); |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2238 | ActivityOptions.abort(options); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2239 | return err; |
| 2240 | } |
| 2241 | |
Jeff Sharkey | 35be756 | 2012-04-18 19:16:15 -0700 | [diff] [blame] | 2242 | final int startAnyPerm = mService.checkPermission( |
| 2243 | START_ANY_ACTIVITY, callingPid, callingUid); |
| 2244 | final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 2245 | callingUid, aInfo.applicationInfo.uid, aInfo.exported); |
Jeff Sharkey | 35be756 | 2012-04-18 19:16:15 -0700 | [diff] [blame] | 2246 | if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2247 | if (resultRecord != null) { |
| 2248 | sendActivityResultLocked(-1, |
| 2249 | resultRecord, resultWho, requestCode, |
| 2250 | Activity.RESULT_CANCELED, null); |
| 2251 | } |
Craig Mautner | 2708430 | 2013-03-25 08:05:25 -0700 | [diff] [blame] | 2252 | mStackSupervisor.setDismissKeyguard(false); |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 2253 | String msg; |
| 2254 | if (!aInfo.exported) { |
| 2255 | msg = "Permission Denial: starting " + intent.toString() |
| 2256 | + " from " + callerApp + " (pid=" + callingPid |
| 2257 | + ", uid=" + callingUid + ")" |
| 2258 | + " not exported from uid " + aInfo.applicationInfo.uid; |
| 2259 | } else { |
| 2260 | msg = "Permission Denial: starting " + intent.toString() |
| 2261 | + " from " + callerApp + " (pid=" + callingPid |
| 2262 | + ", uid=" + callingUid + ")" |
| 2263 | + " requires " + aInfo.permission; |
| 2264 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2265 | Slog.w(TAG, msg); |
| 2266 | throw new SecurityException(msg); |
| 2267 | } |
| 2268 | |
Craig Mautner | 2219a1b | 2013-03-25 09:44:30 -0700 | [diff] [blame] | 2269 | if (mStackSupervisor.isMainStack(this)) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2270 | if (mService.mController != null) { |
| 2271 | boolean abort = false; |
| 2272 | try { |
| 2273 | // The Intent we give to the watcher has the extra data |
| 2274 | // stripped off, since it can contain private information. |
| 2275 | Intent watchIntent = intent.cloneFilter(); |
| 2276 | abort = !mService.mController.activityStarting(watchIntent, |
| 2277 | aInfo.applicationInfo.packageName); |
| 2278 | } catch (RemoteException e) { |
| 2279 | mService.mController = null; |
| 2280 | } |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 2281 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2282 | if (abort) { |
| 2283 | if (resultRecord != null) { |
Craig Mautner | 8d341ef | 2013-03-26 09:03:27 -0700 | [diff] [blame] | 2284 | sendActivityResultLocked(-1, resultRecord, resultWho, requestCode, |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2285 | Activity.RESULT_CANCELED, null); |
| 2286 | } |
| 2287 | // We pretend to the caller that it was really started, but |
| 2288 | // they will just get a cancel result. |
Craig Mautner | 2708430 | 2013-03-25 08:05:25 -0700 | [diff] [blame] | 2289 | mStackSupervisor.setDismissKeyguard(false); |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2290 | ActivityOptions.abort(options); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2291 | return ActivityManager.START_SUCCESS; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2292 | } |
| 2293 | } |
| 2294 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2295 | |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 2296 | ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage, |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2297 | intent, resolvedType, aInfo, mService.mConfiguration, |
| 2298 | resultRecord, resultWho, requestCode, componentSpecified); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2299 | if (outActivity != null) { |
| 2300 | outActivity[0] = r; |
| 2301 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2302 | |
Craig Mautner | 2219a1b | 2013-03-25 09:44:30 -0700 | [diff] [blame] | 2303 | if (mStackSupervisor.isMainStack(this)) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2304 | if (mResumedActivity == null |
| 2305 | || mResumedActivity.info.applicationInfo.uid != callingUid) { |
| 2306 | if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid, "Activity start")) { |
Craig Mautner | 64f2b17 | 2013-03-11 11:40:31 -0700 | [diff] [blame] | 2307 | PendingActivityLaunch pal = |
| 2308 | new PendingActivityLaunch(r, sourceRecord, startFlags, this); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2309 | mService.mPendingActivityLaunches.add(pal); |
Craig Mautner | 2708430 | 2013-03-25 08:05:25 -0700 | [diff] [blame] | 2310 | mStackSupervisor.setDismissKeyguard(false); |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2311 | ActivityOptions.abort(options); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2312 | return ActivityManager.START_SWITCHES_CANCELED; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2313 | } |
| 2314 | } |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 2315 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2316 | if (mService.mDidAppSwitch) { |
| 2317 | // This is the second allowed switch since we stopped switches, |
| 2318 | // so now just generally allow switches. Use case: user presses |
| 2319 | // home (switches disabled, switch to home, mDidAppSwitch now true); |
| 2320 | // user taps a home icon (coming from home so allowed, we hit here |
| 2321 | // and now allow anyone to switch again). |
| 2322 | mService.mAppSwitchesAllowedTime = 0; |
| 2323 | } else { |
| 2324 | mService.mDidAppSwitch = true; |
| 2325 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2326 | } |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 2327 | |
Craig Mautner | 8d341ef | 2013-03-26 09:03:27 -0700 | [diff] [blame] | 2328 | mService.doPendingActivityLaunchesLocked(false); |
| 2329 | |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 2330 | err = startActivityUncheckedLocked(r, sourceRecord, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 2331 | startFlags, true, options); |
Craig Mautner | 2708430 | 2013-03-25 08:05:25 -0700 | [diff] [blame] | 2332 | if (mPausingActivity == null) { |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 2333 | // Someone asked to have the keyguard dismissed on the next |
| 2334 | // activity start, but we are not actually doing an activity |
| 2335 | // switch... just dismiss the keyguard now, because we |
| 2336 | // probably want to see whatever is behind it. |
Craig Mautner | 2708430 | 2013-03-25 08:05:25 -0700 | [diff] [blame] | 2337 | mStackSupervisor.dismissKeyguard(); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 2338 | } |
| 2339 | return err; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2340 | } |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 2341 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2342 | final void moveHomeToFrontFromLaunchLocked(int launchFlags) { |
| 2343 | if ((launchFlags & |
| 2344 | (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) |
| 2345 | == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) { |
| 2346 | // Caller wants to appear on home activity, so before starting |
| 2347 | // their own activity we will bring home to the front. |
| 2348 | moveHomeToFrontLocked(); |
| 2349 | } |
| 2350 | } |
| 2351 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2352 | final int startActivityUncheckedLocked(ActivityRecord r, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 2353 | ActivityRecord sourceRecord, int startFlags, boolean doResume, |
| 2354 | Bundle options) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2355 | final Intent intent = r.intent; |
| 2356 | final int callingUid = r.launchedFromUid; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2357 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2358 | int launchFlags = intent.getFlags(); |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 2359 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2360 | // We'll invoke onUserLeaving before onPause only if the launching |
| 2361 | // activity did not explicitly state that this is an automated launch. |
| 2362 | mUserLeaving = (launchFlags&Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0; |
| 2363 | if (DEBUG_USER_LEAVING) Slog.v(TAG, |
| 2364 | "startActivity() => mUserLeaving=" + mUserLeaving); |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 2365 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2366 | // If the caller has asked not to resume at this point, we make note |
| 2367 | // of this in the record so that we can skip it when trying to find |
| 2368 | // the top running activity. |
| 2369 | if (!doResume) { |
| 2370 | r.delayedResume = true; |
| 2371 | } |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 2372 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2373 | ActivityRecord notTop = (launchFlags&Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) |
| 2374 | != 0 ? r : null; |
| 2375 | |
| 2376 | // If the onlyIfNeeded flag is set, then we can do this if the activity |
| 2377 | // being launched is the same as the one making the call... or, as |
| 2378 | // a special case, if we do not know the caller then we count the |
| 2379 | // current top activity as the caller. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2380 | if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2381 | ActivityRecord checkedCaller = sourceRecord; |
| 2382 | if (checkedCaller == null) { |
| 2383 | checkedCaller = topRunningNonDelayedActivityLocked(notTop); |
| 2384 | } |
| 2385 | if (!checkedCaller.realActivity.equals(r.realActivity)) { |
| 2386 | // Caller is not the same as launcher, so always needed. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2387 | startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2388 | } |
| 2389 | } |
| 2390 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2391 | if (sourceRecord == null) { |
| 2392 | // This activity is not being started from another... in this |
| 2393 | // case we -always- start a new task. |
| 2394 | if ((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) { |
| 2395 | Slog.w(TAG, "startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: " |
| 2396 | + intent); |
| 2397 | launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK; |
| 2398 | } |
| 2399 | } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) { |
| 2400 | // The original activity who is starting us is running as a single |
| 2401 | // instance... this new activity it is starting must go on its |
| 2402 | // own task. |
| 2403 | launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK; |
| 2404 | } else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE |
| 2405 | || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) { |
| 2406 | // The activity being started is a single instance... it always |
| 2407 | // gets launched into its own task. |
| 2408 | launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK; |
| 2409 | } |
| 2410 | |
| 2411 | if (r.resultTo != null && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) { |
| 2412 | // For whatever reason this activity is being launched into a new |
| 2413 | // task... yet the caller has requested a result back. Well, that |
| 2414 | // is pretty messed up, so instead immediately send back a cancel |
| 2415 | // and let the new task continue launched as normal without a |
| 2416 | // dependency on its originator. |
| 2417 | Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result."); |
| 2418 | sendActivityResultLocked(-1, |
| 2419 | r.resultTo, r.resultWho, r.requestCode, |
| 2420 | Activity.RESULT_CANCELED, null); |
| 2421 | r.resultTo = null; |
| 2422 | } |
| 2423 | |
| 2424 | boolean addingToTask = false; |
Dianne Hackborn | 03fcc33 | 2012-05-15 12:49:40 -0700 | [diff] [blame] | 2425 | boolean movedHome = false; |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2426 | TaskRecord reuseTask = null; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2427 | if (((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 && |
| 2428 | (launchFlags&Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0) |
| 2429 | || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK |
| 2430 | || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) { |
| 2431 | // If bring to front is requested, and no result is requested, and |
| 2432 | // we can find a task that was started with this same |
| 2433 | // component, then instead of launching bring that one to the front. |
| 2434 | if (r.resultTo == null) { |
| 2435 | // See if there is a task to bring to the front. If this is |
| 2436 | // a SINGLE_INSTANCE activity, there can be one and only one |
| 2437 | // instance of it in the history, and it is always in its own |
| 2438 | // unique task, so we do a special search. |
| 2439 | ActivityRecord taskTop = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE |
| 2440 | ? findTaskLocked(intent, r.info) |
| 2441 | : findActivityLocked(intent, r.info); |
| 2442 | if (taskTop != null) { |
| 2443 | if (taskTop.task.intent == null) { |
| 2444 | // This task was started because of movement of |
| 2445 | // the activity based on affinity... now that we |
| 2446 | // are actually launching it, we can assign the |
| 2447 | // base intent. |
| 2448 | taskTop.task.setIntent(intent, r.info); |
| 2449 | } |
| 2450 | // If the target task is not in the front, then we need |
| 2451 | // to bring it to the front... except... well, with |
| 2452 | // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like |
| 2453 | // to have the same behavior as if a new instance was |
| 2454 | // being started, which means not bringing it to the front |
| 2455 | // if the caller is not itself in the front. |
| 2456 | ActivityRecord curTop = topRunningNonDelayedActivityLocked(notTop); |
Jean-Baptiste Queru | 66a5d69 | 2010-10-25 17:27:16 -0700 | [diff] [blame] | 2457 | if (curTop != null && curTop.task != taskTop.task) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2458 | r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT); |
| 2459 | boolean callerAtFront = sourceRecord == null |
| 2460 | || curTop.task == sourceRecord.task; |
| 2461 | if (callerAtFront) { |
| 2462 | // We really do want to push this one into the |
| 2463 | // user's face, right now. |
Dianne Hackborn | 03fcc33 | 2012-05-15 12:49:40 -0700 | [diff] [blame] | 2464 | movedHome = true; |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2465 | moveHomeToFrontFromLaunchLocked(launchFlags); |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2466 | moveTaskToFrontLocked(taskTop.task, r, options); |
Dianne Hackborn | 8437587 | 2012-06-01 19:03:50 -0700 | [diff] [blame] | 2467 | options = null; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2468 | } |
| 2469 | } |
| 2470 | // If the caller has requested that the target task be |
| 2471 | // reset, then do so. |
| 2472 | if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) { |
| 2473 | taskTop = resetTaskIfNeededLocked(taskTop, r); |
| 2474 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2475 | if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2476 | // We don't need to start a new activity, and |
| 2477 | // the client said not to do anything if that |
| 2478 | // is the case, so this is it! And for paranoia, make |
| 2479 | // sure we have correctly resumed the top activity. |
| 2480 | if (doResume) { |
Dianne Hackborn | 8437587 | 2012-06-01 19:03:50 -0700 | [diff] [blame] | 2481 | resumeTopActivityLocked(null, options); |
| 2482 | } else { |
| 2483 | ActivityOptions.abort(options); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2484 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2485 | return ActivityManager.START_RETURN_INTENT_TO_CALLER; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2486 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2487 | if ((launchFlags & |
| 2488 | (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) |
| 2489 | == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) { |
| 2490 | // The caller has requested to completely replace any |
Dianne Hackborn | 7da6ac3 | 2010-12-09 19:22:04 -0800 | [diff] [blame] | 2491 | // existing task with its new activity. Well that should |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2492 | // not be too hard... |
| 2493 | reuseTask = taskTop.task; |
Craig Mautner | b0f7dc7 | 2013-04-01 16:34:45 -0700 | [diff] [blame] | 2494 | taskTop.task.performClearTaskLocked(); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2495 | reuseTask.setIntent(r.intent, r.info); |
| 2496 | } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0 |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2497 | || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK |
| 2498 | || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) { |
| 2499 | // In this situation we want to remove all activities |
| 2500 | // from the task up to the one being started. In most |
| 2501 | // cases this means we are resetting the task to its |
| 2502 | // initial state. |
Craig Mautner | b0f7dc7 | 2013-04-01 16:34:45 -0700 | [diff] [blame] | 2503 | ActivityRecord top = taskTop.task.performClearTaskLocked(r, launchFlags); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2504 | if (top != null) { |
| 2505 | if (top.frontOfTask) { |
| 2506 | // Activity aliases may mean we use different |
| 2507 | // intents for the top activity, so make sure |
| 2508 | // the task now has the identity of the new |
| 2509 | // intent. |
| 2510 | top.task.setIntent(r.intent, r.info); |
| 2511 | } |
| 2512 | logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 2513 | top.deliverNewIntentLocked(callingUid, r.intent); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2514 | } else { |
| 2515 | // A special case: we need to |
| 2516 | // start the activity because it is not currently |
| 2517 | // running, and the caller has asked to clear the |
| 2518 | // current task to have this activity at the top. |
| 2519 | addingToTask = true; |
| 2520 | // Now pretend like this activity is being started |
| 2521 | // by the top of its task, so it is put in the |
| 2522 | // right place. |
| 2523 | sourceRecord = taskTop; |
| 2524 | } |
| 2525 | } else if (r.realActivity.equals(taskTop.task.realActivity)) { |
| 2526 | // In this case the top activity on the task is the |
| 2527 | // same as the one being launched, so we take that |
| 2528 | // as a request to bring the task to the foreground. |
| 2529 | // If the top activity in the task is the root |
| 2530 | // activity, deliver this new intent to it if it |
| 2531 | // desires. |
Johan Viktorsson | f363dfd | 2012-02-16 17:05:16 +0100 | [diff] [blame] | 2532 | if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 |
| 2533 | || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2534 | && taskTop.realActivity.equals(r.realActivity)) { |
| 2535 | logStartActivity(EventLogTags.AM_NEW_INTENT, r, taskTop.task); |
| 2536 | if (taskTop.frontOfTask) { |
| 2537 | taskTop.task.setIntent(r.intent, r.info); |
| 2538 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 2539 | taskTop.deliverNewIntentLocked(callingUid, r.intent); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2540 | } else if (!r.intent.filterEquals(taskTop.task.intent)) { |
| 2541 | // In this case we are launching the root activity |
| 2542 | // of the task, but with a different intent. We |
| 2543 | // should start a new instance on top. |
| 2544 | addingToTask = true; |
| 2545 | sourceRecord = taskTop; |
| 2546 | } |
| 2547 | } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) { |
| 2548 | // In this case an activity is being launched in to an |
| 2549 | // existing task, without resetting that task. This |
| 2550 | // is typically the situation of launching an activity |
| 2551 | // from a notification or shortcut. We want to place |
| 2552 | // the new activity on top of the current task. |
| 2553 | addingToTask = true; |
| 2554 | sourceRecord = taskTop; |
| 2555 | } else if (!taskTop.task.rootWasReset) { |
| 2556 | // In this case we are launching in to an existing task |
| 2557 | // that has not yet been started from its front door. |
| 2558 | // The current task has been brought to the front. |
| 2559 | // Ideally, we'd probably like to place this new task |
| 2560 | // at the bottom of its stack, but that's a little hard |
| 2561 | // to do with the current organization of the code so |
| 2562 | // for now we'll just drop it. |
| 2563 | taskTop.task.setIntent(r.intent, r.info); |
| 2564 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2565 | if (!addingToTask && reuseTask == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2566 | // We didn't do anything... but it was needed (a.k.a., client |
| 2567 | // don't use that intent!) And for paranoia, make |
| 2568 | // sure we have correctly resumed the top activity. |
| 2569 | if (doResume) { |
Dianne Hackborn | 8437587 | 2012-06-01 19:03:50 -0700 | [diff] [blame] | 2570 | resumeTopActivityLocked(null, options); |
| 2571 | } else { |
| 2572 | ActivityOptions.abort(options); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2573 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2574 | return ActivityManager.START_TASK_TO_FRONT; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2575 | } |
| 2576 | } |
| 2577 | } |
| 2578 | } |
| 2579 | |
| 2580 | //String uri = r.intent.toURI(); |
| 2581 | //Intent intent2 = new Intent(uri); |
| 2582 | //Slog.i(TAG, "Given intent: " + r.intent); |
| 2583 | //Slog.i(TAG, "URI is: " + uri); |
| 2584 | //Slog.i(TAG, "To intent: " + intent2); |
| 2585 | |
| 2586 | if (r.packageName != null) { |
| 2587 | // If the activity being launched is the same as the one currently |
| 2588 | // at the top, then we need to check if it should only be launched |
| 2589 | // once. |
| 2590 | ActivityRecord top = topRunningNonDelayedActivityLocked(notTop); |
| 2591 | if (top != null && r.resultTo == null) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2592 | if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2593 | if (top.app != null && top.app.thread != null) { |
| 2594 | if ((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 |
| 2595 | || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP |
| 2596 | || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) { |
| 2597 | logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task); |
| 2598 | // For paranoia, make sure we have correctly |
| 2599 | // resumed the top activity. |
| 2600 | if (doResume) { |
| 2601 | resumeTopActivityLocked(null); |
| 2602 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2603 | ActivityOptions.abort(options); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2604 | if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2605 | // We don't need to start a new activity, and |
| 2606 | // the client said not to do anything if that |
| 2607 | // is the case, so this is it! |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2608 | return ActivityManager.START_RETURN_INTENT_TO_CALLER; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2609 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 2610 | top.deliverNewIntentLocked(callingUid, r.intent); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2611 | return ActivityManager.START_DELIVERED_TO_TOP; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2612 | } |
| 2613 | } |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | } else { |
| 2618 | if (r.resultTo != null) { |
| 2619 | sendActivityResultLocked(-1, |
| 2620 | r.resultTo, r.resultWho, r.requestCode, |
| 2621 | Activity.RESULT_CANCELED, null); |
| 2622 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2623 | ActivityOptions.abort(options); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2624 | return ActivityManager.START_CLASS_NOT_FOUND; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2625 | } |
| 2626 | |
| 2627 | boolean newTask = false; |
Dianne Hackborn | 7da6ac3 | 2010-12-09 19:22:04 -0800 | [diff] [blame] | 2628 | boolean keepCurTransition = false; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2629 | |
| 2630 | // Should this be considered a new task? |
| 2631 | if (r.resultTo == null && !addingToTask |
| 2632 | && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2633 | if (reuseTask == null) { |
Craig Mautner | 8d341ef | 2013-03-26 09:03:27 -0700 | [diff] [blame] | 2634 | setTask(r, createTaskRecord(mStackSupervisor.getNextTaskId(), r.info, intent, |
| 2635 | true), null, true); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2636 | if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r |
| 2637 | + " in new task " + r.task); |
| 2638 | } else { |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 2639 | setTask(r, reuseTask, reuseTask, true); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2640 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2641 | newTask = true; |
Dianne Hackborn | 03fcc33 | 2012-05-15 12:49:40 -0700 | [diff] [blame] | 2642 | if (!movedHome) { |
| 2643 | moveHomeToFrontFromLaunchLocked(launchFlags); |
| 2644 | } |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 2645 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2646 | } else if (sourceRecord != null) { |
| 2647 | if (!addingToTask && |
| 2648 | (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) { |
| 2649 | // In this case, we are adding the activity to an existing |
| 2650 | // task, but the caller has asked to clear that task if the |
| 2651 | // activity is already running. |
Craig Mautner | b0f7dc7 | 2013-04-01 16:34:45 -0700 | [diff] [blame] | 2652 | ActivityRecord top = sourceRecord.task.performClearTaskLocked(r, launchFlags); |
Dianne Hackborn | 7da6ac3 | 2010-12-09 19:22:04 -0800 | [diff] [blame] | 2653 | keepCurTransition = true; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2654 | if (top != null) { |
| 2655 | logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 2656 | top.deliverNewIntentLocked(callingUid, r.intent); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2657 | // For paranoia, make sure we have correctly |
| 2658 | // resumed the top activity. |
| 2659 | if (doResume) { |
| 2660 | resumeTopActivityLocked(null); |
| 2661 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2662 | ActivityOptions.abort(options); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2663 | return ActivityManager.START_DELIVERED_TO_TOP; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2664 | } |
| 2665 | } else if (!addingToTask && |
| 2666 | (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) { |
| 2667 | // In this case, we are launching an activity in our own task |
| 2668 | // that may already be running somewhere in the history, and |
| 2669 | // we want to shuffle it to the front of the stack if so. |
Craig Mautner | 56f52db | 2013-02-25 10:03:01 -0800 | [diff] [blame] | 2670 | final ActivityRecord top = findActivityInHistoryLocked(r, sourceRecord.task); |
| 2671 | if (top != null) { |
| 2672 | moveActivityToFrontLocked(top); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2673 | logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task); |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 2674 | top.updateOptionsLocked(options); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 2675 | top.deliverNewIntentLocked(callingUid, r.intent); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2676 | if (doResume) { |
| 2677 | resumeTopActivityLocked(null); |
| 2678 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2679 | return ActivityManager.START_DELIVERED_TO_TOP; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2680 | } |
| 2681 | } |
| 2682 | // An existing activity is starting this new activity, so we want |
| 2683 | // to keep the new one in the same task as the one that is starting |
| 2684 | // it. |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 2685 | setTask(r, sourceRecord.task, sourceRecord.thumbHolder, false); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2686 | if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r |
| 2687 | + " in existing task " + r.task); |
| 2688 | |
| 2689 | } else { |
| 2690 | // This not being started from an existing activity, and not part |
| 2691 | // of a new task... just put it in the top task, though these days |
| 2692 | // this case should never happen. |
Craig Mautner | 9658b31 | 2013-02-28 10:55:59 -0800 | [diff] [blame] | 2693 | ActivityRecord prev = null; |
| 2694 | // Iterate to find the first non-empty task stack. Note that this code can |
| 2695 | // go away once we stop storing tasks with empty mActivities lists. |
| 2696 | for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { |
| 2697 | ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities; |
| 2698 | for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) { |
| 2699 | prev = activities.get(activityNdx); |
| 2700 | break; |
| 2701 | } |
| 2702 | } |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 2703 | setTask(r, prev != null |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2704 | ? prev.task |
Craig Mautner | 8d341ef | 2013-03-26 09:03:27 -0700 | [diff] [blame] | 2705 | : createTaskRecord(mStackSupervisor.getNextTaskId(), r.info, intent, true), |
| 2706 | null, true); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2707 | if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r |
| 2708 | + " in new guessed " + r.task); |
| 2709 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 2710 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 2711 | mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName, |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 2712 | intent, r.getUriPermissionsLocked()); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 2713 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2714 | if (newTask) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 2715 | EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2716 | } |
| 2717 | logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task); |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 2718 | startActivityLocked(r, newTask, doResume, keepCurTransition, options); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2719 | return ActivityManager.START_SUCCESS; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2720 | } |
| 2721 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2722 | void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r, |
| 2723 | long thisTime, long totalTime) { |
| 2724 | for (int i=mWaitingActivityLaunched.size()-1; i>=0; i--) { |
| 2725 | WaitResult w = mWaitingActivityLaunched.get(i); |
| 2726 | w.timeout = timeout; |
| 2727 | if (r != null) { |
| 2728 | w.who = new ComponentName(r.info.packageName, r.info.name); |
| 2729 | } |
| 2730 | w.thisTime = thisTime; |
| 2731 | w.totalTime = totalTime; |
| 2732 | } |
| 2733 | mService.notifyAll(); |
| 2734 | } |
Craig Mautner | 23ac33b | 2013-04-01 16:26:35 -0700 | [diff] [blame] | 2735 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2736 | void reportActivityVisibleLocked(ActivityRecord r) { |
| 2737 | for (int i=mWaitingActivityVisible.size()-1; i>=0; i--) { |
| 2738 | WaitResult w = mWaitingActivityVisible.get(i); |
| 2739 | w.timeout = false; |
| 2740 | if (r != null) { |
| 2741 | w.who = new ComponentName(r.info.packageName, r.info.name); |
| 2742 | } |
| 2743 | w.totalTime = SystemClock.uptimeMillis() - w.thisTime; |
| 2744 | w.thisTime = w.totalTime; |
| 2745 | } |
| 2746 | mService.notifyAll(); |
Craig Mautner | 2708430 | 2013-03-25 08:05:25 -0700 | [diff] [blame] | 2747 | mStackSupervisor.dismissKeyguard(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2748 | } |
| 2749 | |
| 2750 | void sendActivityResultLocked(int callingUid, ActivityRecord r, |
| 2751 | String resultWho, int requestCode, int resultCode, Intent data) { |
| 2752 | |
| 2753 | if (callingUid > 0) { |
| 2754 | mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName, |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 2755 | data, r.getUriPermissionsLocked()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2756 | } |
| 2757 | |
| 2758 | if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r |
| 2759 | + " : who=" + resultWho + " req=" + requestCode |
| 2760 | + " res=" + resultCode + " data=" + data); |
| 2761 | if (mResumedActivity == r && r.app != null && r.app.thread != null) { |
| 2762 | try { |
| 2763 | ArrayList<ResultInfo> list = new ArrayList<ResultInfo>(); |
| 2764 | list.add(new ResultInfo(resultWho, requestCode, |
| 2765 | resultCode, data)); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2766 | r.app.thread.scheduleSendResult(r.appToken, list); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2767 | return; |
| 2768 | } catch (Exception e) { |
| 2769 | Slog.w(TAG, "Exception thrown sending result to " + r, e); |
| 2770 | } |
| 2771 | } |
| 2772 | |
| 2773 | r.addResultLocked(null, resultWho, requestCode, resultCode, data); |
| 2774 | } |
| 2775 | |
| 2776 | private final void stopActivityLocked(ActivityRecord r) { |
| 2777 | if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r); |
| 2778 | if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0 |
| 2779 | || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) { |
| 2780 | if (!r.finishing) { |
Christopher Tate | d3f175c | 2012-06-14 14:16:54 -0700 | [diff] [blame] | 2781 | if (!mService.mSleeping) { |
| 2782 | if (DEBUG_STATES) { |
| 2783 | Slog.d(TAG, "no-history finish of " + r); |
| 2784 | } |
| 2785 | requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 2786 | "no-history", false); |
Christopher Tate | d3f175c | 2012-06-14 14:16:54 -0700 | [diff] [blame] | 2787 | } else { |
| 2788 | if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r |
| 2789 | + " on stop because we're just sleeping"); |
| 2790 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2791 | } |
Christopher Tate | 5007ddd | 2012-06-12 13:08:18 -0700 | [diff] [blame] | 2792 | } |
| 2793 | |
| 2794 | if (r.app != null && r.app.thread != null) { |
Craig Mautner | 2219a1b | 2013-03-25 09:44:30 -0700 | [diff] [blame] | 2795 | if (mStackSupervisor.isMainStack(this)) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2796 | if (mService.mFocusedActivity == r) { |
| 2797 | mService.setFocusedActivityLocked(topRunningActivityLocked(null)); |
| 2798 | } |
| 2799 | } |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 2800 | r.resumeKeyDispatchingLocked(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2801 | try { |
| 2802 | r.stopped = false; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2803 | if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r |
| 2804 | + " (stop requested)"); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2805 | r.state = ActivityState.STOPPING; |
| 2806 | if (DEBUG_VISBILITY) Slog.v( |
| 2807 | TAG, "Stopping visible=" + r.visible + " for " + r); |
| 2808 | if (!r.visible) { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2809 | mService.mWindowManager.setAppVisibility(r.appToken, false); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2810 | } |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2811 | r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags); |
Craig Mautner | e11f2b7 | 2013-04-01 12:37:17 -0700 | [diff] [blame] | 2812 | if (mService.isSleepingOrShuttingDown()) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 2813 | r.setSleeping(true); |
| 2814 | } |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 2815 | Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG); |
| 2816 | msg.obj = r; |
| 2817 | mHandler.sendMessageDelayed(msg, STOP_TIMEOUT); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2818 | } catch (Exception e) { |
| 2819 | // Maybe just ignore exceptions here... if the process |
| 2820 | // has crashed, our death notification will clean things |
| 2821 | // up. |
| 2822 | Slog.w(TAG, "Exception thrown during pause", e); |
| 2823 | // Just in case, assume it to be stopped. |
| 2824 | r.stopped = true; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2825 | if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2826 | r.state = ActivityState.STOPPED; |
| 2827 | if (r.configDestroy) { |
Dianne Hackborn | 28695e0 | 2011-11-02 21:59:51 -0700 | [diff] [blame] | 2828 | destroyActivityLocked(r, true, false, "stop-except"); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2829 | } |
| 2830 | } |
| 2831 | } |
| 2832 | } |
Craig Mautner | 23ac33b | 2013-04-01 16:26:35 -0700 | [diff] [blame] | 2833 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2834 | final ArrayList<ActivityRecord> processStoppingActivitiesLocked( |
| 2835 | boolean remove) { |
| 2836 | int N = mStoppingActivities.size(); |
| 2837 | if (N <= 0) return null; |
| 2838 | |
| 2839 | ArrayList<ActivityRecord> stops = null; |
| 2840 | |
| 2841 | final boolean nowVisible = mResumedActivity != null |
| 2842 | && mResumedActivity.nowVisible |
| 2843 | && !mResumedActivity.waitingVisible; |
| 2844 | for (int i=0; i<N; i++) { |
| 2845 | ActivityRecord s = mStoppingActivities.get(i); |
| 2846 | if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible=" |
| 2847 | + nowVisible + " waitingVisible=" + s.waitingVisible |
| 2848 | + " finishing=" + s.finishing); |
| 2849 | if (s.waitingVisible && nowVisible) { |
| 2850 | mWaitingVisibleActivities.remove(s); |
| 2851 | s.waitingVisible = false; |
| 2852 | if (s.finishing) { |
| 2853 | // If this activity is finishing, it is sitting on top of |
| 2854 | // everyone else but we now know it is no longer needed... |
| 2855 | // so get rid of it. Otherwise, we need to go through the |
| 2856 | // normal flow and hide it once we determine that it is |
| 2857 | // hidden by the activities in front of it. |
| 2858 | if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2859 | mService.mWindowManager.setAppVisibility(s.appToken, false); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2860 | } |
| 2861 | } |
Craig Mautner | e11f2b7 | 2013-04-01 12:37:17 -0700 | [diff] [blame] | 2862 | if ((!s.waitingVisible || mService.isSleepingOrShuttingDown()) && remove) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2863 | if (localLOGV) Slog.v(TAG, "Ready to stop: " + s); |
| 2864 | if (stops == null) { |
| 2865 | stops = new ArrayList<ActivityRecord>(); |
| 2866 | } |
| 2867 | stops.add(s); |
| 2868 | mStoppingActivities.remove(i); |
| 2869 | N--; |
| 2870 | i--; |
| 2871 | } |
| 2872 | } |
| 2873 | |
| 2874 | return stops; |
| 2875 | } |
| 2876 | |
Dianne Hackborn | 80a7ac1 | 2011-09-22 18:32:52 -0700 | [diff] [blame] | 2877 | final void scheduleIdleLocked() { |
| 2878 | Message msg = Message.obtain(); |
| 2879 | msg.what = IDLE_NOW_MSG; |
| 2880 | mHandler.sendMessage(msg); |
| 2881 | } |
| 2882 | |
Craig Mautner | e79d4268 | 2013-04-01 19:01:53 -0700 | [diff] [blame^] | 2883 | // Checked. |
| 2884 | final ActivityRecord activityIdleInternalLocked(IBinder token, boolean fromTimeout, |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2885 | Configuration config) { |
| 2886 | if (localLOGV) Slog.v(TAG, "Activity idle: " + token); |
| 2887 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2888 | ActivityRecord res = null; |
| 2889 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2890 | ArrayList<ActivityRecord> stops = null; |
| 2891 | ArrayList<ActivityRecord> finishes = null; |
| 2892 | ArrayList<ActivityRecord> thumbnails = null; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 2893 | ArrayList<UserStartedState> startingUsers = null; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2894 | int NS = 0; |
| 2895 | int NF = 0; |
| 2896 | int NT = 0; |
| 2897 | IApplicationThread sendThumbnail = null; |
| 2898 | boolean booting = false; |
| 2899 | boolean enableScreen = false; |
Dianne Hackborn | 42e620c | 2012-06-24 13:20:51 -0700 | [diff] [blame] | 2900 | boolean activityRemoved = false; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2901 | |
Craig Mautner | e79d4268 | 2013-04-01 19:01:53 -0700 | [diff] [blame^] | 2902 | ActivityRecord r = ActivityRecord.forToken(token); |
| 2903 | if (r != null) { |
| 2904 | mHandler.removeMessages(IDLE_TIMEOUT_MSG, r); |
| 2905 | r.finishLaunchTickingLocked(); |
| 2906 | } |
| 2907 | |
| 2908 | // Get the activity record. |
| 2909 | if (isInStackLocked(token) != null) { |
| 2910 | res = r; |
| 2911 | |
| 2912 | if (fromTimeout) { |
| 2913 | reportActivityLaunchedLocked(fromTimeout, r, -1, -1); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2914 | } |
| 2915 | |
Craig Mautner | e79d4268 | 2013-04-01 19:01:53 -0700 | [diff] [blame^] | 2916 | // This is a hack to semi-deal with a race condition |
| 2917 | // in the client where it can be constructed with a |
| 2918 | // newer configuration from when we asked it to launch. |
| 2919 | // We'll update with whatever configuration it now says |
| 2920 | // it used to launch. |
| 2921 | if (config != null) { |
| 2922 | r.configuration = config; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2923 | } |
| 2924 | |
Craig Mautner | e79d4268 | 2013-04-01 19:01:53 -0700 | [diff] [blame^] | 2925 | // No longer need to keep the device awake. |
| 2926 | if (mResumedActivity == r && mLaunchingActivity.isHeld()) { |
| 2927 | mHandler.removeMessages(LAUNCH_TIMEOUT_MSG); |
| 2928 | mLaunchingActivity.release(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2929 | } |
| 2930 | |
Craig Mautner | e79d4268 | 2013-04-01 19:01:53 -0700 | [diff] [blame^] | 2931 | // We are now idle. If someone is waiting for a thumbnail from |
| 2932 | // us, we can now deliver. |
| 2933 | r.idle = true; |
| 2934 | mService.scheduleAppGcsLocked(); |
| 2935 | if (r.thumbnailNeeded && r.app != null && r.app.thread != null) { |
| 2936 | sendThumbnail = r.app.thread; |
| 2937 | r.thumbnailNeeded = false; |
| 2938 | } |
| 2939 | |
| 2940 | // If this activity is fullscreen, set up to hide those under it. |
| 2941 | |
| 2942 | if (DEBUG_VISBILITY) Slog.v(TAG, "Idle activity for " + r); |
| 2943 | ensureActivitiesVisibleLocked(null, 0); |
| 2944 | |
| 2945 | //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout); |
Craig Mautner | 2219a1b | 2013-03-25 09:44:30 -0700 | [diff] [blame] | 2946 | if (mStackSupervisor.isMainStack(this)) { |
Craig Mautner | e79d4268 | 2013-04-01 19:01:53 -0700 | [diff] [blame^] | 2947 | if (!mService.mBooted) { |
| 2948 | mService.mBooted = true; |
| 2949 | enableScreen = true; |
| 2950 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2951 | } |
Craig Mautner | e79d4268 | 2013-04-01 19:01:53 -0700 | [diff] [blame^] | 2952 | } else if (fromTimeout) { |
| 2953 | reportActivityLaunchedLocked(fromTimeout, null, -1, -1); |
| 2954 | } |
| 2955 | |
| 2956 | // Atomically retrieve all of the other things to do. |
| 2957 | stops = processStoppingActivitiesLocked(true); |
| 2958 | NS = stops != null ? stops.size() : 0; |
| 2959 | if ((NF=mFinishingActivities.size()) > 0) { |
| 2960 | finishes = new ArrayList<ActivityRecord>(mFinishingActivities); |
| 2961 | mFinishingActivities.clear(); |
| 2962 | } |
| 2963 | if ((NT=mCancelledThumbnails.size()) > 0) { |
| 2964 | thumbnails = new ArrayList<ActivityRecord>(mCancelledThumbnails); |
| 2965 | mCancelledThumbnails.clear(); |
| 2966 | } |
| 2967 | |
| 2968 | if (mStackSupervisor.isMainStack(this)) { |
| 2969 | booting = mService.mBooting; |
| 2970 | mService.mBooting = false; |
| 2971 | } |
| 2972 | if (mStartingUsers.size() > 0) { |
| 2973 | startingUsers = new ArrayList<UserStartedState>(mStartingUsers); |
| 2974 | mStartingUsers.clear(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2975 | } |
| 2976 | |
| 2977 | int i; |
| 2978 | |
| 2979 | // Send thumbnail if requested. |
| 2980 | if (sendThumbnail != null) { |
| 2981 | try { |
| 2982 | sendThumbnail.requestThumbnail(token); |
| 2983 | } catch (Exception e) { |
| 2984 | Slog.w(TAG, "Exception thrown when requesting thumbnail", e); |
| 2985 | mService.sendPendingThumbnail(null, token, null, null, true); |
| 2986 | } |
| 2987 | } |
| 2988 | |
| 2989 | // Stop any activities that are scheduled to do so but have been |
| 2990 | // waiting for the next one to start. |
| 2991 | for (i=0; i<NS; i++) { |
Craig Mautner | e79d4268 | 2013-04-01 19:01:53 -0700 | [diff] [blame^] | 2992 | r = stops.get(i); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2993 | synchronized (mService) { |
| 2994 | if (r.finishing) { |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 2995 | finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2996 | } else { |
| 2997 | stopActivityLocked(r); |
| 2998 | } |
| 2999 | } |
| 3000 | } |
| 3001 | |
| 3002 | // Finish any activities that are scheduled to do so but have been |
| 3003 | // waiting for the next one to start. |
| 3004 | for (i=0; i<NF; i++) { |
Craig Mautner | e79d4268 | 2013-04-01 19:01:53 -0700 | [diff] [blame^] | 3005 | r = finishes.get(i); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3006 | synchronized (mService) { |
Dianne Hackborn | 42e620c | 2012-06-24 13:20:51 -0700 | [diff] [blame] | 3007 | activityRemoved = destroyActivityLocked(r, true, false, "finish-idle"); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3008 | } |
| 3009 | } |
| 3010 | |
| 3011 | // Report back to any thumbnail receivers. |
| 3012 | for (i=0; i<NT; i++) { |
Craig Mautner | e79d4268 | 2013-04-01 19:01:53 -0700 | [diff] [blame^] | 3013 | r = thumbnails.get(i); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3014 | mService.sendPendingThumbnail(r, null, null, null, true); |
| 3015 | } |
| 3016 | |
| 3017 | if (booting) { |
| 3018 | mService.finishBooting(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3019 | } else if (startingUsers != null) { |
| 3020 | for (i=0; i<startingUsers.size(); i++) { |
| 3021 | mService.finishUserSwitch(startingUsers.get(i)); |
| 3022 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3023 | } |
| 3024 | |
| 3025 | mService.trimApplications(); |
| 3026 | //dump(); |
| 3027 | //mWindowManager.dump(); |
| 3028 | |
| 3029 | if (enableScreen) { |
| 3030 | mService.enableScreenAfterBoot(); |
| 3031 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3032 | |
Dianne Hackborn | 42e620c | 2012-06-24 13:20:51 -0700 | [diff] [blame] | 3033 | if (activityRemoved) { |
| 3034 | resumeTopActivityLocked(null); |
| 3035 | } |
| 3036 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3037 | return res; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3038 | } |
| 3039 | |
| 3040 | /** |
| 3041 | * @return Returns true if the activity is being finished, false if for |
| 3042 | * some reason it is being left as-is. |
| 3043 | */ |
| 3044 | final boolean requestFinishActivityLocked(IBinder token, int resultCode, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 3045 | Intent resultData, String reason, boolean oomAdj) { |
Craig Mautner | d74f7d7 | 2013-02-26 13:41:02 -0800 | [diff] [blame] | 3046 | ActivityRecord r = isInStackLocked(token); |
Christopher Tate | d3f175c | 2012-06-14 14:16:54 -0700 | [diff] [blame] | 3047 | if (DEBUG_RESULTS || DEBUG_STATES) Slog.v( |
Craig Mautner | d74f7d7 | 2013-02-26 13:41:02 -0800 | [diff] [blame] | 3048 | TAG, "Finishing activity token=" + token + " r=" |
Christopher Tate | d3f175c | 2012-06-14 14:16:54 -0700 | [diff] [blame] | 3049 | + ", result=" + resultCode + ", data=" + resultData |
| 3050 | + ", reason=" + reason); |
Craig Mautner | d74f7d7 | 2013-02-26 13:41:02 -0800 | [diff] [blame] | 3051 | if (r == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3052 | return false; |
| 3053 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3054 | |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 3055 | finishActivityLocked(r, resultCode, resultData, reason, oomAdj); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3056 | return true; |
| 3057 | } |
| 3058 | |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 3059 | final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) { |
Craig Mautner | 9658b31 | 2013-02-28 10:55:59 -0800 | [diff] [blame] | 3060 | for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { |
| 3061 | ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities; |
| 3062 | for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) { |
| 3063 | ActivityRecord r = activities.get(activityNdx); |
| 3064 | if (r.resultTo == self && r.requestCode == requestCode) { |
| 3065 | if ((r.resultWho == null && resultWho == null) || |
| 3066 | (r.resultWho != null && r.resultWho.equals(resultWho))) { |
| 3067 | finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub", |
| 3068 | false); |
| 3069 | } |
Dianne Hackborn | ecc5a9c | 2012-04-26 18:56:09 -0700 | [diff] [blame] | 3070 | } |
| 3071 | } |
| 3072 | } |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 3073 | mService.updateOomAdjLocked(); |
Dianne Hackborn | ecc5a9c | 2012-04-26 18:56:09 -0700 | [diff] [blame] | 3074 | } |
| 3075 | |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 3076 | final void finishTopRunningActivityLocked(ProcessRecord app) { |
| 3077 | ActivityRecord r = topRunningActivityLocked(null); |
| 3078 | if (r != null && r.app == app) { |
| 3079 | // If the top running activity is from this crashing |
| 3080 | // process, then terminate it to avoid getting in a loop. |
| 3081 | Slog.w(TAG, " Force finishing activity " |
| 3082 | + r.intent.getComponent().flattenToShortString()); |
Craig Mautner | 9658b31 | 2013-02-28 10:55:59 -0800 | [diff] [blame] | 3083 | int taskNdx = mTaskHistory.indexOf(r.task); |
| 3084 | int activityNdx = r.task.mActivities.indexOf(r); |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 3085 | finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false); |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 3086 | // Also terminate any activities below it that aren't yet |
| 3087 | // stopped, to avoid a situation where one will get |
| 3088 | // re-start our crashing activity once it gets resumed again. |
Craig Mautner | 9658b31 | 2013-02-28 10:55:59 -0800 | [diff] [blame] | 3089 | --activityNdx; |
| 3090 | if (activityNdx < 0) { |
| 3091 | do { |
| 3092 | --taskNdx; |
| 3093 | if (taskNdx < 0) { |
| 3094 | break; |
| 3095 | } |
| 3096 | activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1; |
| 3097 | } while (activityNdx < 0); |
| 3098 | } |
| 3099 | if (activityNdx >= 0) { |
| 3100 | r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx); |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 3101 | if (r.state == ActivityState.RESUMED |
| 3102 | || r.state == ActivityState.PAUSING |
| 3103 | || r.state == ActivityState.PAUSED) { |
| 3104 | if (!r.isHomeActivity || mService.mHomeProcess != r.app) { |
| 3105 | Slog.w(TAG, " Force finishing activity " |
| 3106 | + r.intent.getComponent().flattenToShortString()); |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 3107 | finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false); |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 3108 | } |
| 3109 | } |
| 3110 | } |
| 3111 | } |
| 3112 | } |
| 3113 | |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 3114 | final boolean finishActivityAffinityLocked(ActivityRecord r) { |
Craig Mautner | d74f7d7 | 2013-02-26 13:41:02 -0800 | [diff] [blame] | 3115 | ArrayList<ActivityRecord> activities = r.task.mActivities; |
| 3116 | for (int index = activities.indexOf(r); index >= 0; --index) { |
| 3117 | ActivityRecord cur = activities.get(index); |
| 3118 | if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) { |
Dianne Hackborn | ecc5a9c | 2012-04-26 18:56:09 -0700 | [diff] [blame] | 3119 | break; |
| 3120 | } |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 3121 | finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", |
| 3122 | true); |
Dianne Hackborn | ecc5a9c | 2012-04-26 18:56:09 -0700 | [diff] [blame] | 3123 | } |
| 3124 | return true; |
| 3125 | } |
| 3126 | |
Dianne Hackborn | 5c60743 | 2012-02-28 14:44:19 -0800 | [diff] [blame] | 3127 | final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) { |
| 3128 | // send the result |
| 3129 | ActivityRecord resultTo = r.resultTo; |
| 3130 | if (resultTo != null) { |
| 3131 | if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo |
| 3132 | + " who=" + r.resultWho + " req=" + r.requestCode |
| 3133 | + " res=" + resultCode + " data=" + resultData); |
| 3134 | if (r.info.applicationInfo.uid > 0) { |
| 3135 | mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid, |
| 3136 | resultTo.packageName, resultData, |
| 3137 | resultTo.getUriPermissionsLocked()); |
| 3138 | } |
| 3139 | resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode, |
| 3140 | resultData); |
| 3141 | r.resultTo = null; |
| 3142 | } |
| 3143 | else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r); |
| 3144 | |
| 3145 | // Make sure this HistoryRecord is not holding on to other resources, |
| 3146 | // because clients have remote IPC references to this object so we |
| 3147 | // can't assume that will go away and want to avoid circular IPC refs. |
| 3148 | r.results = null; |
| 3149 | r.pendingResults = null; |
| 3150 | r.newIntents = null; |
| 3151 | r.icicle = null; |
| 3152 | } |
| 3153 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3154 | /** |
| 3155 | * @return Returns true if this activity has been removed from the history |
| 3156 | * list, or false if it is still in the list and will be removed later. |
| 3157 | */ |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 3158 | final boolean finishActivityLocked(ActivityRecord r, int resultCode, |
| 3159 | Intent resultData, String reason, boolean oomAdj) { |
| 3160 | return finishActivityLocked(r, resultCode, resultData, reason, false, oomAdj); |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 3161 | } |
| 3162 | |
| 3163 | /** |
| 3164 | * @return Returns true if this activity has been removed from the history |
| 3165 | * list, or false if it is still in the list and will be removed later. |
| 3166 | */ |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 3167 | final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData, |
| 3168 | String reason, boolean immediate, boolean oomAdj) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3169 | if (r.finishing) { |
| 3170 | Slog.w(TAG, "Duplicate finish request for " + r); |
| 3171 | return false; |
| 3172 | } |
| 3173 | |
Dianne Hackborn | 94cb2eb | 2011-01-13 21:09:44 -0800 | [diff] [blame] | 3174 | r.makeFinishing(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3175 | EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 3176 | r.userId, System.identityHashCode(r), |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3177 | r.task.taskId, r.shortComponentName, reason); |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 3178 | final ArrayList<ActivityRecord> activities = r.task.mActivities; |
| 3179 | final int index = activities.indexOf(r); |
| 3180 | if (index < (activities.size() - 1)) { |
| 3181 | ActivityRecord next = activities.get(index+1); |
| 3182 | if (r.frontOfTask) { |
| 3183 | // The next activity is now the front of the task. |
| 3184 | next.frontOfTask = true; |
| 3185 | } |
| 3186 | if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) { |
| 3187 | // If the caller asked that this activity (and all above it) |
| 3188 | // be cleared when the task is reset, don't lose that information, |
| 3189 | // but propagate it up to the next activity. |
| 3190 | next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3191 | } |
| 3192 | } |
| 3193 | |
| 3194 | r.pauseKeyDispatchingLocked(); |
Craig Mautner | 2219a1b | 2013-03-25 09:44:30 -0700 | [diff] [blame] | 3195 | if (mStackSupervisor.isMainStack(this)) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3196 | if (mService.mFocusedActivity == r) { |
| 3197 | mService.setFocusedActivityLocked(topRunningActivityLocked(null)); |
| 3198 | } |
| 3199 | } |
| 3200 | |
Dianne Hackborn | 5c60743 | 2012-02-28 14:44:19 -0800 | [diff] [blame] | 3201 | finishActivityResultsLocked(r, resultCode, resultData); |
Craig Mautner | 2420ead | 2013-04-01 17:13:20 -0700 | [diff] [blame] | 3202 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3203 | if (mService.mPendingThumbnails.size() > 0) { |
| 3204 | // There are clients waiting to receive thumbnails so, in case |
| 3205 | // this is an activity that someone is waiting for, add it |
| 3206 | // to the pending list so we can correctly update the clients. |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 3207 | mCancelledThumbnails.add(r); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3208 | } |
| 3209 | |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 3210 | if (immediate) { |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 3211 | return finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, oomAdj) == null; |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 3212 | } else if (mResumedActivity == r) { |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 3213 | boolean endTask = index <= 0; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3214 | if (DEBUG_TRANSITION) Slog.v(TAG, |
| 3215 | "Prepare close transition: finishing " + r); |
| 3216 | mService.mWindowManager.prepareAppTransition(endTask |
Craig Mautner | 4b71aa1 | 2012-12-27 17:20:01 -0800 | [diff] [blame] | 3217 | ? AppTransition.TRANSIT_TASK_CLOSE |
| 3218 | : AppTransition.TRANSIT_ACTIVITY_CLOSE, false); |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 3219 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3220 | // Tell window manager to prepare for this one to be removed. |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 3221 | mService.mWindowManager.setAppVisibility(r.appToken, false); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3222 | |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 3223 | if (mPausingActivity == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3224 | if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r); |
| 3225 | if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false"); |
| 3226 | startPausingLocked(false, false); |
| 3227 | } |
| 3228 | |
| 3229 | } else if (r.state != ActivityState.PAUSING) { |
| 3230 | // If the activity is PAUSING, we will complete the finish once |
| 3231 | // it is done pausing; else we can just directly finish it here. |
| 3232 | if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r); |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 3233 | return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3234 | } else { |
| 3235 | if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r); |
| 3236 | } |
| 3237 | |
| 3238 | return false; |
| 3239 | } |
| 3240 | |
| 3241 | private static final int FINISH_IMMEDIATELY = 0; |
| 3242 | private static final int FINISH_AFTER_PAUSE = 1; |
| 3243 | private static final int FINISH_AFTER_VISIBLE = 2; |
| 3244 | |
| 3245 | private final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 3246 | int mode, boolean oomAdj) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3247 | // First things first: if this activity is currently visible, |
| 3248 | // and the resumed activity is not yet visible, then hold off on |
| 3249 | // finishing until the resumed one becomes visible. |
| 3250 | if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) { |
| 3251 | if (!mStoppingActivities.contains(r)) { |
| 3252 | mStoppingActivities.add(r); |
| 3253 | if (mStoppingActivities.size() > 3) { |
| 3254 | // If we already have a few activities waiting to stop, |
| 3255 | // then give up on things going idle and start clearing |
| 3256 | // them out. |
Dianne Hackborn | 80a7ac1 | 2011-09-22 18:32:52 -0700 | [diff] [blame] | 3257 | scheduleIdleLocked(); |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 3258 | } else { |
| 3259 | checkReadyForSleepLocked(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3260 | } |
| 3261 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 3262 | if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r |
| 3263 | + " (finish requested)"); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3264 | r.state = ActivityState.STOPPING; |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 3265 | if (oomAdj) { |
| 3266 | mService.updateOomAdjLocked(); |
| 3267 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3268 | return r; |
| 3269 | } |
| 3270 | |
| 3271 | // make sure the record is cleaned out of other places. |
| 3272 | mStoppingActivities.remove(r); |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 3273 | mGoingToSleepActivities.remove(r); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3274 | mWaitingVisibleActivities.remove(r); |
| 3275 | if (mResumedActivity == r) { |
| 3276 | mResumedActivity = null; |
| 3277 | } |
| 3278 | final ActivityState prevState = r.state; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 3279 | if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3280 | r.state = ActivityState.FINISHING; |
| 3281 | |
| 3282 | if (mode == FINISH_IMMEDIATELY |
| 3283 | || prevState == ActivityState.STOPPED |
| 3284 | || prevState == ActivityState.INITIALIZING) { |
| 3285 | // If this activity is already stopped, we can just finish |
| 3286 | // it right now. |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 3287 | boolean activityRemoved = destroyActivityLocked(r, true, |
| 3288 | oomAdj, "finish-imm"); |
Dianne Hackborn | 42e620c | 2012-06-24 13:20:51 -0700 | [diff] [blame] | 3289 | if (activityRemoved) { |
| 3290 | resumeTopActivityLocked(null); |
| 3291 | } |
| 3292 | return activityRemoved ? null : r; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3293 | } |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 3294 | |
| 3295 | // Need to go through the full pause cycle to get this |
| 3296 | // activity into the stopped state and then finish it. |
| 3297 | if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r); |
| 3298 | mFinishingActivities.add(r); |
| 3299 | resumeTopActivityLocked(null); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3300 | return r; |
| 3301 | } |
| 3302 | |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 3303 | final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode, |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 3304 | Intent resultData) { |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 3305 | final ActivityRecord srec = ActivityRecord.forToken(token); |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 3306 | final TaskRecord task = srec.task; |
| 3307 | final ArrayList<ActivityRecord> activities = task.mActivities; |
| 3308 | final int start = activities.indexOf(srec); |
| 3309 | if (!mTaskHistory.contains(task) || (start < 0)) { |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 3310 | return false; |
| 3311 | } |
| 3312 | int finishTo = start - 1; |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 3313 | ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo); |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 3314 | boolean foundParentInTask = false; |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 3315 | final ComponentName dest = destIntent.getComponent(); |
| 3316 | if (start > 0 && dest != null) { |
| 3317 | for (int i = finishTo; i >= 0; i--) { |
| 3318 | ActivityRecord r = activities.get(i); |
| 3319 | if (r.info.packageName.equals(dest.getPackageName()) && |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 3320 | r.info.name.equals(dest.getClassName())) { |
| 3321 | finishTo = i; |
| 3322 | parent = r; |
| 3323 | foundParentInTask = true; |
| 3324 | break; |
| 3325 | } |
| 3326 | } |
| 3327 | } |
| 3328 | |
| 3329 | IActivityController controller = mService.mController; |
| 3330 | if (controller != null) { |
| 3331 | ActivityRecord next = topRunningActivityLocked(srec.appToken, 0); |
| 3332 | if (next != null) { |
| 3333 | // ask watcher if this is allowed |
| 3334 | boolean resumeOK = true; |
| 3335 | try { |
| 3336 | resumeOK = controller.activityResuming(next.packageName); |
| 3337 | } catch (RemoteException e) { |
| 3338 | mService.mController = null; |
| 3339 | } |
| 3340 | |
| 3341 | if (!resumeOK) { |
| 3342 | return false; |
| 3343 | } |
| 3344 | } |
| 3345 | } |
| 3346 | final long origId = Binder.clearCallingIdentity(); |
| 3347 | for (int i = start; i > finishTo; i--) { |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 3348 | ActivityRecord r = activities.get(i); |
| 3349 | requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true); |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 3350 | // Only return the supplied result for the first activity finished |
| 3351 | resultCode = Activity.RESULT_CANCELED; |
| 3352 | resultData = null; |
| 3353 | } |
| 3354 | |
| 3355 | if (parent != null && foundParentInTask) { |
| 3356 | final int parentLaunchMode = parent.info.launchMode; |
| 3357 | final int destIntentFlags = destIntent.getFlags(); |
| 3358 | if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE || |
| 3359 | parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK || |
| 3360 | parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP || |
| 3361 | (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) { |
| 3362 | parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent); |
| 3363 | } else { |
| 3364 | try { |
| 3365 | ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo( |
| 3366 | destIntent.getComponent(), 0, srec.userId); |
| 3367 | int res = startActivityLocked(srec.app.thread, destIntent, |
| 3368 | null, aInfo, parent.appToken, null, |
| 3369 | 0, -1, parent.launchedFromUid, parent.launchedFromPackage, |
| 3370 | 0, null, true, null); |
| 3371 | foundParentInTask = res == ActivityManager.START_SUCCESS; |
| 3372 | } catch (RemoteException e) { |
| 3373 | foundParentInTask = false; |
| 3374 | } |
| 3375 | requestFinishActivityLocked(parent.appToken, resultCode, |
| 3376 | resultData, "navigate-up", true); |
| 3377 | } |
| 3378 | } |
| 3379 | Binder.restoreCallingIdentity(origId); |
| 3380 | return foundParentInTask; |
| 3381 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3382 | /** |
| 3383 | * Perform the common clean-up of an activity record. This is called both |
| 3384 | * as part of destroyActivityLocked() (when destroying the client-side |
| 3385 | * representation) and cleaning things up as a result of its hosting |
| 3386 | * processing going away, in which case there is no remaining client-side |
| 3387 | * state to destroy so only the cleanup here is needed. |
| 3388 | */ |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 3389 | final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices, |
| 3390 | boolean setState) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3391 | if (mResumedActivity == r) { |
| 3392 | mResumedActivity = null; |
| 3393 | } |
| 3394 | if (mService.mFocusedActivity == r) { |
| 3395 | mService.mFocusedActivity = null; |
| 3396 | } |
| 3397 | |
| 3398 | r.configDestroy = false; |
| 3399 | r.frozenBeforeDestroy = false; |
| 3400 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 3401 | if (setState) { |
| 3402 | if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)"); |
| 3403 | r.state = ActivityState.DESTROYED; |
Dianne Hackborn | 0798149 | 2013-01-28 11:36:23 -0800 | [diff] [blame] | 3404 | if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r); |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 3405 | r.app = null; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 3406 | } |
| 3407 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3408 | // Make sure this record is no longer in the pending finishes list. |
| 3409 | // This could happen, for example, if we are trimming activities |
| 3410 | // down to the max limit while they are still waiting to finish. |
| 3411 | mFinishingActivities.remove(r); |
| 3412 | mWaitingVisibleActivities.remove(r); |
Craig Mautner | 2420ead | 2013-04-01 17:13:20 -0700 | [diff] [blame] | 3413 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3414 | // Remove any pending results. |
| 3415 | if (r.finishing && r.pendingResults != null) { |
| 3416 | for (WeakReference<PendingIntentRecord> apr : r.pendingResults) { |
| 3417 | PendingIntentRecord rec = apr.get(); |
| 3418 | if (rec != null) { |
| 3419 | mService.cancelIntentSenderLocked(rec, false); |
| 3420 | } |
| 3421 | } |
| 3422 | r.pendingResults = null; |
| 3423 | } |
| 3424 | |
| 3425 | if (cleanServices) { |
Craig Mautner | 2420ead | 2013-04-01 17:13:20 -0700 | [diff] [blame] | 3426 | cleanUpActivityServicesLocked(r); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3427 | } |
| 3428 | |
| 3429 | if (mService.mPendingThumbnails.size() > 0) { |
| 3430 | // There are clients waiting to receive thumbnails so, in case |
| 3431 | // this is an activity that someone is waiting for, add it |
| 3432 | // to the pending list so we can correctly update the clients. |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 3433 | mCancelledThumbnails.add(r); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3434 | } |
| 3435 | |
| 3436 | // Get rid of any pending idle timeouts. |
Dianne Hackborn | 42e620c | 2012-06-24 13:20:51 -0700 | [diff] [blame] | 3437 | removeTimeoutsForActivityLocked(r); |
| 3438 | } |
| 3439 | |
| 3440 | private void removeTimeoutsForActivityLocked(ActivityRecord r) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3441 | mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r); |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 3442 | mHandler.removeMessages(STOP_TIMEOUT_MSG, r); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3443 | mHandler.removeMessages(IDLE_TIMEOUT_MSG, r); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3444 | mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r); |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3445 | r.finishLaunchTickingLocked(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3446 | } |
| 3447 | |
Dianne Hackborn | 5c60743 | 2012-02-28 14:44:19 -0800 | [diff] [blame] | 3448 | final void removeActivityFromHistoryLocked(ActivityRecord r) { |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 3449 | finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null); |
| 3450 | r.makeFinishing(); |
| 3451 | if (DEBUG_ADD_REMOVE) { |
| 3452 | RuntimeException here = new RuntimeException("here"); |
| 3453 | here.fillInStackTrace(); |
| 3454 | Slog.i(TAG, "Removing activity " + r + " from stack"); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3455 | } |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 3456 | if (r.task != null) { |
| 3457 | removeActivity(r); |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 3458 | } |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 3459 | r.takeFromHistory(); |
| 3460 | removeTimeoutsForActivityLocked(r); |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 3461 | if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)"); |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 3462 | r.state = ActivityState.DESTROYED; |
Dianne Hackborn | 0798149 | 2013-01-28 11:36:23 -0800 | [diff] [blame] | 3463 | if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r); |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 3464 | r.app = null; |
| 3465 | mService.mWindowManager.removeAppToken(r.appToken); |
| 3466 | if (VALIDATE_TOKENS) { |
| 3467 | validateAppTokensLocked(); |
| 3468 | } |
| 3469 | cleanUpActivityServicesLocked(r); |
| 3470 | r.removeUriPermissionsLocked(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3471 | } |
| 3472 | |
| 3473 | /** |
| 3474 | * Perform clean-up of service connections in an activity record. |
| 3475 | */ |
| 3476 | final void cleanUpActivityServicesLocked(ActivityRecord r) { |
| 3477 | // Throw away any services that have been bound by this activity. |
| 3478 | if (r.connections != null) { |
| 3479 | Iterator<ConnectionRecord> it = r.connections.iterator(); |
| 3480 | while (it.hasNext()) { |
| 3481 | ConnectionRecord c = it.next(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3482 | mService.mServices.removeConnectionLocked(c, null, r); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3483 | } |
| 3484 | r.connections = null; |
| 3485 | } |
| 3486 | } |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 3487 | |
| 3488 | final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) { |
| 3489 | Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG); |
| 3490 | msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason); |
| 3491 | mHandler.sendMessage(msg); |
| 3492 | } |
| 3493 | |
Dianne Hackborn | 28695e0 | 2011-11-02 21:59:51 -0700 | [diff] [blame] | 3494 | final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) { |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 3495 | boolean lastIsOpaque = false; |
Dianne Hackborn | 42e620c | 2012-06-24 13:20:51 -0700 | [diff] [blame] | 3496 | boolean activityRemoved = false; |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 3497 | for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { |
| 3498 | final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities; |
| 3499 | for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) { |
| 3500 | final ActivityRecord r = activities.get(activityNdx); |
| 3501 | if (r.finishing) { |
| 3502 | continue; |
| 3503 | } |
| 3504 | if (r.fullscreen) { |
| 3505 | lastIsOpaque = true; |
| 3506 | } |
| 3507 | if (owner != null && r.app != owner) { |
| 3508 | continue; |
| 3509 | } |
| 3510 | if (!lastIsOpaque) { |
| 3511 | continue; |
| 3512 | } |
| 3513 | // We can destroy this one if we have its icicle saved and |
| 3514 | // it is not in the process of pausing/stopping/finishing. |
| 3515 | if (r.app != null && r != mResumedActivity && r != mPausingActivity |
| 3516 | && r.haveState && !r.visible && r.stopped |
| 3517 | && r.state != ActivityState.DESTROYING |
| 3518 | && r.state != ActivityState.DESTROYED) { |
| 3519 | if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state |
| 3520 | + " resumed=" + mResumedActivity |
| 3521 | + " pausing=" + mPausingActivity); |
| 3522 | if (destroyActivityLocked(r, true, oomAdj, reason)) { |
| 3523 | activityRemoved = true; |
| 3524 | } |
Dianne Hackborn | 42e620c | 2012-06-24 13:20:51 -0700 | [diff] [blame] | 3525 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 3526 | } |
| 3527 | } |
Dianne Hackborn | 42e620c | 2012-06-24 13:20:51 -0700 | [diff] [blame] | 3528 | if (activityRemoved) { |
| 3529 | resumeTopActivityLocked(null); |
| 3530 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 3531 | } |
| 3532 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3533 | /** |
| 3534 | * Destroy the current CLIENT SIDE instance of an activity. This may be |
| 3535 | * called both when actually finishing an activity, or when performing |
| 3536 | * a configuration switch where we destroy the current client-side object |
| 3537 | * but then create a new client-side object for this same HistoryRecord. |
| 3538 | */ |
| 3539 | final boolean destroyActivityLocked(ActivityRecord r, |
Dianne Hackborn | 28695e0 | 2011-11-02 21:59:51 -0700 | [diff] [blame] | 3540 | boolean removeFromApp, boolean oomAdj, String reason) { |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 3541 | if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v( |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 3542 | TAG, "Removing activity from " + reason + ": token=" + r |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3543 | + ", app=" + (r.app != null ? r.app.processName : "(null)")); |
| 3544 | EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 3545 | r.userId, System.identityHashCode(r), |
Dianne Hackborn | 28695e0 | 2011-11-02 21:59:51 -0700 | [diff] [blame] | 3546 | r.task.taskId, r.shortComponentName, reason); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3547 | |
| 3548 | boolean removedFromHistory = false; |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 3549 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 3550 | cleanUpActivityLocked(r, false, false); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3551 | |
| 3552 | final boolean hadApp = r.app != null; |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 3553 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3554 | if (hadApp) { |
| 3555 | if (removeFromApp) { |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 3556 | r.app.activities.remove(r); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3557 | if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) { |
| 3558 | mService.mHeavyWeightProcess = null; |
| 3559 | mService.mHandler.sendEmptyMessage( |
| 3560 | ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG); |
| 3561 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3562 | if (r.app.activities.size() == 0) { |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 3563 | // No longer have activities, so update oom adj. |
| 3564 | mService.updateOomAdjLocked(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3565 | } |
| 3566 | } |
| 3567 | |
| 3568 | boolean skipDestroy = false; |
| 3569 | |
| 3570 | try { |
| 3571 | if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 3572 | r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing, |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3573 | r.configChangeFlags); |
| 3574 | } catch (Exception e) { |
| 3575 | // We can just ignore exceptions here... if the process |
| 3576 | // has crashed, our death notification will clean things |
| 3577 | // up. |
| 3578 | //Slog.w(TAG, "Exception thrown during finish", e); |
| 3579 | if (r.finishing) { |
| 3580 | removeActivityFromHistoryLocked(r); |
| 3581 | removedFromHistory = true; |
| 3582 | skipDestroy = true; |
| 3583 | } |
| 3584 | } |
| 3585 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3586 | r.nowVisible = false; |
| 3587 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 3588 | // If the activity is finishing, we need to wait on removing it |
| 3589 | // from the list to give it a chance to do its cleanup. During |
| 3590 | // that time it may make calls back with its token so we need to |
| 3591 | // be able to find it on the list and so we don't want to remove |
| 3592 | // it from the list yet. Otherwise, we can just immediately put |
| 3593 | // it in the destroyed state since we are not removing it from the |
| 3594 | // list. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3595 | if (r.finishing && !skipDestroy) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 3596 | if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r |
| 3597 | + " (destroy requested)"); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3598 | r.state = ActivityState.DESTROYING; |
| 3599 | Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG); |
| 3600 | msg.obj = r; |
| 3601 | mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT); |
| 3602 | } else { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 3603 | if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r |
| 3604 | + " (destroy skipped)"); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3605 | r.state = ActivityState.DESTROYED; |
Dianne Hackborn | 0798149 | 2013-01-28 11:36:23 -0800 | [diff] [blame] | 3606 | if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r); |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 3607 | r.app = null; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3608 | } |
| 3609 | } else { |
| 3610 | // remove this record from the history. |
| 3611 | if (r.finishing) { |
| 3612 | removeActivityFromHistoryLocked(r); |
| 3613 | removedFromHistory = true; |
| 3614 | } else { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 3615 | if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r |
| 3616 | + " (no app)"); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3617 | r.state = ActivityState.DESTROYED; |
Dianne Hackborn | 0798149 | 2013-01-28 11:36:23 -0800 | [diff] [blame] | 3618 | if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r); |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 3619 | r.app = null; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3620 | } |
| 3621 | } |
| 3622 | |
| 3623 | r.configChangeFlags = 0; |
| 3624 | |
| 3625 | if (!mLRUActivities.remove(r) && hadApp) { |
| 3626 | Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list"); |
| 3627 | } |
| 3628 | |
| 3629 | return removedFromHistory; |
| 3630 | } |
| 3631 | |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 3632 | final void activityDestroyedLocked(IBinder token) { |
| 3633 | final long origId = Binder.clearCallingIdentity(); |
| 3634 | try { |
| 3635 | ActivityRecord r = ActivityRecord.forToken(token); |
| 3636 | if (r != null) { |
| 3637 | mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3638 | } |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 3639 | |
| 3640 | if (isInStackLocked(token) != null) { |
| 3641 | if (r.state == ActivityState.DESTROYING) { |
| 3642 | cleanUpActivityLocked(r, true, false); |
| 3643 | removeActivityFromHistoryLocked(r); |
| 3644 | } |
| 3645 | } |
| 3646 | resumeTopActivityLocked(null); |
| 3647 | } finally { |
| 3648 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3649 | } |
| 3650 | } |
| 3651 | |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 3652 | private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list, |
| 3653 | ProcessRecord app, String listName) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3654 | int i = list.size(); |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 3655 | if (DEBUG_CLEANUP) Slog.v( |
| 3656 | TAG, "Removing app " + app + " from list " + listName |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3657 | + " with " + i + " entries"); |
| 3658 | while (i > 0) { |
| 3659 | i--; |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 3660 | ActivityRecord r = list.get(i); |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 3661 | if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3662 | if (r.app == app) { |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 3663 | if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!"); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3664 | list.remove(i); |
Dianne Hackborn | 42e620c | 2012-06-24 13:20:51 -0700 | [diff] [blame] | 3665 | removeTimeoutsForActivityLocked(r); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3666 | } |
| 3667 | } |
| 3668 | } |
| 3669 | |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 3670 | boolean removeHistoryRecordsForAppLocked(ProcessRecord app) { |
| 3671 | removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities"); |
| 3672 | removeHistoryRecordsForAppLocked(mStoppingActivities, app, "mStoppingActivities"); |
| 3673 | removeHistoryRecordsForAppLocked(mGoingToSleepActivities, app, "mGoingToSleepActivities"); |
| 3674 | removeHistoryRecordsForAppLocked(mWaitingVisibleActivities, app, |
| 3675 | "mWaitingVisibleActivities"); |
| 3676 | removeHistoryRecordsForAppLocked(mFinishingActivities, app, "mFinishingActivities"); |
| 3677 | |
| 3678 | boolean hasVisibleActivities = false; |
| 3679 | |
| 3680 | // Clean out the history list. |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 3681 | int i = numActivities(); |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 3682 | if (DEBUG_CLEANUP) Slog.v( |
| 3683 | TAG, "Removing app " + app + " from history with " + i + " entries"); |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 3684 | for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { |
| 3685 | final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities; |
| 3686 | for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) { |
| 3687 | final ActivityRecord r = activities.get(activityNdx); |
| 3688 | --i; |
| 3689 | if (DEBUG_CLEANUP) Slog.v( |
| 3690 | TAG, "Record #" + i + " " + r + ": app=" + r.app); |
| 3691 | if (r.app == app) { |
| 3692 | boolean remove; |
| 3693 | if ((!r.haveState && !r.stateNotNeeded) || r.finishing) { |
| 3694 | // Don't currently have state for the activity, or |
| 3695 | // it is finishing -- always remove it. |
| 3696 | remove = true; |
| 3697 | } else if (r.launchCount > 2 && |
| 3698 | r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) { |
| 3699 | // We have launched this activity too many times since it was |
| 3700 | // able to run, so give up and remove it. |
| 3701 | remove = true; |
| 3702 | } else { |
| 3703 | // The process may be gone, but the activity lives on! |
| 3704 | remove = false; |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 3705 | } |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 3706 | if (remove) { |
| 3707 | if (ActivityStack.DEBUG_ADD_REMOVE || DEBUG_CLEANUP) { |
| 3708 | RuntimeException here = new RuntimeException("here"); |
| 3709 | here.fillInStackTrace(); |
| 3710 | Slog.i(TAG, "Removing activity " + r + " from stack at " + i |
| 3711 | + ": haveState=" + r.haveState |
| 3712 | + " stateNotNeeded=" + r.stateNotNeeded |
| 3713 | + " finishing=" + r.finishing |
| 3714 | + " state=" + r.state, here); |
| 3715 | } |
| 3716 | if (!r.finishing) { |
| 3717 | Slog.w(TAG, "Force removing " + r + ": app died, no saved state"); |
| 3718 | EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY, |
| 3719 | r.userId, System.identityHashCode(r), |
| 3720 | r.task.taskId, r.shortComponentName, |
| 3721 | "proc died without state saved"); |
| 3722 | } |
| 3723 | removeActivityFromHistoryLocked(r); |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 3724 | |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 3725 | } else { |
| 3726 | // We have the current state for this activity, so |
| 3727 | // it can be restarted later when needed. |
| 3728 | if (localLOGV) Slog.v( |
| 3729 | TAG, "Keeping entry, setting app to null"); |
| 3730 | if (r.visible) { |
| 3731 | hasVisibleActivities = true; |
| 3732 | } |
| 3733 | if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity " |
| 3734 | + r); |
| 3735 | r.app = null; |
| 3736 | r.nowVisible = false; |
| 3737 | if (!r.haveState) { |
| 3738 | if (ActivityStack.DEBUG_SAVED_STATE) Slog.i(TAG, |
| 3739 | "App died, clearing saved state of " + r); |
| 3740 | r.icicle = null; |
| 3741 | } |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 3742 | } |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 3743 | |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 3744 | cleanUpActivityLocked(r, true, true); |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 3745 | } |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 3746 | } |
| 3747 | } |
| 3748 | |
| 3749 | return hasVisibleActivities; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3750 | } |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 3751 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 3752 | /** |
| 3753 | * Move the current home activity's task (if one exists) to the front |
| 3754 | * of the stack. |
| 3755 | */ |
| 3756 | final void moveHomeToFrontLocked() { |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 3757 | for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { |
| 3758 | final TaskRecord task = mTaskHistory.get(taskNdx); |
| 3759 | final ArrayList<ActivityRecord> activities = task.mActivities; |
Craig Mautner | 11bf9a5 | 2013-02-19 14:08:51 -0800 | [diff] [blame] | 3760 | for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) { |
| 3761 | final ActivityRecord r = activities.get(activityNdx); |
| 3762 | if (r.isHomeActivity) { |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 3763 | moveTaskToFrontLocked(task, null, null); |
| 3764 | return; |
Craig Mautner | 11bf9a5 | 2013-02-19 14:08:51 -0800 | [diff] [blame] | 3765 | } |
| 3766 | } |
| 3767 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 3768 | } |
| 3769 | |
Dianne Hackborn | 7f58b95 | 2012-04-18 12:59:29 -0700 | [diff] [blame] | 3770 | final void updateTransitLocked(int transit, Bundle options) { |
| 3771 | if (options != null) { |
| 3772 | ActivityRecord r = topRunningActivityLocked(null); |
| 3773 | if (r != null && r.state != ActivityState.RESUMED) { |
| 3774 | r.updateOptionsLocked(options); |
| 3775 | } else { |
| 3776 | ActivityOptions.abort(options); |
| 3777 | } |
| 3778 | } |
| 3779 | mService.mWindowManager.prepareAppTransition(transit, false); |
| 3780 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 3781 | |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 3782 | final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) { |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 3783 | final TaskRecord task = taskForIdLocked(taskId); |
| 3784 | if (task != null) { |
Craig Mautner | aab647e | 2013-02-28 16:31:36 -0800 | [diff] [blame] | 3785 | if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 3786 | mUserLeaving = true; |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 3787 | } |
Craig Mautner | aab647e | 2013-02-28 16:31:36 -0800 | [diff] [blame] | 3788 | if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 3789 | // Caller wants the home activity moved with it. To accomplish this, |
| 3790 | // we'll just move the home task to the top first. |
| 3791 | moveHomeToFrontLocked(); |
| 3792 | } |
| 3793 | moveTaskToFrontLocked(task, null, options); |
| 3794 | return true; |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 3795 | } |
| 3796 | return false; |
| 3797 | } |
| 3798 | |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 3799 | final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) { |
Craig Mautner | 11bf9a5 | 2013-02-19 14:08:51 -0800 | [diff] [blame] | 3800 | if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3801 | |
Craig Mautner | 11bf9a5 | 2013-02-19 14:08:51 -0800 | [diff] [blame] | 3802 | final int numTasks = mTaskHistory.size(); |
| 3803 | final int index = mTaskHistory.indexOf(tr); |
| 3804 | if (numTasks == 0 || index < 0 || index == numTasks - 1) { |
| 3805 | // nothing to do! |
| 3806 | if (reason != null && |
| 3807 | (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) { |
| 3808 | ActivityOptions.abort(options); |
| 3809 | } else { |
| 3810 | updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options); |
| 3811 | } |
| 3812 | return; |
| 3813 | } |
| 3814 | |
| 3815 | // Shift all activities with this task up to the top |
| 3816 | // of the stack, keeping them in the same internal order. |
| 3817 | mTaskHistory.remove(tr); |
| 3818 | mTaskHistory.add(tr); |
| 3819 | |
| 3820 | if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3821 | if (reason != null && |
| 3822 | (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) { |
Craig Mautner | 11bf9a5 | 2013-02-19 14:08:51 -0800 | [diff] [blame] | 3823 | mService.mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3824 | ActivityRecord r = topRunningActivityLocked(null); |
| 3825 | if (r != null) { |
| 3826 | mNoAnimActivities.add(r); |
| 3827 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 3828 | ActivityOptions.abort(options); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3829 | } else { |
Craig Mautner | 4b71aa1 | 2012-12-27 17:20:01 -0800 | [diff] [blame] | 3830 | updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3831 | } |
Craig Mautner | 30e2d72 | 2013-02-12 11:30:16 -0800 | [diff] [blame] | 3832 | |
Craig Mautner | 5854780 | 2013-03-05 08:23:53 -0800 | [diff] [blame] | 3833 | mService.mWindowManager.moveTaskToTop(tr.taskId); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3834 | |
Craig Mautner | 5854780 | 2013-03-05 08:23:53 -0800 | [diff] [blame] | 3835 | resumeTopActivityLocked(null); |
| 3836 | EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId); |
Craig Mautner | 11bf9a5 | 2013-02-19 14:08:51 -0800 | [diff] [blame] | 3837 | |
| 3838 | if (VALIDATE_TOKENS) { |
| 3839 | validateAppTokensLocked(); |
| 3840 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3841 | } |
| 3842 | |
| 3843 | /** |
| 3844 | * Worker method for rearranging history stack. Implements the function of moving all |
| 3845 | * activities for a specific task (gathering them if disjoint) into a single group at the |
| 3846 | * bottom of the stack. |
| 3847 | * |
| 3848 | * If a watcher is installed, the action is preflighted and the watcher has an opportunity |
| 3849 | * to premeptively cancel the move. |
| 3850 | * |
| 3851 | * @param task The taskId to collect and move to the bottom. |
| 3852 | * @return Returns true if the move completed, false if not. |
| 3853 | */ |
| 3854 | final boolean moveTaskToBackLocked(int task, ActivityRecord reason) { |
| 3855 | Slog.i(TAG, "moveTaskToBack: " + task); |
Craig Mautner | b44de0d | 2013-02-21 20:00:58 -0800 | [diff] [blame] | 3856 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3857 | // If we have a watcher, preflight the move before committing to it. First check |
| 3858 | // for *other* available tasks, but if none are available, then try again allowing the |
| 3859 | // current task to be selected. |
Craig Mautner | 2219a1b | 2013-03-25 09:44:30 -0700 | [diff] [blame] | 3860 | if (mStackSupervisor.isMainStack(this) && mService.mController != null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3861 | ActivityRecord next = topRunningActivityLocked(null, task); |
| 3862 | if (next == null) { |
| 3863 | next = topRunningActivityLocked(null, 0); |
| 3864 | } |
| 3865 | if (next != null) { |
| 3866 | // ask watcher if this is allowed |
| 3867 | boolean moveOK = true; |
| 3868 | try { |
| 3869 | moveOK = mService.mController.activityResuming(next.packageName); |
| 3870 | } catch (RemoteException e) { |
| 3871 | mService.mController = null; |
| 3872 | } |
| 3873 | if (!moveOK) { |
| 3874 | return false; |
| 3875 | } |
| 3876 | } |
| 3877 | } |
| 3878 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3879 | if (DEBUG_TRANSITION) Slog.v(TAG, |
| 3880 | "Prepare to back transition: task=" + task); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3881 | |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 3882 | final TaskRecord tr = taskForIdLocked(task); |
| 3883 | if (tr == null) { |
| 3884 | return false; |
| 3885 | } |
Craig Mautner | 11bf9a5 | 2013-02-19 14:08:51 -0800 | [diff] [blame] | 3886 | mTaskHistory.remove(tr); |
| 3887 | mTaskHistory.add(0, tr); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3888 | |
| 3889 | if (reason != null && |
| 3890 | (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) { |
Craig Mautner | 11bf9a5 | 2013-02-19 14:08:51 -0800 | [diff] [blame] | 3891 | mService.mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3892 | ActivityRecord r = topRunningActivityLocked(null); |
| 3893 | if (r != null) { |
| 3894 | mNoAnimActivities.add(r); |
| 3895 | } |
| 3896 | } else { |
Dianne Hackborn | 7da6ac3 | 2010-12-09 19:22:04 -0800 | [diff] [blame] | 3897 | mService.mWindowManager.prepareAppTransition( |
Craig Mautner | 4b71aa1 | 2012-12-27 17:20:01 -0800 | [diff] [blame] | 3898 | AppTransition.TRANSIT_TASK_TO_BACK, false); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3899 | } |
Craig Mautner | 30e2d72 | 2013-02-12 11:30:16 -0800 | [diff] [blame] | 3900 | mService.mWindowManager.moveTaskToBottom(task); |
Craig Mautner | b44de0d | 2013-02-21 20:00:58 -0800 | [diff] [blame] | 3901 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3902 | if (VALIDATE_TOKENS) { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 3903 | validateAppTokensLocked(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3904 | } |
| 3905 | |
Craig Mautner | 5854780 | 2013-03-05 08:23:53 -0800 | [diff] [blame] | 3906 | resumeTopActivityLocked(null); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3907 | return true; |
| 3908 | } |
Dianne Hackborn | 15491c6 | 2012-09-19 10:59:14 -0700 | [diff] [blame] | 3909 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3910 | public ActivityManager.TaskThumbnails getTaskThumbnailsLocked(TaskRecord tr) { |
Craig Mautner | 1603077 | 2013-02-26 16:05:47 -0800 | [diff] [blame] | 3911 | TaskAccessInfo info = getTaskAccessInfoLocked(tr, true); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3912 | ActivityRecord resumed = mResumedActivity; |
| 3913 | if (resumed != null && resumed.thumbHolder == tr) { |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 3914 | info.mainThumbnail = screenshotActivities(resumed); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3915 | } |
Dianne Hackborn | 6a86495 | 2012-09-21 18:46:11 -0700 | [diff] [blame] | 3916 | if (info.mainThumbnail == null) { |
| 3917 | info.mainThumbnail = tr.lastThumbnail; |
| 3918 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3919 | return info; |
| 3920 | } |
| 3921 | |
Dianne Hackborn | 15491c6 | 2012-09-19 10:59:14 -0700 | [diff] [blame] | 3922 | public Bitmap getTaskTopThumbnailLocked(TaskRecord tr) { |
| 3923 | ActivityRecord resumed = mResumedActivity; |
| 3924 | if (resumed != null && resumed.task == tr) { |
| 3925 | // This task is the current resumed task, we just need to take |
| 3926 | // a screenshot of it and return that. |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 3927 | return screenshotActivities(resumed); |
Dianne Hackborn | 15491c6 | 2012-09-19 10:59:14 -0700 | [diff] [blame] | 3928 | } |
| 3929 | // Return the information about the task, to figure out the top |
| 3930 | // thumbnail to return. |
Craig Mautner | 1603077 | 2013-02-26 16:05:47 -0800 | [diff] [blame] | 3931 | TaskAccessInfo info = getTaskAccessInfoLocked(tr, true); |
Dianne Hackborn | 15491c6 | 2012-09-19 10:59:14 -0700 | [diff] [blame] | 3932 | if (info.numSubThumbbails <= 0) { |
Dianne Hackborn | 6a86495 | 2012-09-21 18:46:11 -0700 | [diff] [blame] | 3933 | return info.mainThumbnail != null ? info.mainThumbnail : tr.lastThumbnail; |
Dianne Hackborn | 15491c6 | 2012-09-19 10:59:14 -0700 | [diff] [blame] | 3934 | } |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 3935 | return info.subtasks.get(info.numSubThumbbails-1).holder.lastThumbnail; |
Dianne Hackborn | 15491c6 | 2012-09-19 10:59:14 -0700 | [diff] [blame] | 3936 | } |
| 3937 | |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 3938 | public ActivityRecord removeTaskActivitiesLocked(int taskId, int subTaskIndex, |
| 3939 | boolean taskRequired) { |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 3940 | final TaskRecord task = taskForIdLocked(taskId); |
| 3941 | if (task == null) { |
| 3942 | return null; |
| 3943 | } |
Craig Mautner | 1603077 | 2013-02-26 16:05:47 -0800 | [diff] [blame] | 3944 | TaskAccessInfo info = getTaskAccessInfoLocked(task, false); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3945 | if (info.root == null) { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 3946 | if (taskRequired) { |
| 3947 | Slog.w(TAG, "removeTaskLocked: unknown taskId " + taskId); |
| 3948 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3949 | return null; |
| 3950 | } |
| 3951 | |
| 3952 | if (subTaskIndex < 0) { |
| 3953 | // Just remove the entire task. |
Craig Mautner | b0f7dc7 | 2013-04-01 16:34:45 -0700 | [diff] [blame] | 3954 | task.performClearTaskAtIndexLocked(info.rootIndex); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3955 | return info.root; |
| 3956 | } |
| 3957 | |
| 3958 | if (subTaskIndex >= info.subtasks.size()) { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 3959 | if (taskRequired) { |
| 3960 | Slog.w(TAG, "removeTaskLocked: unknown subTaskIndex " + subTaskIndex); |
| 3961 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3962 | return null; |
| 3963 | } |
| 3964 | |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 3965 | // Remove all of this task's activities starting at the sub task. |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3966 | TaskAccessInfo.SubTask subtask = info.subtasks.get(subTaskIndex); |
Craig Mautner | b0f7dc7 | 2013-04-01 16:34:45 -0700 | [diff] [blame] | 3967 | task.performClearTaskAtIndexLocked(subtask.index); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3968 | return subtask.activity; |
| 3969 | } |
| 3970 | |
Craig Mautner | 1603077 | 2013-02-26 16:05:47 -0800 | [diff] [blame] | 3971 | public TaskAccessInfo getTaskAccessInfoLocked(TaskRecord task, boolean inclThumbs) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3972 | final TaskAccessInfo thumbs = new TaskAccessInfo(); |
| 3973 | // How many different sub-thumbnails? |
Craig Mautner | 1603077 | 2013-02-26 16:05:47 -0800 | [diff] [blame] | 3974 | final ArrayList<ActivityRecord> activities = task.mActivities; |
| 3975 | final int NA = activities.size(); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3976 | int j = 0; |
| 3977 | ThumbnailHolder holder = null; |
| 3978 | while (j < NA) { |
Craig Mautner | 1603077 | 2013-02-26 16:05:47 -0800 | [diff] [blame] | 3979 | ActivityRecord ar = activities.get(j); |
| 3980 | if (!ar.finishing) { |
Dianne Hackborn | 8da429e | 2012-09-23 12:52:19 -0700 | [diff] [blame] | 3981 | thumbs.root = ar; |
| 3982 | thumbs.rootIndex = j; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3983 | holder = ar.thumbHolder; |
Dianne Hackborn | 15491c6 | 2012-09-19 10:59:14 -0700 | [diff] [blame] | 3984 | if (holder != null) { |
| 3985 | thumbs.mainThumbnail = holder.lastThumbnail; |
| 3986 | } |
| 3987 | j++; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3988 | break; |
| 3989 | } |
| 3990 | j++; |
| 3991 | } |
| 3992 | |
| 3993 | if (j >= NA) { |
| 3994 | return thumbs; |
| 3995 | } |
| 3996 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3997 | ArrayList<TaskAccessInfo.SubTask> subtasks = new ArrayList<TaskAccessInfo.SubTask>(); |
| 3998 | thumbs.subtasks = subtasks; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3999 | while (j < NA) { |
Craig Mautner | 1603077 | 2013-02-26 16:05:47 -0800 | [diff] [blame] | 4000 | ActivityRecord ar = activities.get(j); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 4001 | j++; |
| 4002 | if (ar.finishing) { |
| 4003 | continue; |
| 4004 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 4005 | if (ar.thumbHolder != holder && holder != null) { |
| 4006 | thumbs.numSubThumbbails++; |
| 4007 | holder = ar.thumbHolder; |
| 4008 | TaskAccessInfo.SubTask sub = new TaskAccessInfo.SubTask(); |
Dianne Hackborn | 15491c6 | 2012-09-19 10:59:14 -0700 | [diff] [blame] | 4009 | sub.holder = holder; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 4010 | sub.activity = ar; |
| 4011 | sub.index = j-1; |
| 4012 | subtasks.add(sub); |
| 4013 | } |
| 4014 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 4015 | if (thumbs.numSubThumbbails > 0) { |
| 4016 | thumbs.retriever = new IThumbnailRetriever.Stub() { |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 4017 | @Override |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 4018 | public Bitmap getThumbnail(int index) { |
| 4019 | if (index < 0 || index >= thumbs.subtasks.size()) { |
| 4020 | return null; |
| 4021 | } |
Dianne Hackborn | 15491c6 | 2012-09-19 10:59:14 -0700 | [diff] [blame] | 4022 | TaskAccessInfo.SubTask sub = thumbs.subtasks.get(index); |
| 4023 | ActivityRecord resumed = mResumedActivity; |
| 4024 | if (resumed != null && resumed.thumbHolder == sub.holder) { |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 4025 | return screenshotActivities(resumed); |
Dianne Hackborn | 15491c6 | 2012-09-19 10:59:14 -0700 | [diff] [blame] | 4026 | } |
| 4027 | return sub.holder.lastThumbnail; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 4028 | } |
| 4029 | }; |
| 4030 | } |
| 4031 | return thumbs; |
| 4032 | } |
| 4033 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4034 | private final void logStartActivity(int tag, ActivityRecord r, |
| 4035 | TaskRecord task) { |
| 4036 | EventLog.writeEvent(tag, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 4037 | r.userId, System.identityHashCode(r), task.taskId, |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4038 | r.shortComponentName, r.intent.getAction(), |
| 4039 | r.intent.getType(), r.intent.getDataString(), |
| 4040 | r.intent.getFlags()); |
| 4041 | } |
| 4042 | |
| 4043 | /** |
| 4044 | * Make sure the given activity matches the current configuration. Returns |
| 4045 | * false if the activity had to be destroyed. Returns true if the |
| 4046 | * configuration is the same, or the activity will remain running as-is |
| 4047 | * for whatever reason. Ensures the HistoryRecord is updated with the |
| 4048 | * correct configuration and all other bookkeeping is handled. |
| 4049 | */ |
| 4050 | final boolean ensureActivityConfigurationLocked(ActivityRecord r, |
| 4051 | int globalChanges) { |
| 4052 | if (mConfigWillChange) { |
| 4053 | if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG, |
| 4054 | "Skipping config check (will change): " + r); |
| 4055 | return true; |
| 4056 | } |
| 4057 | |
| 4058 | if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG, |
| 4059 | "Ensuring correct configuration: " + r); |
| 4060 | |
| 4061 | // Short circuit: if the two configurations are the exact same |
| 4062 | // object (the common case), then there is nothing to do. |
| 4063 | Configuration newConfig = mService.mConfiguration; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 4064 | if (r.configuration == newConfig && !r.forceNewConfig) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4065 | if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG, |
| 4066 | "Configuration unchanged in " + r); |
| 4067 | return true; |
| 4068 | } |
| 4069 | |
| 4070 | // We don't worry about activities that are finishing. |
| 4071 | if (r.finishing) { |
| 4072 | if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG, |
| 4073 | "Configuration doesn't matter in finishing " + r); |
| 4074 | r.stopFreezingScreenLocked(false); |
| 4075 | return true; |
| 4076 | } |
| 4077 | |
| 4078 | // Okay we now are going to make this activity have the new config. |
| 4079 | // But then we need to figure out how it needs to deal with that. |
| 4080 | Configuration oldConfig = r.configuration; |
| 4081 | r.configuration = newConfig; |
Dianne Hackborn | 58f42a5 | 2011-10-10 13:46:34 -0700 | [diff] [blame] | 4082 | |
| 4083 | // Determine what has changed. May be nothing, if this is a config |
| 4084 | // that has come back from the app after going idle. In that case |
| 4085 | // we just want to leave the official config object now in the |
| 4086 | // activity and do nothing else. |
| 4087 | final int changes = oldConfig.diff(newConfig); |
| 4088 | if (changes == 0 && !r.forceNewConfig) { |
| 4089 | if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG, |
| 4090 | "Configuration no differences in " + r); |
| 4091 | return true; |
| 4092 | } |
| 4093 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4094 | // If the activity isn't currently running, just leave the new |
| 4095 | // configuration and it will pick that up next time it starts. |
| 4096 | if (r.app == null || r.app.thread == null) { |
| 4097 | if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG, |
| 4098 | "Configuration doesn't matter not running " + r); |
| 4099 | r.stopFreezingScreenLocked(false); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 4100 | r.forceNewConfig = false; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4101 | return true; |
| 4102 | } |
| 4103 | |
Dianne Hackborn | 58f42a5 | 2011-10-10 13:46:34 -0700 | [diff] [blame] | 4104 | // Figure out how to handle the changes between the configurations. |
Dianne Hackborn | 3c4c2b7 | 2010-10-05 18:07:54 -0700 | [diff] [blame] | 4105 | if (DEBUG_SWITCH || DEBUG_CONFIGURATION) { |
| 4106 | Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x" |
| 4107 | + Integer.toHexString(changes) + ", handles=0x" |
Dianne Hackborn | e667635 | 2011-06-01 16:51:20 -0700 | [diff] [blame] | 4108 | + Integer.toHexString(r.info.getRealConfigChanged()) |
Dianne Hackborn | 3c4c2b7 | 2010-10-05 18:07:54 -0700 | [diff] [blame] | 4109 | + ", newConfig=" + newConfig); |
| 4110 | } |
Dianne Hackborn | e667635 | 2011-06-01 16:51:20 -0700 | [diff] [blame] | 4111 | if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) { |
Dianne Hackborn | 3c4c2b7 | 2010-10-05 18:07:54 -0700 | [diff] [blame] | 4112 | // Aha, the activity isn't handling the change, so DIE DIE DIE. |
| 4113 | r.configChangeFlags |= changes; |
| 4114 | r.startFreezingScreenLocked(r.app, globalChanges); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 4115 | r.forceNewConfig = false; |
Dianne Hackborn | 3c4c2b7 | 2010-10-05 18:07:54 -0700 | [diff] [blame] | 4116 | if (r.app == null || r.app.thread == null) { |
| 4117 | if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG, |
Dianne Hackborn | b61a026 | 2012-05-14 17:19:18 -0700 | [diff] [blame] | 4118 | "Config is destroying non-running " + r); |
Dianne Hackborn | 28695e0 | 2011-11-02 21:59:51 -0700 | [diff] [blame] | 4119 | destroyActivityLocked(r, true, false, "config"); |
Dianne Hackborn | 3c4c2b7 | 2010-10-05 18:07:54 -0700 | [diff] [blame] | 4120 | } else if (r.state == ActivityState.PAUSING) { |
| 4121 | // A little annoying: we are waiting for this activity to |
| 4122 | // finish pausing. Let's not do anything now, but just |
| 4123 | // flag that it needs to be restarted when done pausing. |
| 4124 | if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG, |
Dianne Hackborn | b61a026 | 2012-05-14 17:19:18 -0700 | [diff] [blame] | 4125 | "Config is skipping already pausing " + r); |
Dianne Hackborn | 3c4c2b7 | 2010-10-05 18:07:54 -0700 | [diff] [blame] | 4126 | r.configDestroy = true; |
| 4127 | return true; |
| 4128 | } else if (r.state == ActivityState.RESUMED) { |
| 4129 | // Try to optimize this case: the configuration is changing |
| 4130 | // and we need to restart the top, resumed activity. |
| 4131 | // Instead of doing the normal handshaking, just say |
| 4132 | // "restart!". |
| 4133 | if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG, |
Dianne Hackborn | b61a026 | 2012-05-14 17:19:18 -0700 | [diff] [blame] | 4134 | "Config is relaunching resumed " + r); |
Dianne Hackborn | 3c4c2b7 | 2010-10-05 18:07:54 -0700 | [diff] [blame] | 4135 | relaunchActivityLocked(r, r.configChangeFlags, true); |
| 4136 | r.configChangeFlags = 0; |
| 4137 | } else { |
| 4138 | if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG, |
Dianne Hackborn | b61a026 | 2012-05-14 17:19:18 -0700 | [diff] [blame] | 4139 | "Config is relaunching non-resumed " + r); |
Dianne Hackborn | 3c4c2b7 | 2010-10-05 18:07:54 -0700 | [diff] [blame] | 4140 | relaunchActivityLocked(r, r.configChangeFlags, false); |
| 4141 | r.configChangeFlags = 0; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4142 | } |
Dianne Hackborn | 3c4c2b7 | 2010-10-05 18:07:54 -0700 | [diff] [blame] | 4143 | |
| 4144 | // All done... tell the caller we weren't able to keep this |
| 4145 | // activity around. |
| 4146 | return false; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4147 | } |
| 4148 | |
| 4149 | // Default case: the activity can handle this new configuration, so |
| 4150 | // hand it over. Note that we don't need to give it the new |
| 4151 | // configuration, since we always send configuration changes to all |
| 4152 | // process when they happen so it can just use whatever configuration |
| 4153 | // it last got. |
| 4154 | if (r.app != null && r.app.thread != null) { |
| 4155 | try { |
| 4156 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 4157 | r.app.thread.scheduleActivityConfigurationChanged(r.appToken); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4158 | } catch (RemoteException e) { |
| 4159 | // If process died, whatever. |
| 4160 | } |
| 4161 | } |
| 4162 | r.stopFreezingScreenLocked(false); |
| 4163 | |
| 4164 | return true; |
| 4165 | } |
| 4166 | |
| 4167 | private final boolean relaunchActivityLocked(ActivityRecord r, |
| 4168 | int changes, boolean andResume) { |
| 4169 | List<ResultInfo> results = null; |
| 4170 | List<Intent> newIntents = null; |
| 4171 | if (andResume) { |
| 4172 | results = r.results; |
| 4173 | newIntents = r.newIntents; |
| 4174 | } |
| 4175 | if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r |
| 4176 | + " with results=" + results + " newIntents=" + newIntents |
| 4177 | + " andResume=" + andResume); |
| 4178 | EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 4179 | : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r), |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4180 | r.task.taskId, r.shortComponentName); |
| 4181 | |
| 4182 | r.startFreezingScreenLocked(r.app, 0); |
| 4183 | |
| 4184 | try { |
Dianne Hackborn | b61a026 | 2012-05-14 17:19:18 -0700 | [diff] [blame] | 4185 | if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, |
| 4186 | (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ") |
| 4187 | + r); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 4188 | r.forceNewConfig = false; |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 4189 | r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 4190 | changes, !andResume, new Configuration(mService.mConfiguration)); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4191 | // Note: don't need to call pauseIfSleepingLocked() here, because |
| 4192 | // the caller will only pass in 'andResume' if this activity is |
| 4193 | // currently resumed, which implies we aren't sleeping. |
| 4194 | } catch (RemoteException e) { |
Dianne Hackborn | b61a026 | 2012-05-14 17:19:18 -0700 | [diff] [blame] | 4195 | if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4196 | } |
| 4197 | |
| 4198 | if (andResume) { |
| 4199 | r.results = null; |
| 4200 | r.newIntents = null; |
Craig Mautner | 2219a1b | 2013-03-25 09:44:30 -0700 | [diff] [blame] | 4201 | if (mStackSupervisor.isMainStack(this)) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4202 | mService.reportResumedActivityLocked(r); |
| 4203 | } |
Dianne Hackborn | b61a026 | 2012-05-14 17:19:18 -0700 | [diff] [blame] | 4204 | r.state = ActivityState.RESUMED; |
| 4205 | } else { |
| 4206 | mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r); |
| 4207 | r.state = ActivityState.PAUSED; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4208 | } |
| 4209 | |
| 4210 | return true; |
| 4211 | } |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 4212 | |
| 4213 | boolean willActivityBeVisibleLocked(IBinder token) { |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 4214 | for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { |
| 4215 | final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities; |
| 4216 | for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) { |
| 4217 | final ActivityRecord r = activities.get(activityNdx); |
| 4218 | if (r.appToken == token) { |
| 4219 | return true; |
| 4220 | } |
| 4221 | if (r.fullscreen && !r.finishing) { |
| 4222 | return false; |
| 4223 | } |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 4224 | } |
| 4225 | } |
| 4226 | return true; |
| 4227 | } |
| 4228 | |
| 4229 | void closeSystemDialogsLocked() { |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 4230 | for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { |
| 4231 | final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities; |
| 4232 | for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) { |
| 4233 | final ActivityRecord r = activities.get(activityNdx); |
| 4234 | if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) { |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 4235 | finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true); |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 4236 | } |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 4237 | } |
| 4238 | } |
| 4239 | } |
| 4240 | |
| 4241 | boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) { |
| 4242 | boolean didSomething = false; |
| 4243 | TaskRecord lastTask = null; |
Craig Mautner | 56f52db | 2013-02-25 10:03:01 -0800 | [diff] [blame] | 4244 | for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { |
| 4245 | final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities; |
| 4246 | int numActivities = activities.size(); |
| 4247 | for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) { |
| 4248 | ActivityRecord r = activities.get(activityNdx); |
| 4249 | final boolean samePackage = r.packageName.equals(name) |
| 4250 | || (name == null && r.userId == userId); |
| 4251 | if ((userId == UserHandle.USER_ALL || r.userId == userId) |
| 4252 | && (samePackage || r.task == lastTask) |
| 4253 | && (r.app == null || evenPersistent || !r.app.persistent)) { |
| 4254 | if (!doit) { |
| 4255 | if (r.finishing) { |
| 4256 | // If this activity is just finishing, then it is not |
| 4257 | // interesting as far as something to stop. |
| 4258 | continue; |
| 4259 | } |
| 4260 | return true; |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 4261 | } |
Craig Mautner | 56f52db | 2013-02-25 10:03:01 -0800 | [diff] [blame] | 4262 | didSomething = true; |
| 4263 | Slog.i(TAG, " Force finishing activity " + r); |
| 4264 | if (samePackage) { |
| 4265 | if (r.app != null) { |
| 4266 | r.app.removed = true; |
| 4267 | } |
| 4268 | r.app = null; |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 4269 | } |
Craig Mautner | 56f52db | 2013-02-25 10:03:01 -0800 | [diff] [blame] | 4270 | lastTask = r.task; |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 4271 | finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop", true); |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 4272 | } |
| 4273 | } |
| 4274 | } |
| 4275 | return didSomething; |
| 4276 | } |
| 4277 | |
| 4278 | ActivityRecord getTasksLocked(int maxNum, IThumbnailReceiver receiver, |
Craig Mautner | aab647e | 2013-02-28 16:31:36 -0800 | [diff] [blame] | 4279 | PendingThumbnailsRecord pending, List<RunningTaskInfo> list) { |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 4280 | ActivityRecord topRecord = null; |
Craig Mautner | aab647e | 2013-02-28 16:31:36 -0800 | [diff] [blame] | 4281 | for (int taskNdx = mTaskHistory.size() - 1; maxNum > 0 && taskNdx >= 0; |
| 4282 | --maxNum, --taskNdx) { |
| 4283 | final TaskRecord task = mTaskHistory.get(taskNdx); |
| 4284 | ActivityRecord r = null; |
| 4285 | ActivityRecord top = null; |
| 4286 | int numActivities = 0; |
| 4287 | int numRunning = 0; |
| 4288 | final ArrayList<ActivityRecord> activities = task.mActivities; |
| 4289 | for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) { |
| 4290 | r = activities.get(activityNdx); |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 4291 | |
Craig Mautner | aab647e | 2013-02-28 16:31:36 -0800 | [diff] [blame] | 4292 | // Initialize state for next task if needed. |
| 4293 | if (top == null || (top.state == ActivityState.INITIALIZING)) { |
| 4294 | top = r; |
| 4295 | numActivities = numRunning = 0; |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 4296 | } |
Craig Mautner | aab647e | 2013-02-28 16:31:36 -0800 | [diff] [blame] | 4297 | |
| 4298 | // Add 'r' into the current task. |
| 4299 | numActivities++; |
| 4300 | if (r.app != null && r.app.thread != null) { |
| 4301 | numRunning++; |
| 4302 | } |
| 4303 | |
| 4304 | if (localLOGV) Slog.v( |
| 4305 | TAG, r.intent.getComponent().flattenToShortString() |
| 4306 | + ": task=" + r.task); |
| 4307 | } |
| 4308 | |
| 4309 | RunningTaskInfo ci = new RunningTaskInfo(); |
| 4310 | ci.id = task.taskId; |
| 4311 | ci.baseActivity = r.intent.getComponent(); |
| 4312 | ci.topActivity = top.intent.getComponent(); |
| 4313 | if (top.thumbHolder != null) { |
| 4314 | ci.description = top.thumbHolder.lastDescription; |
| 4315 | } |
| 4316 | ci.numActivities = numActivities; |
| 4317 | ci.numRunning = numRunning; |
| 4318 | //System.out.println( |
| 4319 | // "#" + maxNum + ": " + " descr=" + ci.description); |
| 4320 | if (receiver != null) { |
| 4321 | if (localLOGV) Slog.v( |
| 4322 | TAG, "State=" + top.state + "Idle=" + top.idle |
| 4323 | + " app=" + top.app |
| 4324 | + " thr=" + (top.app != null ? top.app.thread : null)); |
| 4325 | if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) { |
| 4326 | if (top.idle && top.app != null && top.app.thread != null) { |
| 4327 | topRecord = top; |
| 4328 | } else { |
| 4329 | top.thumbnailNeeded = true; |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 4330 | } |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 4331 | } |
Craig Mautner | aab647e | 2013-02-28 16:31:36 -0800 | [diff] [blame] | 4332 | pending.pendingRecords.add(top); |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 4333 | } |
Craig Mautner | aab647e | 2013-02-28 16:31:36 -0800 | [diff] [blame] | 4334 | list.add(ci); |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 4335 | } |
| 4336 | return topRecord; |
| 4337 | } |
| 4338 | |
| 4339 | public void unhandledBackLocked() { |
Craig Mautner | aab647e | 2013-02-28 16:31:36 -0800 | [diff] [blame] | 4340 | final int top = mTaskHistory.size() - 1; |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 4341 | if (DEBUG_SWITCH) Slog.d( |
| 4342 | TAG, "Performing unhandledBack(): top activity at " + top); |
Craig Mautner | aab647e | 2013-02-28 16:31:36 -0800 | [diff] [blame] | 4343 | if (top >= 0) { |
| 4344 | final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities; |
| 4345 | int activityTop = activities.size() - 1; |
| 4346 | if (activityTop > 0) { |
| 4347 | finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null, |
| 4348 | "unhandled-back", true); |
| 4349 | } |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 4350 | } |
| 4351 | } |
| 4352 | |
Craig Mautner | e79d4268 | 2013-04-01 19:01:53 -0700 | [diff] [blame^] | 4353 | void handleAppDiedLocked(ProcessRecord app, boolean restarting) { |
| 4354 | if (mPausingActivity != null && mPausingActivity.app == app) { |
| 4355 | if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG, |
| 4356 | "App died while pausing: " + mPausingActivity); |
| 4357 | mPausingActivity = null; |
| 4358 | } |
| 4359 | if (mLastPausedActivity != null && mLastPausedActivity.app == app) { |
| 4360 | mLastPausedActivity = null; |
| 4361 | } |
| 4362 | |
| 4363 | // Remove this application's activities from active lists. |
| 4364 | boolean hasVisibleActivities = removeHistoryRecordsForAppLocked(app); |
| 4365 | |
| 4366 | if (!restarting) { |
| 4367 | if (!resumeTopActivityLocked(null)) { |
| 4368 | // If there was nothing to resume, and we are not already |
| 4369 | // restarting this process, but there is a visible activity that |
| 4370 | // is hosted by the process... then make sure all visible |
| 4371 | // activities are running, taking care of restarting this |
| 4372 | // process. |
| 4373 | if (hasVisibleActivities) { |
| 4374 | ensureActivitiesVisibleLocked(null, 0); |
| 4375 | } |
| 4376 | } |
| 4377 | } |
| 4378 | } |
| 4379 | |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 4380 | void handleAppCrashLocked(ProcessRecord app) { |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 4381 | for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { |
| 4382 | final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities; |
| 4383 | for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) { |
| 4384 | final ActivityRecord r = activities.get(activityNdx); |
| 4385 | if (r.app == app) { |
| 4386 | Slog.w(TAG, " Force finishing activity " |
| 4387 | + r.intent.getComponent().flattenToShortString()); |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 4388 | finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false); |
Craig Mautner | d44711d | 2013-02-23 11:24:36 -0800 | [diff] [blame] | 4389 | } |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 4390 | } |
| 4391 | } |
| 4392 | } |
| 4393 | |
| 4394 | void dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll, |
| 4395 | boolean dumpClient, String dumpPackage) { |
Craig Mautner | aab647e | 2013-02-28 16:31:36 -0800 | [diff] [blame] | 4396 | for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { |
| 4397 | final TaskRecord task = mTaskHistory.get(taskNdx); |
| 4398 | pw.print(" Task "); pw.print(taskNdx); pw.print(": id #"); pw.println(task.taskId); |
Craig Mautner | 8d341ef | 2013-03-26 09:03:27 -0700 | [diff] [blame] | 4399 | ActivityStackSupervisor.dumpHistoryList(fd, pw, mTaskHistory.get(taskNdx).mActivities, |
Craig Mautner | aab647e | 2013-02-28 16:31:36 -0800 | [diff] [blame] | 4400 | " ", "Hist", true, !dumpAll, dumpClient, dumpPackage); |
| 4401 | } |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 4402 | } |
| 4403 | |
| 4404 | ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) { |
| 4405 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>(); |
| 4406 | |
| 4407 | if ("all".equals(name)) { |
Craig Mautner | aab647e | 2013-02-28 16:31:36 -0800 | [diff] [blame] | 4408 | for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { |
| 4409 | activities.addAll(mTaskHistory.get(taskNdx).mActivities); |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 4410 | } |
| 4411 | } else if ("top".equals(name)) { |
Craig Mautner | aab647e | 2013-02-28 16:31:36 -0800 | [diff] [blame] | 4412 | final int top = mTaskHistory.size() - 1; |
| 4413 | if (top >= 0) { |
| 4414 | final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities; |
| 4415 | int listTop = list.size() - 1; |
| 4416 | if (listTop >= 0) { |
| 4417 | activities.add(list.get(listTop)); |
| 4418 | } |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 4419 | } |
| 4420 | } else { |
| 4421 | ItemMatcher matcher = new ItemMatcher(); |
| 4422 | matcher.build(name); |
| 4423 | |
Craig Mautner | aab647e | 2013-02-28 16:31:36 -0800 | [diff] [blame] | 4424 | for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { |
| 4425 | for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) { |
| 4426 | if (matcher.match(r1, r1.intent.getComponent())) { |
| 4427 | activities.add(r1); |
| 4428 | } |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 4429 | } |
| 4430 | } |
| 4431 | } |
| 4432 | |
| 4433 | return activities; |
| 4434 | } |
| 4435 | |
| 4436 | ActivityRecord restartPackage(String packageName) { |
| 4437 | ActivityRecord starting = topRunningActivityLocked(null); |
| 4438 | |
| 4439 | // All activities that came from the package must be |
| 4440 | // restarted as if there was a config change. |
Craig Mautner | aab647e | 2013-02-28 16:31:36 -0800 | [diff] [blame] | 4441 | for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { |
| 4442 | final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities; |
| 4443 | for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) { |
| 4444 | final ActivityRecord a = activities.get(activityNdx); |
| 4445 | if (a.info.packageName.equals(packageName)) { |
| 4446 | a.forceNewConfig = true; |
| 4447 | if (starting != null && a == starting && a.visible) { |
| 4448 | a.startFreezingScreenLocked(starting.app, |
| 4449 | ActivityInfo.CONFIG_SCREEN_LAYOUT); |
| 4450 | } |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 4451 | } |
| 4452 | } |
| 4453 | } |
| 4454 | |
| 4455 | return starting; |
| 4456 | } |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 4457 | |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 4458 | private void removeActivity(ActivityRecord r) { |
| 4459 | final TaskRecord task = r.task; |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 4460 | // TODO: use ActivityManagerService.removeTask to do this. |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 4461 | if (task.removeActivity(r)) { |
| 4462 | if (DEBUG_ADD_REMOVE) Slog.i(TAG, "removeActivity: Removing from history, task=" |
| 4463 | + task); |
| 4464 | mTaskHistory.remove(task); |
Craig Mautner | 0247fc8 | 2013-02-28 14:32:06 -0800 | [diff] [blame] | 4465 | } |
| 4466 | } |
| 4467 | |
| 4468 | private void setTask(ActivityRecord r, TaskRecord newTask, ThumbnailHolder newThumbHolder, |
| 4469 | boolean isRoot) { |
| 4470 | if (r.task != null) { |
| 4471 | removeActivity(r); |
| 4472 | } |
| 4473 | r.setTask(newTask, newThumbHolder, isRoot); |
| 4474 | } |
| 4475 | |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 4476 | private TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, |
| 4477 | boolean toTop) { |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 4478 | TaskRecord task = new TaskRecord(taskId, info, intent, this); |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 4479 | if (toTop) { |
| 4480 | mTaskHistory.add(task); |
| 4481 | } else { |
| 4482 | mTaskHistory.add(0, task); |
| 4483 | } |
| 4484 | return task; |
| 4485 | } |
Craig Mautner | c00204b | 2013-03-05 15:02:14 -0800 | [diff] [blame] | 4486 | |
| 4487 | ArrayList<TaskRecord> getAllTasks() { |
| 4488 | return new ArrayList<TaskRecord>(mTaskHistory); |
| 4489 | } |
| 4490 | |
| 4491 | void moveTask(int taskId, boolean toTop) { |
Craig Mautner | 8d341ef | 2013-03-26 09:03:27 -0700 | [diff] [blame] | 4492 | final TaskRecord task = mStackSupervisor.anyTaskForIdLocked(taskId); |
Craig Mautner | c00204b | 2013-03-05 15:02:14 -0800 | [diff] [blame] | 4493 | if (task == null) { |
| 4494 | return; |
| 4495 | } |
| 4496 | task.stack.mTaskHistory.remove(task); |
| 4497 | task.stack = this; |
| 4498 | if (toTop) { |
| 4499 | mTaskHistory.add(task); |
| 4500 | } else { |
| 4501 | mTaskHistory.add(0, task); |
| 4502 | } |
| 4503 | } |
| 4504 | |
| 4505 | public int getStackId() { |
| 4506 | return mStackId; |
| 4507 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4508 | } |