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