SendKeyAction.java revision 250cda56d2fdd75a269843be1f5f2776b2eaed1d
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        // finish action for non-repeatable key.
66        if (!HdmiCecKeycode.isRepeatableKey(mLastKeycode)) {
67            sendKeyUp();
68            finish();
69            return true;
70        }
71        mState = STATE_PROCESSING_KEYCODE;
72        addTimer(mState, IRT_MS);
73        return true;
74    }
75
76    /**
77     * Called when a key event should be handled for the action.
78     *
79     * @param keycode key code of {@link KeyEvent} object
80     * @param isPressed true if the key event is of {@link KeyEvent#ACTION_DOWN}
81     */
82    void processKeyEvent(int keycode, boolean isPressed) {
83        if (mState != STATE_PROCESSING_KEYCODE) {
84            Slog.w(TAG, "Not in a valid state");
85            return;
86        }
87        // A new key press event that comes in with a key code different from the last
88        // one sets becomes a new key code to be used for press-and-hold operation.
89        // Removes any pending timer and starts a new timer for itself.
90        // Key release event indicates that the action shall be finished. Send UCR
91        // command and terminate the action. Other release events are ignored.
92        if (isPressed) {
93            if (keycode != mLastKeycode) {
94                if (!HdmiCecKeycode.isRepeatableKey(keycode)) {
95                    sendKeyUp();
96                    finish();
97                    return;
98                }
99                mActionTimer.clearTimerMessage();
100                sendKeyDown(keycode);
101                addTimer(mState, IRT_MS);
102                mLastKeycode = keycode;
103            }
104        } else {
105            if (keycode == mLastKeycode) {
106                sendKeyUp();
107                finish();
108            }
109        }
110    }
111
112    private void sendKeyDown(int keycode) {
113        byte[] keycodeAndParam = getCecKeycodeAndParam(keycode);
114        if (keycodeAndParam == null) {
115            return;
116        }
117        sendCommand(HdmiCecMessageBuilder.buildUserControlPressed(getSourceAddress(),
118                mTargetAddress, keycodeAndParam));
119    }
120
121    private void sendKeyUp() {
122        sendCommand(HdmiCecMessageBuilder.buildUserControlReleased(getSourceAddress(),
123                mTargetAddress));
124    }
125
126    @Override
127    public boolean processCommand(HdmiCecMessage cmd) {
128        // Send key action doesn't need any incoming CEC command, hence does not consume it.
129        return false;
130    }
131
132    @Override
133    public void handleTimerEvent(int state) {
134        // Timer event occurs every IRT_MS milliseconds to perform key-repeat (or press-and-hold)
135        // operation. If the last received key code is as same as the one with which the action
136        // is started, plus there was no key release event in last IRT_MS timeframe, send a UCP
137        // command and start another timer to schedule the next press-and-hold command.
138        if (mState != STATE_PROCESSING_KEYCODE) {
139            Slog.w(TAG, "Not in a valid state");
140            return;
141        }
142        sendKeyDown(mLastKeycode);
143        addTimer(mState, IRT_MS);
144    }
145
146    // Converts the Android key code to corresponding CEC key code definition. Those CEC keys
147    // with additional parameters should be mapped from individual Android key code. 'Select
148    // Broadcast' with the parameter 'cable', for instance, shall have its counterpart such as
149    // KeyEvent.KEYCODE_TV_BROADCAST_CABLE.
150    // The return byte array contains both UI command (keycode) and optional parameter.
151    private byte[] getCecKeycodeAndParam(int keycode) {
152        return HdmiCecKeycode.androidKeyToCecKey(keycode);
153    }
154}
155