1 /** 2 * Copyright (C) 2019 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 17 package com.android.systemui.shared.system; 18 19 import static android.os.Trace.TRACE_TAG_INPUT; 20 21 import android.os.Looper; 22 import android.os.Trace; 23 import android.util.Log; 24 import android.view.BatchedInputEventReceiver; 25 import android.view.Choreographer; 26 import android.view.InputChannel; 27 import android.view.InputEvent; 28 import android.view.MotionEvent; 29 30 /** 31 * @see android.view.InputChannel 32 */ 33 public class InputChannelCompat { 34 35 /** 36 * Callback for receiving event callbacks 37 */ 38 public interface InputEventListener { 39 /** 40 * @param ev event to be handled 41 */ onInputEvent(InputEvent ev)42 void onInputEvent(InputEvent ev); 43 } 44 45 /** 46 * Version of addBatch method which preserves time accuracy in nanoseconds instead of 47 * converting the time to milliseconds. 48 * @param src old MotionEvent where the target should be appended 49 * @param target new MotionEvent which should be added to the src 50 * @return true if the merge was successful 51 * 52 * @see MotionEvent#addBatch(MotionEvent) 53 */ mergeMotionEvent(MotionEvent src, MotionEvent target)54 public static boolean mergeMotionEvent(MotionEvent src, MotionEvent target) { 55 return target.addBatch(src); 56 } 57 58 /** 59 * @see BatchedInputEventReceiver 60 */ 61 public static class InputEventReceiver { 62 63 private final String mName; 64 private final BatchedInputEventReceiver mReceiver; 65 66 @Deprecated InputEventReceiver(InputChannel inputChannel, Looper looper, Choreographer choreographer, final InputEventListener listener)67 public InputEventReceiver(InputChannel inputChannel, Looper looper, 68 Choreographer choreographer, final InputEventListener listener) { 69 this("unknown", inputChannel, looper, choreographer, listener); 70 } 71 InputEventReceiver(String name, InputChannel inputChannel, Looper looper, Choreographer choreographer, final InputEventListener listener)72 public InputEventReceiver(String name, InputChannel inputChannel, Looper looper, 73 Choreographer choreographer, final InputEventListener listener) { 74 mName = name; 75 mReceiver = new BatchedInputEventReceiver(inputChannel, looper, choreographer) { 76 @Override 77 public void onInputEvent(InputEvent event) { 78 listener.onInputEvent(event); 79 finishInputEvent(event, true /* handled */); 80 } 81 }; 82 } 83 84 /** 85 * @see BatchedInputEventReceiver#setBatchingEnabled() 86 */ setBatchingEnabled(boolean batchingEnabled)87 public void setBatchingEnabled(boolean batchingEnabled) { 88 mReceiver.setBatchingEnabled(batchingEnabled); 89 } 90 91 /** 92 * @see BatchedInputEventReceiver#dispose() 93 */ dispose()94 public void dispose() { 95 mReceiver.dispose(); 96 Trace.instant(TRACE_TAG_INPUT, "InputMonitorCompat-" + mName + " receiver disposed"); 97 Log.d(InputMonitorCompat.TAG, "Input event receiver for monitor (" + mName 98 + ") disposed"); 99 } 100 } 101 } 102