SendKeyAction.java revision 8fa36b110be29d92a9aba070fa4666eefb14b584
1/* 2 * Copyright (C) 2014 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 */ 16package com.android.server.hdmi; 17 18import static com.android.server.hdmi.HdmiConstants.IRT_MS; 19 20import android.hardware.hdmi.HdmiCecMessage; 21import android.util.Slog; 22import android.view.KeyEvent; 23 24/** 25 * Feature action that transmits remote control key command (User Control Press/ 26 * User Control Release) to CEC bus. 27 * 28 * <p>This action is created when a new key event is passed to CEC service. It optionally 29 * does key repeat (a.k.a. press-and-hold) operation until it receives a key release event. 30 * If another key press event is received before the key in use is released, CEC service 31 * does not create a new action but recycles the current one by updating the key used 32 * for press-and-hold operation. 33 * 34 * <p>Package-private, accessed by {@link HdmiControlService} only. 35 */ 36final class SendKeyAction extends FeatureAction { 37 private static final String TAG = "SendKeyAction"; 38 39 // State in which the action is at work. The state is set in {@link #start()} and 40 // persists throughout the process till it is set back to {@code STATE_NONE} at the end. 41 private static final int STATE_PROCESSING_KEYCODE = 1; 42 43 // Logical address of the device to which the UCP/UCP commands are sent. 44 private final int mTargetAddress; 45 46 // The key code of the last key press event the action is passed via processKeyEvent. 47 private int mLastKeyCode; 48 49 /** 50 * Constructor. 51 * 52 * @param source {@link HdmiCecLocalDevice} instance 53 * @param targetAddress logical address of the device to send the keys to 54 * @param keyCode remote control key code as defined in {@link KeyEvent} 55 */ 56 SendKeyAction(HdmiCecLocalDevice source, int targetAddress, int keyCode) { 57 super(source); 58 mTargetAddress = targetAddress; 59 mLastKeyCode = keyCode; 60 } 61 62 @Override 63 public boolean start() { 64 sendKeyDown(mLastKeyCode); 65 mState = STATE_PROCESSING_KEYCODE; 66 addTimer(mState, IRT_MS); 67 return true; 68 } 69 70 /** 71 * Called when a key event should be handled for the action. 72 * 73 * @param keyCode key code of {@link KeyEvent} object 74 * @param isPressed true if the key event is of {@link KeyEvent#ACTION_DOWN} 75 */ 76 void processKeyEvent(int keyCode, boolean isPressed) { 77 if (mState != STATE_PROCESSING_KEYCODE) { 78 Slog.w(TAG, "Not in a valid state"); 79 return; 80 } 81 // A new key press event that comes in with a key code different from the last 82 // one sets becomes a new key code to be used for press-and-hold operation. 83 // Removes any pending timer and starts a new timer for itself. 84 // Key release event indicates that the action shall be finished. Send UCR 85 // command and terminate the action. Other release events are ignored. 86 if (isPressed) { 87 if (keyCode != mLastKeyCode) { 88 mActionTimer.clearTimerMessage(); 89 sendKeyDown(keyCode); 90 addTimer(mState, IRT_MS); 91 mLastKeyCode = keyCode; 92 } 93 } else { 94 if (keyCode == mLastKeyCode) { 95 sendKeyUp(); 96 finish(); 97 } 98 } 99 } 100 101 private void sendKeyDown(int keyCode) { 102 byte[] keyCodeAndParam = getCecKeyCodeAndParam(keyCode); 103 if (keyCodeAndParam == null) { 104 return; 105 } 106 sendCommand(HdmiCecMessageBuilder.buildUserControlPressed(getSourceAddress(), 107 mTargetAddress, keyCodeAndParam)); 108 } 109 110 private void sendKeyUp() { 111 sendCommand(HdmiCecMessageBuilder.buildUserControlReleased(getSourceAddress(), 112 mTargetAddress)); 113 } 114 115 @Override 116 public boolean processCommand(HdmiCecMessage cmd) { 117 // Send key action doesn't need any incoming CEC command, hence does not consume it. 118 return false; 119 } 120 121 @Override 122 public void handleTimerEvent(int state) { 123 // Timer event occurs every IRT_MS milliseconds to perform key-repeat (or press-and-hold) 124 // operation. If the last received key code is as same as the one with which the action 125 // is started, plus there was no key release event in last IRT_MS timeframe, send a UCP 126 // command and start another timer to schedule the next press-and-hold command. 127 if (mState != STATE_PROCESSING_KEYCODE) { 128 Slog.w(TAG, "Not in a valid state"); 129 return; 130 } 131 sendKeyDown(mLastKeyCode); 132 addTimer(mState, IRT_MS); 133 } 134 135 // Converts the Android key code to corresponding CEC key code definition. Those CEC keys 136 // with additional parameters should be mapped from individual Android key code. 'Select 137 // Broadcast' with the parameter 'cable', for instance, shall have its counterpart such as 138 // KeyEvent.KEYCODE_TV_BROADCAST_CABLE. 139 // The return byte array contains both UI command (keycode) and optional parameter. 140 private byte[] getCecKeyCodeAndParam(int keyCode) { 141 return HdmiCecKeycode.androidKeyToCecKey(keyCode); 142 } 143} 144