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.content.ComponentName;
20import android.content.Intent;
21import android.content.pm.ActivityInfo;
22import android.os.UserHandle;
23import android.util.Slog;
24
25import java.io.PrintWriter;
26
27class TaskRecord extends ThumbnailHolder {
28    final int taskId;       // Unique identifier for this task.
29    final String affinity;  // The affinity name for this task, or null.
30    Intent intent;          // The original intent that started the task.
31    Intent affinityIntent;  // Intent of affinity-moved activity that started this task.
32    ComponentName origActivity; // The non-alias activity component of the intent.
33    ComponentName realActivity; // The actual activity component that started the task.
34    int numActivities;      // Current number of activities in this task.
35    long lastActiveTime;    // Last time this task was active, including sleep.
36    boolean rootWasReset;   // True if the intent at the root of the task had
37                            // the FLAG_ACTIVITY_RESET_TASK_IF_NEEDED flag.
38    boolean askedCompatMode;// Have asked the user about compat mode for this task.
39
40    String stringName;      // caching of toString() result.
41    int userId;             // user for which this task was created
42
43    TaskRecord(int _taskId, ActivityInfo info, Intent _intent) {
44        taskId = _taskId;
45        affinity = info.taskAffinity;
46        setIntent(_intent, info);
47    }
48
49    void touchActiveTime() {
50        lastActiveTime = android.os.SystemClock.elapsedRealtime();
51    }
52
53    long getInactiveDuration() {
54        return android.os.SystemClock.elapsedRealtime() - lastActiveTime;
55    }
56
57    void setIntent(Intent _intent, ActivityInfo info) {
58        stringName = null;
59
60        if (info.targetActivity == null) {
61            if (_intent != null) {
62                // If this Intent has a selector, we want to clear it for the
63                // recent task since it is not relevant if the user later wants
64                // to re-launch the app.
65                if (_intent.getSelector() != null || _intent.getSourceBounds() != null) {
66                    _intent = new Intent(_intent);
67                    _intent.setSelector(null);
68                    _intent.setSourceBounds(null);
69                }
70            }
71            if (ActivityManagerService.DEBUG_TASKS) Slog.v(ActivityManagerService.TAG,
72                    "Setting Intent of " + this + " to " + _intent);
73            intent = _intent;
74            realActivity = _intent != null ? _intent.getComponent() : null;
75            origActivity = null;
76        } else {
77            ComponentName targetComponent = new ComponentName(
78                    info.packageName, info.targetActivity);
79            if (_intent != null) {
80                Intent targetIntent = new Intent(_intent);
81                targetIntent.setComponent(targetComponent);
82                targetIntent.setSelector(null);
83                targetIntent.setSourceBounds(null);
84                if (ActivityManagerService.DEBUG_TASKS) Slog.v(ActivityManagerService.TAG,
85                        "Setting Intent of " + this + " to target " + targetIntent);
86                intent = targetIntent;
87                realActivity = targetComponent;
88                origActivity = _intent.getComponent();
89            } else {
90                intent = null;
91                realActivity = targetComponent;
92                origActivity = new ComponentName(info.packageName, info.name);
93            }
94        }
95
96        if (intent != null &&
97                (intent.getFlags()&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
98            // Once we are set to an Intent with this flag, we count this
99            // task as having a true root activity.
100            rootWasReset = true;
101        }
102
103        if (info.applicationInfo != null) {
104            userId = UserHandle.getUserId(info.applicationInfo.uid);
105        }
106    }
107
108    void dump(PrintWriter pw, String prefix) {
109        if (numActivities != 0 || rootWasReset || userId != 0) {
110            pw.print(prefix); pw.print("numActivities="); pw.print(numActivities);
111                    pw.print(" rootWasReset="); pw.print(rootWasReset);
112                    pw.print(" userId="); pw.println(userId);
113        }
114        if (affinity != null) {
115            pw.print(prefix); pw.print("affinity="); pw.println(affinity);
116        }
117        if (intent != null) {
118            StringBuilder sb = new StringBuilder(128);
119            sb.append(prefix); sb.append("intent={");
120            intent.toShortString(sb, false, true, false, true);
121            sb.append('}');
122            pw.println(sb.toString());
123        }
124        if (affinityIntent != null) {
125            StringBuilder sb = new StringBuilder(128);
126            sb.append(prefix); sb.append("affinityIntent={");
127            affinityIntent.toShortString(sb, false, true, false, true);
128            sb.append('}');
129            pw.println(sb.toString());
130        }
131        if (origActivity != null) {
132            pw.print(prefix); pw.print("origActivity=");
133            pw.println(origActivity.flattenToShortString());
134        }
135        if (realActivity != null) {
136            pw.print(prefix); pw.print("realActivity=");
137            pw.println(realActivity.flattenToShortString());
138        }
139        if (!askedCompatMode) {
140            pw.print(prefix); pw.print("askedCompatMode="); pw.println(askedCompatMode);
141        }
142        pw.print(prefix); pw.print("lastThumbnail="); pw.print(lastThumbnail);
143                pw.print(" lastDescription="); pw.println(lastDescription);
144        pw.print(prefix); pw.print("lastActiveTime="); pw.print(lastActiveTime);
145                pw.print(" (inactive for ");
146                pw.print((getInactiveDuration()/1000)); pw.println("s)");
147    }
148
149    public String toString() {
150        if (stringName != null) {
151            return stringName;
152        }
153        StringBuilder sb = new StringBuilder(128);
154        sb.append("TaskRecord{");
155        sb.append(Integer.toHexString(System.identityHashCode(this)));
156        sb.append(" #");
157        sb.append(taskId);
158        if (affinity != null) {
159            sb.append(" A ");
160            sb.append(affinity);
161        } else if (intent != null) {
162            sb.append(" I ");
163            sb.append(intent.getComponent().flattenToShortString());
164        } else if (affinityIntent != null) {
165            sb.append(" aI ");
166            sb.append(affinityIntent.getComponent().flattenToShortString());
167        } else {
168            sb.append(" ??");
169        }
170        sb.append(" U ");
171        sb.append(userId);
172        sb.append('}');
173        return stringName = sb.toString();
174    }
175}
176