1 /*
2  * Copyright (C) 2023 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.common.split;
18 
19 import static com.android.wm.shell.common.split.SplitScreenConstants.CONTROLLED_ACTIVITY_TYPES;
20 import static com.android.wm.shell.common.split.SplitScreenConstants.CONTROLLED_WINDOWING_MODES;
21 import static com.android.wm.shell.common.split.SplitScreenConstants.SPLIT_POSITION_BOTTOM_OR_RIGHT;
22 import static com.android.wm.shell.common.split.SplitScreenConstants.SPLIT_POSITION_TOP_OR_LEFT;
23 import static com.android.wm.shell.common.split.SplitScreenConstants.SPLIT_POSITION_UNDEFINED;
24 
25 import android.annotation.Nullable;
26 import android.app.ActivityManager;
27 import android.app.PendingIntent;
28 import android.content.Intent;
29 
30 import com.android.internal.util.ArrayUtils;
31 import com.android.wm.shell.ShellTaskOrganizer;
32 
33 /** Helper utility class for split screen components to use. */
34 public class SplitScreenUtils {
35     /** Reverse the split position. */
36     @SplitScreenConstants.SplitPosition
reverseSplitPosition(@plitScreenConstants.SplitPosition int position)37     public static int reverseSplitPosition(@SplitScreenConstants.SplitPosition int position) {
38         switch (position) {
39             case SPLIT_POSITION_TOP_OR_LEFT:
40                 return SPLIT_POSITION_BOTTOM_OR_RIGHT;
41             case SPLIT_POSITION_BOTTOM_OR_RIGHT:
42                 return SPLIT_POSITION_TOP_OR_LEFT;
43             case SPLIT_POSITION_UNDEFINED:
44             default:
45                 return SPLIT_POSITION_UNDEFINED;
46         }
47     }
48 
49     /** Returns true if the task is valid for split screen. */
isValidToSplit(ActivityManager.RunningTaskInfo taskInfo)50     public static boolean isValidToSplit(ActivityManager.RunningTaskInfo taskInfo) {
51         return taskInfo != null && taskInfo.supportsMultiWindow
52                 && ArrayUtils.contains(CONTROLLED_ACTIVITY_TYPES, taskInfo.getActivityType())
53                 && ArrayUtils.contains(CONTROLLED_WINDOWING_MODES, taskInfo.getWindowingMode());
54     }
55 
56     /** Retrieve package name from an intent */
57     @Nullable
getPackageName(Intent intent)58     public static String getPackageName(Intent intent) {
59         if (intent == null || intent.getComponent() == null) {
60             return null;
61         }
62         return intent.getComponent().getPackageName();
63     }
64 
65     /** Retrieve package name from a PendingIntent */
66     @Nullable
getPackageName(PendingIntent pendingIntent)67     public static String getPackageName(PendingIntent pendingIntent) {
68         if (pendingIntent == null || pendingIntent.getIntent() == null) {
69             return null;
70         }
71         return getPackageName(pendingIntent.getIntent());
72     }
73 
74     /** Retrieve package name from a taskId */
75     @Nullable
getPackageName(int taskId, ShellTaskOrganizer taskOrganizer)76     public static String getPackageName(int taskId, ShellTaskOrganizer taskOrganizer) {
77         final ActivityManager.RunningTaskInfo taskInfo = taskOrganizer.getRunningTaskInfo(taskId);
78         return taskInfo != null ? getPackageName(taskInfo.baseIntent) : null;
79     }
80 
81     /** Retrieve user id from a taskId */
getUserId(int taskId, ShellTaskOrganizer taskOrganizer)82     public static int getUserId(int taskId, ShellTaskOrganizer taskOrganizer) {
83         final ActivityManager.RunningTaskInfo taskInfo = taskOrganizer.getRunningTaskInfo(taskId);
84         return taskInfo != null ? taskInfo.userId : -1;
85     }
86 
87     /** Returns true if package names and user ids match. */
samePackage(String packageName1, String packageName2, int userId1, int userId2)88     public static boolean samePackage(String packageName1, String packageName2,
89             int userId1, int userId2) {
90         return (packageName1 != null && packageName1.equals(packageName2)) && (userId1 == userId2);
91     }
92 
93     /** Generates a common log message for split screen failures */
splitFailureMessage(String caller, String reason)94     public static String splitFailureMessage(String caller, String reason) {
95         return "(" + caller + ") Splitscreen aborted: " + reason;
96     }
97 }
98