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