| 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.Log; |
| 24 |
|
| 25 |
import dalvik.system.CloseGuard; |
| 26 |
|
| 27 |
import java.lang.ref.WeakReference; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public abstract class InputEventSender { |
| 34 |
private static final String TAG = "InputEventSender"; |
| 35 |
|
| 36 |
private final CloseGuard mCloseGuard = CloseGuard.get(); |
| 37 |
|
| 38 |
private long mSenderPtr; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
private InputChannel mInputChannel; |
| 43 |
private MessageQueue mMessageQueue; |
| 44 |
|
| 45 |
private static native long nativeInit(WeakReference<InputEventSender> sender, |
| 46 |
InputChannel inputChannel, MessageQueue messageQueue); |
| 47 |
private static native void nativeDispose(long senderPtr); |
| 48 |
private static native boolean nativeSendKeyEvent(long senderPtr, int seq, KeyEvent event); |
| 49 |
private static native boolean nativeSendMotionEvent(long senderPtr, int seq, MotionEvent event); |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public InputEventSender(InputChannel inputChannel, Looper looper) { |
| 58 |
if (inputChannel == null) { |
| 59 |
throw new IllegalArgumentException("inputChannel must not be null"); |
| 60 |
} |
| 61 |
if (looper == null) { |
| 62 |
throw new IllegalArgumentException("looper must not be null"); |
| 63 |
} |
| 64 |
|
| 65 |
mInputChannel = inputChannel; |
| 66 |
mMessageQueue = looper.getQueue(); |
| 67 |
mSenderPtr = nativeInit(new WeakReference<InputEventSender>(this), |
| 68 |
mInputChannel, mMessageQueue); |
| 69 |
|
| 70 |
mCloseGuard.open("InputEventSender.dispose"); |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
protected void finalize() throws Throwable { |
| 75 |
try { |
| 76 |
dispose(true); |
| 77 |
} finally { |
| 78 |
super.finalize(); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public void dispose() { |
| 86 |
dispose(false); |
| 87 |
} |
| 88 |
|
| 89 |
private void dispose(boolean finalized) { |
| 90 |
if (mCloseGuard != null) { |
| 91 |
if (finalized) { |
| 92 |
mCloseGuard.warnIfOpen(); |
| 93 |
} |
| 94 |
mCloseGuard.close(); |
| 95 |
} |
| 96 |
|
| 97 |
if (mSenderPtr != 0) { |
| 98 |
nativeDispose(mSenderPtr); |
| 99 |
mSenderPtr = 0; |
| 100 |
} |
| 101 |
mInputChannel = null; |
| 102 |
mMessageQueue = null; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public void onInputEventFinished(int seq, boolean handled) { |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public void onTimelineReported(int inputEventId, long gpuCompletedTime, long presentTime) { |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public final boolean sendInputEvent(int seq, InputEvent event) { |
| 134 |
if (event == null) { |
| 135 |
throw new IllegalArgumentException("event must not be null"); |
| 136 |
} |
| 137 |
if (mSenderPtr == 0) { |
| 138 |
Log.w(TAG, "Attempted to send an input event but the input event " |
| 139 |
+ "sender has already been disposed."); |
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
if (event instanceof KeyEvent) { |
| 144 |
return nativeSendKeyEvent(mSenderPtr, seq, (KeyEvent)event); |
| 145 |
} else { |
| 146 |
return nativeSendMotionEvent(mSenderPtr, seq, (MotionEvent)event); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
@SuppressWarnings("unused") |
| 152 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 153 |
private void dispatchInputEventFinished(int seq, boolean handled) { |
| 154 |
onInputEventFinished(seq, handled); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
@SuppressWarnings("unused") |
| 159 |
private void dispatchTimelineReported( |
| 160 |
int inputEventId, long gpuCompletedTime, long presentTime) { |
| 161 |
onTimelineReported(inputEventId, gpuCompletedTime, presentTime); |
| 162 |
} |
| 163 |
} |