1 /*
2  * Copyright (C) 2022 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.hardware.input;
18 
19 import android.annotation.RequiresPermission;
20 import android.companion.virtual.IVirtualDevice;
21 import android.os.IBinder;
22 import android.os.RemoteException;
23 
24 import java.io.Closeable;
25 
26 /**
27  * The base class for all virtual input devices such as VirtualKeyboard, VirtualMouse.
28  * This implements the shared functionality such as closing the device and keeping track of
29  * identifiers.
30  *
31  * @hide
32  */
33 abstract class VirtualInputDevice implements Closeable {
34 
35     /**
36      * The virtual device to which this VirtualInputDevice belongs to.
37      */
38     protected final IVirtualDevice mVirtualDevice;
39 
40     /**
41      * The token used to uniquely identify the virtual input device.
42      */
43     protected final IBinder mToken;
44 
45     /** @hide */
VirtualInputDevice( IVirtualDevice virtualDevice, IBinder token)46     VirtualInputDevice(
47             IVirtualDevice virtualDevice, IBinder token) {
48         mVirtualDevice = virtualDevice;
49         mToken = token;
50     }
51 
52     /**
53      * @return The device id of this device.
54      * @hide
55      */
getInputDeviceId()56     public int getInputDeviceId() {
57         try {
58             return mVirtualDevice.getInputDeviceId(mToken);
59         } catch (RemoteException e) {
60             throw e.rethrowFromSystemServer();
61         }
62     }
63 
64     @Override
65     @RequiresPermission(android.Manifest.permission.CREATE_VIRTUAL_DEVICE)
close()66     public void close() {
67         try {
68             mVirtualDevice.unregisterInputDevice(mToken);
69         } catch (RemoteException e) {
70             throw e.rethrowFromSystemServer();
71         }
72     }
73 }
74