1 /* 2 * Copyright (C) 2022 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; 18 19 import android.annotation.NonNull; 20 import android.annotation.RequiresPermission; 21 import android.annotation.SystemApi; 22 import android.companion.virtual.IVirtualDevice; 23 import android.os.IBinder; 24 import android.os.RemoteException; 25 import android.view.KeyEvent; 26 27 import java.util.Arrays; 28 import java.util.Collections; 29 import java.util.HashSet; 30 import java.util.Set; 31 32 /** 33 * A virtual dpad representing a key input mechanism on a remote device. 34 * 35 * <p>This registers an InputDevice that is interpreted like a physically-connected device and 36 * dispatches received events to it.</p> 37 * 38 * @hide 39 */ 40 @SystemApi 41 public class VirtualDpad extends VirtualInputDevice { 42 43 private final Set<Integer> mSupportedKeyCodes = 44 Collections.unmodifiableSet( 45 new HashSet<>( 46 Arrays.asList( 47 KeyEvent.KEYCODE_BACK, 48 KeyEvent.KEYCODE_DPAD_UP, 49 KeyEvent.KEYCODE_DPAD_DOWN, 50 KeyEvent.KEYCODE_DPAD_LEFT, 51 KeyEvent.KEYCODE_DPAD_RIGHT, 52 KeyEvent.KEYCODE_DPAD_CENTER))); 53 54 /** @hide */ VirtualDpad(IVirtualDevice virtualDevice, IBinder token)55 public VirtualDpad(IVirtualDevice virtualDevice, IBinder token) { 56 super(virtualDevice, token); 57 } 58 59 /** 60 * Sends a key event to the system. 61 * 62 * <p>Supported key codes are: 63 * <ul> 64 * <li>{@link KeyEvent.KEYCODE_DPAD_UP}</li> 65 * <li>{@link KeyEvent.KEYCODE_DPAD_DOWN}</li> 66 * <li>{@link KeyEvent.KEYCODE_DPAD_LEFT}</li> 67 * <li>{@link KeyEvent.KEYCODE_DPAD_RIGHT}</li> 68 * <li>{@link KeyEvent.KEYCODE_DPAD_CENTER}</li> 69 * <li>{@link KeyEvent.KEYCODE_BACK}</li> 70 * </ul> 71 * 72 * @param event the event to send 73 */ 74 @RequiresPermission(android.Manifest.permission.CREATE_VIRTUAL_DEVICE) sendKeyEvent(@onNull VirtualKeyEvent event)75 public void sendKeyEvent(@NonNull VirtualKeyEvent event) { 76 try { 77 if (!mSupportedKeyCodes.contains(event.getKeyCode())) { 78 throw new IllegalArgumentException( 79 "Unsupported key code " 80 + event.getKeyCode() 81 + " sent to a VirtualDpad input device."); 82 } 83 mVirtualDevice.sendDpadKeyEvent(mToken, event); 84 } catch (RemoteException e) { 85 throw e.rethrowFromSystemServer(); 86 } 87 } 88 } 89