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