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