OneTouchRecordAction.java revision 5352081c662299b618335bf3024058fa04ef2dfd
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 HdmiCecFeatureAction {
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                });
73        mState = STATE_WAITING_FOR_RECORD_STATUS;
74        addTimer(mState, RECORD_STATUS_TIMEOUT_MS);
75    }
76
77    @Override
78    boolean processCommand(HdmiCecMessage cmd) {
79        if (mState != STATE_WAITING_FOR_RECORD_STATUS || mRecorderAddress != cmd.getSource()) {
80            return false;
81        }
82
83        switch (cmd.getOpcode()) {
84            case Constants.MESSAGE_RECORD_STATUS:
85                return handleRecordStatus(cmd);
86        }
87        return false;
88    }
89
90    private boolean handleRecordStatus(HdmiCecMessage cmd) {
91        // Only handle message coming from original recorder.
92        if (cmd.getSource() != mRecorderAddress) {
93            return false;
94        }
95
96        int recordStatus = cmd.getParams()[0];
97        tv().announceOneTouchRecordResult(recordStatus);
98        Slog.i(TAG, "Got record status:" + recordStatus + " from " + cmd.getSource());
99
100        // If recording started successfully, change state and keep this action until <Record Off>
101        // received. Otherwise, finish action.
102        switch (recordStatus) {
103            case ONE_TOUCH_RECORD_RECORDING_CURRENTLY_SELECTED_SOURCE:
104            case ONE_TOUCH_RECORD_RECORDING_DIGITAL_SERVICE:
105            case ONE_TOUCH_RECORD_RECORDING_ANALOGUE_SERVICE:
106            case ONE_TOUCH_RECORD_RECORDING_EXTERNAL_INPUT:
107                mState = STATE_RECORDING_IN_PROGRESS;
108                mActionTimer.clearTimerMessage();
109                break;
110            default:
111                finish();
112                break;
113        }
114        return true;
115    }
116
117    @Override
118    void handleTimerEvent(int state) {
119        if (mState != state) {
120            Slog.w(TAG, "Timeout in invalid state:[Expected:" + mState + ", Actual:" + state + "]");
121            return;
122        }
123
124        tv().announceOneTouchRecordResult(ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION);
125        finish();
126    }
127
128    int getRecorderAddress() {
129        return mRecorderAddress;
130    }
131}
132