blob: a3b9766dbce063789f0ebf102a14b67b82921a45 [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 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
17package com.android.server.am;
18
Craig Mautner8d341ef2013-03-26 09:03:27 -070019import static com.android.server.am.ActivityManagerService.DEBUG_CLEANUP;
20import static com.android.server.am.ActivityManagerService.DEBUG_PAUSE;
21import static com.android.server.am.ActivityManagerService.TAG;
22
Craig Mautner20e72272013-04-01 13:45:53 -070023import android.app.IThumbnailReceiver;
24import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner2219a1b2013-03-25 09:44:30 -070025import android.content.Context;
26import android.content.Intent;
27import android.content.pm.ActivityInfo;
Craig Mautner8d341ef2013-03-26 09:03:27 -070028import android.os.Bundle;
Craig Mautner2219a1b2013-03-25 09:44:30 -070029import android.os.Looper;
Craig Mautner8d341ef2013-03-26 09:03:27 -070030import android.os.RemoteException;
Craig Mautner8d341ef2013-03-26 09:03:27 -070031import android.util.Slog;
Craig Mautner2219a1b2013-03-25 09:44:30 -070032
Craig Mautner8d341ef2013-03-26 09:03:27 -070033import java.io.FileDescriptor;
34import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -070035import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -070036import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -070037import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -070038
39public class ActivityStackSupervisor {
Craig Mautner2219a1b2013-03-25 09:44:30 -070040 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -070041
42 final ActivityManagerService mService;
Craig Mautner2219a1b2013-03-25 09:44:30 -070043 final Context mContext;
44 final Looper mLooper;
Craig Mautner27084302013-03-25 08:05:25 -070045
46 /** Dismiss the keyguard after the next activity is displayed? */
47 private boolean mDismissKeyguardOnNextActivity = false;
48
Craig Mautner8d341ef2013-03-26 09:03:27 -070049 /** Identifier counter for all ActivityStacks */
50 private int mLastStackId = 0;
51
52 /** Task identifier that activities are currently being started in. Incremented each time a
53 * new task is created. */
54 private int mCurTaskId = 0;
55
56 /** The stack containing the launcher app */
Craig Mautner2219a1b2013-03-25 09:44:30 -070057 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -070058
59 /** The stack currently receiving input or launching the next activity */
Craig Mautner2219a1b2013-03-25 09:44:30 -070060 private ActivityStack mMainStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -070061
62 /** All the non-launcher stacks */
Craig Mautner2219a1b2013-03-25 09:44:30 -070063 private ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
64
65 public ActivityStackSupervisor(ActivityManagerService service, Context context,
66 Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -070067 mService = service;
Craig Mautner2219a1b2013-03-25 09:44:30 -070068 mContext = context;
69 mLooper = looper;
70 }
71
72 void init() {
73 mHomeStack = new ActivityStack(mService, mContext, mLooper, HOME_STACK_ID, this);
74 setMainStack(mHomeStack);
Craig Mautner8d341ef2013-03-26 09:03:27 -070075 mStacks.add(mHomeStack);
Craig Mautner27084302013-03-25 08:05:25 -070076 }
77
78 void dismissKeyguard() {
79 if (mDismissKeyguardOnNextActivity) {
80 mDismissKeyguardOnNextActivity = false;
81 mService.mWindowManager.dismissKeyguard();
82 }
83 }
84
Craig Mautner20e72272013-04-01 13:45:53 -070085 boolean isHomeStackMain() {
86 return mHomeStack == mMainStack;
87 }
88
Craig Mautner2219a1b2013-03-25 09:44:30 -070089 boolean isMainStack(ActivityStack stack) {
90 return stack == mMainStack;
91 }
92
Craig Mautner20e72272013-04-01 13:45:53 -070093 ActivityStack getMainStack() {
94 return mMainStack;
95 }
96
Craig Mautner2219a1b2013-03-25 09:44:30 -070097 void setMainStack(ActivityStack stack) {
98 mMainStack = stack;
99 }
100
Craig Mautner27084302013-03-25 08:05:25 -0700101 void setDismissKeyguard(boolean dismiss) {
102 mDismissKeyguardOnNextActivity = dismiss;
103 }
104
Craig Mautner8d341ef2013-03-26 09:03:27 -0700105 TaskRecord anyTaskForIdLocked(int id) {
106 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
107 ActivityStack stack = mStacks.get(stackNdx);
108 TaskRecord task = stack.taskForIdLocked(id);
109 if (task != null) {
110 return task;
111 }
112 }
113 return null;
114 }
115
116 int getNextTaskId() {
117 do {
118 mCurTaskId++;
119 if (mCurTaskId <= 0) {
120 mCurTaskId = 1;
121 }
122 } while (anyTaskForIdLocked(mCurTaskId) != null);
123 return mCurTaskId;
124 }
125
Craig Mautner20e72272013-04-01 13:45:53 -0700126 boolean attachApplicationLocked(ProcessRecord app, boolean headless) throws Exception {
127 boolean didSomething = false;
128 final String processName = app.processName;
129 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
130 final ActivityStack stack = mStacks.get(stackNdx);
131 ActivityRecord hr = stack.topRunningActivityLocked(null);
132 if (hr != null) {
133 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
134 && processName.equals(hr.processName)) {
135 try {
136 if (headless) {
137 Slog.e(TAG, "Starting activities not supported on headless device: "
138 + hr);
139 } else if (stack.realStartActivityLocked(hr, app, true, true)) {
140 didSomething = true;
141 }
142 } catch (Exception e) {
143 Slog.w(TAG, "Exception in new application when starting activity "
144 + hr.intent.getComponent().flattenToShortString(), e);
145 throw e;
146 }
147 } else {
148 stack.ensureActivitiesVisibleLocked(hr, null, processName, 0);
149 }
150 }
151 }
152 return didSomething;
153 }
154
155 boolean allResumedActivitiesIdle() {
156 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
157 if (mStacks.get(stackNdx).mResumedActivity == null ||
158 !mStacks.get(stackNdx).mResumedActivity.idle) {
159 return false;
160 }
161 }
162 return true;
163 }
164
165 ActivityRecord getTasksLocked(int maxNum, IThumbnailReceiver receiver,
166 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
167 ActivityRecord r = null;
168 final int numStacks = mStacks.size();
169 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
170 final ActivityStack stack = mStacks.get(stackNdx);
171 final ActivityRecord ar =
172 stack.getTasksLocked(maxNum - list.size(), receiver, pending, list);
173 if (isMainStack(stack)) {
174 r = ar;
175 }
176 }
177 return r;
178 }
179
Craig Mautner2219a1b2013-03-25 09:44:30 -0700180 void startHomeActivity(Intent intent, ActivityInfo aInfo) {
181 mHomeStack.startActivityLocked(null, intent, null, aInfo, null, null, 0, 0, 0, null, 0,
182 null, false, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700183 }
184
185 void handleAppDiedLocked(ProcessRecord app, boolean restarting) {
186 // Just in case.
187 final int numStacks = mStacks.size();
188 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
189 final ActivityStack stack = mStacks.get(stackNdx);
190 if (stack.mPausingActivity != null && stack.mPausingActivity.app == app) {
191 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
192 "App died while pausing: " + stack.mPausingActivity);
193 stack.mPausingActivity = null;
194 }
195 if (stack.mLastPausedActivity != null && stack.mLastPausedActivity.app == app) {
196 stack.mLastPausedActivity = null;
197 }
198
199 // Remove this application's activities from active lists.
200 boolean hasVisibleActivities = stack.removeHistoryRecordsForAppLocked(app);
201
202 if (!restarting) {
203 if (!stack.resumeTopActivityLocked(null)) {
204 // If there was nothing to resume, and we are not already
205 // restarting this process, but there is a visible activity that
206 // is hosted by the process... then make sure all visible
207 // activities are running, taking care of restarting this
208 // process.
209 if (hasVisibleActivities) {
210 stack.ensureActivitiesVisibleLocked(null, 0);
211 }
212 }
213 }
214 }
215 }
216
217 void closeSystemDialogsLocked() {
218 final int numStacks = mStacks.size();
219 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
220 final ActivityStack stack = mStacks.get(stackNdx);
221 stack.closeSystemDialogsLocked();
222 }
223 }
224
225 /**
226 * @return true if some activity was finished (or would have finished if doit were true).
227 */
228 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
229 boolean didSomething = false;
230 final int numStacks = mStacks.size();
231 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
232 final ActivityStack stack = mStacks.get(stackNdx);
233 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
234 didSomething = true;
235 }
236 }
237 return didSomething;
238 }
239
240 void resumeTopActivityLocked() {
241 final int start, end;
Craig Mautner20e72272013-04-01 13:45:53 -0700242 if (isHomeStackMain()) {
Craig Mautner8d341ef2013-03-26 09:03:27 -0700243 start = 0;
244 end = 1;
245 } else {
246 start = 1;
247 end = mStacks.size();
248 }
249 for (int stackNdx = start; stackNdx < end; ++stackNdx) {
250 mStacks.get(stackNdx).resumeTopActivityLocked(null);
251 }
252 }
253
254 void finishTopRunningActivityLocked(ProcessRecord app) {
255 final int numStacks = mStacks.size();
256 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
257 final ActivityStack stack = mStacks.get(stackNdx);
258 stack.finishTopRunningActivityLocked(app);
259 }
260 }
261
262 void scheduleIdleLocked() {
263 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
264 mStacks.get(stackNdx).scheduleIdleLocked();
265 }
266 }
267
268 void findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
269 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
270 if (mStacks.get(stackNdx).findTaskToMoveToFrontLocked(taskId, flags, options)) {
271 return;
272 }
273 }
274 }
275
276 private ActivityStack getStack(int stackId) {
277 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
278 final ActivityStack stack = mStacks.get(stackNdx);
279 if (stack.getStackId() == stackId) {
280 return stack;
281 }
282 }
283 return null;
284 }
285
286 int createStack(int relativeStackId, int position, float weight) {
287 synchronized (this) {
288 while (true) {
289 if (++mLastStackId <= HOME_STACK_ID) {
290 mLastStackId = HOME_STACK_ID + 1;
291 }
292 if (getStack(mLastStackId) == null) {
293 break;
294 }
295 }
296 mStacks.add(new ActivityStack(mService, mContext, mLooper, mLastStackId, this));
297 return mLastStackId;
298 }
299 }
300
301 void moveTaskToStack(int taskId, int stackId, boolean toTop) {
302 final ActivityStack stack = getStack(stackId);
303 if (stack == null) {
304 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
305 return;
306 }
307 stack.moveTask(taskId, toTop);
308 }
309
310 void goingToSleepLocked() {
311 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
312 mStacks.get(stackNdx).stopIfSleepingLocked();
313 }
314 }
315
316 boolean shutdownLocked(int timeout) {
317 boolean timedout = false;
318 final int numStacks = mStacks.size();
319 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
320 final ActivityStack stack = mStacks.get(stackNdx);
321 if (stack.mResumedActivity != null) {
322 stack.stopIfSleepingLocked();
323 final long endTime = System.currentTimeMillis() + timeout;
324 while (stack.mResumedActivity != null || stack.mPausingActivity != null) {
325 long delay = endTime - System.currentTimeMillis();
326 if (delay <= 0) {
327 Slog.w(TAG, "Activity manager shutdown timed out");
328 timedout = true;
329 break;
330 }
331 try {
332 mService.wait();
333 } catch (InterruptedException e) {
334 }
335 }
336 }
337 }
338 return timedout;
339 }
340
341 void comeOutOfSleepIfNeededLocked() {
342 final int numStacks = mStacks.size();
343 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
344 final ActivityStack stack = mStacks.get(stackNdx);
345 stack.awakeFromSleepingLocked();
346 stack.resumeTopActivityLocked(null);
347 }
348 }
349
350 void handleAppCrashLocked(ProcessRecord app) {
351 final int numStacks = mStacks.size();
352 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
353 final ActivityStack stack = mStacks.get(stackNdx);
354 stack.handleAppCrashLocked(app);
355 }
356 }
357
358 boolean updateConfigurationLocked(int changes, ActivityRecord starting) {
359 boolean kept = true;
360 final int numStacks = mStacks.size();
361 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
362 final ActivityStack stack = mStacks.get(stackNdx);
363 if (changes != 0 && starting == null) {
364 // If the configuration changed, and the caller is not already
365 // in the process of starting an activity, then find the top
366 // activity to check if its configuration needs to change.
367 starting = stack.topRunningActivityLocked(null);
368 }
369
370 if (starting != null) {
371 if (!stack.ensureActivityConfigurationLocked(starting, changes)) {
372 kept = false;
373 }
374 // And we need to make sure at this point that all other activities
375 // are made visible with the correct configuration.
376 stack.ensureActivitiesVisibleLocked(starting, changes);
377 }
378 }
379 return kept;
380 }
381
382 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
383 final int numStacks = mStacks.size();
384 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
385 final ActivityStack stack = mStacks.get(stackNdx);
386 stack.scheduleDestroyActivities(app, false, reason);
387 }
388 }
389
390 boolean switchUserLocked(int userId, UserStartedState uss) {
391 boolean haveActivities = false;
392 final int numStacks = mStacks.size();
393 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
394 final ActivityStack stack = mStacks.get(stackNdx);
395 haveActivities |= stack.switchUserLocked(userId, uss);
396 }
397 return haveActivities;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700398 }
399
Craig Mautner27084302013-03-25 08:05:25 -0700400 public void dump(PrintWriter pw, String prefix) {
401 pw.print(prefix); pw.print("mDismissKeyguardOnNextActivity:");
402 pw.println(mDismissKeyguardOnNextActivity);
403 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700404
Craig Mautner20e72272013-04-01 13:45:53 -0700405 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
406 return mMainStack.getDumpActivitiesLocked(name);
407 }
408
Craig Mautner8d341ef2013-03-26 09:03:27 -0700409 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
410 boolean dumpClient, String dumpPackage) {
411 final int numStacks = mStacks.size();
412 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
413 final ActivityStack stack = mStacks.get(stackNdx);
414 pw.print(" Stack #"); pw.print(mStacks.indexOf(stack)); pw.println(":");
415 stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage);
416 pw.println(" ");
417 pw.println(" Running activities (most recent first):");
418 dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false, !dumpAll, false,
419 dumpPackage);
420 if (stack.mWaitingVisibleActivities.size() > 0) {
421 pw.println(" ");
422 pw.println(" Activities waiting for another to become visible:");
423 dumpHistoryList(fd, pw, stack.mWaitingVisibleActivities, " ", "Wait", false,
424 !dumpAll, false, dumpPackage);
425 }
426 if (stack.mStoppingActivities.size() > 0) {
427 pw.println(" ");
428 pw.println(" Activities waiting to stop:");
429 dumpHistoryList(fd, pw, stack.mStoppingActivities, " ", "Stop", false,
430 !dumpAll, false, dumpPackage);
431 }
432 if (stack.mGoingToSleepActivities.size() > 0) {
433 pw.println(" ");
434 pw.println(" Activities waiting to sleep:");
435 dumpHistoryList(fd, pw, stack.mGoingToSleepActivities, " ", "Sleep", false,
436 !dumpAll, false, dumpPackage);
437 }
438 if (stack.mFinishingActivities.size() > 0) {
439 pw.println(" ");
440 pw.println(" Activities waiting to finish:");
441 dumpHistoryList(fd, pw, stack.mFinishingActivities, " ", "Fin", false,
442 !dumpAll, false, dumpPackage);
443 }
444 }
445
446 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
447 final ActivityStack stack = mStacks.get(stackNdx);
448 pw.print(" Stack #"); pw.println(mStacks.indexOf(stack));
449 if (stack.mPausingActivity != null) {
450 pw.println(" mPausingActivity: " + stack.mPausingActivity);
451 }
452 pw.println(" mResumedActivity: " + stack.mResumedActivity);
453 if (dumpAll) {
454 pw.println(" mLastPausedActivity: " + stack.mLastPausedActivity);
455 pw.println(" mSleepTimeout: " + stack.mSleepTimeout);
456 }
457 }
458
459 if (dumpAll) {
460 pw.println(" ");
461 pw.println(" mCurTaskId: " + mCurTaskId);
462 }
463 return true;
464 }
465
466 static final void dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
467 String prefix, String label, boolean complete, boolean brief, boolean client,
468 String dumpPackage) {
469 TaskRecord lastTask = null;
470 boolean needNL = false;
471 final String innerPrefix = prefix + " ";
472 final String[] args = new String[0];
473 for (int i=list.size()-1; i>=0; i--) {
474 final ActivityRecord r = list.get(i);
475 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
476 continue;
477 }
478 final boolean full = !brief && (complete || !r.isInHistory());
479 if (needNL) {
480 pw.println(" ");
481 needNL = false;
482 }
483 if (lastTask != r.task) {
484 lastTask = r.task;
485 pw.print(prefix);
486 pw.print(full ? "* " : " ");
487 pw.println(lastTask);
488 if (full) {
489 lastTask.dump(pw, prefix + " ");
490 } else if (complete) {
491 // Complete + brief == give a summary. Isn't that obvious?!?
492 if (lastTask.intent != null) {
493 pw.print(prefix); pw.print(" ");
494 pw.println(lastTask.intent.toInsecureStringWithClip());
495 }
496 }
497 }
498 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
499 pw.print(" #"); pw.print(i); pw.print(": ");
500 pw.println(r);
501 if (full) {
502 r.dump(pw, innerPrefix);
503 } else if (complete) {
504 // Complete + brief == give a summary. Isn't that obvious?!?
505 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
506 if (r.app != null) {
507 pw.print(innerPrefix); pw.println(r.app);
508 }
509 }
510 if (client && r.app != null && r.app.thread != null) {
511 // flush anything that is already in the PrintWriter since the thread is going
512 // to write to the file descriptor directly
513 pw.flush();
514 try {
515 TransferPipe tp = new TransferPipe();
516 try {
517 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
518 r.appToken, innerPrefix, args);
519 // Short timeout, since blocking here can
520 // deadlock with the application.
521 tp.go(fd, 2000);
522 } finally {
523 tp.kill();
524 }
525 } catch (IOException e) {
526 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
527 } catch (RemoteException e) {
528 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
529 }
530 needNL = true;
531 }
532 }
533 }
Craig Mautner27084302013-03-25 08:05:25 -0700534}