| 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.IBinder; |
| 22 |
import android.os.Looper; |
| 23 |
import android.os.MessageQueue; |
| 24 |
import android.os.Trace; |
| 25 |
import android.util.Log; |
| 26 |
import android.util.SparseIntArray; |
| 27 |
|
| 28 |
import dalvik.system.CloseGuard; |
| 29 |
|
| 30 |
import java.io.PrintWriter; |
| 31 |
import java.lang.ref.Reference; |
| 32 |
import java.lang.ref.WeakReference; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public abstract class InputEventReceiver { |
| 39 |
private static final String TAG = "InputEventReceiver"; |
| 40 |
|
| 41 |
private final CloseGuard mCloseGuard = CloseGuard.get(); |
| 42 |
|
| 43 |
private long mReceiverPtr; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
private InputChannel mInputChannel; |
| 48 |
private MessageQueue mMessageQueue; |
| 49 |
|
| 50 |
|
| 51 |
private final SparseIntArray mSeqMap = new SparseIntArray(); |
| 52 |
|
| 53 |
private static native long nativeInit(WeakReference<InputEventReceiver> receiver, |
| 54 |
InputChannel inputChannel, MessageQueue messageQueue); |
| 55 |
private static native void nativeDispose(long receiverPtr); |
| 56 |
private static native void nativeFinishInputEvent(long receiverPtr, int seq, boolean handled); |
| 57 |
private static native void nativeReportTimeline(long receiverPtr, int inputEventId, |
| 58 |
long gpuCompletedTime, long presentTime); |
| 59 |
private static native boolean nativeConsumeBatchedInputEvents(long receiverPtr, |
| 60 |
long frameTimeNanos); |
| 61 |
private static native String nativeDump(long receiverPtr, String prefix); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public InputEventReceiver(InputChannel inputChannel, Looper looper) { |
| 70 |
if (inputChannel == null) { |
| 71 |
throw new IllegalArgumentException("inputChannel must not be null"); |
| 72 |
} |
| 73 |
if (looper == null) { |
| 74 |
throw new IllegalArgumentException("looper must not be null"); |
| 75 |
} |
| 76 |
|
| 77 |
mInputChannel = inputChannel; |
| 78 |
mMessageQueue = looper.getQueue(); |
| 79 |
mReceiverPtr = nativeInit(new WeakReference<InputEventReceiver>(this), |
| 80 |
mInputChannel, mMessageQueue); |
| 81 |
|
| 82 |
mCloseGuard.open("InputEventReceiver.dispose"); |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
protected void finalize() throws Throwable { |
| 87 |
try { |
| 88 |
dispose(true); |
| 89 |
} finally { |
| 90 |
super.finalize(); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public void dispose() { |
| 99 |
dispose(false); |
| 100 |
} |
| 101 |
|
| 102 |
private void dispose(boolean finalized) { |
| 103 |
if (mCloseGuard != null) { |
| 104 |
if (finalized) { |
| 105 |
mCloseGuard.warnIfOpen(); |
| 106 |
} |
| 107 |
mCloseGuard.close(); |
| 108 |
} |
| 109 |
|
| 110 |
if (mReceiverPtr != 0) { |
| 111 |
nativeDispose(mReceiverPtr); |
| 112 |
mReceiverPtr = 0; |
| 113 |
} |
| 114 |
|
| 115 |
if (mInputChannel != null) { |
| 116 |
mInputChannel.dispose(); |
| 117 |
mInputChannel = null; |
| 118 |
} |
| 119 |
mMessageQueue = null; |
| 120 |
Reference.reachabilityFence(this); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 132 |
public void onInputEvent(InputEvent event) { |
| 133 |
finishInputEvent(event, false); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public void onFocusEvent(boolean hasFocus) { |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public void onPointerCaptureEvent(boolean pointerCaptureEnabled) { |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public void onDragEvent(boolean isExiting, float x, float y) { |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public void onTouchModeChanged(boolean inTouchMode) { |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public void onBatchedInputEventPending(int source) { |
| 191 |
consumeBatchedInputEvents(-1); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public final void finishInputEvent(InputEvent event, boolean handled) { |
| 202 |
if (event == null) { |
| 203 |
throw new IllegalArgumentException("event must not be null"); |
| 204 |
} |
| 205 |
if (mReceiverPtr == 0) { |
| 206 |
Log.w(TAG, "Attempted to finish an input event but the input event " |
| 207 |
+ "receiver has already been disposed."); |
| 208 |
} else { |
| 209 |
int index = mSeqMap.indexOfKey(event.getSequenceNumber()); |
| 210 |
if (index < 0) { |
| 211 |
Log.w(TAG, "Attempted to finish an input event that is not in progress."); |
| 212 |
} else { |
| 213 |
int seq = mSeqMap.valueAt(index); |
| 214 |
mSeqMap.removeAt(index); |
| 215 |
nativeFinishInputEvent(mReceiverPtr, seq, handled); |
| 216 |
} |
| 217 |
} |
| 218 |
event.recycleIfNeededAfterDispatch(); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public final void reportTimeline(int inputEventId, long gpuCompletedTime, long presentTime) { |
| 225 |
Trace.traceBegin(Trace.TRACE_TAG_INPUT, "reportTimeline"); |
| 226 |
nativeReportTimeline(mReceiverPtr, inputEventId, gpuCompletedTime, presentTime); |
| 227 |
Trace.traceEnd(Trace.TRACE_TAG_INPUT); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public final boolean consumeBatchedInputEvents(long frameTimeNanos) { |
| 243 |
if (mReceiverPtr == 0) { |
| 244 |
Log.w(TAG, "Attempted to consume batched input events but the input event " |
| 245 |
+ "receiver has already been disposed."); |
| 246 |
} else { |
| 247 |
return nativeConsumeBatchedInputEvents(mReceiverPtr, frameTimeNanos); |
| 248 |
} |
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public IBinder getToken() { |
| 256 |
if (mInputChannel == null) { |
| 257 |
return null; |
| 258 |
} |
| 259 |
return mInputChannel.getToken(); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
@SuppressWarnings("unused") |
| 264 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 265 |
private void dispatchInputEvent(int seq, InputEvent event) { |
| 266 |
mSeqMap.put(event.getSequenceNumber(), seq); |
| 267 |
onInputEvent(event); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
public void dump(String prefix, PrintWriter writer) { |
| 276 |
writer.println(prefix + getClass().getName()); |
| 277 |
writer.println(prefix + " mInputChannel: " + mInputChannel); |
| 278 |
writer.println(prefix + " mSeqMap: " + mSeqMap); |
| 279 |
writer.println(prefix + " mReceiverPtr:\n" + nativeDump(mReceiverPtr, prefix + " ")); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
public interface Factory { |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
InputEventReceiver createInputEventReceiver(InputChannel inputChannel, Looper looper); |
| 290 |
} |
| 291 |
} |