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 android.content.ComponentName;
20 import android.content.Context;
21 import android.content.pm.ApplicationInfo;
22 import android.content.pm.PackageManager;
23 import android.os.UserHandle;
24 import android.util.Log;
25 
26 import com.android.launcher3.pm.UserCache;
27 import com.android.launcher3.util.ComponentKey;
28 import com.android.launcher3.util.PackageManagerHelper;
29 import com.android.systemui.shared.recents.model.Task;
30 import com.android.systemui.shared.system.RemoteAnimationTargetCompat;
31 
32 import java.util.List;
33 
34 /**
35  * Contains helpful methods for retrieving data from {@link Task}s.
36  */
37 public final class TaskUtils {
38 
39     private static final String TAG = "TaskUtils";
40 
TaskUtils()41     private TaskUtils() {}
42 
43     /**
44      * TODO: remove this once we switch to getting the icon and label from IconCache.
45      */
getTitle(Context context, Task task)46     public static CharSequence getTitle(Context context, Task task) {
47         UserHandle user = UserHandle.of(task.key.userId);
48         ApplicationInfo applicationInfo = new PackageManagerHelper(context)
49                 .getApplicationInfo(task.getTopComponent().getPackageName(), user, 0);
50         if (applicationInfo == null) {
51             Log.e(TAG, "Failed to get title for task " + task);
52             return "";
53         }
54         PackageManager packageManager = context.getPackageManager();
55         return packageManager.getUserBadgedLabel(
56             applicationInfo.loadLabel(packageManager), user);
57     }
58 
getLaunchComponentKeyForTask(Task.TaskKey taskKey)59     public static ComponentKey getLaunchComponentKeyForTask(Task.TaskKey taskKey) {
60         final ComponentName cn = taskKey.sourceComponent != null
61                 ? taskKey.sourceComponent
62                 : taskKey.getComponent();
63         return new ComponentKey(cn, UserHandle.of(taskKey.userId));
64     }
65 
66 
taskIsATargetWithMode(RemoteAnimationTargetCompat[] targets, int taskId, int mode)67     public static boolean taskIsATargetWithMode(RemoteAnimationTargetCompat[] targets,
68             int taskId, int mode) {
69         for (RemoteAnimationTargetCompat target : targets) {
70             if (target.mode == mode && target.taskId == taskId) {
71                 return true;
72             }
73         }
74         return false;
75     }
76 
checkCurrentOrManagedUserId(int currentUserId, Context context)77     public static boolean checkCurrentOrManagedUserId(int currentUserId, Context context) {
78         if (currentUserId == UserHandle.myUserId()) {
79             return true;
80         }
81         List<UserHandle> allUsers = UserCache.INSTANCE.get(context).getUserProfiles();
82         for (int i = allUsers.size() - 1; i >= 0; i--) {
83             if (currentUserId == allUsers.get(i).getIdentifier()) {
84                 return true;
85             }
86         }
87         return false;
88     }
89 }
90