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