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