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.companion.virtual;
18 
19 import android.app.PendingIntent;
20 import android.companion.virtual.IVirtualDeviceIntentInterceptor;
21 import android.companion.virtual.audio.IAudioConfigChangedCallback;
22 import android.companion.virtual.audio.IAudioRoutingCallback;
23 import android.companion.virtual.sensor.VirtualSensor;
24 import android.companion.virtual.sensor.VirtualSensorConfig;
25 import android.companion.virtual.sensor.VirtualSensorEvent;
26 import android.content.IntentFilter;
27 import android.graphics.Point;
28 import android.graphics.PointF;
29 import android.hardware.input.VirtualDpadConfig;
30 import android.hardware.input.VirtualKeyboardConfig;
31 import android.hardware.input.VirtualKeyEvent;
32 import android.hardware.input.VirtualMouseButtonEvent;
33 import android.hardware.input.VirtualMouseConfig;
34 import android.hardware.input.VirtualMouseRelativeEvent;
35 import android.hardware.input.VirtualMouseScrollEvent;
36 import android.hardware.input.VirtualTouchEvent;
37 import android.hardware.input.VirtualTouchscreenConfig;
38 import android.hardware.input.VirtualNavigationTouchpadConfig;
39 import android.os.ResultReceiver;
40 
41 /**
42  * Interface for a virtual device for communication between the system server and the process of
43  * the owner of the virtual device.
44  *
45  * @hide
46  */
47 interface IVirtualDevice {
48 
49     /**
50      * Returns the CDM association ID of this virtual device.
51      *
52      * @see AssociationInfo#getId()
53      */
getAssociationId()54     int getAssociationId();
55 
56     /**
57      * Returns the unique ID of this virtual device.
58      */
getDeviceId()59     int getDeviceId();
60 
61     /**
62      * Closes the virtual device and frees all associated resources.
63      */
64     @EnforcePermission("CREATE_VIRTUAL_DEVICE")
close()65     void close();
66 
67     /**
68      * Notifies that an audio session being started.
69      */
70     @EnforcePermission("CREATE_VIRTUAL_DEVICE")
onAudioSessionStarting(int displayId, IAudioRoutingCallback routingCallback, IAudioConfigChangedCallback configChangedCallback)71     void onAudioSessionStarting(int displayId, IAudioRoutingCallback routingCallback,
72             IAudioConfigChangedCallback configChangedCallback);
73 
74     /**
75      * Notifies that an audio session has ended.
76      */
77     @EnforcePermission("CREATE_VIRTUAL_DEVICE")
onAudioSessionEnded()78     void onAudioSessionEnded();
79 
80     /**
81      * Creates a new dpad and registers it with the input framework with the given token.
82      */
83     @EnforcePermission("CREATE_VIRTUAL_DEVICE")
createVirtualDpad(in VirtualDpadConfig config, IBinder token)84     void createVirtualDpad(in VirtualDpadConfig config, IBinder token);
85 
86     /**
87      * Creates a new keyboard and registers it with the input framework with the given token.
88      */
89     @EnforcePermission("CREATE_VIRTUAL_DEVICE")
createVirtualKeyboard(in VirtualKeyboardConfig config, IBinder token)90     void createVirtualKeyboard(in VirtualKeyboardConfig config, IBinder token);
91 
92     /**
93      * Creates a new mouse and registers it with the input framework with the given token.
94      */
95     @EnforcePermission("CREATE_VIRTUAL_DEVICE")
createVirtualMouse(in VirtualMouseConfig config, IBinder token)96     void createVirtualMouse(in VirtualMouseConfig config, IBinder token);
97 
98     /**
99      * Creates a new touchscreen and registers it with the input framework with the given token.
100      */
101     @EnforcePermission("CREATE_VIRTUAL_DEVICE")
createVirtualTouchscreen(in VirtualTouchscreenConfig config, IBinder token)102     void createVirtualTouchscreen(in VirtualTouchscreenConfig config, IBinder token);
103 
104     /**
105      * Creates a new navigation touchpad and registers it with the input framework with the given
106      * token.
107      */
108     @EnforcePermission("CREATE_VIRTUAL_DEVICE")
createVirtualNavigationTouchpad(in VirtualNavigationTouchpadConfig config, IBinder token)109     void createVirtualNavigationTouchpad(in VirtualNavigationTouchpadConfig config, IBinder token);
110 
111     /**
112      * Removes the input device corresponding to the given token from the framework.
113      */
114     @EnforcePermission("CREATE_VIRTUAL_DEVICE")
unregisterInputDevice(IBinder token)115     void unregisterInputDevice(IBinder token);
116 
117     /**
118      * Returns the ID of the device corresponding to the given token, as registered with the input
119      * framework.
120      */
getInputDeviceId(IBinder token)121     int getInputDeviceId(IBinder token);
122 
123     /**
124     * Injects a key event to the virtual dpad corresponding to the given token.
125     */
126     @EnforcePermission("CREATE_VIRTUAL_DEVICE")
sendDpadKeyEvent(IBinder token, in VirtualKeyEvent event)127     boolean sendDpadKeyEvent(IBinder token, in VirtualKeyEvent event);
128 
129     /**
130     * Injects a key event to the virtual keyboard corresponding to the given token.
131     */
132     @EnforcePermission("CREATE_VIRTUAL_DEVICE")
sendKeyEvent(IBinder token, in VirtualKeyEvent event)133     boolean sendKeyEvent(IBinder token, in VirtualKeyEvent event);
134 
135     /**
136     * Injects a button event to the virtual mouse corresponding to the given token.
137     */
138     @EnforcePermission("CREATE_VIRTUAL_DEVICE")
sendButtonEvent(IBinder token, in VirtualMouseButtonEvent event)139     boolean sendButtonEvent(IBinder token, in VirtualMouseButtonEvent event);
140 
141     /**
142     * Injects a relative event to the virtual mouse corresponding to the given token.
143     */
144     @EnforcePermission("CREATE_VIRTUAL_DEVICE")
sendRelativeEvent(IBinder token, in VirtualMouseRelativeEvent event)145     boolean sendRelativeEvent(IBinder token, in VirtualMouseRelativeEvent event);
146 
147     /**
148     * Injects a scroll event to the virtual mouse corresponding to the given token.
149     */
150     @EnforcePermission("CREATE_VIRTUAL_DEVICE")
sendScrollEvent(IBinder token, in VirtualMouseScrollEvent event)151     boolean sendScrollEvent(IBinder token, in VirtualMouseScrollEvent event);
152 
153     /**
154     * Injects a touch event to the virtual touch input device corresponding to the given token.
155     */
156     @EnforcePermission("CREATE_VIRTUAL_DEVICE")
sendTouchEvent(IBinder token, in VirtualTouchEvent event)157     boolean sendTouchEvent(IBinder token, in VirtualTouchEvent event);
158 
159     /**
160      * Returns all virtual sensors created for this device.
161      */
162     @EnforcePermission("CREATE_VIRTUAL_DEVICE")
getVirtualSensorList()163     List<VirtualSensor> getVirtualSensorList();
164 
165     /**
166      * Sends an event to the virtual sensor corresponding to the given token.
167      */
168     @EnforcePermission("CREATE_VIRTUAL_DEVICE")
sendSensorEvent(IBinder token, in VirtualSensorEvent event)169     boolean sendSensorEvent(IBinder token, in VirtualSensorEvent event);
170 
171     /**
172      * Launches a pending intent on the given display that is owned by this virtual device.
173      */
launchPendingIntent(int displayId, in PendingIntent pendingIntent, in ResultReceiver resultReceiver)174     void launchPendingIntent(int displayId, in PendingIntent pendingIntent,
175             in ResultReceiver resultReceiver);
176 
177     /**
178      * Returns the current cursor position of the mouse corresponding to the given token, in x and y
179      * coordinates.
180      */
getCursorPosition(IBinder token)181     PointF getCursorPosition(IBinder token);
182 
183     /** Sets whether to show or hide the cursor while this virtual device is active. */
184     @EnforcePermission("CREATE_VIRTUAL_DEVICE")
setShowPointerIcon(boolean showPointerIcon)185     void setShowPointerIcon(boolean showPointerIcon);
186 
187     /**
188      * Registers an intent interceptor that will intercept an intent attempting to launch
189      * when matching the provided IntentFilter and calls the callback with the intercepted
190      * intent.
191      */
192     @EnforcePermission("CREATE_VIRTUAL_DEVICE")
registerIntentInterceptor(in IVirtualDeviceIntentInterceptor intentInterceptor, in IntentFilter filter)193     void registerIntentInterceptor(in IVirtualDeviceIntentInterceptor intentInterceptor,
194             in IntentFilter filter);
195 
196     /**
197      * Unregisters a previously registered intent interceptor.
198      */
199     @EnforcePermission("CREATE_VIRTUAL_DEVICE")
unregisterIntentInterceptor(in IVirtualDeviceIntentInterceptor intentInterceptor)200     void unregisterIntentInterceptor(in IVirtualDeviceIntentInterceptor intentInterceptor);
201 }
202