| 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.Handler; |
| 21 |
import android.os.Looper; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class BatchedInputEventReceiver extends InputEventReceiver { |
| 28 |
private Choreographer mChoreographer; |
| 29 |
private boolean mBatchingEnabled; |
| 30 |
private boolean mBatchedInputScheduled; |
| 31 |
private final Handler mHandler; |
| 32 |
private final Runnable mConsumeBatchedInputEvents = new Runnable() { |
| 33 |
@Override |
| 34 |
public void run() { |
| 35 |
consumeBatchedInputEvents(-1); |
| 36 |
} |
| 37 |
}; |
| 38 |
|
| 39 |
@UnsupportedAppUsage |
| 40 |
public BatchedInputEventReceiver( |
| 41 |
InputChannel inputChannel, Looper looper, Choreographer choreographer) { |
| 42 |
super(inputChannel, looper); |
| 43 |
mChoreographer = choreographer; |
| 44 |
mBatchingEnabled = true; |
| 45 |
mHandler = new Handler(looper); |
| 46 |
} |
| 47 |
|
| 48 |
@Override |
| 49 |
public void onBatchedInputEventPending(int source) { |
| 50 |
if (mBatchingEnabled) { |
| 51 |
scheduleBatchedInput(); |
| 52 |
} else { |
| 53 |
consumeBatchedInputEvents(-1); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public void dispose() { |
| 59 |
unscheduleBatchedInput(); |
| 60 |
consumeBatchedInputEvents(-1); |
| 61 |
super.dispose(); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public void setBatchingEnabled(boolean batchingEnabled) { |
| 69 |
if (mBatchingEnabled == batchingEnabled) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
mBatchingEnabled = batchingEnabled; |
| 74 |
mHandler.removeCallbacks(mConsumeBatchedInputEvents); |
| 75 |
if (!batchingEnabled) { |
| 76 |
unscheduleBatchedInput(); |
| 77 |
mHandler.post(mConsumeBatchedInputEvents); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
protected void doConsumeBatchedInput(long frameTimeNanos) { |
| 82 |
if (mBatchedInputScheduled) { |
| 83 |
mBatchedInputScheduled = false; |
| 84 |
if (consumeBatchedInputEvents(frameTimeNanos) && frameTimeNanos != -1) { |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
scheduleBatchedInput(); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
private void scheduleBatchedInput() { |
| 96 |
if (!mBatchedInputScheduled) { |
| 97 |
mBatchedInputScheduled = true; |
| 98 |
mChoreographer.postCallback(Choreographer.CALLBACK_INPUT, mBatchedInputRunnable, null); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
private void unscheduleBatchedInput() { |
| 103 |
if (mBatchedInputScheduled) { |
| 104 |
mBatchedInputScheduled = false; |
| 105 |
mChoreographer.removeCallbacks( |
| 106 |
Choreographer.CALLBACK_INPUT, mBatchedInputRunnable, null); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
private final class BatchedInputRunnable implements Runnable { |
| 111 |
@Override |
| 112 |
public void run() { |
| 113 |
doConsumeBatchedInput(mChoreographer.getFrameTimeNanos()); |
| 114 |
} |
| 115 |
} |
| 116 |
private final BatchedInputRunnable mBatchedInputRunnable = new BatchedInputRunnable(); |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public static class SimpleBatchedInputEventReceiver extends BatchedInputEventReceiver { |
| 123 |
|
| 124 |
|
| 125 |
public interface InputEventListener { |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
boolean onInputEvent(InputEvent event); |
| 131 |
} |
| 132 |
|
| 133 |
protected InputEventListener mListener; |
| 134 |
|
| 135 |
public SimpleBatchedInputEventReceiver(InputChannel inputChannel, Looper looper, |
| 136 |
Choreographer choreographer, InputEventListener listener) { |
| 137 |
super(inputChannel, looper, choreographer); |
| 138 |
mListener = listener; |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public void onInputEvent(InputEvent event) { |
| 143 |
boolean handled = false; |
| 144 |
try { |
| 145 |
handled = mListener.onInputEvent(event); |
| 146 |
} finally { |
| 147 |
finishInputEvent(event, handled); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
} |