1 /*
2  * Copyright 2015 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 android.hardware.input.cts;
18 
19 import android.app.Activity;
20 import android.os.Bundle;
21 import android.util.Log;
22 import android.view.KeyEvent;
23 import android.view.MotionEvent;
24 
25 import java.util.ArrayList;
26 import java.util.function.Consumer;
27 
28 public class InputCtsActivity extends Activity {
29     private static final String TAG = "InputCtsActivity";
30 
31     private InputCallback mInputCallback;
32     private Consumer<Boolean> mPointerCaptureCallback;
33     private final ArrayList<Integer> mUnhandledKeys = new ArrayList<>();
34     private boolean mConsumeGenericMotionEvents = true;
35 
36     @Override
onCreate(Bundle savedInstanceState)37     public void onCreate(Bundle savedInstanceState) {
38         super.onCreate(savedInstanceState);
39     }
40 
41     @Override
dispatchGenericMotionEvent(MotionEvent ev)42     public boolean dispatchGenericMotionEvent(MotionEvent ev) {
43         if (!mConsumeGenericMotionEvents) {
44             Log.i(TAG, "Unhandled generic motion event: "  + ev);
45             return false;
46         }
47         if (mInputCallback != null) {
48             mInputCallback.onMotionEvent(ev);
49         }
50         return mConsumeGenericMotionEvents;
51     }
52 
53     @Override
dispatchTouchEvent(MotionEvent ev)54     public boolean dispatchTouchEvent(MotionEvent ev) {
55         if (mInputCallback != null) {
56             mInputCallback.onMotionEvent(ev);
57         }
58         return true;
59     }
60 
61     @Override
dispatchTrackballEvent(MotionEvent ev)62     public boolean dispatchTrackballEvent(MotionEvent ev) {
63         if (mInputCallback != null) {
64             mInputCallback.onMotionEvent(ev);
65         }
66         return true;
67     }
68 
69     @Override
dispatchKeyEvent(KeyEvent ev)70     public boolean dispatchKeyEvent(KeyEvent ev) {
71         // Do not handle keys in UnhandledKeys list, let it fallback
72         if (mUnhandledKeys.contains(ev.getKeyCode())) {
73             Log.i(TAG, "Unhandled keyEvent: "  + ev);
74             return false;
75         }
76         if (mInputCallback != null) {
77             mInputCallback.onKeyEvent(ev);
78         }
79         return true;
80     }
81 
82     @Override
onPointerCaptureChanged(boolean hasCapture)83     public void onPointerCaptureChanged(boolean hasCapture) {
84         if (mPointerCaptureCallback != null) {
85             mPointerCaptureCallback.accept(hasCapture);
86         }
87     }
88 
setInputCallback(InputCallback callback)89     public void setInputCallback(InputCallback callback) {
90         mInputCallback = callback;
91     }
92 
setPointerCaptureCallback(Consumer<Boolean> callback)93     public void setPointerCaptureCallback(Consumer<Boolean> callback) {
94         mPointerCaptureCallback = callback;
95     }
96 
addUnhandleKeyCode(int keyCode)97     public void addUnhandleKeyCode(int keyCode) {
98         mUnhandledKeys.add(keyCode);
99     }
100 
clearUnhandleKeyCode()101     public void clearUnhandleKeyCode() {
102         mUnhandledKeys.clear();
103     }
104 
setConsumeGenericMotionEvents(boolean enable)105     public void setConsumeGenericMotionEvents(boolean enable) {
106         mConsumeGenericMotionEvents = enable;
107     }
108 }
109