SendKeyAction.java revision 8566be3c51d97d9a4b62bfec52b1c88ade65dd43
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.HdmiConfig.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 HdmiCecFeatureAction {
36    private static final String TAG = "SendKeyAction";
37
38    // Amount of time this action waits for a new release key input event. When timed out,
39    // the action sends out UCR and finishes its lifecycle. Used to deal with missing key release
40    // event, which can lead the device on the receiving end to generating unintended key repeats.
41    private static final int AWAIT_RELEASE_KEY_MS = 1000;
42
43    // State in which the action is at work. The state is set in {@link #start()} and
44    // persists throughout the process till it is set back to {@code STATE_NONE} at the end.
45    private static final int STATE_PROCESSING_KEYCODE = 1;
46
47    // Logical address of the device to which the UCP/UCP commands are sent.
48    private final int mTargetAddress;
49
50    // The key code of the last key press event the action is passed via processKeyEvent.
51    private int mLastKeycode;
52
53    // The time stamp when the last CEC key command was sent. Used to determine the press-and-hold
54    // operation.
55    private long mLastSendKeyTime;
56
57    /**
58     * Constructor.
59     *
60     * @param source {@link HdmiCecLocalDevice} instance
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(HdmiCecLocalDevice source, int targetAddress, int keycode) {
65        super(source);
66        mTargetAddress = targetAddress;
67        mLastKeycode = keycode;
68    }
69
70    @Override
71    public boolean start() {
72        sendKeyDown(mLastKeycode);
73        mLastSendKeyTime = getCurrentTime();
74        // finish action for non-repeatable key.
75        if (!HdmiCecKeycode.isRepeatableKey(mLastKeycode)) {
76            sendKeyUp();
77            finish();
78            return true;
79        }
80        mState = STATE_PROCESSING_KEYCODE;
81        addTimer(mState, AWAIT_RELEASE_KEY_MS);
82        return true;
83    }
84
85    private long getCurrentTime() {
86        return System.currentTimeMillis();
87    }
88
89    /**
90     * Called when a key event should be handled for the action.
91     *
92     * @param keycode key code of {@link KeyEvent} object
93     * @param isPressed true if the key event is of {@link KeyEvent#ACTION_DOWN}
94     */
95    void processKeyEvent(int keycode, boolean isPressed) {
96        if (mState != STATE_PROCESSING_KEYCODE) {
97            Slog.w(TAG, "Not in a valid state");
98            return;
99        }
100        if (isPressed) {
101            // A new key press event that comes in with a key code different from the last
102            // one becomes a new key code to be used for press-and-hold operation.
103            if (keycode != mLastKeycode) {
104                sendKeyDown(keycode);
105                mLastSendKeyTime = getCurrentTime();
106                if (!HdmiCecKeycode.isRepeatableKey(keycode)) {
107                    sendKeyUp();
108                    finish();
109                    return;
110                }
111            } else {
112                // Press-and-hold key transmission takes place if Android key inputs are
113                // repeatedly coming in and more than IRT_MS has passed since the last
114                // press-and-hold key transmission.
115                if (getCurrentTime() - mLastSendKeyTime >= IRT_MS) {
116                    sendKeyDown(keycode);
117                    mLastSendKeyTime = getCurrentTime();
118                }
119            }
120            mActionTimer.clearTimerMessage();
121            addTimer(mState, AWAIT_RELEASE_KEY_MS);
122            mLastKeycode = keycode;
123        } else {
124            // Key release event indicates that the action shall be finished. Send UCR
125            // command and terminate the action. Other release events are ignored.
126            if (keycode == mLastKeycode) {
127                sendKeyUp();
128                finish();
129            }
130        }
131    }
132
133    private void sendKeyDown(int keycode) {
134        byte[] cecKeycodeAndParams = HdmiCecKeycode.androidKeyToCecKey(keycode);
135        if (cecKeycodeAndParams == null) {
136            return;
137        }
138        sendCommand(HdmiCecMessageBuilder.buildUserControlPressed(getSourceAddress(),
139                mTargetAddress, cecKeycodeAndParams));
140    }
141
142    private void sendKeyUp() {
143        sendCommand(HdmiCecMessageBuilder.buildUserControlReleased(getSourceAddress(),
144                mTargetAddress));
145    }
146
147    @Override
148    public boolean processCommand(HdmiCecMessage cmd) {
149        // Send key action doesn't need any incoming CEC command, hence does not consume it.
150        return false;
151    }
152
153    @Override
154    public void handleTimerEvent(int state) {
155        // Timeout on waiting for the release key event. Send UCR and quit the action.
156        if (mState != STATE_PROCESSING_KEYCODE) {
157            Slog.w(TAG, "Not in a valid state");
158            return;
159        }
160        sendKeyUp();
161        finish();
162    }
163}
164