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