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