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 com.android.server.companion.virtual; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.companion.virtual.IVirtualDevice; 22 import android.companion.virtual.VirtualDevice; 23 import android.companion.virtual.VirtualDeviceManager; 24 import android.companion.virtual.VirtualDeviceParams; 25 import android.companion.virtual.sensor.VirtualSensor; 26 import android.content.Context; 27 import android.hardware.display.IVirtualDisplayCallback; 28 import android.os.LocaleList; 29 import android.util.ArraySet; 30 import android.window.DisplayWindowPolicyController; 31 32 import java.util.Set; 33 import java.util.function.Consumer; 34 35 /** 36 * Virtual device manager local service interface. 37 * Only for use within system server. 38 */ 39 public abstract class VirtualDeviceManagerInternal { 40 41 /** Interface to listen to the changes on the list of app UIDs running on any virtual device. */ 42 public interface AppsOnVirtualDeviceListener { 43 /** Notifies that running apps on any virtual device has changed */ onAppsOnAnyVirtualDeviceChanged(Set<Integer> allRunningUids)44 void onAppsOnAnyVirtualDeviceChanged(Set<Integer> allRunningUids); 45 } 46 47 /** Register a listener for changes of running app UIDs on any virtual device. */ registerAppsOnVirtualDeviceListener( @onNull AppsOnVirtualDeviceListener listener)48 public abstract void registerAppsOnVirtualDeviceListener( 49 @NonNull AppsOnVirtualDeviceListener listener); 50 51 /** Unregister a listener for changes of running app UIDs on any virtual device. */ unregisterAppsOnVirtualDeviceListener( @onNull AppsOnVirtualDeviceListener listener)52 public abstract void unregisterAppsOnVirtualDeviceListener( 53 @NonNull AppsOnVirtualDeviceListener listener); 54 55 /** Register a listener for removal of persistent device IDs. */ registerPersistentDeviceIdRemovedListener( @onNull Consumer<String> persistentDeviceIdRemovedListener)56 public abstract void registerPersistentDeviceIdRemovedListener( 57 @NonNull Consumer<String> persistentDeviceIdRemovedListener); 58 59 /** Unregister a listener for the removal of persistent device IDs. */ unregisterPersistentDeviceIdRemovedListener( @onNull Consumer<String> persistentDeviceIdRemovedListener)60 public abstract void unregisterPersistentDeviceIdRemovedListener( 61 @NonNull Consumer<String> persistentDeviceIdRemovedListener); 62 63 /** 64 * Notifies that the set of apps running on virtual devices has changed. 65 * This method only notifies the listeners when the union of running UIDs on all virtual devices 66 * has changed. 67 */ onAppsOnVirtualDeviceChanged()68 public abstract void onAppsOnVirtualDeviceChanged(); 69 70 /** 71 * Notifies that an authentication prompt is about to be shown for an app with the given uid. 72 */ onAuthenticationPrompt(int uid)73 public abstract void onAuthenticationPrompt(int uid); 74 75 /** 76 * Notifies the given persistent device IDs have been removed. 77 */ onPersistentDeviceIdsRemoved(Set<String> removedPersistentDeviceIds)78 public abstract void onPersistentDeviceIdsRemoved(Set<String> removedPersistentDeviceIds); 79 80 /** 81 * Gets the owner uid for a deviceId. 82 * 83 * @param deviceId which device we're asking about 84 * @return the uid of the app which created and owns the VirtualDevice with the given deviceId, 85 * or {@link android.os.Process#INVALID_UID} if no such device exists. 86 */ getDeviceOwnerUid(int deviceId)87 public abstract int getDeviceOwnerUid(int deviceId); 88 89 /** 90 * Returns the VirtualSensor for the given deviceId and sensor handle, if any. 91 * 92 * @param deviceId the virtual device that owns the sensor 93 * @param handle the sensor handle 94 * @return the VirtualSensor with the given handle, or {@code null} if no such sensor exists. 95 */ getVirtualSensor(int deviceId, int handle)96 public abstract @Nullable VirtualSensor getVirtualSensor(int deviceId, int handle); 97 98 /** 99 * Finds VirtualDevices where an app is running. 100 * 101 * @param uid - the app's uid 102 * @return a set of id's of VirtualDevices where the app with the given uid is running. 103 * *Note* this only checks VirtualDevices, and does not include information about whether 104 * the app is running on the default device or not. 105 */ getDeviceIdsForUid(int uid)106 public abstract @NonNull ArraySet<Integer> getDeviceIdsForUid(int uid); 107 108 /** 109 * Notifies that a virtual display was created. 110 * 111 * @param virtualDevice The virtual device that owns the virtual display. 112 * @param displayId The display id of the created virtual display. 113 * @param callback The callback of the virtual display. 114 * @param dwpc The DisplayWindowPolicyController of the created virtual display. 115 */ onVirtualDisplayCreated(IVirtualDevice virtualDevice, int displayId, IVirtualDisplayCallback callback, DisplayWindowPolicyController dwpc)116 public abstract void onVirtualDisplayCreated(IVirtualDevice virtualDevice, int displayId, 117 IVirtualDisplayCallback callback, DisplayWindowPolicyController dwpc); 118 119 /** 120 * Notifies that a virtual display is removed. 121 * 122 * @param virtualDevice The virtual device where the virtual display located. 123 * @param displayId The display id of the removed virtual display. 124 */ onVirtualDisplayRemoved(IVirtualDevice virtualDevice, int displayId)125 public abstract void onVirtualDisplayRemoved(IVirtualDevice virtualDevice, int displayId); 126 127 /** 128 * Returns the flags that should be added to any virtual displays created on this virtual 129 * device. 130 */ getBaseVirtualDisplayFlags(IVirtualDevice virtualDevice)131 public abstract int getBaseVirtualDisplayFlags(IVirtualDevice virtualDevice); 132 133 /** 134 * Returns the preferred locale hints of the Virtual Device on which the given app is running, 135 * or {@code null} if the hosting virtual device doesn't have a virtual keyboard or the app is 136 * not on any virtual device. 137 * 138 * If an app is on multiple virtual devices, the locale of the virtual device created the 139 * earliest will be returned. 140 * 141 * See {@link android.hardware.input.VirtualKeyboardConfig#setLanguageTag() for how the locale 142 * is specified for virtual keyboard. 143 */ 144 @Nullable getPreferredLocaleListForUid(int uid)145 public abstract LocaleList getPreferredLocaleListForUid(int uid); 146 147 /** 148 * Returns true if the given {@code uid} is currently running on any virtual devices. This is 149 * determined by whether the app has any activities in the task stack on a virtual-device-owned 150 * display. 151 */ isAppRunningOnAnyVirtualDevice(int uid)152 public abstract boolean isAppRunningOnAnyVirtualDevice(int uid); 153 154 /** 155 * @return whether the input device with the given id was created by a virtual device. 156 */ isInputDeviceOwnedByVirtualDevice(int inputDeviceId)157 public abstract boolean isInputDeviceOwnedByVirtualDevice(int inputDeviceId); 158 159 /** 160 * Gets the ids of VirtualDisplays owned by a VirtualDevice. 161 * 162 * @param deviceId which device we're asking about 163 * @return the set of display ids for all VirtualDisplays owned by the device 164 */ getDisplayIdsForDevice(int deviceId)165 public abstract @NonNull ArraySet<Integer> getDisplayIdsForDevice(int deviceId); 166 167 /** 168 * Checks whether the passed {@code deviceId} is a valid virtual device ID or not. 169 * 170 * <p>{@link Context#DEVICE_ID_DEFAULT} is not valid as it is the ID of the default 171 * device which is not a virtual device.</p> 172 */ isValidVirtualDeviceId(int deviceId)173 public abstract boolean isValidVirtualDeviceId(int deviceId); 174 175 /** 176 * Returns the ID of the device which owns the display with the given ID. 177 * 178 * <p>In case the virtual display ID is invalid or doesn't belong to a virtual device, then 179 * {@link android.content.Context#DEVICE_ID_DEFAULT} is returned.</p> 180 */ getDeviceIdForDisplayId(int displayId)181 public abstract int getDeviceIdForDisplayId(int displayId); 182 183 /** Returns the dim duration for the displays of the device with the given ID. */ getDimDurationMillisForDeviceId(int deviceId)184 public abstract long getDimDurationMillisForDeviceId(int deviceId); 185 186 /** Returns the screen off timeout of the displays of the device with the given ID. */ getScreenOffTimeoutMillisForDeviceId(int deviceId)187 public abstract long getScreenOffTimeoutMillisForDeviceId(int deviceId); 188 189 /** 190 * Gets the persistent ID for the VirtualDevice with the given device ID. 191 * 192 * @param deviceId which device we're asking about 193 * @return the persistent ID for this device, or {@code null} if no such ID exists. 194 * 195 * @see VirtualDevice#getPersistentDeviceId() 196 */ getPersistentIdForDevice(int deviceId)197 public abstract @Nullable String getPersistentIdForDevice(int deviceId); 198 199 /** 200 * Returns all current persistent device IDs, including the ones for which no virtual device 201 * exists, as long as one may have existed or can be created. 202 */ getAllPersistentDeviceIds()203 public abstract @NonNull Set<String> getAllPersistentDeviceIds(); 204 205 /** 206 * Creates a virtual device where applications can launch and receive input events injected by 207 * the creator. 208 * 209 * <p>A Companion Device Manager association is not required. Only the system may create such 210 * virtual devices.</p> 211 */ createVirtualDevice( @onNull VirtualDeviceParams params)212 public abstract @NonNull VirtualDeviceManager.VirtualDevice createVirtualDevice( 213 @NonNull VirtualDeviceParams params); 214 215 /** 216 * Returns the details of the virtual device with the given ID, if any. 217 * 218 * <p>The returned object is a read-only representation of the virtual device that expose its 219 * properties.</p> 220 * 221 * <p>Note that if the virtual device is closed and becomes invalid, the returned object will 222 * not be updated and may contain stale values. Use a {@link VirtualDeviceListener} for real 223 * time updates of the availability of virtual devices.</p> 224 * 225 * @return the virtual device with the requested ID, or {@code null} if no such device exists or 226 * it has already been closed. 227 */ 228 @Nullable getVirtualDevice(int deviceId)229 public abstract VirtualDevice getVirtualDevice(int deviceId); 230 } 231