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