blob: cb0a0f0c2771e1b4e93cc5f1b02079fc54bdeefd [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 Hackborn7e269642010-08-25 19:50:20 -070050class 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 Hackborn0dad3642010-09-09 21:25:35 -070077 long launchTime; // when we starting launching this activity
78 long startTime; // last time this activity was started
Amith Yamasanieaeb6632009-06-03 15:16:10 -070079 long cpuTimeAtResume; // the cpu time of host process at the time of resuming activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 Configuration configuration; // configuration activity was last running in
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070081 ActivityRecord resultTo; // who started this entry, so will get our reply
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 final String resultWho; // additional identifier for use by resultTo.
83 final int requestCode; // code given by requester (resultTo)
84 ArrayList results; // pending ActivityResult objs we have received
85 HashSet<WeakReference<PendingIntentRecord>> pendingResults; // all pending intents for this act
86 ArrayList newIntents; // any pending new intents for single-top mode
87 HashSet<ConnectionRecord> connections; // All ConnectionRecord we hold
Dianne Hackborn7e269642010-08-25 19:50:20 -070088 UriPermissionOwner uriPermissions; // current special URI access perms.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 ProcessRecord app; // if non-null, hosting application
90 Bitmap thumbnail; // icon representation of paused screen
91 CharSequence description; // textual description of paused screen
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070092 ActivityState state; // current state we are in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 Bundle icicle; // last saved activity state
94 boolean frontOfTask; // is this the root activity of its task?
95 boolean launchFailed; // set if a launched failed, to abort on 2nd try
96 boolean haveState; // have we gotten the last activity state?
97 boolean stopped; // is activity pause finished?
Dianne Hackborn95fc68f2009-05-19 18:37:45 -070098 boolean delayedResume; // not yet resumed because of stopped app switches?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 boolean finishing; // activity in pending finish list?
100 boolean configDestroy; // need to destroy due to config change?
101 int configChangeFlags; // which config values have changed
102 boolean keysPaused; // has key dispatching been paused for it?
103 boolean inHistory; // are we in the history stack?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 int launchMode; // the launch mode activity attribute.
105 boolean visible; // does this activity's window need to be shown?
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800106 boolean sleeping; // have we told the activity to sleep?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 boolean waitingVisible; // true if waiting for a new act to become vis
108 boolean nowVisible; // is this activity's window visible?
109 boolean thumbnailNeeded;// has someone requested a thumbnail?
110 boolean idle; // has the activity gone idle?
111 boolean hasBeenLaunched;// has this activity ever been launched?
112 boolean frozenBeforeDestroy;// has been frozen but not yet destroyed.
Daniel Sandler69a48172010-06-23 16:29:36 -0400113 boolean immersive; // immersive mode (don't interrupt if possible)
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400114 boolean forceNewConfig; // force re-create with new config next time
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700116 String stringName; // for caching of toString().
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);
139 pw.print(prefix); pw.print("configuration="); pw.println(configuration);
140 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 Hackborn0dad3642010-09-09 21:25:35 -0700180 if (launchTime != 0 || startTime != 0) {
181 pw.print(prefix); pw.print("launchTime=");
182 TimeUtils.formatDuration(launchTime, pw); pw.print(" startTime=");
183 TimeUtils.formatDuration(startTime, pw); pw.println("");
184 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700185 if (waitingVisible || nowVisible) {
186 pw.print(prefix); pw.print("waitingVisible="); pw.print(waitingVisible);
187 pw.print(" nowVisible="); pw.println(nowVisible);
188 }
189 if (configDestroy || configChangeFlags != 0) {
190 pw.print(prefix); pw.print("configDestroy="); pw.print(configDestroy);
191 pw.print(" configChangeFlags=");
192 pw.println(Integer.toHexString(configChangeFlags));
193 }
194 if (connections != null) {
195 pw.print(prefix); pw.print("connections="); pw.println(connections);
196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 }
198
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700199 ActivityRecord(ActivityManagerService _service, ActivityStack _stack, ProcessRecord _caller,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 int _launchedFromUid, Intent _intent, String _resolvedType,
201 ActivityInfo aInfo, Configuration _configuration,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700202 ActivityRecord _resultTo, String _resultWho, int _reqCode,
The Android Open Source Project4df24232009-03-05 14:34:35 -0800203 boolean _componentSpecified) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 service = _service;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700205 stack = _stack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 info = aInfo;
207 launchedFromUid = _launchedFromUid;
208 intent = _intent;
209 shortComponentName = _intent.getComponent().flattenToShortString();
210 resolvedType = _resolvedType;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800211 componentSpecified = _componentSpecified;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 configuration = _configuration;
213 resultTo = _resultTo;
214 resultWho = _resultWho;
215 requestCode = _reqCode;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700216 state = ActivityState.INITIALIZING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 frontOfTask = false;
218 launchFailed = false;
219 haveState = false;
220 stopped = false;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700221 delayedResume = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 finishing = false;
223 configDestroy = false;
224 keysPaused = false;
225 inHistory = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 visible = true;
227 waitingVisible = false;
228 nowVisible = false;
229 thumbnailNeeded = false;
230 idle = false;
231 hasBeenLaunched = false;
232
233 if (aInfo != null) {
234 if (aInfo.targetActivity == null
235 || aInfo.launchMode == ActivityInfo.LAUNCH_MULTIPLE
236 || aInfo.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
237 realActivity = _intent.getComponent();
238 } else {
239 realActivity = new ComponentName(aInfo.packageName,
240 aInfo.targetActivity);
241 }
242 taskAffinity = aInfo.taskAffinity;
243 stateNotNeeded = (aInfo.flags&
244 ActivityInfo.FLAG_STATE_NOT_NEEDED) != 0;
245 baseDir = aInfo.applicationInfo.sourceDir;
246 resDir = aInfo.applicationInfo.publicSourceDir;
247 dataDir = aInfo.applicationInfo.dataDir;
248 nonLocalizedLabel = aInfo.nonLocalizedLabel;
249 labelRes = aInfo.labelRes;
250 if (nonLocalizedLabel == null && labelRes == 0) {
251 ApplicationInfo app = aInfo.applicationInfo;
252 nonLocalizedLabel = app.nonLocalizedLabel;
253 labelRes = app.labelRes;
254 }
255 icon = aInfo.getIconResource();
256 theme = aInfo.getThemeResource();
Dianne Hackborn247fe742011-01-08 17:25:57 -0800257 realTheme = theme;
258 if (realTheme == 0) {
259 realTheme = aInfo.applicationInfo.targetSdkVersion
260 < Build.VERSION_CODES.HONEYCOMB
261 ? android.R.style.Theme
262 : android.R.style.Theme_Holo;
263 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -0800264 if ((aInfo.flags&ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
265 windowFlags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 if ((aInfo.flags&ActivityInfo.FLAG_MULTIPROCESS) != 0
268 && _caller != null
269 && (aInfo.applicationInfo.uid == Process.SYSTEM_UID
270 || aInfo.applicationInfo.uid == _caller.info.uid)) {
271 processName = _caller.processName;
272 } else {
273 processName = aInfo.processName;
274 }
275
276 if (intent != null && (aInfo.flags & ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS) != 0) {
277 intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
278 }
279
280 packageName = aInfo.applicationInfo.packageName;
281 launchMode = aInfo.launchMode;
282
283 AttributeCache.Entry ent = AttributeCache.instance().get(packageName,
Dianne Hackborn247fe742011-01-08 17:25:57 -0800284 realTheme, com.android.internal.R.styleable.Window);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 fullscreen = ent != null && !ent.array.getBoolean(
286 com.android.internal.R.styleable.Window_windowIsFloating, false)
287 && !ent.array.getBoolean(
288 com.android.internal.R.styleable.Window_windowIsTranslucent, false);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800289 noDisplay = ent != null && ent.array.getBoolean(
290 com.android.internal.R.styleable.Window_windowNoDisplay, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291
The Android Open Source Project4df24232009-03-05 14:34:35 -0800292 if (!_componentSpecified || _launchedFromUid == Process.myUid()
293 || _launchedFromUid == 0) {
294 // If we know the system has determined the component, then
295 // we can consider this to be a home activity...
296 if (Intent.ACTION_MAIN.equals(_intent.getAction()) &&
297 _intent.hasCategory(Intent.CATEGORY_HOME) &&
298 _intent.getCategories().size() == 1 &&
299 _intent.getData() == null &&
300 _intent.getType() == null &&
301 (intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
302 !"android".equals(realActivity.getClassName())) {
303 // This sure looks like a home activity!
304 // Note the last check is so we don't count the resolver
305 // activity as being home... really, we don't care about
306 // doing anything special with something that comes from
307 // the core framework package.
308 isHomeActivity = true;
309 } else {
310 isHomeActivity = false;
311 }
312 } else {
313 isHomeActivity = false;
314 }
Daniel Sandler69a48172010-06-23 16:29:36 -0400315
316 immersive = (aInfo.flags & ActivityInfo.FLAG_IMMERSIVE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 } else {
318 realActivity = null;
319 taskAffinity = null;
320 stateNotNeeded = false;
321 baseDir = null;
322 resDir = null;
323 dataDir = null;
324 processName = null;
325 packageName = null;
326 fullscreen = true;
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800327 noDisplay = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800328 isHomeActivity = false;
Daniel Sandler69a48172010-06-23 16:29:36 -0400329 immersive = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 }
331 }
332
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -0800333 void makeFinishing() {
334 if (!finishing) {
335 finishing = true;
336 if (task != null) {
337 task.numActivities--;
338 }
339 }
340 }
341
Dianne Hackborn7e269642010-08-25 19:50:20 -0700342 UriPermissionOwner getUriPermissionsLocked() {
343 if (uriPermissions == null) {
344 uriPermissions = new UriPermissionOwner(service, this);
345 }
346 return uriPermissions;
347 }
348
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700349 void addResultLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 int requestCode, int resultCode,
351 Intent resultData) {
352 ActivityResult r = new ActivityResult(from, resultWho,
353 requestCode, resultCode, resultData);
354 if (results == null) {
355 results = new ArrayList();
356 }
357 results.add(r);
358 }
359
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700360 void removeResultsLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 int requestCode) {
362 if (results != null) {
363 for (int i=results.size()-1; i>=0; i--) {
364 ActivityResult r = (ActivityResult)results.get(i);
365 if (r.mFrom != from) continue;
366 if (r.mResultWho == null) {
367 if (resultWho != null) continue;
368 } else {
369 if (!r.mResultWho.equals(resultWho)) continue;
370 }
371 if (r.mRequestCode != requestCode) continue;
372
373 results.remove(i);
374 }
375 }
376 }
377
378 void addNewIntentLocked(Intent intent) {
379 if (newIntents == null) {
380 newIntents = new ArrayList();
381 }
382 newIntents.add(intent);
383 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700384
385 /**
386 * Deliver a new Intent to an existing activity, so that its onNewIntent()
387 * method will be called at the proper time.
388 */
Dianne Hackborn39792d22010-08-19 18:01:52 -0700389 final void deliverNewIntentLocked(int callingUid, Intent intent) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700390 boolean sent = false;
391 if (state == ActivityState.RESUMED
392 && app != null && app.thread != null) {
393 try {
394 ArrayList<Intent> ar = new ArrayList<Intent>();
Dianne Hackborn39792d22010-08-19 18:01:52 -0700395 intent = new Intent(intent);
396 ar.add(intent);
397 service.grantUriPermissionFromIntentLocked(callingUid, packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -0700398 intent, getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700399 app.thread.scheduleNewIntent(ar, this);
400 sent = true;
Dianne Hackborn39792d22010-08-19 18:01:52 -0700401 } catch (RemoteException e) {
402 Slog.w(ActivityManagerService.TAG,
403 "Exception thrown sending new intent to " + this, e);
404 } catch (NullPointerException e) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700405 Slog.w(ActivityManagerService.TAG,
406 "Exception thrown sending new intent to " + this, e);
407 }
408 }
409 if (!sent) {
410 addNewIntentLocked(new Intent(intent));
411 }
412 }
413
414 void removeUriPermissionsLocked() {
Dianne Hackborn7e269642010-08-25 19:50:20 -0700415 if (uriPermissions != null) {
416 uriPermissions.removeUriPermissionsLocked();
417 uriPermissions = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700418 }
419 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420
421 void pauseKeyDispatchingLocked() {
422 if (!keysPaused) {
423 keysPaused = true;
424 service.mWindowManager.pauseKeyDispatching(this);
425 }
426 }
427
428 void resumeKeyDispatchingLocked() {
429 if (keysPaused) {
430 keysPaused = false;
431 service.mWindowManager.resumeKeyDispatching(this);
432 }
433 }
434
435 // IApplicationToken
436
437 public boolean mayFreezeScreenLocked(ProcessRecord app) {
438 // Only freeze the screen if this activity is currently attached to
439 // an application, and that application is not blocked or unresponding.
440 // In any other case, we can't count on getting the screen unfrozen,
441 // so it is best to leave as-is.
Dianne Hackborn5f4d6432010-12-21 20:40:11 -0800442 return app != null && !app.crashing && !app.notResponding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 }
444
445 public void startFreezingScreenLocked(ProcessRecord app, int configChanges) {
446 if (mayFreezeScreenLocked(app)) {
447 service.mWindowManager.startAppFreezingScreen(this, configChanges);
448 }
449 }
450
451 public void stopFreezingScreenLocked(boolean force) {
452 if (force || frozenBeforeDestroy) {
453 frozenBeforeDestroy = false;
454 service.mWindowManager.stopAppFreezingScreen(this, force);
455 }
456 }
457
458 public void windowsVisible() {
459 synchronized(service) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700460 if (launchTime != 0) {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700461 final long curTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700462 final long thisTime = curTime - launchTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700463 final long totalTime = stack.mInitialStartTime != 0
464 ? (curTime - stack.mInitialStartTime) : thisTime;
Dianne Hackborn6447ca32009-04-07 19:50:08 -0700465 if (ActivityManagerService.SHOW_ACTIVITY_START_TIME) {
Doug Zongker2bec3d42009-12-04 12:52:44 -0800466 EventLog.writeEvent(EventLogTags.ACTIVITY_LAUNCH_TIME,
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700467 System.identityHashCode(this), shortComponentName,
468 thisTime, totalTime);
469 StringBuilder sb = service.mStringBuilder;
470 sb.setLength(0);
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700471 sb.append("Displayed ");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700472 sb.append(shortComponentName);
473 sb.append(": ");
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700474 TimeUtils.formatDuration(thisTime, sb);
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700475 if (thisTime != totalTime) {
476 sb.append(" (total ");
477 TimeUtils.formatDuration(totalTime, sb);
478 sb.append(")");
479 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700480 Log.i(ActivityManagerService.TAG, sb.toString());
Dianne Hackborn6447ca32009-04-07 19:50:08 -0700481 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700482 stack.reportActivityLaunchedLocked(false, this, thisTime, totalTime);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700483 if (totalTime > 0) {
484 service.mUsageStatsService.noteLaunchTime(realActivity, (int)totalTime);
Dianne Hackborn6447ca32009-04-07 19:50:08 -0700485 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700486 launchTime = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700487 stack.mInitialStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700489 startTime = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700490 stack.reportActivityVisibleLocked(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 if (ActivityManagerService.DEBUG_SWITCH) Log.v(
492 ActivityManagerService.TAG, "windowsVisible(): " + this);
493 if (!nowVisible) {
494 nowVisible = true;
495 if (!idle) {
496 // Instead of doing the full stop routine here, let's just
497 // hide any activities we now can, and let them stop when
498 // the normal idle happens.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700499 stack.processStoppingActivitiesLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 } else {
501 // If this activity was already idle, then we now need to
502 // make sure we perform the full stop of any activities
503 // that are waiting to do so. This is because we won't
504 // do that while they are still waiting for this one to
505 // become visible.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700506 final int N = stack.mWaitingVisibleActivities.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 if (N > 0) {
508 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700509 ActivityRecord r = (ActivityRecord)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700510 stack.mWaitingVisibleActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 r.waitingVisible = false;
512 if (ActivityManagerService.DEBUG_SWITCH) Log.v(
513 ActivityManagerService.TAG,
514 "Was waiting for visible: " + r);
515 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700516 stack.mWaitingVisibleActivities.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 Message msg = Message.obtain();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700518 msg.what = ActivityStack.IDLE_NOW_MSG;
519 stack.mHandler.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 }
521 }
522 service.scheduleAppGcsLocked();
523 }
524 }
525 }
526
527 public void windowsGone() {
528 if (ActivityManagerService.DEBUG_SWITCH) Log.v(
529 ActivityManagerService.TAG, "windowsGone(): " + this);
530 nowVisible = false;
531 }
532
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700533 private ActivityRecord getWaitingHistoryRecordLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 // First find the real culprit... if we are waiting
535 // for another app to start, then we have paused dispatching
536 // for this activity.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700537 ActivityRecord r = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 if (r.waitingVisible) {
539 // Hmmm, who might we be waiting for?
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700540 r = stack.mResumedActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700542 r = stack.mPausingActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 }
544 // Both of those null? Fall back to 'this' again
545 if (r == null) {
546 r = this;
547 }
548 }
549
550 return r;
551 }
552
553 public boolean keyDispatchingTimedOut() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700554 ActivityRecord r;
Dianne Hackbornad5499d2010-03-29 18:08:45 -0700555 ProcessRecord anrApp = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 synchronized(service) {
Dianne Hackbornad5499d2010-03-29 18:08:45 -0700557 r = getWaitingHistoryRecordLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 if (r != null && r.app != null) {
559 if (r.app.debugging) {
560 return false;
561 }
562
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700563 if (service.mDidDexOpt) {
564 // Give more time since we were dexopting.
565 service.mDidDexOpt = false;
566 return false;
567 }
568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 if (r.app.instrumentationClass == null) {
Dianne Hackbornad5499d2010-03-29 18:08:45 -0700570 anrApp = r.app;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 } else {
572 Bundle info = new Bundle();
573 info.putString("shortMsg", "keyDispatchingTimedOut");
574 info.putString("longMsg", "Timed out while dispatching key event");
575 service.finishInstrumentationLocked(
576 r.app, Activity.RESULT_CANCELED, info);
577 }
578 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 }
Dianne Hackbornad5499d2010-03-29 18:08:45 -0700580
581 if (anrApp != null) {
582 service.appNotResponding(anrApp, r, this,
583 "keyDispatchingTimedOut");
584 }
585
586 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 }
588
589 /** Returns the key dispatching timeout for this application token. */
590 public long getKeyDispatchingTimeout() {
591 synchronized(service) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700592 ActivityRecord r = getWaitingHistoryRecordLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 if (r == null || r.app == null
594 || r.app.instrumentationClass == null) {
595 return ActivityManagerService.KEY_DISPATCHING_TIMEOUT;
596 }
597
598 return ActivityManagerService.INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT;
599 }
600 }
601
602 /**
603 * This method will return true if the activity is either visible, is becoming visible, is
604 * currently pausing, or is resumed.
605 */
606 public boolean isInterestingToUserLocked() {
607 return visible || nowVisible || state == ActivityState.PAUSING ||
608 state == ActivityState.RESUMED;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800609 }
610
611 public void setSleeping(boolean _sleeping) {
612 if (sleeping == _sleeping) {
613 return;
614 }
615 if (app != null && app.thread != null) {
616 try {
617 app.thread.scheduleSleeping(this, _sleeping);
618 if (sleeping && !stack.mGoingToSleepActivities.contains(this)) {
619 stack.mGoingToSleepActivities.add(this);
620 }
621 sleeping = _sleeping;
622 } catch (RemoteException e) {
623 Slog.w(ActivityStack.TAG, "Exception thrown when sleeping: "
624 + intent.getComponent(), e);
625 }
626 }
627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 public String toString() {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700630 if (stringName != null) {
631 return stringName;
632 }
633 StringBuilder sb = new StringBuilder(128);
Dianne Hackborn30d71892010-12-11 10:37:55 -0800634 sb.append("ActivityRecord{");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700635 sb.append(Integer.toHexString(System.identityHashCode(this)));
636 sb.append(' ');
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700637 sb.append(intent.getComponent().flattenToShortString());
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700638 sb.append('}');
639 return stringName = sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 }
641}