blob: c5f4c23b7b1588fdf65d8e6576324bc66b5b3b2f [file] [log] [blame]
Jeff Brownc28867a2013-03-26 15:42:39 -07001/*
2 * Copyright (C) 2013 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
17package android.view;
18
Mathew Inwoode5ad5982018-08-17 15:07:52 +010019import android.annotation.UnsupportedAppUsage;
Jeff Brownc28867a2013-03-26 15:42:39 -070020import android.os.Looper;
21import android.os.MessageQueue;
22import android.util.Log;
23
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070024import dalvik.system.CloseGuard;
25
Jeff Browna4ca8ea2013-04-02 18:01:38 -070026import java.lang.ref.WeakReference;
27
Jeff Brownc28867a2013-03-26 15:42:39 -070028/**
29 * Provides a low-level mechanism for an application to send input events.
30 * @hide
31 */
32public abstract class InputEventSender {
33 private static final String TAG = "InputEventSender";
34
35 private final CloseGuard mCloseGuard = CloseGuard.get();
36
Ashok Bhata931d5212014-01-08 14:04:51 +000037 private long mSenderPtr;
Jeff Brownc28867a2013-03-26 15:42:39 -070038
39 // We keep references to the input channel and message queue objects here so that
40 // they are not GC'd while the native peer of the receiver is using them.
41 private InputChannel mInputChannel;
42 private MessageQueue mMessageQueue;
43
Ashok Bhata931d5212014-01-08 14:04:51 +000044 private static native long nativeInit(WeakReference<InputEventSender> sender,
Jeff Brownc28867a2013-03-26 15:42:39 -070045 InputChannel inputChannel, MessageQueue messageQueue);
Ashok Bhata931d5212014-01-08 14:04:51 +000046 private static native void nativeDispose(long senderPtr);
47 private static native boolean nativeSendKeyEvent(long senderPtr, int seq, KeyEvent event);
48 private static native boolean nativeSendMotionEvent(long senderPtr, int seq, MotionEvent event);
Jeff Brownc28867a2013-03-26 15:42:39 -070049
50 /**
51 * Creates an input event sender bound to the specified input channel.
52 *
53 * @param inputChannel The input channel.
54 * @param looper The looper to use when invoking callbacks.
55 */
56 public InputEventSender(InputChannel inputChannel, Looper looper) {
57 if (inputChannel == null) {
58 throw new IllegalArgumentException("inputChannel must not be null");
59 }
60 if (looper == null) {
61 throw new IllegalArgumentException("looper must not be null");
62 }
63
64 mInputChannel = inputChannel;
65 mMessageQueue = looper.getQueue();
Jeff Browna4ca8ea2013-04-02 18:01:38 -070066 mSenderPtr = nativeInit(new WeakReference<InputEventSender>(this),
67 inputChannel, mMessageQueue);
Jeff Brownc28867a2013-03-26 15:42:39 -070068
69 mCloseGuard.open("dispose");
70 }
71
72 @Override
73 protected void finalize() throws Throwable {
74 try {
75 dispose(true);
76 } finally {
77 super.finalize();
78 }
79 }
80
81 /**
82 * Disposes the receiver.
83 */
84 public void dispose() {
85 dispose(false);
86 }
87
88 private void dispose(boolean finalized) {
89 if (mCloseGuard != null) {
90 if (finalized) {
91 mCloseGuard.warnIfOpen();
92 }
93 mCloseGuard.close();
94 }
95
96 if (mSenderPtr != 0) {
97 nativeDispose(mSenderPtr);
98 mSenderPtr = 0;
99 }
100 mInputChannel = null;
101 mMessageQueue = null;
102 }
103
104 /**
105 * Called when an input event is finished.
106 *
107 * @param seq The input event sequence number.
108 * @param handled True if the input event was handled.
109 */
110 public void onInputEventFinished(int seq, boolean handled) {
111 }
112
113 /**
114 * Sends an input event.
115 * Must be called on the same Looper thread to which the sender is attached.
116 *
117 * @param seq The input event sequence number.
118 * @param event The input event to send.
119 * @return True if the entire event was sent successfully. May return false
120 * if the input channel buffer filled before all samples were dispatched.
121 */
122 public final boolean sendInputEvent(int seq, InputEvent event) {
123 if (event == null) {
124 throw new IllegalArgumentException("event must not be null");
125 }
126 if (mSenderPtr == 0) {
127 Log.w(TAG, "Attempted to send an input event but the input event "
128 + "sender has already been disposed.");
129 return false;
130 }
131
132 if (event instanceof KeyEvent) {
133 return nativeSendKeyEvent(mSenderPtr, seq, (KeyEvent)event);
134 } else {
135 return nativeSendMotionEvent(mSenderPtr, seq, (MotionEvent)event);
136 }
137 }
138
139 // Called from native code.
140 @SuppressWarnings("unused")
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100141 @UnsupportedAppUsage
Jeff Brownc28867a2013-03-26 15:42:39 -0700142 private void dispatchInputEventFinished(int seq, boolean handled) {
143 onInputEventFinished(seq, handled);
144 }
145}