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