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