blob: cc9e78ec85a60388b1e2f7a094212637ea1c2e75 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 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
19import com.android.server.AttributeCache;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070020import com.android.server.am.ActivityStack.ActivityState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021
22import android.app.Activity;
23import android.content.ComponentName;
24import android.content.Intent;
25import android.content.pm.ActivityInfo;
26import android.content.pm.ApplicationInfo;
27import android.content.res.Configuration;
28import android.graphics.Bitmap;
Dianne Hackborn247fe742011-01-08 17:25:57 -080029import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.os.Bundle;
31import android.os.Message;
32import android.os.Process;
Dianne Hackborn39792d22010-08-19 18:01:52 -070033import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.os.SystemClock;
35import android.util.EventLog;
36import android.util.Log;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070037import android.util.Slog;
Dianne Hackborn0dad3642010-09-09 21:25:35 -070038import android.util.TimeUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.view.IApplicationToken;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080040import android.view.WindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
42import java.io.PrintWriter;
43import java.lang.ref.WeakReference;
44import java.util.ArrayList;
45import java.util.HashSet;
46
47/**
48 * An entry in the history stack, representing an activity.
49 */
Dianne Hackbornf26fd992011-04-08 18:14:09 -070050final class ActivityRecord extends IApplicationToken.Stub {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 final ActivityManagerService service; // owner
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070052 final ActivityStack stack; // owner
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 final ActivityInfo info; // all about me
54 final int launchedFromUid; // always the uid who started the activity.
55 final Intent intent; // the original intent that generated us
56 final ComponentName realActivity; // the intent component, or target of an alias.
57 final String shortComponentName; // the short component name of the intent
58 final String resolvedType; // as per original caller;
59 final String packageName; // the package implementing intent's component
60 final String processName; // process where this component wants to run
61 final String taskAffinity; // as per ActivityInfo.taskAffinity
62 final boolean stateNotNeeded; // As per ActivityInfo.flags
Dianne Hackbornff801ec2011-01-22 18:05:38 -080063 final boolean fullscreen; // covers the full screen?
64 final boolean noDisplay; // activity is not displayed?
The Android Open Source Project4df24232009-03-05 14:34:35 -080065 final boolean componentSpecified; // did caller specifiy an explicit component?
66 final boolean isHomeActivity; // do we consider this to be a home activity?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 final String baseDir; // where activity source (resources etc) located
68 final String resDir; // where public activity source (public resources etc) located
69 final String dataDir; // where activity data should go
70 CharSequence nonLocalizedLabel; // the label information from the package mgr.
71 int labelRes; // the label information from the package mgr.
72 int icon; // resource identifier of activity's icon.
73 int theme; // resource identifier of activity's theme.
Dianne Hackborn247fe742011-01-08 17:25:57 -080074 int realTheme; // actual theme resource we will use, never 0.
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080075 int windowFlags; // custom window flags for preview window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 TaskRecord task; // the task this is in.
Dianne Hackbornf26fd992011-04-08 18:14:09 -070077 ThumbnailHolder thumbHolder; // where our thumbnails should go.
Dianne Hackborn0dad3642010-09-09 21:25:35 -070078 long launchTime; // when we starting launching this activity
79 long startTime; // last time this activity was started
Amith Yamasanieaeb6632009-06-03 15:16:10 -070080 long cpuTimeAtResume; // the cpu time of host process at the time of resuming activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 Configuration configuration; // configuration activity was last running in
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070082 ActivityRecord resultTo; // who started this entry, so will get our reply
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 final String resultWho; // additional identifier for use by resultTo.
84 final int requestCode; // code given by requester (resultTo)
85 ArrayList results; // pending ActivityResult objs we have received
86 HashSet<WeakReference<PendingIntentRecord>> pendingResults; // all pending intents for this act
87 ArrayList newIntents; // any pending new intents for single-top mode
88 HashSet<ConnectionRecord> connections; // All ConnectionRecord we hold
Dianne Hackborn7e269642010-08-25 19:50:20 -070089 UriPermissionOwner uriPermissions; // current special URI access perms.
Dianne Hackbornf26fd992011-04-08 18:14:09 -070090 ProcessRecord app; // if non-null, hosting application
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070091 ActivityState state; // current state we are in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 Bundle icicle; // last saved activity state
93 boolean frontOfTask; // is this the root activity of its task?
94 boolean launchFailed; // set if a launched failed, to abort on 2nd try
95 boolean haveState; // have we gotten the last activity state?
96 boolean stopped; // is activity pause finished?
Dianne Hackborn95fc68f2009-05-19 18:37:45 -070097 boolean delayedResume; // not yet resumed because of stopped app switches?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 boolean finishing; // activity in pending finish list?
99 boolean configDestroy; // need to destroy due to config change?
100 int configChangeFlags; // which config values have changed
101 boolean keysPaused; // has key dispatching been paused for it?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 int launchMode; // the launch mode activity attribute.
103 boolean visible; // does this activity's window need to be shown?
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800104 boolean sleeping; // have we told the activity to sleep?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 boolean waitingVisible; // true if waiting for a new act to become vis
106 boolean nowVisible; // is this activity's window visible?
107 boolean thumbnailNeeded;// has someone requested a thumbnail?
108 boolean idle; // has the activity gone idle?
109 boolean hasBeenLaunched;// has this activity ever been launched?
110 boolean frozenBeforeDestroy;// has been frozen but not yet destroyed.
Daniel Sandler69a48172010-06-23 16:29:36 -0400111 boolean immersive; // immersive mode (don't interrupt if possible)
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400112 boolean forceNewConfig; // force re-create with new config next time
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700114 String stringName; // for caching of toString().
115
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700116 private boolean inHistory; // are we in the history stack?
117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700119 pw.print(prefix); pw.print("packageName="); pw.print(packageName);
120 pw.print(" processName="); pw.println(processName);
121 pw.print(prefix); pw.print("launchedFromUid="); pw.print(launchedFromUid);
122 pw.print(" app="); pw.println(app);
123 pw.print(prefix); pw.println(intent);
124 pw.print(prefix); pw.print("frontOfTask="); pw.print(frontOfTask);
125 pw.print(" task="); pw.println(task);
126 pw.print(prefix); pw.print("taskAffinity="); pw.println(taskAffinity);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700127 pw.print(prefix); pw.print("realActivity=");
128 pw.println(realActivity.flattenToShortString());
129 pw.print(prefix); pw.print("base="); pw.print(baseDir);
130 if (!resDir.equals(baseDir)) pw.print(" res="); pw.print(resDir);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700131 pw.print(" data="); pw.println(dataDir);
132 pw.print(prefix); pw.print("labelRes=0x");
133 pw.print(Integer.toHexString(labelRes));
134 pw.print(" icon=0x"); pw.print(Integer.toHexString(icon));
135 pw.print(" theme=0x"); pw.println(Integer.toHexString(theme));
136 pw.print(prefix); pw.print("stateNotNeeded="); pw.print(stateNotNeeded);
137 pw.print(" componentSpecified="); pw.print(componentSpecified);
138 pw.print(" isHomeActivity="); pw.println(isHomeActivity);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700139 pw.print(prefix); pw.print("config="); pw.println(configuration);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700140 if (resultTo != null || resultWho != null) {
141 pw.print(prefix); pw.print("resultTo="); pw.print(resultTo);
142 pw.print(" resultWho="); pw.print(resultWho);
143 pw.print(" resultCode="); pw.println(requestCode);
144 }
145 if (results != null) {
146 pw.print(prefix); pw.print("results="); pw.println(results);
147 }
148 if (pendingResults != null) {
149 pw.print(prefix); pw.print("pendingResults="); pw.println(pendingResults);
150 }
Dianne Hackborn7e269642010-08-25 19:50:20 -0700151 if (uriPermissions != null) {
152 if (uriPermissions.readUriPermissions != null) {
153 pw.print(prefix); pw.print("readUriPermissions=");
154 pw.println(uriPermissions.readUriPermissions);
155 }
156 if (uriPermissions.writeUriPermissions != null) {
157 pw.print(prefix); pw.print("writeUriPermissions=");
158 pw.println(uriPermissions.writeUriPermissions);
159 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700160 }
161 pw.print(prefix); pw.print("launchFailed="); pw.print(launchFailed);
162 pw.print(" haveState="); pw.print(haveState);
163 pw.print(" icicle="); pw.println(icicle);
164 pw.print(prefix); pw.print("state="); pw.print(state);
165 pw.print(" stopped="); pw.print(stopped);
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700166 pw.print(" delayedResume="); pw.print(delayedResume);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700167 pw.print(" finishing="); pw.println(finishing);
168 pw.print(prefix); pw.print("keysPaused="); pw.print(keysPaused);
169 pw.print(" inHistory="); pw.print(inHistory);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700170 pw.print(" visible="); pw.print(visible);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800171 pw.print(" sleeping="); pw.print(sleeping);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700172 pw.print(" idle="); pw.println(idle);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800173 pw.print(prefix); pw.print("fullscreen="); pw.print(fullscreen);
174 pw.print(" noDisplay="); pw.print(noDisplay);
175 pw.print(" immersive="); pw.print(immersive);
176 pw.print(" launchMode="); pw.println(launchMode);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800177 pw.print(prefix); pw.print("frozenBeforeDestroy="); pw.print(frozenBeforeDestroy);
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400178 pw.print(" thumbnailNeeded="); pw.print(thumbnailNeeded);
179 pw.print(" forceNewConfig="); pw.println(forceNewConfig);
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700180 pw.print(prefix); pw.print("thumbHolder="); pw.println(thumbHolder);
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700181 if (launchTime != 0 || startTime != 0) {
182 pw.print(prefix); pw.print("launchTime=");
183 TimeUtils.formatDuration(launchTime, pw); pw.print(" startTime=");
184 TimeUtils.formatDuration(startTime, pw); pw.println("");
185 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700186 if (waitingVisible || nowVisible) {
187 pw.print(prefix); pw.print("waitingVisible="); pw.print(waitingVisible);
188 pw.print(" nowVisible="); pw.println(nowVisible);
189 }
190 if (configDestroy || configChangeFlags != 0) {
191 pw.print(prefix); pw.print("configDestroy="); pw.print(configDestroy);
192 pw.print(" configChangeFlags=");
193 pw.println(Integer.toHexString(configChangeFlags));
194 }
195 if (connections != null) {
196 pw.print(prefix); pw.print("connections="); pw.println(connections);
197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 }
199
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700200 ActivityRecord(ActivityManagerService _service, ActivityStack _stack, ProcessRecord _caller,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 int _launchedFromUid, Intent _intent, String _resolvedType,
202 ActivityInfo aInfo, Configuration _configuration,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700203 ActivityRecord _resultTo, String _resultWho, int _reqCode,
The Android Open Source Project4df24232009-03-05 14:34:35 -0800204 boolean _componentSpecified) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 service = _service;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700206 stack = _stack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 info = aInfo;
208 launchedFromUid = _launchedFromUid;
209 intent = _intent;
210 shortComponentName = _intent.getComponent().flattenToShortString();
211 resolvedType = _resolvedType;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800212 componentSpecified = _componentSpecified;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 configuration = _configuration;
214 resultTo = _resultTo;
215 resultWho = _resultWho;
216 requestCode = _reqCode;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700217 state = ActivityState.INITIALIZING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 frontOfTask = false;
219 launchFailed = false;
220 haveState = false;
221 stopped = false;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700222 delayedResume = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 finishing = false;
224 configDestroy = false;
225 keysPaused = false;
226 inHistory = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 visible = true;
228 waitingVisible = false;
229 nowVisible = false;
230 thumbnailNeeded = false;
231 idle = false;
232 hasBeenLaunched = false;
233
234 if (aInfo != null) {
235 if (aInfo.targetActivity == null
236 || aInfo.launchMode == ActivityInfo.LAUNCH_MULTIPLE
237 || aInfo.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
238 realActivity = _intent.getComponent();
239 } else {
240 realActivity = new ComponentName(aInfo.packageName,
241 aInfo.targetActivity);
242 }
243 taskAffinity = aInfo.taskAffinity;
244 stateNotNeeded = (aInfo.flags&
245 ActivityInfo.FLAG_STATE_NOT_NEEDED) != 0;
246 baseDir = aInfo.applicationInfo.sourceDir;
247 resDir = aInfo.applicationInfo.publicSourceDir;
248 dataDir = aInfo.applicationInfo.dataDir;
249 nonLocalizedLabel = aInfo.nonLocalizedLabel;
250 labelRes = aInfo.labelRes;
251 if (nonLocalizedLabel == null && labelRes == 0) {
252 ApplicationInfo app = aInfo.applicationInfo;
253 nonLocalizedLabel = app.nonLocalizedLabel;
254 labelRes = app.labelRes;
255 }
256 icon = aInfo.getIconResource();
257 theme = aInfo.getThemeResource();
Dianne Hackborn247fe742011-01-08 17:25:57 -0800258 realTheme = theme;
259 if (realTheme == 0) {
260 realTheme = aInfo.applicationInfo.targetSdkVersion
261 < Build.VERSION_CODES.HONEYCOMB
262 ? android.R.style.Theme
263 : android.R.style.Theme_Holo;
264 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -0800265 if ((aInfo.flags&ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
266 windowFlags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
267 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 if ((aInfo.flags&ActivityInfo.FLAG_MULTIPROCESS) != 0
269 && _caller != null
270 && (aInfo.applicationInfo.uid == Process.SYSTEM_UID
271 || aInfo.applicationInfo.uid == _caller.info.uid)) {
272 processName = _caller.processName;
273 } else {
274 processName = aInfo.processName;
275 }
276
277 if (intent != null && (aInfo.flags & ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS) != 0) {
278 intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
279 }
280
281 packageName = aInfo.applicationInfo.packageName;
282 launchMode = aInfo.launchMode;
283
284 AttributeCache.Entry ent = AttributeCache.instance().get(packageName,
Dianne Hackborn247fe742011-01-08 17:25:57 -0800285 realTheme, com.android.internal.R.styleable.Window);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 fullscreen = ent != null && !ent.array.getBoolean(
287 com.android.internal.R.styleable.Window_windowIsFloating, false)
288 && !ent.array.getBoolean(
289 com.android.internal.R.styleable.Window_windowIsTranslucent, false);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800290 noDisplay = ent != null && ent.array.getBoolean(
291 com.android.internal.R.styleable.Window_windowNoDisplay, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292
The Android Open Source Project4df24232009-03-05 14:34:35 -0800293 if (!_componentSpecified || _launchedFromUid == Process.myUid()
294 || _launchedFromUid == 0) {
295 // If we know the system has determined the component, then
296 // we can consider this to be a home activity...
297 if (Intent.ACTION_MAIN.equals(_intent.getAction()) &&
298 _intent.hasCategory(Intent.CATEGORY_HOME) &&
299 _intent.getCategories().size() == 1 &&
300 _intent.getData() == null &&
301 _intent.getType() == null &&
302 (intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
303 !"android".equals(realActivity.getClassName())) {
304 // This sure looks like a home activity!
305 // Note the last check is so we don't count the resolver
306 // activity as being home... really, we don't care about
307 // doing anything special with something that comes from
308 // the core framework package.
309 isHomeActivity = true;
310 } else {
311 isHomeActivity = false;
312 }
313 } else {
314 isHomeActivity = false;
315 }
Daniel Sandler69a48172010-06-23 16:29:36 -0400316
317 immersive = (aInfo.flags & ActivityInfo.FLAG_IMMERSIVE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 } else {
319 realActivity = null;
320 taskAffinity = null;
321 stateNotNeeded = false;
322 baseDir = null;
323 resDir = null;
324 dataDir = null;
325 processName = null;
326 packageName = null;
327 fullscreen = true;
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800328 noDisplay = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800329 isHomeActivity = false;
Daniel Sandler69a48172010-06-23 16:29:36 -0400330 immersive = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 }
332 }
333
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700334 void setTask(TaskRecord newTask, ThumbnailHolder newThumbHolder, boolean isRoot) {
335 if (inHistory && !finishing) {
336 if (task != null) {
337 task.numActivities--;
338 }
339 if (newTask != null) {
340 newTask.numActivities++;
341 }
342 }
343 if (newThumbHolder == null) {
344 newThumbHolder = newTask;
345 }
346 task = newTask;
347 if (!isRoot && (intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
348 // This is the start of a new sub-task.
349 if (thumbHolder == null) {
350 thumbHolder = new ThumbnailHolder();
351 }
352 } else {
353 thumbHolder = newThumbHolder;
354 }
355 }
356
357 void putInHistory() {
358 if (!inHistory) {
359 inHistory = true;
360 if (task != null && !finishing) {
361 task.numActivities++;
362 }
363 }
364 }
365
366 void takeFromHistory() {
367 if (inHistory) {
368 inHistory = false;
369 if (task != null && !finishing) {
370 task.numActivities--;
371 }
372 }
373 }
374
375 boolean isInHistory() {
376 return inHistory;
377 }
378
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -0800379 void makeFinishing() {
380 if (!finishing) {
381 finishing = true;
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700382 if (task != null && inHistory) {
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -0800383 task.numActivities--;
384 }
385 }
386 }
387
Dianne Hackborn7e269642010-08-25 19:50:20 -0700388 UriPermissionOwner getUriPermissionsLocked() {
389 if (uriPermissions == null) {
390 uriPermissions = new UriPermissionOwner(service, this);
391 }
392 return uriPermissions;
393 }
394
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700395 void addResultLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 int requestCode, int resultCode,
397 Intent resultData) {
398 ActivityResult r = new ActivityResult(from, resultWho,
399 requestCode, resultCode, resultData);
400 if (results == null) {
401 results = new ArrayList();
402 }
403 results.add(r);
404 }
405
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700406 void removeResultsLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 int requestCode) {
408 if (results != null) {
409 for (int i=results.size()-1; i>=0; i--) {
410 ActivityResult r = (ActivityResult)results.get(i);
411 if (r.mFrom != from) continue;
412 if (r.mResultWho == null) {
413 if (resultWho != null) continue;
414 } else {
415 if (!r.mResultWho.equals(resultWho)) continue;
416 }
417 if (r.mRequestCode != requestCode) continue;
418
419 results.remove(i);
420 }
421 }
422 }
423
424 void addNewIntentLocked(Intent intent) {
425 if (newIntents == null) {
426 newIntents = new ArrayList();
427 }
428 newIntents.add(intent);
429 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700430
431 /**
432 * Deliver a new Intent to an existing activity, so that its onNewIntent()
433 * method will be called at the proper time.
434 */
Dianne Hackborn39792d22010-08-19 18:01:52 -0700435 final void deliverNewIntentLocked(int callingUid, Intent intent) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700436 boolean sent = false;
437 if (state == ActivityState.RESUMED
438 && app != null && app.thread != null) {
439 try {
440 ArrayList<Intent> ar = new ArrayList<Intent>();
Dianne Hackborn39792d22010-08-19 18:01:52 -0700441 intent = new Intent(intent);
442 ar.add(intent);
443 service.grantUriPermissionFromIntentLocked(callingUid, packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -0700444 intent, getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700445 app.thread.scheduleNewIntent(ar, this);
446 sent = true;
Dianne Hackborn39792d22010-08-19 18:01:52 -0700447 } catch (RemoteException e) {
448 Slog.w(ActivityManagerService.TAG,
449 "Exception thrown sending new intent to " + this, e);
450 } catch (NullPointerException e) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700451 Slog.w(ActivityManagerService.TAG,
452 "Exception thrown sending new intent to " + this, e);
453 }
454 }
455 if (!sent) {
456 addNewIntentLocked(new Intent(intent));
457 }
458 }
459
460 void removeUriPermissionsLocked() {
Dianne Hackborn7e269642010-08-25 19:50:20 -0700461 if (uriPermissions != null) {
462 uriPermissions.removeUriPermissionsLocked();
463 uriPermissions = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700464 }
465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466
467 void pauseKeyDispatchingLocked() {
468 if (!keysPaused) {
469 keysPaused = true;
470 service.mWindowManager.pauseKeyDispatching(this);
471 }
472 }
473
474 void resumeKeyDispatchingLocked() {
475 if (keysPaused) {
476 keysPaused = false;
477 service.mWindowManager.resumeKeyDispatching(this);
478 }
479 }
480
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700481 void updateThumbnail(Bitmap newThumbnail, CharSequence description) {
482 if ((intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
483 // This is a logical break in the task; it repre
484 }
485 if (thumbHolder != null) {
486 if (newThumbnail != null) {
487 thumbHolder.lastThumbnail = newThumbnail;
488 }
489 thumbHolder.lastDescription = description;
490 }
491 }
492
493 void clearThumbnail() {
494 if (thumbHolder != null) {
495 thumbHolder.lastThumbnail = null;
496 thumbHolder.lastDescription = null;
497 }
498 }
499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 // IApplicationToken
501
502 public boolean mayFreezeScreenLocked(ProcessRecord app) {
503 // Only freeze the screen if this activity is currently attached to
504 // an application, and that application is not blocked or unresponding.
505 // In any other case, we can't count on getting the screen unfrozen,
506 // so it is best to leave as-is.
Dianne Hackborn5f4d6432010-12-21 20:40:11 -0800507 return app != null && !app.crashing && !app.notResponding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 }
509
510 public void startFreezingScreenLocked(ProcessRecord app, int configChanges) {
511 if (mayFreezeScreenLocked(app)) {
512 service.mWindowManager.startAppFreezingScreen(this, configChanges);
513 }
514 }
515
516 public void stopFreezingScreenLocked(boolean force) {
517 if (force || frozenBeforeDestroy) {
518 frozenBeforeDestroy = false;
519 service.mWindowManager.stopAppFreezingScreen(this, force);
520 }
521 }
522
523 public void windowsVisible() {
524 synchronized(service) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700525 if (launchTime != 0) {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700526 final long curTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700527 final long thisTime = curTime - launchTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700528 final long totalTime = stack.mInitialStartTime != 0
529 ? (curTime - stack.mInitialStartTime) : thisTime;
Dianne Hackborn6447ca32009-04-07 19:50:08 -0700530 if (ActivityManagerService.SHOW_ACTIVITY_START_TIME) {
Doug Zongker2bec3d42009-12-04 12:52:44 -0800531 EventLog.writeEvent(EventLogTags.ACTIVITY_LAUNCH_TIME,
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700532 System.identityHashCode(this), shortComponentName,
533 thisTime, totalTime);
534 StringBuilder sb = service.mStringBuilder;
535 sb.setLength(0);
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700536 sb.append("Displayed ");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700537 sb.append(shortComponentName);
538 sb.append(": ");
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700539 TimeUtils.formatDuration(thisTime, sb);
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700540 if (thisTime != totalTime) {
541 sb.append(" (total ");
542 TimeUtils.formatDuration(totalTime, sb);
543 sb.append(")");
544 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700545 Log.i(ActivityManagerService.TAG, sb.toString());
Dianne Hackborn6447ca32009-04-07 19:50:08 -0700546 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700547 stack.reportActivityLaunchedLocked(false, this, thisTime, totalTime);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700548 if (totalTime > 0) {
549 service.mUsageStatsService.noteLaunchTime(realActivity, (int)totalTime);
Dianne Hackborn6447ca32009-04-07 19:50:08 -0700550 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700551 launchTime = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700552 stack.mInitialStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700554 startTime = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700555 stack.reportActivityVisibleLocked(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 if (ActivityManagerService.DEBUG_SWITCH) Log.v(
557 ActivityManagerService.TAG, "windowsVisible(): " + this);
558 if (!nowVisible) {
559 nowVisible = true;
560 if (!idle) {
561 // Instead of doing the full stop routine here, let's just
562 // hide any activities we now can, and let them stop when
563 // the normal idle happens.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700564 stack.processStoppingActivitiesLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 } else {
566 // If this activity was already idle, then we now need to
567 // make sure we perform the full stop of any activities
568 // that are waiting to do so. This is because we won't
569 // do that while they are still waiting for this one to
570 // become visible.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700571 final int N = stack.mWaitingVisibleActivities.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 if (N > 0) {
573 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700574 ActivityRecord r = (ActivityRecord)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700575 stack.mWaitingVisibleActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 r.waitingVisible = false;
577 if (ActivityManagerService.DEBUG_SWITCH) Log.v(
578 ActivityManagerService.TAG,
579 "Was waiting for visible: " + r);
580 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700581 stack.mWaitingVisibleActivities.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 Message msg = Message.obtain();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700583 msg.what = ActivityStack.IDLE_NOW_MSG;
584 stack.mHandler.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 }
586 }
587 service.scheduleAppGcsLocked();
588 }
589 }
590 }
591
592 public void windowsGone() {
593 if (ActivityManagerService.DEBUG_SWITCH) Log.v(
594 ActivityManagerService.TAG, "windowsGone(): " + this);
595 nowVisible = false;
596 }
597
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700598 private ActivityRecord getWaitingHistoryRecordLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 // First find the real culprit... if we are waiting
600 // for another app to start, then we have paused dispatching
601 // for this activity.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700602 ActivityRecord r = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 if (r.waitingVisible) {
604 // Hmmm, who might we be waiting for?
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700605 r = stack.mResumedActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700607 r = stack.mPausingActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 }
609 // Both of those null? Fall back to 'this' again
610 if (r == null) {
611 r = this;
612 }
613 }
614
615 return r;
616 }
617
618 public boolean keyDispatchingTimedOut() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700619 ActivityRecord r;
Dianne Hackbornad5499d2010-03-29 18:08:45 -0700620 ProcessRecord anrApp = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 synchronized(service) {
Dianne Hackbornad5499d2010-03-29 18:08:45 -0700622 r = getWaitingHistoryRecordLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 if (r != null && r.app != null) {
624 if (r.app.debugging) {
625 return false;
626 }
627
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700628 if (service.mDidDexOpt) {
629 // Give more time since we were dexopting.
630 service.mDidDexOpt = false;
631 return false;
632 }
633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 if (r.app.instrumentationClass == null) {
Dianne Hackbornad5499d2010-03-29 18:08:45 -0700635 anrApp = r.app;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 } else {
637 Bundle info = new Bundle();
638 info.putString("shortMsg", "keyDispatchingTimedOut");
639 info.putString("longMsg", "Timed out while dispatching key event");
640 service.finishInstrumentationLocked(
641 r.app, Activity.RESULT_CANCELED, info);
642 }
643 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 }
Dianne Hackbornad5499d2010-03-29 18:08:45 -0700645
646 if (anrApp != null) {
647 service.appNotResponding(anrApp, r, this,
648 "keyDispatchingTimedOut");
649 }
650
651 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 }
653
654 /** Returns the key dispatching timeout for this application token. */
655 public long getKeyDispatchingTimeout() {
656 synchronized(service) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700657 ActivityRecord r = getWaitingHistoryRecordLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 if (r == null || r.app == null
659 || r.app.instrumentationClass == null) {
660 return ActivityManagerService.KEY_DISPATCHING_TIMEOUT;
661 }
662
663 return ActivityManagerService.INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT;
664 }
665 }
666
667 /**
668 * This method will return true if the activity is either visible, is becoming visible, is
669 * currently pausing, or is resumed.
670 */
671 public boolean isInterestingToUserLocked() {
672 return visible || nowVisible || state == ActivityState.PAUSING ||
673 state == ActivityState.RESUMED;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800674 }
675
676 public void setSleeping(boolean _sleeping) {
677 if (sleeping == _sleeping) {
678 return;
679 }
680 if (app != null && app.thread != null) {
681 try {
682 app.thread.scheduleSleeping(this, _sleeping);
683 if (sleeping && !stack.mGoingToSleepActivities.contains(this)) {
684 stack.mGoingToSleepActivities.add(this);
685 }
686 sleeping = _sleeping;
687 } catch (RemoteException e) {
688 Slog.w(ActivityStack.TAG, "Exception thrown when sleeping: "
689 + intent.getComponent(), e);
690 }
691 }
692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 public String toString() {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700695 if (stringName != null) {
696 return stringName;
697 }
698 StringBuilder sb = new StringBuilder(128);
Dianne Hackborn30d71892010-12-11 10:37:55 -0800699 sb.append("ActivityRecord{");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700700 sb.append(Integer.toHexString(System.identityHashCode(this)));
701 sb.append(' ');
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700702 sb.append(intent.getComponent().flattenToShortString());
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700703 sb.append('}');
704 return stringName = sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 }
706}