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.hardware.input;
18 
19 import android.annotation.NonNull;
20 import android.annotation.RequiresPermission;
21 import android.annotation.SystemApi;
22 import android.companion.virtual.IVirtualDevice;
23 import android.graphics.PointF;
24 import android.os.IBinder;
25 import android.os.RemoteException;
26 import android.view.MotionEvent;
27 
28 /**
29  * A virtual mouse representing a relative input mechanism on a remote device, such as a mouse or
30  * trackpad.
31  *
32  * This registers an InputDevice that is interpreted like a physically-connected device and
33  * dispatches received events to it.
34  *
35  * @hide
36  */
37 @SystemApi
38 public class VirtualMouse extends VirtualInputDevice {
39 
40     /** @hide */
VirtualMouse(IVirtualDevice virtualDevice, IBinder token)41     public VirtualMouse(IVirtualDevice virtualDevice, IBinder token) {
42         super(virtualDevice, token);
43     }
44 
45     /**
46      * Send a mouse button event to the system.
47      *
48      * @param event the event
49      * @throws IllegalStateException if the display this mouse is associated with is not currently
50      * targeted
51      */
52     @RequiresPermission(android.Manifest.permission.CREATE_VIRTUAL_DEVICE)
sendButtonEvent(@onNull VirtualMouseButtonEvent event)53     public void sendButtonEvent(@NonNull VirtualMouseButtonEvent event) {
54         try {
55             mVirtualDevice.sendButtonEvent(mToken, event);
56         } catch (RemoteException e) {
57             throw e.rethrowFromSystemServer();
58         }
59     }
60 
61     /**
62      * Sends a scrolling event to the system. See {@link MotionEvent#AXIS_VSCROLL} and
63      * {@link MotionEvent#AXIS_SCROLL}.
64      *
65      * @param event the event
66      * @throws IllegalStateException if the display this mouse is associated with is not currently
67      * targeted
68      */
69     @RequiresPermission(android.Manifest.permission.CREATE_VIRTUAL_DEVICE)
sendScrollEvent(@onNull VirtualMouseScrollEvent event)70     public void sendScrollEvent(@NonNull VirtualMouseScrollEvent event) {
71         try {
72             mVirtualDevice.sendScrollEvent(mToken, event);
73         } catch (RemoteException e) {
74             throw e.rethrowFromSystemServer();
75         }
76     }
77 
78     /**
79      * Sends a relative movement event to the system.
80      *
81      * @param event the event
82      * @throws IllegalStateException if the display this mouse is associated with is not currently
83      * targeted
84      */
85     @RequiresPermission(android.Manifest.permission.CREATE_VIRTUAL_DEVICE)
sendRelativeEvent(@onNull VirtualMouseRelativeEvent event)86     public void sendRelativeEvent(@NonNull VirtualMouseRelativeEvent event) {
87         try {
88             mVirtualDevice.sendRelativeEvent(mToken, event);
89         } catch (RemoteException e) {
90             throw e.rethrowFromSystemServer();
91         }
92     }
93 
94     /**
95      * Gets the current cursor position.
96      *
97      * @return the position, expressed as x and y coordinates
98      * @throws IllegalStateException if the display this mouse is associated with is not currently
99      * targeted
100      */
101     @RequiresPermission(android.Manifest.permission.CREATE_VIRTUAL_DEVICE)
getCursorPosition()102     public @NonNull PointF getCursorPosition() {
103         try {
104             return mVirtualDevice.getCursorPosition(mToken);
105         } catch (RemoteException e) {
106             throw e.rethrowFromSystemServer();
107         }
108     }
109 }
110