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 android.graphics.Matrix;
20 import android.os.Looper;
21 import android.view.BatchedInputEventReceiver;
22 import android.view.Choreographer;
23 import android.view.InputChannel;
24 import android.view.InputEvent;
25 import android.view.MotionEvent;
26 
27 /**
28  * @see android.view.InputChannel
29  */
30 public class InputChannelCompat {
31 
32     /**
33      * Callback for receiving event callbacks
34      */
35     public interface InputEventListener {
36         /**
37          * @param ev event to be handled
38          */
onInputEvent(InputEvent ev)39         void onInputEvent(InputEvent ev);
40     }
41 
42     /**
43      * Version of addBatch method which preserves time accuracy in nanoseconds instead of
44      * converting the time to milliseconds.
45      * @param src old MotionEvent where the target should be appended
46      * @param target new MotionEvent which should be added to the src
47      * @return true if the merge was successful
48      *
49      * @see MotionEvent#addBatch(MotionEvent)
50      */
mergeMotionEvent(MotionEvent src, MotionEvent target)51     public static boolean mergeMotionEvent(MotionEvent src, MotionEvent target) {
52         return target.addBatch(src);
53     }
54 
55     /** @see MotionEvent#createRotateMatrix */
createRotationMatrix( int rotation, int displayW, int displayH)56     public static Matrix createRotationMatrix(
57             /*@Surface.Rotation*/ int rotation, int displayW, int displayH) {
58         return MotionEvent.createRotateMatrix(rotation, displayW, displayH);
59     }
60 
61     /**
62      * @see BatchedInputEventReceiver
63      */
64     public static class InputEventReceiver {
65 
66         private final BatchedInputEventReceiver mReceiver;
67 
InputEventReceiver(InputChannel inputChannel, Looper looper, Choreographer choreographer, final InputEventListener listener)68         public InputEventReceiver(InputChannel inputChannel, Looper looper,
69                 Choreographer choreographer, final InputEventListener listener) {
70             mReceiver = new BatchedInputEventReceiver(inputChannel, looper, choreographer) {
71 
72                 @Override
73                 public void onInputEvent(InputEvent event) {
74                     listener.onInputEvent(event);
75                     finishInputEvent(event, true /* handled */);
76                 }
77             };
78         }
79 
80         /**
81          * @see BatchedInputEventReceiver#setBatchingEnabled()
82          */
setBatchingEnabled(boolean batchingEnabled)83         public void setBatchingEnabled(boolean batchingEnabled) {
84             mReceiver.setBatchingEnabled(batchingEnabled);
85         }
86 
87         /**
88          * @see BatchedInputEventReceiver#dispose()
89          */
dispose()90         public void dispose() {
91             mReceiver.dispose();
92         }
93     }
94 }
95