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.sensor.VirtualSensor;
24 import android.content.Context;
25 import android.os.LocaleList;
26 import android.util.ArraySet;
27 
28 import java.util.Set;
29 import java.util.function.Consumer;
30 
31 /**
32  * Virtual device manager local service interface.
33  * Only for use within system server.
34  */
35 public abstract class VirtualDeviceManagerInternal {
36 
37     /** Interface to listen to the changes on the list of app UIDs running on any virtual device. */
38     public interface AppsOnVirtualDeviceListener {
39         /** Notifies that running apps on any virtual device has changed */
onAppsOnAnyVirtualDeviceChanged(Set<Integer> allRunningUids)40         void onAppsOnAnyVirtualDeviceChanged(Set<Integer> allRunningUids);
41     }
42 
43     /** Register a listener for changes of running app UIDs on any virtual device. */
registerAppsOnVirtualDeviceListener( @onNull AppsOnVirtualDeviceListener listener)44     public abstract void registerAppsOnVirtualDeviceListener(
45             @NonNull AppsOnVirtualDeviceListener listener);
46 
47     /** Unregister a listener for changes of running app UIDs on any virtual device. */
unregisterAppsOnVirtualDeviceListener( @onNull AppsOnVirtualDeviceListener listener)48     public abstract void unregisterAppsOnVirtualDeviceListener(
49             @NonNull AppsOnVirtualDeviceListener listener);
50 
51     /** Register a listener for removal of persistent device IDs. */
registerPersistentDeviceIdRemovedListener( @onNull Consumer<String> persistentDeviceIdRemovedListener)52     public abstract void registerPersistentDeviceIdRemovedListener(
53             @NonNull Consumer<String> persistentDeviceIdRemovedListener);
54 
55     /** Unregister a listener for the removal of persistent device IDs. */
unregisterPersistentDeviceIdRemovedListener( @onNull Consumer<String> persistentDeviceIdRemovedListener)56     public abstract void unregisterPersistentDeviceIdRemovedListener(
57             @NonNull Consumer<String> persistentDeviceIdRemovedListener);
58 
59     /**
60      * Notifies that the set of apps running on virtual devices has changed.
61      * This method only notifies the listeners when the union of running UIDs on all virtual devices
62      * has changed.
63      */
onAppsOnVirtualDeviceChanged()64     public abstract void onAppsOnVirtualDeviceChanged();
65 
66     /**
67      * Notifies that an authentication prompt is about to be shown for an app with the given uid.
68      */
onAuthenticationPrompt(int uid)69     public abstract void onAuthenticationPrompt(int uid);
70 
71     /**
72      * Notifies the given persistent device IDs have been removed.
73      */
onPersistentDeviceIdsRemoved(Set<String> removedPersistentDeviceIds)74     public abstract void onPersistentDeviceIdsRemoved(Set<String> removedPersistentDeviceIds);
75 
76     /**
77      * Gets the owner uid for a deviceId.
78      *
79      * @param deviceId which device we're asking about
80      * @return the uid of the app which created and owns the VirtualDevice with the given deviceId,
81      * or {@link android.os.Process#INVALID_UID} if no such device exists.
82      */
getDeviceOwnerUid(int deviceId)83     public abstract int getDeviceOwnerUid(int deviceId);
84 
85     /**
86      * Returns the VirtualSensor for the given deviceId and sensor handle, if any.
87      *
88      * @param deviceId the virtual device that owns the sensor
89      * @param handle the sensor handle
90      * @return the VirtualSensor with the given handle, or {@code null} if no such sensor exists.
91      */
getVirtualSensor(int deviceId, int handle)92     public abstract @Nullable VirtualSensor getVirtualSensor(int deviceId, int handle);
93 
94     /**
95      * Finds VirtualDevices where an app is running.
96      *
97      * @param uid - the app's uid
98      * @return a set of id's of VirtualDevices where the app with the given uid is running.
99      * *Note* this only checks VirtualDevices, and does not include information about whether
100      * the app is running on the default device or not.
101      */
getDeviceIdsForUid(int uid)102     public abstract @NonNull ArraySet<Integer> getDeviceIdsForUid(int uid);
103 
104     /**
105      * Notifies that a virtual display is removed.
106      *
107      * @param virtualDevice The virtual device where the virtual display located.
108      * @param displayId     The display id of the removed virtual display.
109      */
onVirtualDisplayRemoved(IVirtualDevice virtualDevice, int displayId)110     public abstract void onVirtualDisplayRemoved(IVirtualDevice virtualDevice, int displayId);
111 
112     /**
113      * Returns the flags that should be added to any virtual displays created on this virtual
114      * device.
115      */
getBaseVirtualDisplayFlags(IVirtualDevice virtualDevice)116     public abstract int getBaseVirtualDisplayFlags(IVirtualDevice virtualDevice);
117 
118     /**
119      * Returns the preferred locale hints of the Virtual Device on which the given app is running,
120      * or {@code null} if the hosting virtual device doesn't have a virtual keyboard or the app is
121      * not on any virtual device.
122      *
123      * If an app is on multiple virtual devices, the locale of the virtual device created the
124      * earliest will be returned.
125      *
126      * See {@link android.hardware.input.VirtualKeyboardConfig#setLanguageTag() for how the locale
127      * is specified for virtual keyboard.
128      */
129     @Nullable
getPreferredLocaleListForUid(int uid)130     public abstract LocaleList getPreferredLocaleListForUid(int uid);
131 
132     /**
133      * Returns true if the given {@code uid} is currently running on any virtual devices. This is
134      * determined by whether the app has any activities in the task stack on a virtual-device-owned
135      * display.
136      */
isAppRunningOnAnyVirtualDevice(int uid)137     public abstract boolean isAppRunningOnAnyVirtualDevice(int uid);
138 
139     /**
140      * @return whether the input device with the given id was created by a virtual device.
141      */
isInputDeviceOwnedByVirtualDevice(int inputDeviceId)142     public abstract boolean isInputDeviceOwnedByVirtualDevice(int inputDeviceId);
143 
144     /**
145      * Gets the ids of VirtualDisplays owned by a VirtualDevice.
146      *
147      * @param deviceId which device we're asking about
148      * @return the set of display ids for all VirtualDisplays owned by the device
149      */
getDisplayIdsForDevice(int deviceId)150     public abstract @NonNull ArraySet<Integer> getDisplayIdsForDevice(int deviceId);
151 
152     /**
153      * Checks whether the passed {@code deviceId} is a valid virtual device ID or not.
154      *
155      * <p>{@link Context#DEVICE_ID_DEFAULT} is not valid as it is the ID of the default
156      * device which is not a virtual device.</p>
157      */
isValidVirtualDeviceId(int deviceId)158     public abstract boolean isValidVirtualDeviceId(int deviceId);
159 
160     /**
161      * Returns the ID of the device which owns the display with the given ID.
162      *
163      * <p>In case the virtual display ID is invalid or doesn't belong to a virtual device, then
164      * {@link android.content.Context#DEVICE_ID_DEFAULT} is returned.</p>
165      */
getDeviceIdForDisplayId(int displayId)166     public abstract int getDeviceIdForDisplayId(int displayId);
167 
168     /**
169      * Gets the persistent ID for the VirtualDevice with the given device ID.
170      *
171      * @param deviceId which device we're asking about
172      * @return the persistent ID for this device, or {@code null} if no such ID exists.
173      *
174      * @see VirtualDevice#getPersistentDeviceId()
175      */
getPersistentIdForDevice(int deviceId)176     public abstract @Nullable String getPersistentIdForDevice(int deviceId);
177 
178     /**
179      * Returns all current persistent device IDs, including the ones for which no virtual device
180      * exists, as long as one may have existed or can be created.
181      */
getAllPersistentDeviceIds()182     public abstract @NonNull Set<String> getAllPersistentDeviceIds();
183 }
184