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