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