1 /*
2  * Copyright (C) 2020 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 com.android.wm.shell;
18 
19 import static android.app.ActivityTaskManager.INVALID_TASK_ID;
20 import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
21 import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
22 
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.mock;
25 
26 import android.annotation.NonNull;
27 import android.app.ActivityManager;
28 import android.app.WindowConfiguration;
29 import android.content.ComponentName;
30 import android.content.Intent;
31 import android.graphics.Point;
32 import android.graphics.Rect;
33 import android.os.IBinder;
34 import android.view.Display;
35 import android.window.IWindowContainerToken;
36 import android.window.WindowContainerToken;
37 
38 public final class TestRunningTaskInfoBuilder {
39     static int sNextTaskId = 500;
40     private Rect mBounds = new Rect(0, 0, 100, 100);
41 
42     private WindowContainerToken mToken = createMockWCToken();
43     private int mParentTaskId = INVALID_TASK_ID;
44     private int mUid = INVALID_TASK_ID;
45     private int mTaskId = INVALID_TASK_ID;
46     private int mUserId = -1;
47     private Intent mBaseIntent = new Intent();
48     private ComponentName mBaseActivity = null;
49     private @WindowConfiguration.ActivityType int mActivityType = ACTIVITY_TYPE_STANDARD;
50     private @WindowConfiguration.WindowingMode int mWindowingMode = WINDOWING_MODE_UNDEFINED;
51     private @WindowConfiguration.ActivityType int mTopActivityType = ACTIVITY_TYPE_STANDARD;
52     private int mDisplayId = Display.DEFAULT_DISPLAY;
53     private ActivityManager.TaskDescription.Builder mTaskDescriptionBuilder = null;
54     private final Point mPositionInParent = new Point();
55     private boolean mIsVisible = false;
56     private boolean mIsTopActivityTransparent = false;
57     private boolean mIsActivityStackTransparent = false;
58     private int mNumActivities = 1;
59     private long mLastActiveTime;
60 
createMockWCToken()61     public static WindowContainerToken createMockWCToken() {
62         final IWindowContainerToken itoken = mock(IWindowContainerToken.class);
63         final IBinder asBinder = mock(IBinder.class);
64         doReturn(asBinder).when(itoken).asBinder();
65         return new WindowContainerToken(itoken);
66     }
67 
setToken(WindowContainerToken token)68     public TestRunningTaskInfoBuilder setToken(WindowContainerToken token) {
69         mToken = token;
70         return this;
71     }
72 
setBounds(Rect bounds)73     public TestRunningTaskInfoBuilder setBounds(Rect bounds) {
74         mBounds.set(bounds);
75         return this;
76     }
77 
setParentTaskId(int taskId)78     public TestRunningTaskInfoBuilder setParentTaskId(int taskId) {
79         mParentTaskId = taskId;
80         return this;
81     }
82 
83     /** Sets the task info's effective UID. */
setUid(int uid)84     public TestRunningTaskInfoBuilder setUid(int uid) {
85         mUid = uid;
86         return this;
87     }
88 
89     /** Sets the task info's UID. */
setTaskId(int taskId)90     public TestRunningTaskInfoBuilder setTaskId(int taskId) {
91         mTaskId = taskId;
92         return this;
93     }
94 
95     /** Sets the task info's user id. */
setUserId(int userId)96     public TestRunningTaskInfoBuilder setUserId(int userId) {
97         mUserId = userId;
98         return this;
99     }
100 
101     /**
102      * Set {@link ActivityManager.RunningTaskInfo#baseIntent} for the task info, by default
103      * an empty intent is assigned
104      */
setBaseIntent(@onNull Intent intent)105     public TestRunningTaskInfoBuilder setBaseIntent(@NonNull Intent intent) {
106         mBaseIntent = intent;
107         return this;
108     }
109 
110     /**
111      * Set {@link ActivityManager.RunningTaskInfo#baseActivity} for the task info.
112      */
setBaseActivity(@onNull ComponentName activity)113     public TestRunningTaskInfoBuilder setBaseActivity(@NonNull ComponentName activity) {
114         mBaseActivity = activity;
115         return this;
116     }
117 
setActivityType( @indowConfiguration.ActivityType int activityType)118     public TestRunningTaskInfoBuilder setActivityType(
119             @WindowConfiguration.ActivityType int activityType) {
120         mActivityType = activityType;
121         return this;
122     }
123 
setTopActivityType( @indowConfiguration.ActivityType int activityType)124     public TestRunningTaskInfoBuilder setTopActivityType(
125             @WindowConfiguration.ActivityType int activityType) {
126         mTopActivityType = activityType;
127         return this;
128     }
129 
setWindowingMode( @indowConfiguration.WindowingMode int windowingMode)130     public TestRunningTaskInfoBuilder setWindowingMode(
131             @WindowConfiguration.WindowingMode int windowingMode) {
132         mWindowingMode = windowingMode;
133         return this;
134     }
135 
setDisplayId(int displayId)136     public TestRunningTaskInfoBuilder setDisplayId(int displayId) {
137         mDisplayId = displayId;
138         return this;
139     }
140 
setTaskDescriptionBuilder( ActivityManager.TaskDescription.Builder builder)141     public TestRunningTaskInfoBuilder setTaskDescriptionBuilder(
142             ActivityManager.TaskDescription.Builder builder) {
143         mTaskDescriptionBuilder = builder;
144         return this;
145     }
146 
setPositionInParent(int x, int y)147     public TestRunningTaskInfoBuilder setPositionInParent(int x, int y) {
148         mPositionInParent.set(x, y);
149         return this;
150     }
151 
setVisible(boolean isVisible)152     public TestRunningTaskInfoBuilder setVisible(boolean isVisible) {
153         mIsVisible = isVisible;
154         return this;
155     }
156 
setTopActivityTransparent(boolean isTopActivityTransparent)157     public TestRunningTaskInfoBuilder setTopActivityTransparent(boolean isTopActivityTransparent) {
158         mIsTopActivityTransparent = isTopActivityTransparent;
159         return this;
160     }
161 
setActivityStackTransparent( boolean isActivityStackTransparent)162     public TestRunningTaskInfoBuilder setActivityStackTransparent(
163             boolean isActivityStackTransparent) {
164         mIsActivityStackTransparent = isActivityStackTransparent;
165         return this;
166     }
167 
setNumActivities(int numActivities)168     public TestRunningTaskInfoBuilder setNumActivities(int numActivities) {
169         mNumActivities = numActivities;
170         return this;
171     }
172 
setLastActiveTime(long lastActiveTime)173     public TestRunningTaskInfoBuilder setLastActiveTime(long lastActiveTime) {
174         mLastActiveTime = lastActiveTime;
175         return this;
176     }
177 
build()178     public ActivityManager.RunningTaskInfo build() {
179         final ActivityManager.RunningTaskInfo info = new ActivityManager.RunningTaskInfo();
180         info.taskId = (mTaskId == INVALID_TASK_ID) ? sNextTaskId++ : mTaskId;
181         info.effectiveUid = mUid;
182         info.baseIntent = mBaseIntent;
183         info.parentTaskId = mParentTaskId;
184         info.displayId = mDisplayId;
185         info.configuration.windowConfiguration.setBounds(mBounds);
186         info.configuration.windowConfiguration.setActivityType(mActivityType);
187         info.configuration.windowConfiguration.setWindowingMode(mWindowingMode);
188         info.topActivityType = mTopActivityType;
189         info.token = mToken;
190         info.isResizeable = true;
191         info.supportsMultiWindow = true;
192         info.taskDescription =
193                 mTaskDescriptionBuilder != null ? mTaskDescriptionBuilder.build() : null;
194         info.positionInParent = mPositionInParent;
195         info.isVisible = mIsVisible;
196         info.isTopActivityTransparent = mIsTopActivityTransparent;
197         info.isActivityStackTransparent = mIsActivityStackTransparent;
198         info.numActivities = mNumActivities;
199         info.lastActiveTime = mLastActiveTime;
200         info.userId = mUserId;
201         info.baseActivity = mBaseActivity;
202         return info;
203     }
204 }
205