SendKeyAction.java revision cd97baf4d08f9a23797eb6a8eb0d57bba1086bec
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 android.hardware.hdmi.HdmiCecMessage; 19import android.view.KeyEvent; 20import android.util.Slog; 21 22import libcore.util.EmptyArray; 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 // IRT(Initiator Repetition Time) in millisecond as recommended in the standard. 44 // Outgoing UCP commands, when in 'Press and Hold' mode, should be this much apart 45 // from the adjacent one so as not to place unnecessarily heavy load on the CEC line. 46 // TODO: This value might need tweaking per product basis. Consider putting it 47 // in config.xml to allow customization. 48 private static final int IRT_MS = 450; 49 50 // Logical address of the device to which the UCP/UCP commands are sent. 51 private final int mTargetAddress; 52 53 // The key code of the last key press event the action is passed via processKeyEvent. 54 private int mLastKeyCode; 55 56 /** 57 * Constructor. 58 * 59 * @param service {@link HdmiControlService} instance 60 * @param sourceAddress logical address to be used as source address 61 * @param targetAddress logical address of the device to send the keys to 62 * @param keyCode remote control key code as defined in {@link KeyEvent} 63 */ 64 SendKeyAction(HdmiControlService service, int sourceAddress, int targetAddress, int keyCode) { 65 super(service, sourceAddress); 66 mTargetAddress = targetAddress; 67 mLastKeyCode = keyCode; 68 } 69 70 @Override 71 public boolean start() { 72 sendKeyDown(mLastKeyCode); 73 mState = STATE_PROCESSING_KEYCODE; 74 addTimer(mState, IRT_MS); 75 return true; 76 } 77 78 /** 79 * Called when a key event should be handled for the action. 80 * 81 * @param keyCode key code of {@link KeyEvent} object 82 * @param isPressed true if the key event is of {@link KeyEvent#ACTION_DOWN} 83 * @param param additional parameter that comes with the key event 84 */ 85 void processKeyEvent(int keyCode, boolean isPressed) { 86 if (mState != STATE_PROCESSING_KEYCODE) { 87 Slog.w(TAG, "Not in a valid state"); 88 return; 89 } 90 // A new key press event that comes in with a key code different from the last 91 // one sets becomes a new key code to be used for press-and-hold operation. 92 // Removes any pending timer and starts a new timer for itself. 93 // Key release event indicates that the action shall be finished. Send UCR 94 // command and terminate the action. Other release events are ignored. 95 if (isPressed) { 96 if (keyCode != mLastKeyCode) { 97 mActionTimer.clearTimerMessage(); 98 sendKeyDown(keyCode); 99 addTimer(mState, IRT_MS); 100 mLastKeyCode = keyCode; 101 } 102 } else { 103 if (keyCode == mLastKeyCode) { 104 sendKeyUp(); 105 finish(); 106 } 107 } 108 } 109 110 private void sendKeyDown(int keyCode) { 111 byte[] keyCodeAndParam = getCecKeyCodeAndParam(keyCode); 112 if (keyCodeAndParam == null) { 113 return; 114 } 115 sendCommand(HdmiCecMessageBuilder.buildUserControlPressed(mSourceAddress, mTargetAddress, 116 keyCodeAndParam)); 117 } 118 119 private void sendKeyUp() { 120 sendCommand(HdmiCecMessageBuilder.buildUserControlReleased(mSourceAddress, mTargetAddress)); 121 } 122 123 @Override 124 public boolean processCommand(HdmiCecMessage cmd) { 125 // Send key action doesn't need any incoming CEC command, hence does not consume it. 126 return false; 127 } 128 129 @Override 130 public void handleTimerEvent(int state) { 131 // Timer event occurs every IRT_MS milliseconds to perform key-repeat (or press-and-hold) 132 // operation. If the last received key code is as same as the one with which the action 133 // is started, plus there was no key release event in last IRT_MS timeframe, send a UCP 134 // command and start another timer to schedule the next press-and-hold command. 135 if (mState != STATE_PROCESSING_KEYCODE) { 136 Slog.w(TAG, "Not in a valid state"); 137 return; 138 } 139 sendKeyDown(mLastKeyCode); 140 addTimer(mState, IRT_MS); 141 } 142 143 // Converts the Android key code to corresponding CEC key code definition. Those CEC keys 144 // with additional parameters should be mapped from individual Android key code. 'Select 145 // Broadcast' with the parameter 'cable', for instance, shall have its counterpart such as 146 // KeyEvent.KEYCODE_TV_BROADCAST_CABLE. 147 // The return byte array contains both UI command (keycode) and optional parameter. 148 private byte[] getCecKeyCodeAndParam(int keyCode) { 149 // TODO: Convert to CEC keycode and (optionally) parameter. 150 // return androidKeyToCecKey(keyCode); 151 return EmptyArray.BYTE; 152 } 153} 154