Debian Codegraph @

core/java/android/hardware/input/VirtualMouse.java

raw symbols SCIP
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 import java.io.Closeable;
29
30 /**
31 * A virtual mouse representing a relative input mechanism on a remote device, such as a mouse or
32 * trackpad.
33 *
34 * This registers an InputDevice that is interpreted like a physically-connected device and
35 * dispatches received events to it.
36 *
37 * @hide
38 */
39 @SystemApi
40 public class VirtualMouse implements Closeable {
41
42 private final IVirtualDevice mVirtualDevice;
43 private final IBinder mToken;
44
45 /** @hide */
46 public VirtualMouse(IVirtualDevice virtualDevice, IBinder token) {
47 mVirtualDevice = virtualDevice;
48 mToken = token;
49 }
50
51 @Override
52 @RequiresPermission(android.Manifest.permission.CREATE_VIRTUAL_DEVICE)
53 public void close() {
54 try {
55 mVirtualDevice.unregisterInputDevice(mToken);
56 } catch (RemoteException e) {
57 throw e.rethrowFromSystemServer();
58 }
59 }
60
61 /**
62 * Send a mouse button event to the system.
63 *
64 * @param event the event
65 * @throws IllegalStateException if the display this mouse is associated with is not currently
66 * targeted
67 */
68 @RequiresPermission(android.Manifest.permission.CREATE_VIRTUAL_DEVICE)
69 public void sendButtonEvent(@NonNull VirtualMouseButtonEvent event) {
70 try {
71 mVirtualDevice.sendButtonEvent(mToken, event);
72 } catch (RemoteException e) {
73 throw e.rethrowFromSystemServer();
74 }
75 }
76
77 /**
78 * Sends a scrolling event to the system. See {@link MotionEvent#AXIS_VSCROLL} and
79 * {@link MotionEvent#AXIS_SCROLL}.
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)
86 public void sendScrollEvent(@NonNull VirtualMouseScrollEvent event) {
87 try {
88 mVirtualDevice.sendScrollEvent(mToken, event);
89 } catch (RemoteException e) {
90 throw e.rethrowFromSystemServer();
91 }
92 }
93
94 /**
95 * Sends a relative movement event to the system.
96 *
97 * @param event the event
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)
102 public void sendRelativeEvent(@NonNull VirtualMouseRelativeEvent event) {
103 try {
104 mVirtualDevice.sendRelativeEvent(mToken, event);
105 } catch (RemoteException e) {
106 throw e.rethrowFromSystemServer();
107 }
108 }
109
110 /**
111 * Gets the current cursor position.
112 *
113 * @return the position, expressed as x and y coordinates
114 * @throws IllegalStateException if the display this mouse is associated with is not currently
115 * targeted
116 */
117 @RequiresPermission(android.Manifest.permission.CREATE_VIRTUAL_DEVICE)
118 public @NonNull PointF getCursorPosition() {
119 try {
120 return mVirtualDevice.getCursorPosition(mToken);
121 } catch (RemoteException e) {
122 throw e.rethrowFromSystemServer();
123 }
124 }
125 }