OneTouchRecordAction.java revision 12e5dcefe136b58562f39604e6a8460ac92cb895
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 */
16
17package com.android.server.hdmi;
18
19import static android.hardware.hdmi.HdmiControlManager.ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION;
20import static android.hardware.hdmi.HdmiControlManager.ONE_TOUCH_RECORD_RECORDING_ANALOGUE_SERVICE;
21import static android.hardware.hdmi.HdmiControlManager.ONE_TOUCH_RECORD_RECORDING_CURRENTLY_SELECTED_SOURCE;
22import static android.hardware.hdmi.HdmiControlManager.ONE_TOUCH_RECORD_RECORDING_DIGITAL_SERVICE;
23import static android.hardware.hdmi.HdmiControlManager.ONE_TOUCH_RECORD_RECORDING_EXTERNAL_INPUT;
24
25import android.util.Slog;
26
27import com.android.server.hdmi.HdmiControlService.SendMessageCallback;
28
29/**
30 * Feature action that performs one touch record.
31 */
32public class OneTouchRecordAction extends FeatureAction {
33    private static final String TAG = "OneTouchRecordAction";
34
35    // Timer out for waiting <Record Status> 120s
36    private static final int RECORD_STATUS_TIMEOUT_MS = 120000;
37
38    // State that waits for <Record Status> once sending <Record On>
39    private static final int STATE_WAITING_FOR_RECORD_STATUS = 1;
40    // State that describes recording in progress.
41    private static final int STATE_RECORDING_IN_PROGRESS = 2;
42
43    private final int mRecorderAddress;
44    private final byte[] mRecordSource;
45
46    OneTouchRecordAction(HdmiCecLocalDevice source, int recorderAddress, byte[] recordSource) {
47        super(source);
48        mRecorderAddress = recorderAddress;
49        mRecordSource = recordSource;
50    }
51
52    @Override
53    boolean start() {
54        sendRecordOn();
55        return true;
56    }
57
58    private void sendRecordOn() {
59        sendCommand(HdmiCecMessageBuilder.buildRecordOn(getSourceAddress(), mRecorderAddress,
60                mRecordSource),
61                new SendMessageCallback() {
62                @Override
63                    public void onSendCompleted(int error) {
64                        // if failed to send <Record On>, display error message and finish action.
65                        if (error != Constants.SEND_RESULT_SUCCESS) {
66                            tv().announceOneTouchRecordResult(
67                                    ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION);
68                            finish();
69                            return;
70                        }
71
72                        mState = STATE_WAITING_FOR_RECORD_STATUS;
73                        addTimer(mState, RECORD_STATUS_TIMEOUT_MS);
74                    }
75                });
76    }
77
78    @Override
79    boolean processCommand(HdmiCecMessage cmd) {
80        if (mState != STATE_WAITING_FOR_RECORD_STATUS) {
81            return false;
82        }
83
84        switch (cmd.getOpcode()) {
85            case Constants.MESSAGE_RECORD_STATUS:
86                return handleRecordStatus(cmd);
87
88        }
89        return false;
90    }
91
92    private boolean handleRecordStatus(HdmiCecMessage cmd) {
93        // Only handle message coming from original recorder.
94        if (cmd.getSource() != mRecorderAddress) {
95            return false;
96        }
97
98        int recordStatus = cmd.getParams()[0];
99        tv().announceOneTouchRecordResult(recordStatus);
100        Slog.i(TAG, "Got record status:" + recordStatus + " from " + cmd.getSource());
101
102        // If recording started successfully, change state and keep this action until <Record Off>
103        // received. Otherwise, finish action.
104        switch (recordStatus) {
105            case ONE_TOUCH_RECORD_RECORDING_CURRENTLY_SELECTED_SOURCE:
106            case ONE_TOUCH_RECORD_RECORDING_DIGITAL_SERVICE:
107            case ONE_TOUCH_RECORD_RECORDING_ANALOGUE_SERVICE:
108            case ONE_TOUCH_RECORD_RECORDING_EXTERNAL_INPUT:
109                mState = STATE_RECORDING_IN_PROGRESS;
110                mActionTimer.clearTimerMessage();
111                break;
112            default:
113                finish();
114                break;
115        }
116        return true;
117    }
118
119    @Override
120    void handleTimerEvent(int state) {
121        if (mState != state) {
122            Slog.w(TAG, "Timeout in invalid state:[Expected:" + mState + ", Actual:" + state + "]");
123            return;
124        }
125
126        tv().announceOneTouchRecordResult(ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION);
127        finish();
128    }
129
130    int getRecorderAddress() {
131        return mRecorderAddress;
132    }
133}
134