Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipUtils.java

raw symbols SCIP
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.pip;
18
19 import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
20 import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
21
22 import android.annotation.Nullable;
23 import android.app.ActivityTaskManager;
24 import android.app.ActivityTaskManager.RootTaskInfo;
25 import android.app.RemoteAction;
26 import android.content.ComponentName;
27 import android.content.Context;
28 import android.os.RemoteException;
29 import android.util.Log;
30 import android.util.Pair;
31 import android.window.TaskSnapshot;
32
33 import com.android.internal.protolog.common.ProtoLog;
34 import com.android.wm.shell.protolog.ShellProtoLogGroup;
35
36 import java.util.List;
37 import java.util.Objects;
38
39 /** A class that includes convenience methods. */
40 public class PipUtils {
41 private static final String TAG = "PipUtils";
42
43 // Minimum difference between two floats (e.g. aspect ratios) to consider them not equal.
44 private static final double EPSILON = 1e-7;
45
46 /**
47 * @return the ComponentName and user id of the top non-SystemUI activity in the pinned stack.
48 * The component name may be null if no such activity exists.
49 */
50 public static Pair<ComponentName, Integer> getTopPipActivity(Context context) {
51 try {
52 final String sysUiPackageName = context.getPackageName();
53 final RootTaskInfo pinnedTaskInfo = ActivityTaskManager.getService().getRootTaskInfo(
54 WINDOWING_MODE_PINNED, ACTIVITY_TYPE_UNDEFINED);
55 if (pinnedTaskInfo != null && pinnedTaskInfo.childTaskIds != null
56 && pinnedTaskInfo.childTaskIds.length > 0) {
57 for (int i = pinnedTaskInfo.childTaskNames.length - 1; i >= 0; i--) {
58 ComponentName cn = ComponentName.unflattenFromString(
59 pinnedTaskInfo.childTaskNames[i]);
60 if (cn != null && !cn.getPackageName().equals(sysUiPackageName)) {
61 return new Pair<>(cn, pinnedTaskInfo.childTaskUserIds[i]);
62 }
63 }
64 }
65 } catch (RemoteException e) {
66 ProtoLog.w(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
67 "%s: Unable to get pinned stack.", TAG);
68 }
69 return new Pair<>(null, 0);
70 }
71
72 /**
73 * @return true if the aspect ratios differ
74 */
75 public static boolean aspectRatioChanged(float aspectRatio1, float aspectRatio2) {
76 return Math.abs(aspectRatio1 - aspectRatio2) > EPSILON;
77 }
78
79 /**
80 * Checks whether title, description and intent match.
81 * Comparing icons would be good, but using equals causes false negatives
82 */
83 public static boolean remoteActionsMatch(RemoteAction action1, RemoteAction action2) {
84 if (action1 == action2) return true;
85 if (action1 == null || action2 == null) return false;
86 return action1.isEnabled() == action2.isEnabled()
87 && action1.shouldShowIcon() == action2.shouldShowIcon()
88 && Objects.equals(action1.getTitle(), action2.getTitle())
89 && Objects.equals(action1.getContentDescription(), action2.getContentDescription())
90 && Objects.equals(action1.getActionIntent(), action2.getActionIntent());
91 }
92
93 /**
94 * Returns true if the actions in the lists match each other according to {@link
95 * PipUtils#remoteActionsMatch(RemoteAction, RemoteAction)}, including their position.
96 */
97 public static boolean remoteActionsChanged(List<RemoteAction> list1, List<RemoteAction> list2) {
98 if (list1 == null && list2 == null) {
99 return false;
100 }
101 if (list1 == null || list2 == null) {
102 return true;
103 }
104 if (list1.size() != list2.size()) {
105 return true;
106 }
107 for (int i = 0; i < list1.size(); i++) {
108 if (!remoteActionsMatch(list1.get(i), list2.get(i))) {
109 return true;
110 }
111 }
112 return false;
113 }
114
115 /** @return {@link TaskSnapshot} for a given task id. */
116 @Nullable
117 public static TaskSnapshot getTaskSnapshot(int taskId, boolean isLowResolution) {
118 if (taskId <= 0) return null;
119 try {
120 return ActivityTaskManager.getService().getTaskSnapshot(
121 taskId, isLowResolution, false /* takeSnapshotIfNeeded */);
122 } catch (RemoteException e) {
123 Log.e(TAG, "Failed to get task snapshot, taskId=" + taskId, e);
124 return null;
125 }
126 }
127 }