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 35 @Override onCreate(Bundle savedInstanceState)36 public void onCreate(Bundle savedInstanceState) { 37 super.onCreate(savedInstanceState); 38 } 39 40 @Override dispatchGenericMotionEvent(MotionEvent ev)41 public boolean dispatchGenericMotionEvent(MotionEvent ev) { 42 if (mInputCallback != null) { 43 mInputCallback.onMotionEvent(ev); 44 } 45 return true; 46 } 47 48 @Override dispatchTouchEvent(MotionEvent ev)49 public boolean dispatchTouchEvent(MotionEvent ev) { 50 if (mInputCallback != null) { 51 mInputCallback.onMotionEvent(ev); 52 } 53 return true; 54 } 55 56 @Override dispatchTrackballEvent(MotionEvent ev)57 public boolean dispatchTrackballEvent(MotionEvent ev) { 58 if (mInputCallback != null) { 59 mInputCallback.onMotionEvent(ev); 60 } 61 return true; 62 } 63 64 @Override dispatchKeyEvent(KeyEvent ev)65 public boolean dispatchKeyEvent(KeyEvent ev) { 66 // Do not handle keys in UnhandledKeys list, let it fallback 67 if (mUnhandledKeys.contains(ev.getKeyCode())) { 68 Log.i(TAG, "Unhandled keyEvent: " + ev); 69 return false; 70 } 71 if (mInputCallback != null) { 72 mInputCallback.onKeyEvent(ev); 73 } 74 return true; 75 } 76 77 @Override onPointerCaptureChanged(boolean hasCapture)78 public void onPointerCaptureChanged(boolean hasCapture) { 79 if (mPointerCaptureCallback != null) { 80 mPointerCaptureCallback.accept(hasCapture); 81 } 82 } 83 setInputCallback(InputCallback callback)84 public void setInputCallback(InputCallback callback) { 85 mInputCallback = callback; 86 } 87 setPointerCaptureCallback(Consumer<Boolean> callback)88 public void setPointerCaptureCallback(Consumer<Boolean> callback) { 89 mPointerCaptureCallback = callback; 90 } 91 addUnhandleKeyCode(int keyCode)92 public void addUnhandleKeyCode(int keyCode) { 93 mUnhandledKeys.add(keyCode); 94 } 95 clearUnhandleKeyCode()96 public void clearUnhandleKeyCode() { 97 mUnhandledKeys.clear(); 98 } 99 } 100