ActivityRecord.java revision f4c909bcb87d6f103c9f9e8255fa61bd86f4de67
1/*
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 android.os.Trace;
20import com.android.internal.R.styleable;
21import com.android.internal.app.ResolverActivity;
22import com.android.server.AttributeCache;
23import com.android.server.am.ActivityStack.ActivityState;
24import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
25
26import android.app.ActivityOptions;
27import android.app.ResultInfo;
28import android.content.ComponentName;
29import android.content.Intent;
30import android.content.pm.ActivityInfo;
31import android.content.pm.ApplicationInfo;
32import android.content.res.CompatibilityInfo;
33import android.content.res.Configuration;
34import android.graphics.Bitmap;
35import android.graphics.Rect;
36import android.os.Build;
37import android.os.Bundle;
38import android.os.IBinder;
39import android.os.Message;
40import android.os.Process;
41import android.os.RemoteException;
42import android.os.SystemClock;
43import android.os.UserHandle;
44import android.util.EventLog;
45import android.util.Log;
46import android.util.Slog;
47import android.util.TimeUtils;
48import android.view.IApplicationToken;
49import android.view.WindowManager;
50
51import java.io.PrintWriter;
52import java.lang.ref.WeakReference;
53import java.util.ArrayList;
54import java.util.HashSet;
55
56/**
57 * An entry in the history stack, representing an activity.
58 */
59final class ActivityRecord {
60    static final String TAG = ActivityManagerService.TAG;
61    static final boolean DEBUG_SAVED_STATE = ActivityStackSupervisor.DEBUG_SAVED_STATE;
62    final public static String RECENTS_PACKAGE_NAME = "com.android.systemui.recent";
63
64    final ActivityManagerService service; // owner
65    final IApplicationToken.Stub appToken; // window manager token
66    final ActivityInfo info; // all about me
67    final int launchedFromUid; // always the uid who started the activity.
68    final String launchedFromPackage; // always the package who started the activity.
69    final int userId;          // Which user is this running for?
70    final Intent intent;    // the original intent that generated us
71    final ComponentName realActivity;  // the intent component, or target of an alias.
72    final String shortComponentName; // the short component name of the intent
73    final String resolvedType; // as per original caller;
74    final String packageName; // the package implementing intent's component
75    final String processName; // process where this component wants to run
76    final String taskAffinity; // as per ActivityInfo.taskAffinity
77    final boolean stateNotNeeded; // As per ActivityInfo.flags
78    boolean fullscreen; // covers the full screen?
79    final boolean noDisplay;  // activity is not displayed?
80    final boolean componentSpecified;  // did caller specifiy an explicit component?
81
82    static final int APPLICATION_ACTIVITY_TYPE = 0;
83    static final int HOME_ACTIVITY_TYPE = 1;
84    static final int RECENTS_ACTIVITY_TYPE = 2;
85    int mActivityType;
86
87    final String baseDir;   // where activity source (resources etc) located
88    final String resDir;   // where public activity source (public resources etc) located
89    final String dataDir;   // where activity data should go
90    CharSequence nonLocalizedLabel;  // the label information from the package mgr.
91    int labelRes;           // the label information from the package mgr.
92    int icon;               // resource identifier of activity's icon.
93    int logo;               // resource identifier of activity's logo.
94    int theme;              // resource identifier of activity's theme.
95    int realTheme;          // actual theme resource we will use, never 0.
96    int windowFlags;        // custom window flags for preview window.
97    TaskRecord task;        // the task this is in.
98    ThumbnailHolder thumbHolder; // where our thumbnails should go.
99    long displayStartTime;  // when we started launching this activity
100    long fullyDrawnStartTime; // when we started launching this activity
101    long startTime;         // last time this activity was started
102    long lastVisibleTime;   // last time this activity became visible
103    long cpuTimeAtResume;   // the cpu time of host process at the time of resuming activity
104    long pauseTime;         // last time we started pausing the activity
105    long launchTickTime;    // base time for launch tick messages
106    Configuration configuration; // configuration activity was last running in
107    CompatibilityInfo compat;// last used compatibility mode
108    ActivityRecord resultTo; // who started this entry, so will get our reply
109    final String resultWho; // additional identifier for use by resultTo.
110    final int requestCode;  // code given by requester (resultTo)
111    ArrayList<ResultInfo> results; // pending ActivityResult objs we have received
112    HashSet<WeakReference<PendingIntentRecord>> pendingResults; // all pending intents for this act
113    ArrayList<Intent> newIntents; // any pending new intents for single-top mode
114    ActivityOptions pendingOptions; // most recently given options
115    HashSet<ConnectionRecord> connections; // All ConnectionRecord we hold
116    UriPermissionOwner uriPermissions; // current special URI access perms.
117    ProcessRecord app;      // if non-null, hosting application
118    ActivityState state;    // current state we are in
119    Bundle  icicle;         // last saved activity state
120    boolean frontOfTask;    // is this the root activity of its task?
121    boolean launchFailed;   // set if a launched failed, to abort on 2nd try
122    boolean haveState;      // have we gotten the last activity state?
123    boolean stopped;        // is activity pause finished?
124    boolean delayedResume;  // not yet resumed because of stopped app switches?
125    boolean finishing;      // activity in pending finish list?
126    boolean configDestroy;  // need to destroy due to config change?
127    int configChangeFlags;  // which config values have changed
128    boolean keysPaused;     // has key dispatching been paused for it?
129    int launchMode;         // the launch mode activity attribute.
130    boolean visible;        // does this activity's window need to be shown?
131    boolean sleeping;       // have we told the activity to sleep?
132    boolean waitingVisible; // true if waiting for a new act to become vis
133    boolean nowVisible;     // is this activity's window visible?
134    boolean thumbnailNeeded;// has someone requested a thumbnail?
135    boolean idle;           // has the activity gone idle?
136    boolean hasBeenLaunched;// has this activity ever been launched?
137    boolean frozenBeforeDestroy;// has been frozen but not yet destroyed.
138    boolean immersive;      // immersive mode (don't interrupt if possible)
139    boolean forceNewConfig; // force re-create with new config next time
140    int launchCount;        // count of launches since last state
141    long lastLaunchTime;    // time of last lauch of this activity
142    ArrayList<ActivityContainer> mChildContainers = new ArrayList<ActivityContainer>();
143
144    String stringName;      // for caching of toString().
145
146    private boolean inHistory;  // are we in the history stack?
147    final ActivityStackSupervisor mStackSupervisor;
148    ActivityContainer mInitialActivityContainer;
149
150    void dump(PrintWriter pw, String prefix) {
151        final long now = SystemClock.uptimeMillis();
152        pw.print(prefix); pw.print("packageName="); pw.print(packageName);
153                pw.print(" processName="); pw.println(processName);
154        pw.print(prefix); pw.print("launchedFromUid="); pw.print(launchedFromUid);
155                pw.print(" launchedFromPackage="); pw.print(launchedFromPackage);
156                pw.print(" userId="); pw.println(userId);
157        pw.print(prefix); pw.print("app="); pw.println(app);
158        pw.print(prefix); pw.println(intent.toInsecureStringWithClip());
159        pw.print(prefix); pw.print("frontOfTask="); pw.print(frontOfTask);
160                pw.print(" task="); pw.println(task);
161        pw.print(prefix); pw.print("taskAffinity="); pw.println(taskAffinity);
162        pw.print(prefix); pw.print("realActivity=");
163                pw.println(realActivity.flattenToShortString());
164        pw.print(prefix); pw.print("baseDir="); pw.println(baseDir);
165        if (!resDir.equals(baseDir)) {
166            pw.print(prefix); pw.print("resDir="); pw.println(resDir);
167        }
168        pw.print(prefix); pw.print("dataDir="); pw.println(dataDir);
169        pw.print(prefix); pw.print("stateNotNeeded="); pw.print(stateNotNeeded);
170                pw.print(" componentSpecified="); pw.print(componentSpecified);
171                pw.print(" mActivityType="); pw.println(mActivityType);
172        pw.print(prefix); pw.print("compat="); pw.print(compat);
173                pw.print(" labelRes=0x"); pw.print(Integer.toHexString(labelRes));
174                pw.print(" icon=0x"); pw.print(Integer.toHexString(icon));
175                pw.print(" theme=0x"); pw.println(Integer.toHexString(theme));
176        pw.print(prefix); pw.print("config="); pw.println(configuration);
177        if (resultTo != null || resultWho != null) {
178            pw.print(prefix); pw.print("resultTo="); pw.print(resultTo);
179                    pw.print(" resultWho="); pw.print(resultWho);
180                    pw.print(" resultCode="); pw.println(requestCode);
181        }
182        if (results != null) {
183            pw.print(prefix); pw.print("results="); pw.println(results);
184        }
185        if (pendingResults != null && pendingResults.size() > 0) {
186            pw.print(prefix); pw.println("Pending Results:");
187            for (WeakReference<PendingIntentRecord> wpir : pendingResults) {
188                PendingIntentRecord pir = wpir != null ? wpir.get() : null;
189                pw.print(prefix); pw.print("  - ");
190                if (pir == null) {
191                    pw.println("null");
192                } else {
193                    pw.println(pir);
194                    pir.dump(pw, prefix + "    ");
195                }
196            }
197        }
198        if (newIntents != null && newIntents.size() > 0) {
199            pw.print(prefix); pw.println("Pending New Intents:");
200            for (int i=0; i<newIntents.size(); i++) {
201                Intent intent = newIntents.get(i);
202                pw.print(prefix); pw.print("  - ");
203                if (intent == null) {
204                    pw.println("null");
205                } else {
206                    pw.println(intent.toShortString(false, true, false, true));
207                }
208            }
209        }
210        if (pendingOptions != null) {
211            pw.print(prefix); pw.print("pendingOptions="); pw.println(pendingOptions);
212        }
213        if (uriPermissions != null) {
214            if (uriPermissions.readUriPermissions != null) {
215                pw.print(prefix); pw.print("readUriPermissions=");
216                        pw.println(uriPermissions.readUriPermissions);
217            }
218            if (uriPermissions.writeUriPermissions != null) {
219                pw.print(prefix); pw.print("writeUriPermissions=");
220                        pw.println(uriPermissions.writeUriPermissions);
221            }
222        }
223        pw.print(prefix); pw.print("launchFailed="); pw.print(launchFailed);
224                pw.print(" launchCount="); pw.print(launchCount);
225                pw.print(" lastLaunchTime=");
226                if (lastLaunchTime == 0) pw.print("0");
227                else TimeUtils.formatDuration(lastLaunchTime, now, pw);
228                pw.println();
229        pw.print(prefix); pw.print("haveState="); pw.print(haveState);
230                pw.print(" icicle="); pw.println(icicle);
231        pw.print(prefix); pw.print("state="); pw.print(state);
232                pw.print(" stopped="); pw.print(stopped);
233                pw.print(" delayedResume="); pw.print(delayedResume);
234                pw.print(" finishing="); pw.println(finishing);
235        pw.print(prefix); pw.print("keysPaused="); pw.print(keysPaused);
236                pw.print(" inHistory="); pw.print(inHistory);
237                pw.print(" visible="); pw.print(visible);
238                pw.print(" sleeping="); pw.print(sleeping);
239                pw.print(" idle="); pw.println(idle);
240        pw.print(prefix); pw.print("fullscreen="); pw.print(fullscreen);
241                pw.print(" noDisplay="); pw.print(noDisplay);
242                pw.print(" immersive="); pw.print(immersive);
243                pw.print(" launchMode="); pw.println(launchMode);
244        pw.print(prefix); pw.print("frozenBeforeDestroy="); pw.print(frozenBeforeDestroy);
245                pw.print(" thumbnailNeeded="); pw.print(thumbnailNeeded);
246                pw.print(" forceNewConfig="); pw.println(forceNewConfig);
247        pw.print(prefix); pw.print("mActivityType=");
248                pw.println(activityTypeToString(mActivityType));
249        pw.print(prefix); pw.print("thumbHolder: ");
250                pw.print(Integer.toHexString(System.identityHashCode(thumbHolder)));
251                if (thumbHolder != null) {
252                    pw.print(" bm="); pw.print(thumbHolder.lastThumbnail);
253                    pw.print(" desc="); pw.print(thumbHolder.lastDescription);
254                }
255                pw.println();
256        if (displayStartTime != 0 || startTime != 0) {
257            pw.print(prefix); pw.print("displayStartTime=");
258                    if (displayStartTime == 0) pw.print("0");
259                    else TimeUtils.formatDuration(displayStartTime, now, pw);
260                    pw.print(" startTime=");
261                    if (startTime == 0) pw.print("0");
262                    else TimeUtils.formatDuration(startTime, now, pw);
263                    pw.println();
264        }
265        if (lastVisibleTime != 0 || waitingVisible || nowVisible) {
266            pw.print(prefix); pw.print("waitingVisible="); pw.print(waitingVisible);
267                    pw.print(" nowVisible="); pw.print(nowVisible);
268                    pw.print(" lastVisibleTime=");
269                    if (lastVisibleTime == 0) pw.print("0");
270                    else TimeUtils.formatDuration(lastVisibleTime, now, pw);
271                    pw.println();
272        }
273        if (configDestroy || configChangeFlags != 0) {
274            pw.print(prefix); pw.print("configDestroy="); pw.print(configDestroy);
275                    pw.print(" configChangeFlags=");
276                    pw.println(Integer.toHexString(configChangeFlags));
277        }
278        if (connections != null) {
279            pw.print(prefix); pw.print("connections="); pw.println(connections);
280        }
281    }
282
283    static class Token extends IApplicationToken.Stub {
284        final WeakReference<ActivityRecord> weakActivity;
285
286        Token(ActivityRecord activity) {
287            weakActivity = new WeakReference<ActivityRecord>(activity);
288        }
289
290        @Override public void windowsDrawn() {
291            ActivityRecord activity = weakActivity.get();
292            if (activity != null) {
293                activity.windowsDrawn();
294            }
295        }
296
297        @Override public void windowsVisible() {
298            ActivityRecord activity = weakActivity.get();
299            if (activity != null) {
300                activity.windowsVisible();
301            }
302        }
303
304        @Override public void windowsGone() {
305            ActivityRecord activity = weakActivity.get();
306            if (activity != null) {
307                activity.windowsGone();
308            }
309        }
310
311        @Override public boolean keyDispatchingTimedOut(String reason) {
312            ActivityRecord activity = weakActivity.get();
313            return activity != null && activity.keyDispatchingTimedOut(reason);
314        }
315
316        @Override public long getKeyDispatchingTimeout() {
317            ActivityRecord activity = weakActivity.get();
318            if (activity != null) {
319                return activity.getKeyDispatchingTimeout();
320            }
321            return 0;
322        }
323
324        @Override
325        public String toString() {
326            StringBuilder sb = new StringBuilder(128);
327            sb.append("Token{");
328            sb.append(Integer.toHexString(System.identityHashCode(this)));
329            sb.append(' ');
330            sb.append(weakActivity.get());
331            sb.append('}');
332            return sb.toString();
333        }
334    }
335
336    static ActivityRecord forToken(IBinder token) {
337        try {
338            return token != null ? ((Token)token).weakActivity.get() : null;
339        } catch (ClassCastException e) {
340            Slog.w(ActivityManagerService.TAG, "Bad activity token: " + token, e);
341            return null;
342        }
343    }
344
345    boolean isNotResolverActivity() {
346        return !ResolverActivity.class.getName().equals(realActivity.getClassName());
347    }
348
349    ActivityRecord(ActivityManagerService _service, ProcessRecord _caller,
350            int _launchedFromUid, String _launchedFromPackage, Intent _intent, String _resolvedType,
351            ActivityInfo aInfo, Configuration _configuration,
352            ActivityRecord _resultTo, String _resultWho, int _reqCode,
353            boolean _componentSpecified, ActivityStackSupervisor supervisor,
354            ActivityContainer container) {
355        service = _service;
356        appToken = new Token(this);
357        info = aInfo;
358        launchedFromUid = _launchedFromUid;
359        launchedFromPackage = _launchedFromPackage;
360        userId = UserHandle.getUserId(aInfo.applicationInfo.uid);
361        intent = _intent;
362        shortComponentName = _intent.getComponent().flattenToShortString();
363        resolvedType = _resolvedType;
364        componentSpecified = _componentSpecified;
365        configuration = _configuration;
366        resultTo = _resultTo;
367        resultWho = _resultWho;
368        requestCode = _reqCode;
369        state = ActivityState.INITIALIZING;
370        frontOfTask = false;
371        launchFailed = false;
372        stopped = false;
373        delayedResume = false;
374        finishing = false;
375        configDestroy = false;
376        keysPaused = false;
377        inHistory = false;
378        visible = true;
379        waitingVisible = false;
380        nowVisible = false;
381        thumbnailNeeded = false;
382        idle = false;
383        hasBeenLaunched = false;
384        mStackSupervisor = supervisor;
385        mInitialActivityContainer = container;
386
387        // This starts out true, since the initial state of an activity
388        // is that we have everything, and we shouldn't never consider it
389        // lacking in state to be removed if it dies.
390        haveState = true;
391
392        if (aInfo != null) {
393            if (aInfo.targetActivity == null
394                    || aInfo.launchMode == ActivityInfo.LAUNCH_MULTIPLE
395                    || aInfo.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
396                realActivity = _intent.getComponent();
397            } else {
398                realActivity = new ComponentName(aInfo.packageName,
399                        aInfo.targetActivity);
400            }
401            taskAffinity = aInfo.taskAffinity;
402            stateNotNeeded = (aInfo.flags&
403                    ActivityInfo.FLAG_STATE_NOT_NEEDED) != 0;
404            baseDir = aInfo.applicationInfo.sourceDir;
405            resDir = aInfo.applicationInfo.publicSourceDir;
406            dataDir = aInfo.applicationInfo.dataDir;
407            nonLocalizedLabel = aInfo.nonLocalizedLabel;
408            labelRes = aInfo.labelRes;
409            if (nonLocalizedLabel == null && labelRes == 0) {
410                ApplicationInfo app = aInfo.applicationInfo;
411                nonLocalizedLabel = app.nonLocalizedLabel;
412                labelRes = app.labelRes;
413            }
414            icon = aInfo.getIconResource();
415            logo = aInfo.getLogoResource();
416            theme = aInfo.getThemeResource();
417            realTheme = theme;
418            if (realTheme == 0) {
419                realTheme = aInfo.applicationInfo.targetSdkVersion
420                        < Build.VERSION_CODES.HONEYCOMB
421                        ? android.R.style.Theme
422                        : android.R.style.Theme_Holo;
423            }
424            if ((aInfo.flags&ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
425                windowFlags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
426            }
427            if ((aInfo.flags&ActivityInfo.FLAG_MULTIPROCESS) != 0
428                    && _caller != null
429                    && (aInfo.applicationInfo.uid == Process.SYSTEM_UID
430                            || aInfo.applicationInfo.uid == _caller.info.uid)) {
431                processName = _caller.processName;
432            } else {
433                processName = aInfo.processName;
434            }
435
436            if (intent != null && (aInfo.flags & ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS) != 0) {
437                intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
438            }
439
440            packageName = aInfo.applicationInfo.packageName;
441            launchMode = aInfo.launchMode;
442
443            AttributeCache.Entry ent = AttributeCache.instance().get(packageName,
444                    realTheme, com.android.internal.R.styleable.Window, userId);
445            fullscreen = ent != null && !ent.array.getBoolean(
446                    com.android.internal.R.styleable.Window_windowIsFloating, false)
447                    && !ent.array.getBoolean(
448                    com.android.internal.R.styleable.Window_windowIsTranslucent, false);
449            noDisplay = ent != null && ent.array.getBoolean(
450                    com.android.internal.R.styleable.Window_windowNoDisplay, false);
451
452            if ((!_componentSpecified || _launchedFromUid == Process.myUid()
453                    || _launchedFromUid == 0) &&
454                    Intent.ACTION_MAIN.equals(_intent.getAction()) &&
455                    _intent.hasCategory(Intent.CATEGORY_HOME) &&
456                    _intent.getCategories().size() == 1 &&
457                    _intent.getData() == null &&
458                    _intent.getType() == null &&
459                    (intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
460                    isNotResolverActivity()) {
461                // This sure looks like a home activity!
462                mActivityType = HOME_ACTIVITY_TYPE;
463            } else if (realActivity.getClassName().contains(RECENTS_PACKAGE_NAME)) {
464                mActivityType = RECENTS_ACTIVITY_TYPE;
465            } else {
466                mActivityType = APPLICATION_ACTIVITY_TYPE;
467            }
468
469            immersive = (aInfo.flags & ActivityInfo.FLAG_IMMERSIVE) != 0;
470        } else {
471            realActivity = null;
472            taskAffinity = null;
473            stateNotNeeded = false;
474            baseDir = null;
475            resDir = null;
476            dataDir = null;
477            processName = null;
478            packageName = null;
479            fullscreen = true;
480            noDisplay = false;
481            mActivityType = APPLICATION_ACTIVITY_TYPE;
482            immersive = false;
483        }
484    }
485
486    void setTask(TaskRecord newTask, ThumbnailHolder newThumbHolder, boolean isRoot) {
487        if (task != null && task.removeActivity(this)) {
488            if (task != newTask) {
489                task.stack.removeTask(task);
490            } else {
491                Slog.d(TAG, "!!! REMOVE THIS LOG !!! setTask: nearly removed stack=" +
492                        (newTask == null ? null : newTask.stack));
493            }
494        }
495        if (inHistory && !finishing) {
496            if (task != null) {
497                task.numActivities--;
498            }
499            if (newTask != null) {
500                newTask.numActivities++;
501            }
502        }
503        if (newThumbHolder == null) {
504            newThumbHolder = newTask;
505        }
506        task = newTask;
507        if (!isRoot && (intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
508            // This is the start of a new sub-task.
509            if (thumbHolder == null) {
510                thumbHolder = new ThumbnailHolder();
511            }
512        } else {
513            thumbHolder = newThumbHolder;
514        }
515    }
516
517    boolean changeWindowTranslucency(boolean toOpaque) {
518        if (fullscreen == toOpaque) {
519            return false;
520        }
521        AttributeCache.Entry ent =
522                AttributeCache.instance().get(packageName, realTheme, styleable.Window, userId);
523        if (ent == null
524                || !ent.array.getBoolean(styleable.Window_windowIsTranslucent, false)
525                || ent.array.getBoolean(styleable.Window_windowIsFloating, false)) {
526            return false;
527        }
528
529        // Keep track of the number of fullscreen activities in this task.
530        task.numFullscreen += toOpaque ? +1 : -1;
531
532        fullscreen = toOpaque;
533        return true;
534    }
535
536    void putInHistory() {
537        if (!inHistory) {
538            inHistory = true;
539            if (task != null && !finishing) {
540                task.numActivities++;
541            }
542        }
543    }
544
545    void takeFromHistory() {
546        if (inHistory) {
547            inHistory = false;
548            if (task != null && !finishing) {
549                task.numActivities--;
550                task = null;
551            }
552            clearOptionsLocked();
553        }
554    }
555
556    boolean isInHistory() {
557        return inHistory;
558    }
559
560    boolean isHomeActivity() {
561        return mActivityType == HOME_ACTIVITY_TYPE;
562    }
563
564    boolean isRecentsActivity() {
565        return mActivityType == RECENTS_ACTIVITY_TYPE;
566    }
567
568    boolean isApplicationActivity() {
569        return mActivityType == APPLICATION_ACTIVITY_TYPE;
570    }
571
572    void makeFinishing() {
573        if (!finishing) {
574            finishing = true;
575            if (task != null && inHistory) {
576                task.numActivities--;
577            }
578            if (stopped) {
579                clearOptionsLocked();
580            }
581        }
582    }
583
584    boolean isRootActivity() {
585        final ArrayList<ActivityRecord> activities = task.mActivities;
586        return activities.size() == 0 || this == activities.get(0);
587    }
588
589    UriPermissionOwner getUriPermissionsLocked() {
590        if (uriPermissions == null) {
591            uriPermissions = new UriPermissionOwner(service, this);
592        }
593        return uriPermissions;
594    }
595
596    void addResultLocked(ActivityRecord from, String resultWho,
597            int requestCode, int resultCode,
598            Intent resultData) {
599        ActivityResult r = new ActivityResult(from, resultWho,
600        		requestCode, resultCode, resultData);
601        if (results == null) {
602            results = new ArrayList<ResultInfo>();
603        }
604        results.add(r);
605    }
606
607    void removeResultsLocked(ActivityRecord from, String resultWho,
608            int requestCode) {
609        if (results != null) {
610            for (int i=results.size()-1; i>=0; i--) {
611                ActivityResult r = (ActivityResult)results.get(i);
612                if (r.mFrom != from) continue;
613                if (r.mResultWho == null) {
614                    if (resultWho != null) continue;
615                } else {
616                    if (!r.mResultWho.equals(resultWho)) continue;
617                }
618                if (r.mRequestCode != requestCode) continue;
619
620                results.remove(i);
621            }
622        }
623    }
624
625    void addNewIntentLocked(Intent intent) {
626        if (newIntents == null) {
627            newIntents = new ArrayList<Intent>();
628        }
629        newIntents.add(intent);
630    }
631
632    /**
633     * Deliver a new Intent to an existing activity, so that its onNewIntent()
634     * method will be called at the proper time.
635     */
636    final void deliverNewIntentLocked(int callingUid, Intent intent) {
637        // The activity now gets access to the data associated with this Intent.
638        service.grantUriPermissionFromIntentLocked(callingUid, packageName,
639                intent, getUriPermissionsLocked());
640        // We want to immediately deliver the intent to the activity if
641        // it is currently the top resumed activity...  however, if the
642        // device is sleeping, then all activities are stopped, so in that
643        // case we will deliver it if this is the current top activity on its
644        // stack.
645        boolean unsent = true;
646        if ((state == ActivityState.RESUMED || (service.mSleeping
647                        && task.stack.topRunningActivityLocked(null) == this))
648                && app != null && app.thread != null) {
649            try {
650                ArrayList<Intent> ar = new ArrayList<Intent>();
651                intent = new Intent(intent);
652                ar.add(intent);
653                app.thread.scheduleNewIntent(ar, appToken);
654                unsent = false;
655            } catch (RemoteException e) {
656                Slog.w(ActivityManagerService.TAG,
657                        "Exception thrown sending new intent to " + this, e);
658            } catch (NullPointerException e) {
659                Slog.w(ActivityManagerService.TAG,
660                        "Exception thrown sending new intent to " + this, e);
661            }
662        }
663        if (unsent) {
664            addNewIntentLocked(new Intent(intent));
665        }
666    }
667
668    void updateOptionsLocked(Bundle options) {
669        if (options != null) {
670            if (pendingOptions != null) {
671                pendingOptions.abort();
672            }
673            pendingOptions = new ActivityOptions(options);
674        }
675    }
676
677    void updateOptionsLocked(ActivityOptions options) {
678        if (options != null) {
679            if (pendingOptions != null) {
680                pendingOptions.abort();
681            }
682            pendingOptions = options;
683        }
684    }
685
686    void applyOptionsLocked() {
687        if (pendingOptions != null) {
688            final int animationType = pendingOptions.getAnimationType();
689            switch (animationType) {
690                case ActivityOptions.ANIM_CUSTOM:
691                    service.mWindowManager.overridePendingAppTransition(
692                            pendingOptions.getPackageName(),
693                            pendingOptions.getCustomEnterResId(),
694                            pendingOptions.getCustomExitResId(),
695                            pendingOptions.getOnAnimationStartListener());
696                    break;
697                case ActivityOptions.ANIM_SCALE_UP:
698                    service.mWindowManager.overridePendingAppTransitionScaleUp(
699                            pendingOptions.getStartX(), pendingOptions.getStartY(),
700                            pendingOptions.getStartWidth(), pendingOptions.getStartHeight());
701                    if (intent.getSourceBounds() == null) {
702                        intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
703                                pendingOptions.getStartY(),
704                                pendingOptions.getStartX()+pendingOptions.getStartWidth(),
705                                pendingOptions.getStartY()+pendingOptions.getStartHeight()));
706                    }
707                    break;
708                case ActivityOptions.ANIM_THUMBNAIL_SCALE_UP:
709                case ActivityOptions.ANIM_THUMBNAIL_SCALE_DOWN:
710                    boolean scaleUp = (animationType == ActivityOptions.ANIM_THUMBNAIL_SCALE_UP);
711                    service.mWindowManager.overridePendingAppTransitionThumb(
712                            pendingOptions.getThumbnail(),
713                            pendingOptions.getStartX(), pendingOptions.getStartY(),
714                            pendingOptions.getOnAnimationStartListener(),
715                            scaleUp);
716                    if (intent.getSourceBounds() == null) {
717                        intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
718                                pendingOptions.getStartY(),
719                                pendingOptions.getStartX()
720                                        + pendingOptions.getThumbnail().getWidth(),
721                                pendingOptions.getStartY()
722                                        + pendingOptions.getThumbnail().getHeight()));
723                    }
724                    break;
725            }
726            pendingOptions = null;
727        }
728    }
729
730    void clearOptionsLocked() {
731        if (pendingOptions != null) {
732            pendingOptions.abort();
733            pendingOptions = null;
734        }
735    }
736
737    ActivityOptions takeOptionsLocked() {
738        ActivityOptions opts = pendingOptions;
739        pendingOptions = null;
740        return opts;
741    }
742
743    void removeUriPermissionsLocked() {
744        if (uriPermissions != null) {
745            uriPermissions.removeUriPermissionsLocked();
746            uriPermissions = null;
747        }
748    }
749
750    void pauseKeyDispatchingLocked() {
751        if (!keysPaused) {
752            keysPaused = true;
753            service.mWindowManager.pauseKeyDispatching(appToken);
754        }
755    }
756
757    void resumeKeyDispatchingLocked() {
758        if (keysPaused) {
759            keysPaused = false;
760            service.mWindowManager.resumeKeyDispatching(appToken);
761        }
762    }
763
764    void updateThumbnail(Bitmap newThumbnail, CharSequence description) {
765        if (thumbHolder != null) {
766            if (newThumbnail != null) {
767                if (ActivityManagerService.DEBUG_THUMBNAILS) Slog.i(ActivityManagerService.TAG,
768                        "Setting thumbnail of " + this + " holder " + thumbHolder
769                        + " to " + newThumbnail);
770                thumbHolder.lastThumbnail = newThumbnail;
771            }
772            thumbHolder.lastDescription = description;
773        }
774    }
775
776    void startLaunchTickingLocked() {
777        if (ActivityManagerService.IS_USER_BUILD) {
778            return;
779        }
780        if (launchTickTime == 0) {
781            launchTickTime = SystemClock.uptimeMillis();
782            continueLaunchTickingLocked();
783        }
784    }
785
786    boolean continueLaunchTickingLocked() {
787        if (launchTickTime != 0) {
788            final ActivityStack stack = task.stack;
789            Message msg = stack.mHandler.obtainMessage(ActivityStack.LAUNCH_TICK_MSG, this);
790            stack.mHandler.removeMessages(ActivityStack.LAUNCH_TICK_MSG);
791            stack.mHandler.sendMessageDelayed(msg, ActivityStack.LAUNCH_TICK);
792            return true;
793        }
794        return false;
795    }
796
797    void finishLaunchTickingLocked() {
798        launchTickTime = 0;
799        task.stack.mHandler.removeMessages(ActivityStack.LAUNCH_TICK_MSG);
800    }
801
802    // IApplicationToken
803
804    public boolean mayFreezeScreenLocked(ProcessRecord app) {
805        // Only freeze the screen if this activity is currently attached to
806        // an application, and that application is not blocked or unresponding.
807        // In any other case, we can't count on getting the screen unfrozen,
808        // so it is best to leave as-is.
809        return app != null && !app.crashing && !app.notResponding;
810    }
811
812    public void startFreezingScreenLocked(ProcessRecord app, int configChanges) {
813        if (mayFreezeScreenLocked(app)) {
814            service.mWindowManager.startAppFreezingScreen(appToken, configChanges);
815        }
816    }
817
818    public void stopFreezingScreenLocked(boolean force) {
819        if (force || frozenBeforeDestroy) {
820            frozenBeforeDestroy = false;
821            service.mWindowManager.stopAppFreezingScreen(appToken, force);
822        }
823    }
824
825    public void reportFullyDrawnLocked() {
826        final long curTime = SystemClock.uptimeMillis();
827        if (displayStartTime != 0) {
828            reportLaunchTimeLocked(curTime);
829        }
830        if (fullyDrawnStartTime != 0) {
831            final ActivityStack stack = task.stack;
832            final long thisTime = curTime - fullyDrawnStartTime;
833            final long totalTime = stack.mFullyDrawnStartTime != 0
834                    ? (curTime - stack.mFullyDrawnStartTime) : thisTime;
835            if (ActivityManagerService.SHOW_ACTIVITY_START_TIME) {
836                Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
837                EventLog.writeEvent(EventLogTags.AM_ACTIVITY_FULLY_DRAWN_TIME,
838                        userId, System.identityHashCode(this), shortComponentName,
839                        thisTime, totalTime);
840                StringBuilder sb = service.mStringBuilder;
841                sb.setLength(0);
842                sb.append("Fully drawn ");
843                sb.append(shortComponentName);
844                sb.append(": ");
845                TimeUtils.formatDuration(thisTime, sb);
846                if (thisTime != totalTime) {
847                    sb.append(" (total ");
848                    TimeUtils.formatDuration(totalTime, sb);
849                    sb.append(")");
850                }
851                Log.i(ActivityManagerService.TAG, sb.toString());
852            }
853            if (totalTime > 0) {
854                service.mUsageStatsService.noteFullyDrawnTime(realActivity, (int) totalTime);
855            }
856            fullyDrawnStartTime = 0;
857            stack.mFullyDrawnStartTime = 0;
858        }
859    }
860
861    private void reportLaunchTimeLocked(final long curTime) {
862        final ActivityStack stack = task.stack;
863        final long thisTime = curTime - displayStartTime;
864        final long totalTime = stack.mLaunchStartTime != 0
865                ? (curTime - stack.mLaunchStartTime) : thisTime;
866        if (ActivityManagerService.SHOW_ACTIVITY_START_TIME) {
867            Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
868            EventLog.writeEvent(EventLogTags.AM_ACTIVITY_LAUNCH_TIME,
869                    userId, System.identityHashCode(this), shortComponentName,
870                    thisTime, totalTime);
871            StringBuilder sb = service.mStringBuilder;
872            sb.setLength(0);
873            sb.append("Displayed ");
874            sb.append(shortComponentName);
875            sb.append(": ");
876            TimeUtils.formatDuration(thisTime, sb);
877            if (thisTime != totalTime) {
878                sb.append(" (total ");
879                TimeUtils.formatDuration(totalTime, sb);
880                sb.append(")");
881            }
882            Log.i(ActivityManagerService.TAG, sb.toString());
883        }
884        mStackSupervisor.reportActivityLaunchedLocked(false, this, thisTime, totalTime);
885        if (totalTime > 0) {
886            service.mUsageStatsService.noteLaunchTime(realActivity, (int)totalTime);
887        }
888        displayStartTime = 0;
889        stack.mLaunchStartTime = 0;
890    }
891
892    public void windowsDrawn() {
893        synchronized(service) {
894            if (displayStartTime != 0) {
895                reportLaunchTimeLocked(SystemClock.uptimeMillis());
896            }
897            startTime = 0;
898            finishLaunchTickingLocked();
899        }
900    }
901
902    public void windowsVisible() {
903        synchronized(service) {
904            mStackSupervisor.reportActivityVisibleLocked(this);
905            if (ActivityManagerService.DEBUG_SWITCH) Log.v(
906                    ActivityManagerService.TAG, "windowsVisible(): " + this);
907            if (!nowVisible) {
908                nowVisible = true;
909                lastVisibleTime = SystemClock.uptimeMillis();
910                if (!idle) {
911                    // Instead of doing the full stop routine here, let's just
912                    // hide any activities we now can, and let them stop when
913                    // the normal idle happens.
914                    mStackSupervisor.processStoppingActivitiesLocked(false);
915                } else {
916                    // If this activity was already idle, then we now need to
917                    // make sure we perform the full stop of any activities
918                    // that are waiting to do so.  This is because we won't
919                    // do that while they are still waiting for this one to
920                    // become visible.
921                    final int N = mStackSupervisor.mWaitingVisibleActivities.size();
922                    if (N > 0) {
923                        for (int i=0; i<N; i++) {
924                            ActivityRecord r = mStackSupervisor.mWaitingVisibleActivities.get(i);
925                            r.waitingVisible = false;
926                            if (ActivityManagerService.DEBUG_SWITCH) Log.v(
927                                    ActivityManagerService.TAG,
928                                    "Was waiting for visible: " + r);
929                        }
930                        mStackSupervisor.mWaitingVisibleActivities.clear();
931                        mStackSupervisor.scheduleIdleLocked();
932                    }
933                }
934                service.scheduleAppGcsLocked();
935            }
936        }
937    }
938
939    public void windowsGone() {
940        if (ActivityManagerService.DEBUG_SWITCH) Log.v(
941                ActivityManagerService.TAG, "windowsGone(): " + this);
942        nowVisible = false;
943    }
944
945    private ActivityRecord getWaitingHistoryRecordLocked() {
946        // First find the real culprit...  if we are waiting
947        // for another app to start, then we have paused dispatching
948        // for this activity.
949        ActivityRecord r = this;
950        final ActivityStack stack = task.stack;
951        if (r.waitingVisible) {
952            // Hmmm, who might we be waiting for?
953            r = stack.mResumedActivity;
954            if (r == null) {
955                r = stack.mPausingActivity;
956            }
957            // Both of those null?  Fall back to 'this' again
958            if (r == null) {
959                r = this;
960            }
961        }
962
963        return r;
964    }
965
966    public boolean keyDispatchingTimedOut(String reason) {
967        ActivityRecord r;
968        ProcessRecord anrApp;
969        synchronized(service) {
970            r = getWaitingHistoryRecordLocked();
971            anrApp = r != null ? r.app : null;
972        }
973        return service.inputDispatchingTimedOut(anrApp, r, this, false, reason);
974    }
975
976    /** Returns the key dispatching timeout for this application token. */
977    public long getKeyDispatchingTimeout() {
978        synchronized(service) {
979            ActivityRecord r = getWaitingHistoryRecordLocked();
980            return ActivityManagerService.getInputDispatchingTimeoutLocked(r);
981        }
982    }
983
984    /**
985     * This method will return true if the activity is either visible, is becoming visible, is
986     * currently pausing, or is resumed.
987     */
988    public boolean isInterestingToUserLocked() {
989        return visible || nowVisible || state == ActivityState.PAUSING ||
990                state == ActivityState.RESUMED;
991    }
992
993    public void setSleeping(boolean _sleeping) {
994        if (sleeping == _sleeping) {
995            return;
996        }
997        if (app != null && app.thread != null) {
998            try {
999                app.thread.scheduleSleeping(appToken, _sleeping);
1000                if (_sleeping && !mStackSupervisor.mGoingToSleepActivities.contains(this)) {
1001                    mStackSupervisor.mGoingToSleepActivities.add(this);
1002                }
1003                sleeping = _sleeping;
1004            } catch (RemoteException e) {
1005                Slog.w(TAG, "Exception thrown when sleeping: " + intent.getComponent(), e);
1006            }
1007        }
1008    }
1009
1010    static void activityResumedLocked(IBinder token) {
1011        final ActivityRecord r = ActivityRecord.forToken(token);
1012        if (DEBUG_SAVED_STATE) Slog.i(TAG, "Resumed activity; dropping state of: " + r);
1013        r.icicle = null;
1014        r.haveState = false;
1015    }
1016
1017    static int getTaskForActivityLocked(IBinder token, boolean onlyRoot) {
1018        final ActivityRecord r = ActivityRecord.forToken(token);
1019        if (r == null) {
1020            return -1;
1021        }
1022        final TaskRecord task = r.task;
1023        switch (task.mActivities.indexOf(r)) {
1024            case -1: return -1;
1025            case 0: return task.taskId;
1026            default: return onlyRoot ? -1 : task.taskId;
1027        }
1028    }
1029
1030    static ActivityRecord isInStackLocked(IBinder token) {
1031        final ActivityRecord r = ActivityRecord.forToken(token);
1032        if (r != null) {
1033            return r.task.stack.isInStackLocked(token);
1034        }
1035        return null;
1036    }
1037
1038    static ActivityStack getStackLocked(IBinder token) {
1039        final ActivityRecord r = ActivityRecord.isInStackLocked(token);
1040        if (r != null) {
1041            return r.task.stack;
1042        }
1043        return null;
1044    }
1045
1046    private String activityTypeToString(int type) {
1047        switch (type) {
1048            case APPLICATION_ACTIVITY_TYPE: return "APPLICATION_ACTIVITY_TYPE";
1049            case HOME_ACTIVITY_TYPE: return "HOME_ACTIVITY_TYPE";
1050            case RECENTS_ACTIVITY_TYPE: return "RECENTS_ACTIVITY_TYPE";
1051            default: return Integer.toString(type);
1052        }
1053    }
1054
1055    @Override
1056    public String toString() {
1057        if (stringName != null) {
1058            return stringName + " t" + (task == null ? -1 : task.taskId) +
1059                    (finishing ? " f}" : "}");
1060        }
1061        StringBuilder sb = new StringBuilder(128);
1062        sb.append("ActivityRecord{");
1063        sb.append(Integer.toHexString(System.identityHashCode(this)));
1064        sb.append(" u");
1065        sb.append(userId);
1066        sb.append(' ');
1067        sb.append(intent.getComponent().flattenToShortString());
1068        stringName = sb.toString();
1069        return toString();
1070    }
1071}
1072