1 /* 2 * Copyright 2020 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.view.cts.input; 18 19 import static org.junit.Assert.fail; 20 21 import android.app.Activity; 22 import android.os.Bundle; 23 import android.view.KeyEvent; 24 25 import java.util.concurrent.BlockingQueue; 26 import java.util.concurrent.LinkedBlockingQueue; 27 import java.util.concurrent.TimeUnit; 28 29 public class InputDeviceKeyLayoutMapTestActivity extends Activity { 30 private static final String TAG = "InputDeviceKeyLayoutMapTestActivity"; 31 32 private final BlockingQueue<KeyEvent> mEvents = new LinkedBlockingQueue<>(); 33 34 @Override onCreate(Bundle savedInstanceState)35 public void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 } 38 39 @Override dispatchKeyEvent(KeyEvent ev)40 public boolean dispatchKeyEvent(KeyEvent ev) { 41 try { 42 mEvents.put(new KeyEvent(ev)); 43 } catch (InterruptedException ex) { 44 fail("interrupted while adding a KeyEvent to the queue"); 45 } 46 return true; 47 } 48 49 /** 50 * Get a KeyEvent from event queue or timeout. 51 * @param timeoutSeconds Timeout in unit of second 52 * @return KeyEvent delivered to test activity, null if timeout. 53 */ getKeyEvent(int timeoutSeconds)54 public KeyEvent getKeyEvent(int timeoutSeconds) { 55 try { 56 return mEvents.poll(timeoutSeconds, TimeUnit.SECONDS); 57 } catch (InterruptedException e) { 58 throw new RuntimeException("unexpectedly interrupted while waiting for InputEvent", e); 59 } 60 } 61 62 } 63