blob: 91ef50d761807d1e5d92c03a855babdd113ff615 [file] [log] [blame]
Jeff Brown32cbc38552011-12-01 14:01:49 -08001/*
2 * Copyright (C) 2011 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
19import dalvik.system.CloseGuard;
20
21import android.os.Looper;
22import android.os.MessageQueue;
23import android.util.Log;
Jeff Brown072ec962012-02-07 14:46:57 -080024import android.util.SparseIntArray;
Jeff Brown32cbc38552011-12-01 14:01:49 -080025
Jeff Browna4ca8ea2013-04-02 18:01:38 -070026import java.lang.ref.WeakReference;
27
Jeff Brown32cbc38552011-12-01 14:01:49 -080028/**
29 * Provides a low-level mechanism for an application to receive input events.
30 * @hide
31 */
32public abstract class InputEventReceiver {
33 private static final String TAG = "InputEventReceiver";
34
35 private final CloseGuard mCloseGuard = CloseGuard.get();
36
Ashok Bhata931d5212014-01-08 14:04:51 +000037 private long mReceiverPtr;
Jeff Brown32cbc38552011-12-01 14:01:49 -080038
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
Jeff Brown072ec962012-02-07 14:46:57 -080044 // Map from InputEvent sequence numbers to dispatcher sequence numbers.
45 private final SparseIntArray mSeqMap = new SparseIntArray();
Jeff Brown32cbc38552011-12-01 14:01:49 -080046
Ashok Bhata931d5212014-01-08 14:04:51 +000047 private static native long nativeInit(WeakReference<InputEventReceiver> receiver,
Jeff Brown32cbc38552011-12-01 14:01:49 -080048 InputChannel inputChannel, MessageQueue messageQueue);
Ashok Bhata931d5212014-01-08 14:04:51 +000049 private static native void nativeDispose(long receiverPtr);
50 private static native void nativeFinishInputEvent(long receiverPtr, int seq, boolean handled);
51 private static native boolean nativeConsumeBatchedInputEvents(long receiverPtr,
Jeff Brown771526c2012-04-27 15:13:25 -070052 long frameTimeNanos);
Jeff Brown32cbc38552011-12-01 14:01:49 -080053
54 /**
55 * Creates an input event receiver bound to the specified input channel.
56 *
57 * @param inputChannel The input channel.
58 * @param looper The looper to use when invoking callbacks.
59 */
60 public InputEventReceiver(InputChannel inputChannel, Looper looper) {
61 if (inputChannel == null) {
62 throw new IllegalArgumentException("inputChannel must not be null");
63 }
64 if (looper == null) {
65 throw new IllegalArgumentException("looper must not be null");
66 }
67
68 mInputChannel = inputChannel;
69 mMessageQueue = looper.getQueue();
Jeff Browna4ca8ea2013-04-02 18:01:38 -070070 mReceiverPtr = nativeInit(new WeakReference<InputEventReceiver>(this),
71 inputChannel, mMessageQueue);
Jeff Brown32cbc38552011-12-01 14:01:49 -080072
73 mCloseGuard.open("dispose");
74 }
75
76 @Override
77 protected void finalize() throws Throwable {
78 try {
Jeff Brown3e7e7f02012-08-27 14:33:53 -070079 dispose(true);
Jeff Brown32cbc38552011-12-01 14:01:49 -080080 } finally {
81 super.finalize();
82 }
83 }
84
85 /**
86 * Disposes the receiver.
87 */
88 public void dispose() {
Jeff Brown3e7e7f02012-08-27 14:33:53 -070089 dispose(false);
90 }
91
92 private void dispose(boolean finalized) {
Jeff Brown32cbc38552011-12-01 14:01:49 -080093 if (mCloseGuard != null) {
Jeff Brown3e7e7f02012-08-27 14:33:53 -070094 if (finalized) {
95 mCloseGuard.warnIfOpen();
96 }
Jeff Brown32cbc38552011-12-01 14:01:49 -080097 mCloseGuard.close();
98 }
Jeff Brown3e7e7f02012-08-27 14:33:53 -070099
Jeff Brown32cbc38552011-12-01 14:01:49 -0800100 if (mReceiverPtr != 0) {
101 nativeDispose(mReceiverPtr);
102 mReceiverPtr = 0;
103 }
104 mInputChannel = null;
105 mMessageQueue = null;
106 }
107
108 /**
109 * Called when an input event is received.
110 * The recipient should process the input event and then call {@link #finishInputEvent}
111 * to indicate whether the event was handled. No new input events will be received
112 * until {@link #finishInputEvent} is called.
113 *
114 * @param event The input event that was received.
115 */
116 public void onInputEvent(InputEvent event) {
117 finishInputEvent(event, false);
118 }
119
120 /**
Jeff Brown072ec962012-02-07 14:46:57 -0800121 * Called when a batched input event is pending.
122 *
123 * The batched input event will continue to accumulate additional movement
124 * samples until the recipient calls {@link #consumeBatchedInputEvents} or
125 * an event is received that ends the batch and causes it to be consumed
126 * immediately (such as a pointer up event).
127 */
128 public void onBatchedInputEventPending() {
Jeff Brown771526c2012-04-27 15:13:25 -0700129 consumeBatchedInputEvents(-1);
Jeff Brown072ec962012-02-07 14:46:57 -0800130 }
131
132 /**
Jeff Brown32cbc38552011-12-01 14:01:49 -0800133 * Finishes an input event and indicates whether it was handled.
Jeff Brown072ec962012-02-07 14:46:57 -0800134 * Must be called on the same Looper thread to which the receiver is attached.
Jeff Brown32cbc38552011-12-01 14:01:49 -0800135 *
136 * @param event The input event that was finished.
137 * @param handled True if the event was handled.
138 */
Jeff Brown072ec962012-02-07 14:46:57 -0800139 public final void finishInputEvent(InputEvent event, boolean handled) {
Jeff Brown32cbc38552011-12-01 14:01:49 -0800140 if (event == null) {
141 throw new IllegalArgumentException("event must not be null");
142 }
143 if (mReceiverPtr == 0) {
144 Log.w(TAG, "Attempted to finish an input event but the input event "
145 + "receiver has already been disposed.");
146 } else {
Jeff Brown072ec962012-02-07 14:46:57 -0800147 int index = mSeqMap.indexOfKey(event.getSequenceNumber());
148 if (index < 0) {
Jeff Brown32cbc38552011-12-01 14:01:49 -0800149 Log.w(TAG, "Attempted to finish an input event that is not in progress.");
150 } else {
Jeff Brown072ec962012-02-07 14:46:57 -0800151 int seq = mSeqMap.valueAt(index);
152 mSeqMap.removeAt(index);
153 nativeFinishInputEvent(mReceiverPtr, seq, handled);
Jeff Brown32cbc38552011-12-01 14:01:49 -0800154 }
155 }
Jeff Brown92cc2d82011-12-02 01:19:47 -0800156 event.recycleIfNeededAfterDispatch();
Jeff Brown32cbc38552011-12-01 14:01:49 -0800157 }
158
Jeff Brown072ec962012-02-07 14:46:57 -0800159 /**
160 * Consumes all pending batched input events.
161 * Must be called on the same Looper thread to which the receiver is attached.
162 *
163 * This method forces all batched input events to be delivered immediately.
164 * Should be called just before animating or drawing a new frame in the UI.
Jeff Brown771526c2012-04-27 15:13:25 -0700165 *
166 * @param frameTimeNanos The time in the {@link System#nanoTime()} time base
167 * when the current display frame started rendering, or -1 if unknown.
Michael Wright62ce65d2013-10-25 14:50:36 -0700168 *
169 * @return Whether a batch was consumed
Jeff Brown072ec962012-02-07 14:46:57 -0800170 */
Michael Wright62ce65d2013-10-25 14:50:36 -0700171 public final boolean consumeBatchedInputEvents(long frameTimeNanos) {
Jeff Brown072ec962012-02-07 14:46:57 -0800172 if (mReceiverPtr == 0) {
173 Log.w(TAG, "Attempted to consume batched input events but the input event "
174 + "receiver has already been disposed.");
175 } else {
Michael Wright62ce65d2013-10-25 14:50:36 -0700176 return nativeConsumeBatchedInputEvents(mReceiverPtr, frameTimeNanos);
Jeff Brown072ec962012-02-07 14:46:57 -0800177 }
Michael Wright62ce65d2013-10-25 14:50:36 -0700178 return false;
Jeff Brown072ec962012-02-07 14:46:57 -0800179 }
180
Jeff Brown32cbc38552011-12-01 14:01:49 -0800181 // Called from native code.
182 @SuppressWarnings("unused")
Jeff Brown072ec962012-02-07 14:46:57 -0800183 private void dispatchInputEvent(int seq, InputEvent event) {
184 mSeqMap.put(event.getSequenceNumber(), seq);
Jeff Brown32cbc38552011-12-01 14:01:49 -0800185 onInputEvent(event);
186 }
187
Jeff Brown072ec962012-02-07 14:46:57 -0800188 // Called from native code.
189 @SuppressWarnings("unused")
190 private void dispatchBatchedInputEventPending() {
191 onBatchedInputEventPending();
192 }
193
Jeff Brown32cbc38552011-12-01 14:01:49 -0800194 public static interface Factory {
195 public InputEventReceiver createInputEventReceiver(
196 InputChannel inputChannel, Looper looper);
197 }
198}