1 /* 2 * Copyright (C) 2021 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 android.window; 18 19 import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED; 20 import static android.view.Display.INVALID_DISPLAY; 21 22 import android.annotation.NonNull; 23 import android.annotation.Nullable; 24 import android.annotation.UserIdInt; 25 import android.app.WindowConfiguration; 26 import android.companion.virtualdevice.flags.Flags; 27 import android.content.ComponentName; 28 import android.content.Intent; 29 import android.content.pm.ActivityInfo; 30 import android.util.ArraySet; 31 32 import java.io.PrintWriter; 33 import java.util.List; 34 import java.util.Set; 35 36 /** 37 * Abstract class to control the policies of the windows that can be displayed on the virtual 38 * display. 39 * 40 * @hide 41 */ 42 public abstract class DisplayWindowPolicyController { 43 /** 44 * The window flags that we are interested in. 45 * @see android.view.WindowManager.LayoutParams 46 * @see #keepActivityOnWindowFlagsChanged 47 */ 48 private int mWindowFlags; 49 50 /** 51 * The system window flags that we are interested in. 52 * @see android.view.WindowManager.LayoutParams 53 * @see #keepActivityOnWindowFlagsChanged 54 */ 55 private int mSystemWindowFlags; 56 57 /** 58 * The set of windowing mode that are supported in this display. 59 * @see android.app.WindowConfiguration.WindowingMode 60 */ 61 private final Set<Integer> mSupportedWindowingModes = new ArraySet<>(); 62 63 /** 64 * A controller to control the policies of the windows that can be displayed on the virtual 65 * display. 66 */ DisplayWindowPolicyController()67 public DisplayWindowPolicyController() { 68 synchronized (mSupportedWindowingModes) { 69 mSupportedWindowingModes.add(WindowConfiguration.WINDOWING_MODE_FULLSCREEN); 70 if (Flags.virtualDisplayMultiWindowModeSupport()) { 71 mSupportedWindowingModes.add(WindowConfiguration.WINDOWING_MODE_MULTI_WINDOW); 72 } 73 } 74 } 75 76 /** 77 * Returns {@code true} if the given window flags contain the flags that we're interested in. 78 */ isInterestedWindowFlags(int windowFlags, int systemWindowFlags)79 public final boolean isInterestedWindowFlags(int windowFlags, int systemWindowFlags) { 80 return (mWindowFlags & windowFlags) != 0 || (mSystemWindowFlags & systemWindowFlags) != 0; 81 } 82 83 /** 84 * Sets the window flags that we’re interested in and expected 85 * #keepActivityOnWindowFlagsChanged to be called if any changes. 86 */ setInterestedWindowFlags(int windowFlags, int systemWindowFlags)87 public final void setInterestedWindowFlags(int windowFlags, int systemWindowFlags) { 88 mWindowFlags = windowFlags; 89 mSystemWindowFlags = systemWindowFlags; 90 } 91 92 /** 93 * Returns {@code true} if the given windowing mode is supported in this display. 94 */ isWindowingModeSupported( @indowConfiguration.WindowingMode int windowingMode)95 public final boolean isWindowingModeSupported( 96 @WindowConfiguration.WindowingMode int windowingMode) { 97 synchronized (mSupportedWindowingModes) { 98 return mSupportedWindowingModes.contains(windowingMode); 99 } 100 } 101 102 /** 103 * Sets the windowing modes are supported in this display. 104 * 105 * @param supportedWindowingModes The set of 106 * {@link android.app.WindowConfiguration.WindowingMode}. 107 */ setSupportedWindowingModes(Set<Integer> supportedWindowingModes)108 public final void setSupportedWindowingModes(Set<Integer> supportedWindowingModes) { 109 synchronized (mSupportedWindowingModes) { 110 mSupportedWindowingModes.clear(); 111 mSupportedWindowingModes.addAll(supportedWindowingModes); 112 } 113 } 114 115 /** 116 * @return the custom home component specified for the relevant display, if any. 117 */ 118 @Nullable getCustomHomeComponent()119 public abstract ComponentName getCustomHomeComponent(); 120 121 /** 122 * Returns {@code true} if all of the given activities can be launched on this virtual display 123 * in the configuration defined by the rest of the arguments. 124 * 125 * @see #canContainActivity 126 */ canContainActivities(@onNull List<ActivityInfo> activities, @WindowConfiguration.WindowingMode int windowingMode)127 public boolean canContainActivities(@NonNull List<ActivityInfo> activities, 128 @WindowConfiguration.WindowingMode int windowingMode) { 129 for (int i = 0; i < activities.size(); i++) { 130 if (!canContainActivity(activities.get(i), windowingMode, 131 /*launchingFromDisplayId=*/ INVALID_DISPLAY, /*isNewTask=*/ false)) { 132 return false; 133 } 134 } 135 return true; 136 } 137 138 /** 139 * Returns {@code true} if the given activity can be launched on this virtual display in the 140 * configuration defined by the rest of the arguments. If the given intent would be intercepted 141 * by the display owner then this means that the activity cannot be launched. 142 */ canActivityBeLaunched(@onNull ActivityInfo activityInfo, @Nullable Intent intent, @WindowConfiguration.WindowingMode int windowingMode, int launchingFromDisplayId, boolean isNewTask)143 public abstract boolean canActivityBeLaunched(@NonNull ActivityInfo activityInfo, 144 @Nullable Intent intent, @WindowConfiguration.WindowingMode int windowingMode, 145 int launchingFromDisplayId, boolean isNewTask); 146 147 /** 148 * Returns {@code true} if the given activity can be launched on this virtual display in the 149 * configuration defined by the rest of the arguments. 150 */ canContainActivity(@onNull ActivityInfo activityInfo, @WindowConfiguration.WindowingMode int windowingMode, int launchingFromDisplayId, boolean isNewTask)151 protected abstract boolean canContainActivity(@NonNull ActivityInfo activityInfo, 152 @WindowConfiguration.WindowingMode int windowingMode, 153 int launchingFromDisplayId, boolean isNewTask); 154 155 /** 156 * Called when an Activity window is layouted with the new changes where contains the 157 * window flags that we’re interested in. 158 * Returns {@code false} if the Activity cannot remain on the display and the activity task will 159 * be moved back to default display. 160 */ keepActivityOnWindowFlagsChanged( ActivityInfo activityInfo, int windowFlags, int systemWindowFlags)161 public abstract boolean keepActivityOnWindowFlagsChanged( 162 ActivityInfo activityInfo, int windowFlags, int systemWindowFlags); 163 164 /** 165 * Returns {@code true} if the tasks which is on this virtual display can be showed in the 166 * host device of the recently launched activities list. 167 */ canShowTasksInHostDeviceRecents()168 public abstract boolean canShowTasksInHostDeviceRecents(); 169 170 /** 171 * This is called when the top activity of the display is changed. 172 */ onTopActivityChanged(ComponentName topActivity, int uid, @UserIdInt int userId)173 public void onTopActivityChanged(ComponentName topActivity, int uid, @UserIdInt int userId) {} 174 175 /** 176 * This is called when the apps that contains running activities on the display has changed. 177 * The running activities refer to the non-finishing activities regardless of they are running 178 * in a process. 179 */ onRunningAppsChanged(ArraySet<Integer> runningUids)180 public void onRunningAppsChanged(ArraySet<Integer> runningUids) {} 181 182 /** 183 * This is called when an Activity is entering PIP. 184 * Returns {@code true} if the Activity is allowed to enter PIP. 185 */ isEnteringPipAllowed(int uid)186 public boolean isEnteringPipAllowed(int uid) { 187 return isWindowingModeSupported(WINDOWING_MODE_PINNED); 188 } 189 190 /** Dump debug data */ dump(String prefix, final PrintWriter pw)191 public void dump(String prefix, final PrintWriter pw) { 192 pw.println(prefix + "DisplayWindowPolicyController{" + super.toString() + "}"); 193 pw.println(prefix + " mWindowFlags=" + mWindowFlags); 194 pw.println(prefix + " mSystemWindowFlags=" + mSystemWindowFlags); 195 } 196 } 197