1 /*
2  * Copyright (C) 2018 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 
17 package android.app;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.compat.annotation.UnsupportedAppUsage;
22 import android.content.ComponentName;
23 import android.content.Intent;
24 import android.content.res.Configuration;
25 import android.os.Parcel;
26 import android.os.RemoteException;
27 import android.util.Log;
28 
29 /**
30  * Stores information about a particular Task.
31  */
32 public class TaskInfo {
33     private static final String TAG = "TaskInfo";
34 
35     /**
36      * The id of the user the task was running as.
37      * @hide
38      */
39     @UnsupportedAppUsage
40     public int userId;
41 
42     /**
43      * The id of the ActivityStack that currently contains this task.
44      * @hide
45      */
46     @UnsupportedAppUsage
47     public int stackId;
48 
49     /**
50      * The identifier for this task.
51      */
52     public int taskId;
53 
54     /**
55      * Whether or not this task has any running activities.
56      */
57     public boolean isRunning;
58 
59     /**
60      * The base intent of the task (generally the intent that launched the task). This intent can
61      * be used to relaunch the task (if it is no longer running) or brought to the front if it is.
62      */
63     @NonNull
64     public Intent baseIntent;
65 
66     /**
67      * The component of the first activity in the task, can be considered the "application" of this
68      * task.
69      */
70     @Nullable
71     public ComponentName baseActivity;
72 
73     /**
74      * The component of the top activity in the task, currently showing to the user.
75      */
76     @Nullable
77     public ComponentName topActivity;
78 
79     /**
80      * The component of the target activity if this task was started from an activity alias.
81      * Otherwise, this is null.
82      */
83     @Nullable
84     public ComponentName origActivity;
85 
86     /**
87      * The component of the activity that started this task (may be the component of the activity
88      * alias).
89      * @hide
90      */
91     @Nullable
92     public ComponentName realActivity;
93 
94     /**
95      * The number of activities in this task (including running).
96      */
97     public int numActivities;
98 
99     /**
100      * The last time this task was active since boot (including time spent in sleep).
101      * @hide
102      */
103     @UnsupportedAppUsage
104     public long lastActiveTime;
105 
106     /**
107      * The id of the display this task is associated with.
108      * @hide
109      */
110     public int displayId;
111 
112     /**
113      * The recent activity values for the highest activity in the stack to have set the values.
114      * {@link Activity#setTaskDescription(android.app.ActivityManager.TaskDescription)}.
115      */
116     @Nullable
117     public ActivityManager.TaskDescription taskDescription;
118 
119     /**
120      * True if the task can go in the split-screen primary stack.
121      * @hide
122      */
123     @UnsupportedAppUsage
124     public boolean supportsSplitScreenMultiWindow;
125 
126     /**
127      * The resize mode of the task. See {@link ActivityInfo#resizeMode}.
128      * @hide
129      */
130     @UnsupportedAppUsage
131     public int resizeMode;
132 
133     /**
134      * The current configuration of the task.
135      * @hide
136      */
137     @NonNull
138     @UnsupportedAppUsage
139     public final Configuration configuration = new Configuration();
140 
TaskInfo()141     TaskInfo() {
142         // Do nothing
143     }
144 
TaskInfo(Parcel source)145     private TaskInfo(Parcel source) {
146         readFromParcel(source);
147     }
148 
149     /**
150      * @param reducedResolution
151      * @return
152      * @hide
153      */
getTaskSnapshot(boolean reducedResolution)154     public ActivityManager.TaskSnapshot getTaskSnapshot(boolean reducedResolution) {
155         try {
156             return ActivityManager.getService().getTaskSnapshot(taskId, reducedResolution);
157         } catch (RemoteException e) {
158             Log.e(TAG, "Failed to get task snapshot, taskId=" + taskId, e);
159             return null;
160         }
161     }
162 
163     /**
164      * Reads the TaskInfo from a parcel.
165      */
readFromParcel(Parcel source)166     void readFromParcel(Parcel source) {
167         userId = source.readInt();
168         stackId = source.readInt();
169         taskId = source.readInt();
170         displayId = source.readInt();
171         isRunning = source.readBoolean();
172         baseIntent = source.readInt() != 0
173                 ? Intent.CREATOR.createFromParcel(source)
174                 : null;
175         baseActivity = ComponentName.readFromParcel(source);
176         topActivity = ComponentName.readFromParcel(source);
177         origActivity = ComponentName.readFromParcel(source);
178         realActivity = ComponentName.readFromParcel(source);
179 
180         numActivities = source.readInt();
181         lastActiveTime = source.readLong();
182 
183         taskDescription = source.readInt() != 0
184                 ? ActivityManager.TaskDescription.CREATOR.createFromParcel(source)
185                 : null;
186         supportsSplitScreenMultiWindow = source.readBoolean();
187         resizeMode = source.readInt();
188         configuration.readFromParcel(source);
189     }
190 
191     /**
192      * Writes the TaskInfo to a parcel.
193      */
writeToParcel(Parcel dest, int flags)194     void writeToParcel(Parcel dest, int flags) {
195         dest.writeInt(userId);
196         dest.writeInt(stackId);
197         dest.writeInt(taskId);
198         dest.writeInt(displayId);
199         dest.writeBoolean(isRunning);
200 
201         if (baseIntent != null) {
202             dest.writeInt(1);
203             baseIntent.writeToParcel(dest, 0);
204         } else {
205             dest.writeInt(0);
206         }
207         ComponentName.writeToParcel(baseActivity, dest);
208         ComponentName.writeToParcel(topActivity, dest);
209         ComponentName.writeToParcel(origActivity, dest);
210         ComponentName.writeToParcel(realActivity, dest);
211 
212         dest.writeInt(numActivities);
213         dest.writeLong(lastActiveTime);
214 
215         if (taskDescription != null) {
216             dest.writeInt(1);
217             taskDescription.writeToParcel(dest, flags);
218         } else {
219             dest.writeInt(0);
220         }
221         dest.writeBoolean(supportsSplitScreenMultiWindow);
222         dest.writeInt(resizeMode);
223         configuration.writeToParcel(dest, flags);
224     }
225 
226     @Override
toString()227     public String toString() {
228         return "TaskInfo{userId=" + userId + " stackId=" + stackId + " taskId=" + taskId
229                 + " displayId=" + displayId
230                 + " isRunning=" + isRunning
231                 + " baseIntent=" + baseIntent + " baseActivity=" + baseActivity
232                 + " topActivity=" + topActivity + " origActivity=" + origActivity
233                 + " realActivity=" + realActivity
234                 + " numActivities=" + numActivities
235                 + " lastActiveTime=" + lastActiveTime
236                 + " supportsSplitScreenMultiWindow=" + supportsSplitScreenMultiWindow
237                 + " resizeMode=" + resizeMode;
238     }
239 }
240