| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
import android.os.Build; |
| 21 |
import android.os.Looper; |
| 22 |
import android.os.MessageQueue; |
| 23 |
import android.util.LongSparseArray; |
| 24 |
import android.util.Pools.Pool; |
| 25 |
import android.util.Pools.SimplePool; |
| 26 |
|
| 27 |
import dalvik.system.CloseGuard; |
| 28 |
|
| 29 |
import java.lang.ref.WeakReference; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public final class InputQueue { |
| 36 |
private final LongSparseArray<ActiveInputEvent> mActiveEventArray = |
| 37 |
new LongSparseArray<ActiveInputEvent>(20); |
| 38 |
private final Pool<ActiveInputEvent> mActiveInputEventPool = |
| 39 |
new SimplePool<ActiveInputEvent>(20); |
| 40 |
|
| 41 |
private final CloseGuard mCloseGuard = CloseGuard.get(); |
| 42 |
|
| 43 |
private long mPtr; |
| 44 |
|
| 45 |
private static native long nativeInit(WeakReference<InputQueue> weakQueue, |
| 46 |
MessageQueue messageQueue); |
| 47 |
private static native long nativeSendKeyEvent(long ptr, KeyEvent e, boolean preDispatch); |
| 48 |
private static native long nativeSendMotionEvent(long ptr, MotionEvent e); |
| 49 |
private static native void nativeDispose(long ptr); |
| 50 |
|
| 51 |
|
| 52 |
public InputQueue() { |
| 53 |
mPtr = nativeInit(new WeakReference<InputQueue>(this), Looper.myQueue()); |
| 54 |
|
| 55 |
mCloseGuard.open("InputQueue.dispose"); |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
protected void finalize() throws Throwable { |
| 60 |
try { |
| 61 |
dispose(true); |
| 62 |
} finally { |
| 63 |
super.finalize(); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
public void dispose() { |
| 69 |
dispose(false); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
public void dispose(boolean finalized) { |
| 74 |
if (mCloseGuard != null) { |
| 75 |
if (finalized) { |
| 76 |
mCloseGuard.warnIfOpen(); |
| 77 |
} |
| 78 |
mCloseGuard.close(); |
| 79 |
} |
| 80 |
|
| 81 |
if (mPtr != 0) { |
| 82 |
nativeDispose(mPtr); |
| 83 |
mPtr = 0; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
public long getNativePtr() { |
| 89 |
return mPtr; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
public void sendInputEvent(InputEvent e, Object token, boolean predispatch, |
| 94 |
FinishedInputEventCallback callback) { |
| 95 |
ActiveInputEvent event = obtainActiveInputEvent(token, callback); |
| 96 |
long id; |
| 97 |
if (e instanceof KeyEvent) { |
| 98 |
id = nativeSendKeyEvent(mPtr, (KeyEvent) e, predispatch); |
| 99 |
} else { |
| 100 |
id = nativeSendMotionEvent(mPtr, (MotionEvent) e); |
| 101 |
} |
| 102 |
mActiveEventArray.put(id, event); |
| 103 |
} |
| 104 |
|
| 105 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 106 |
private void finishInputEvent(long id, boolean handled) { |
| 107 |
int index = mActiveEventArray.indexOfKey(id); |
| 108 |
if (index >= 0) { |
| 109 |
ActiveInputEvent e = mActiveEventArray.valueAt(index); |
| 110 |
mActiveEventArray.removeAt(index); |
| 111 |
e.mCallback.onFinishedInputEvent(e.mToken, handled); |
| 112 |
recycleActiveInputEvent(e); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
private ActiveInputEvent obtainActiveInputEvent(Object token, |
| 117 |
FinishedInputEventCallback callback) { |
| 118 |
ActiveInputEvent e = mActiveInputEventPool.acquire(); |
| 119 |
if (e == null) { |
| 120 |
e = new ActiveInputEvent(); |
| 121 |
} |
| 122 |
e.mToken = token; |
| 123 |
e.mCallback = callback; |
| 124 |
return e; |
| 125 |
} |
| 126 |
|
| 127 |
private void recycleActiveInputEvent(ActiveInputEvent e) { |
| 128 |
e.recycle(); |
| 129 |
mActiveInputEventPool.release(e); |
| 130 |
} |
| 131 |
|
| 132 |
private final class ActiveInputEvent { |
| 133 |
public Object mToken; |
| 134 |
public FinishedInputEventCallback mCallback; |
| 135 |
|
| 136 |
public void recycle() { |
| 137 |
mToken = null; |
| 138 |
mCallback = null; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public static interface Callback { |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
void onInputQueueCreated(InputQueue queue); |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
void onInputQueueDestroyed(InputQueue queue); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
public static interface FinishedInputEventCallback { |
| 162 |
void onFinishedInputEvent(Object token, boolean handled); |
| 163 |
} |
| 164 |
|
| 165 |
} |