Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.view; |
| 18 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 20 | import android.os.Looper; |
| 21 | import android.os.MessageQueue; |
| 22 | import android.util.Log; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 23 | import android.util.SparseIntArray; |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 24 | |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 25 | import dalvik.system.CloseGuard; |
| 26 | |
Jeff Brown | a4ca8ea | 2013-04-02 18:01:38 -0700 | [diff] [blame] | 27 | import java.lang.ref.WeakReference; |
| 28 | |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 29 | /** |
| 30 | * Provides a low-level mechanism for an application to receive input events. |
| 31 | * @hide |
| 32 | */ |
| 33 | public abstract class InputEventReceiver { |
| 34 | private static final String TAG = "InputEventReceiver"; |
| 35 | |
| 36 | private final CloseGuard mCloseGuard = CloseGuard.get(); |
| 37 | |
Ashok Bhat | a931d521 | 2014-01-08 14:04:51 +0000 | [diff] [blame] | 38 | private long mReceiverPtr; |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 39 | |
| 40 | // We keep references to the input channel and message queue objects here so that |
| 41 | // they are not GC'd while the native peer of the receiver is using them. |
| 42 | private InputChannel mInputChannel; |
| 43 | private MessageQueue mMessageQueue; |
| 44 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 45 | // Map from InputEvent sequence numbers to dispatcher sequence numbers. |
| 46 | private final SparseIntArray mSeqMap = new SparseIntArray(); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 47 | |
Ashok Bhat | a931d521 | 2014-01-08 14:04:51 +0000 | [diff] [blame] | 48 | private static native long nativeInit(WeakReference<InputEventReceiver> receiver, |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 49 | InputChannel inputChannel, MessageQueue messageQueue); |
Ashok Bhat | a931d521 | 2014-01-08 14:04:51 +0000 | [diff] [blame] | 50 | private static native void nativeDispose(long receiverPtr); |
| 51 | private static native void nativeFinishInputEvent(long receiverPtr, int seq, boolean handled); |
| 52 | private static native boolean nativeConsumeBatchedInputEvents(long receiverPtr, |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 53 | long frameTimeNanos); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Creates an input event receiver bound to the specified input channel. |
| 57 | * |
| 58 | * @param inputChannel The input channel. |
| 59 | * @param looper The looper to use when invoking callbacks. |
| 60 | */ |
| 61 | public InputEventReceiver(InputChannel inputChannel, Looper looper) { |
| 62 | if (inputChannel == null) { |
| 63 | throw new IllegalArgumentException("inputChannel must not be null"); |
| 64 | } |
| 65 | if (looper == null) { |
| 66 | throw new IllegalArgumentException("looper must not be null"); |
| 67 | } |
| 68 | |
| 69 | mInputChannel = inputChannel; |
| 70 | mMessageQueue = looper.getQueue(); |
Jeff Brown | a4ca8ea | 2013-04-02 18:01:38 -0700 | [diff] [blame] | 71 | mReceiverPtr = nativeInit(new WeakReference<InputEventReceiver>(this), |
| 72 | inputChannel, mMessageQueue); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 73 | |
| 74 | mCloseGuard.open("dispose"); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | protected void finalize() throws Throwable { |
| 79 | try { |
Jeff Brown | 3e7e7f0 | 2012-08-27 14:33:53 -0700 | [diff] [blame] | 80 | dispose(true); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 81 | } finally { |
| 82 | super.finalize(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Disposes the receiver. |
| 88 | */ |
| 89 | public void dispose() { |
Jeff Brown | 3e7e7f0 | 2012-08-27 14:33:53 -0700 | [diff] [blame] | 90 | dispose(false); |
| 91 | } |
| 92 | |
| 93 | private void dispose(boolean finalized) { |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 94 | if (mCloseGuard != null) { |
Jeff Brown | 3e7e7f0 | 2012-08-27 14:33:53 -0700 | [diff] [blame] | 95 | if (finalized) { |
| 96 | mCloseGuard.warnIfOpen(); |
| 97 | } |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 98 | mCloseGuard.close(); |
| 99 | } |
Jeff Brown | 3e7e7f0 | 2012-08-27 14:33:53 -0700 | [diff] [blame] | 100 | |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 101 | if (mReceiverPtr != 0) { |
| 102 | nativeDispose(mReceiverPtr); |
| 103 | mReceiverPtr = 0; |
| 104 | } |
Arthur Hung | 12a06ce | 2019-07-22 15:35:17 +0800 | [diff] [blame] | 105 | |
| 106 | if (mInputChannel != null) { |
| 107 | mInputChannel.dispose(); |
| 108 | mInputChannel = null; |
| 109 | } |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 110 | mMessageQueue = null; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Called when an input event is received. |
| 115 | * The recipient should process the input event and then call {@link #finishInputEvent} |
| 116 | * to indicate whether the event was handled. No new input events will be received |
| 117 | * until {@link #finishInputEvent} is called. |
| 118 | * |
| 119 | * @param event The input event that was received. |
| 120 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 121 | @UnsupportedAppUsage |
Siarhei Vishniakou | 85ddfff | 2018-01-31 16:49:36 -0800 | [diff] [blame] | 122 | public void onInputEvent(InputEvent event) { |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 123 | finishInputEvent(event, false); |
| 124 | } |
| 125 | |
| 126 | /** |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 127 | * Called when a batched input event is pending. |
| 128 | * |
| 129 | * The batched input event will continue to accumulate additional movement |
| 130 | * samples until the recipient calls {@link #consumeBatchedInputEvents} or |
| 131 | * an event is received that ends the batch and causes it to be consumed |
| 132 | * immediately (such as a pointer up event). |
| 133 | */ |
| 134 | public void onBatchedInputEventPending() { |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 135 | consumeBatchedInputEvents(-1); |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 139 | * Finishes an input event and indicates whether it was handled. |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 140 | * Must be called on the same Looper thread to which the receiver is attached. |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 141 | * |
| 142 | * @param event The input event that was finished. |
| 143 | * @param handled True if the event was handled. |
| 144 | */ |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 145 | public final void finishInputEvent(InputEvent event, boolean handled) { |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 146 | if (event == null) { |
| 147 | throw new IllegalArgumentException("event must not be null"); |
| 148 | } |
| 149 | if (mReceiverPtr == 0) { |
| 150 | Log.w(TAG, "Attempted to finish an input event but the input event " |
| 151 | + "receiver has already been disposed."); |
| 152 | } else { |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 153 | int index = mSeqMap.indexOfKey(event.getSequenceNumber()); |
| 154 | if (index < 0) { |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 155 | Log.w(TAG, "Attempted to finish an input event that is not in progress."); |
| 156 | } else { |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 157 | int seq = mSeqMap.valueAt(index); |
| 158 | mSeqMap.removeAt(index); |
| 159 | nativeFinishInputEvent(mReceiverPtr, seq, handled); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 160 | } |
| 161 | } |
Jeff Brown | 92cc2d8 | 2011-12-02 01:19:47 -0800 | [diff] [blame] | 162 | event.recycleIfNeededAfterDispatch(); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 165 | /** |
| 166 | * Consumes all pending batched input events. |
| 167 | * Must be called on the same Looper thread to which the receiver is attached. |
| 168 | * |
| 169 | * This method forces all batched input events to be delivered immediately. |
| 170 | * Should be called just before animating or drawing a new frame in the UI. |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 171 | * |
| 172 | * @param frameTimeNanos The time in the {@link System#nanoTime()} time base |
| 173 | * when the current display frame started rendering, or -1 if unknown. |
Michael Wright | 62ce65d | 2013-10-25 14:50:36 -0700 | [diff] [blame] | 174 | * |
| 175 | * @return Whether a batch was consumed |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 176 | */ |
Michael Wright | 62ce65d | 2013-10-25 14:50:36 -0700 | [diff] [blame] | 177 | public final boolean consumeBatchedInputEvents(long frameTimeNanos) { |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 178 | if (mReceiverPtr == 0) { |
| 179 | Log.w(TAG, "Attempted to consume batched input events but the input event " |
| 180 | + "receiver has already been disposed."); |
| 181 | } else { |
Michael Wright | 62ce65d | 2013-10-25 14:50:36 -0700 | [diff] [blame] | 182 | return nativeConsumeBatchedInputEvents(mReceiverPtr, frameTimeNanos); |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 183 | } |
Michael Wright | 62ce65d | 2013-10-25 14:50:36 -0700 | [diff] [blame] | 184 | return false; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 187 | // Called from native code. |
| 188 | @SuppressWarnings("unused") |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 189 | @UnsupportedAppUsage |
Siarhei Vishniakou | 85ddfff | 2018-01-31 16:49:36 -0800 | [diff] [blame] | 190 | private void dispatchInputEvent(int seq, InputEvent event) { |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 191 | mSeqMap.put(event.getSequenceNumber(), seq); |
Siarhei Vishniakou | 85ddfff | 2018-01-31 16:49:36 -0800 | [diff] [blame] | 192 | onInputEvent(event); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 195 | // Called from native code. |
| 196 | @SuppressWarnings("unused") |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 197 | @UnsupportedAppUsage |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 198 | private void dispatchBatchedInputEventPending() { |
| 199 | onBatchedInputEventPending(); |
| 200 | } |
| 201 | |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 202 | public static interface Factory { |
| 203 | public InputEventReceiver createInputEventReceiver( |
| 204 | InputChannel inputChannel, Looper looper); |
| 205 | } |
| 206 | } |