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 com.android.quickstep;
18 
19 import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;
20 
21 import android.annotation.NonNull;
22 import android.annotation.UserIdInt;
23 import android.content.ComponentName;
24 import android.content.Context;
25 import android.content.pm.ApplicationInfo;
26 import android.content.pm.PackageManager;
27 import android.os.UserHandle;
28 import android.util.Log;
29 import android.view.RemoteAnimationTarget;
30 
31 import androidx.annotation.Nullable;
32 
33 import com.android.launcher3.pm.UserCache;
34 import com.android.launcher3.util.ComponentKey;
35 import com.android.launcher3.util.PackageManagerHelper;
36 import com.android.launcher3.util.TraceHelper;
37 import com.android.systemui.shared.recents.model.Task;
38 import com.android.systemui.shared.system.ActivityManagerWrapper;
39 
40 import java.util.List;
41 
42 /**
43  * Contains helpful methods for retrieving data from {@link Task}s.
44  */
45 public final class TaskUtils {
46 
47     private static final String TAG = "TaskUtils";
48 
TaskUtils()49     private TaskUtils() {}
50 
51     /**
52      * TODO: remove this once we switch to getting the icon and label from IconCache.
53      */
getTitle(Context context, Task task)54     public static CharSequence getTitle(Context context, Task task) {
55         return TraceHelper.allowIpcs("TaskUtils.getTitle", () ->
56                 getTitle(context, task.key.userId, task.getTopComponent().getPackageName()));
57     }
58 
getTitle( @onNull Context context, @UserIdInt @Nullable Integer userId, @Nullable String packageName)59     public static CharSequence getTitle(
60             @NonNull Context context,
61             @UserIdInt @Nullable Integer userId,
62             @Nullable String packageName) {
63         if (userId == null || packageName == null) {
64             if (userId == null) {
65                 Log.e(TAG, "Failed to get title; missing userId");
66             }
67             if (packageName == null) {
68                 Log.e(TAG, "Failed to get title; missing packageName");
69             }
70             return "";
71         }
72         UserHandle user = UserHandle.of(userId);
73         ApplicationInfo applicationInfo = PackageManagerHelper.INSTANCE.get(context)
74                 .getApplicationInfo(packageName, user, 0);
75         if (applicationInfo == null) {
76             Log.e(TAG, "Failed to get title for userId=" + userId + ", packageName=" + packageName);
77             return "";
78         }
79         PackageManager packageManager = context.getPackageManager();
80         return packageManager.getUserBadgedLabel(
81                 applicationInfo.loadLabel(packageManager), user);
82     }
83 
getLaunchComponentKeyForTask(Task.TaskKey taskKey)84     public static ComponentKey getLaunchComponentKeyForTask(Task.TaskKey taskKey) {
85         final ComponentName cn = taskKey.sourceComponent != null
86                 ? taskKey.sourceComponent
87                 : taskKey.getComponent();
88         return new ComponentKey(cn, UserHandle.of(taskKey.userId));
89     }
90 
91 
taskIsATargetWithMode(RemoteAnimationTarget[] targets, int taskId, int mode)92     public static boolean taskIsATargetWithMode(RemoteAnimationTarget[] targets,
93             int taskId, int mode) {
94         for (RemoteAnimationTarget target : targets) {
95             if (target.mode == mode && target.taskId == taskId) {
96                 return true;
97             }
98         }
99         return false;
100     }
101 
checkCurrentOrManagedUserId(int currentUserId, Context context)102     public static boolean checkCurrentOrManagedUserId(int currentUserId, Context context) {
103         if (currentUserId == UserHandle.myUserId()) {
104             return true;
105         }
106         List<UserHandle> allUsers = UserCache.INSTANCE.get(context).getUserProfiles();
107         for (int i = allUsers.size() - 1; i >= 0; i--) {
108             if (currentUserId == allUsers.get(i).getIdentifier()) {
109                 return true;
110             }
111         }
112         return false;
113     }
114 
115     /**
116      * Requests that the system close any open system windows (including other SystemUI).
117      */
closeSystemWindowsAsync(String reason)118     public static void closeSystemWindowsAsync(String reason) {
119         UI_HELPER_EXECUTOR.execute(
120                 () -> ActivityManagerWrapper.getInstance().closeSystemWindows(reason));
121     }
122 }
123