blob: aebc60152bf06d4623841962c68b8bd142a761dc [file] [log] [blame]
Jeff Brown46b9ac02010-04-22 18:58:52 -07001/*
2 * Copyright (C) 2010 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
Michael Wrighta44dd262013-04-10 21:12:00 -070019import dalvik.system.CloseGuard;
20
Michael Wrighta44dd262013-04-10 21:12:00 -070021import android.os.Looper;
22import android.os.MessageQueue;
23import android.util.Pools.Pool;
24import android.util.Pools.SimplePool;
Ashok Bhata931d5212014-01-08 14:04:51 +000025import android.util.LongSparseArray;
Michael Wrighta44dd262013-04-10 21:12:00 -070026
27import java.lang.ref.WeakReference;
28
Jeff Brown46b9ac02010-04-22 18:58:52 -070029/**
30 * An input queue provides a mechanism for an application to receive incoming
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -070031 * input events. Currently only usable from native code.
Jeff Brown46b9ac02010-04-22 18:58:52 -070032 */
33public final class InputQueue {
Ashok Bhata931d5212014-01-08 14:04:51 +000034 private final LongSparseArray<ActiveInputEvent> mActiveEventArray =
35 new LongSparseArray<ActiveInputEvent>(20);
Michael Wrighta44dd262013-04-10 21:12:00 -070036 private final Pool<ActiveInputEvent> mActiveInputEventPool =
37 new SimplePool<ActiveInputEvent>(20);
38
39 private final CloseGuard mCloseGuard = CloseGuard.get();
40
Ashok Bhata931d5212014-01-08 14:04:51 +000041 private long mPtr;
Michael Wrighta44dd262013-04-10 21:12:00 -070042
Ashok Bhata931d5212014-01-08 14:04:51 +000043 private static native long nativeInit(WeakReference<InputQueue> weakQueue,
Michael Wrighta44dd262013-04-10 21:12:00 -070044 MessageQueue messageQueue);
Ashok Bhata931d5212014-01-08 14:04:51 +000045 private static native long nativeSendKeyEvent(long ptr, KeyEvent e, boolean preDispatch);
46 private static native long nativeSendMotionEvent(long ptr, MotionEvent e);
47 private static native void nativeDispose(long ptr);
Michael Wrighta44dd262013-04-10 21:12:00 -070048
49 /** @hide */
50 public InputQueue() {
51 mPtr = nativeInit(new WeakReference<InputQueue>(this), Looper.myQueue());
52
53 mCloseGuard.open("dispose");
54 }
55
56 @Override
57 protected void finalize() throws Throwable {
58 try {
59 dispose(true);
60 } finally {
61 super.finalize();
62 }
63 }
64
65 /** @hide */
66 public void dispose() {
67 dispose(false);
68 }
69
70 /** @hide */
71 public void dispose(boolean finalized) {
72 if (mCloseGuard != null) {
73 if (finalized) {
74 mCloseGuard.warnIfOpen();
75 }
76 mCloseGuard.close();
77 }
78
79 if (mPtr != 0) {
80 nativeDispose(mPtr);
81 mPtr = 0;
82 }
83 }
84
85 /** @hide */
Ashok Bhata931d5212014-01-08 14:04:51 +000086 public long getNativePtr() {
Michael Wrighta44dd262013-04-10 21:12:00 -070087 return mPtr;
88 }
89
90 /** @hide */
91 public void sendInputEvent(InputEvent e, Object token, boolean predispatch,
92 FinishedInputEventCallback callback) {
93 ActiveInputEvent event = obtainActiveInputEvent(token, callback);
Ashok Bhata931d5212014-01-08 14:04:51 +000094 long id;
Michael Wrighta44dd262013-04-10 21:12:00 -070095 if (e instanceof KeyEvent) {
96 id = nativeSendKeyEvent(mPtr, (KeyEvent) e, predispatch);
97 } else {
98 id = nativeSendMotionEvent(mPtr, (MotionEvent) e);
99 }
100 mActiveEventArray.put(id, event);
101 }
102
Ashok Bhata931d5212014-01-08 14:04:51 +0000103 private void finishInputEvent(long id, boolean handled) {
Michael Wrighta44dd262013-04-10 21:12:00 -0700104 int index = mActiveEventArray.indexOfKey(id);
105 if (index >= 0) {
106 ActiveInputEvent e = mActiveEventArray.valueAt(index);
107 mActiveEventArray.removeAt(index);
108 e.mCallback.onFinishedInputEvent(e.mToken, handled);
109 recycleActiveInputEvent(e);
110 }
111 }
112
113 private ActiveInputEvent obtainActiveInputEvent(Object token,
114 FinishedInputEventCallback callback) {
115 ActiveInputEvent e = mActiveInputEventPool.acquire();
116 if (e == null) {
117 e = new ActiveInputEvent();
118 }
119 e.mToken = token;
120 e.mCallback = callback;
121 return e;
122 }
123
124 private void recycleActiveInputEvent(ActiveInputEvent e) {
125 e.recycle();
126 mActiveInputEventPool.release(e);
127 }
128
129 private final class ActiveInputEvent {
130 public Object mToken;
131 public FinishedInputEventCallback mCallback;
132
133 public void recycle() {
134 mToken = null;
135 mCallback = null;
136 }
137 }
138
Dianne Hackbornbfba7ca2010-09-24 17:18:14 -0700139 /**
140 * Interface to receive notification of when an InputQueue is associated
141 * and dissociated with a thread.
142 */
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -0700143 public static interface Callback {
Dianne Hackbornbfba7ca2010-09-24 17:18:14 -0700144 /**
145 * Called when the given InputQueue is now associated with the
146 * thread making this call, so it can start receiving events from it.
147 */
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -0700148 void onInputQueueCreated(InputQueue queue);
Michael Wrighta44dd262013-04-10 21:12:00 -0700149
Dianne Hackbornbfba7ca2010-09-24 17:18:14 -0700150 /**
151 * Called when the given InputQueue is no longer associated with
152 * the thread and thus not dispatching events.
153 */
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -0700154 void onInputQueueDestroyed(InputQueue queue);
155 }
156
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -0700157 /** @hide */
Michael Wrighta44dd262013-04-10 21:12:00 -0700158 public static interface FinishedInputEventCallback {
159 void onFinishedInputEvent(Object token, boolean handled);
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -0700160 }
Michael Wrighta44dd262013-04-10 21:12:00 -0700161
Jeff Brown46b9ac02010-04-22 18:58:52 -0700162}