blob: d80b81f6fe4a3aeee91e7c711905c1b8c651dc58 [file] [log] [blame]
Jungshik Jang3dcdd712014-07-07 13:44:07 +09001/*
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
Jungshik Jang12e5dce2014-07-24 15:27:44 +090019import 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;
Jungshik Jangb6591b82014-07-23 16:10:23 +090024
25import android.util.Slog;
26
27import com.android.server.hdmi.HdmiControlService.SendMessageCallback;
Jungshik Janga6b2a7a2014-07-16 18:04:49 +090028
Jungshik Jang3dcdd712014-07-07 13:44:07 +090029/**
Jungshik Jang12e5dce2014-07-24 15:27:44 +090030 * Feature action that performs one touch record.
Jungshik Jang3dcdd712014-07-07 13:44:07 +090031 */
Jungshik Jangb509c2e2014-08-07 13:45:01 +090032public class OneTouchRecordAction extends HdmiCecFeatureAction {
Jungshik Jangb6591b82014-07-23 16:10:23 +090033 private static final String TAG = "OneTouchRecordAction";
Jungshik Jang3dcdd712014-07-07 13:44:07 +090034
Jungshik Jang12e5dce2014-07-24 15:27:44 +090035 // Timer out for waiting <Record Status> 120s
36 private static final int RECORD_STATUS_TIMEOUT_MS = 120000;
Jungshik Jangb6591b82014-07-23 16:10:23 +090037
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) {
Jungshik Jang3dcdd712014-07-07 13:44:07 +090047 super(source);
48 mRecorderAddress = recorderAddress;
Jungshik Jangb6591b82014-07-23 16:10:23 +090049 mRecordSource = recordSource;
Jungshik Jang3dcdd712014-07-07 13:44:07 +090050 }
51
52 @Override
53 boolean start() {
Jungshik Jangb6591b82014-07-23 16:10:23 +090054 sendRecordOn();
55 return true;
Jungshik Jang3dcdd712014-07-07 13:44:07 +090056 }
57
Jungshik Jangb6591b82014-07-23 16:10:23 +090058 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) {
Jungshik Jang12e5dce2014-07-24 15:27:44 +090066 tv().announceOneTouchRecordResult(
Jungshik Jang326aef02014-11-05 12:50:35 +090067 mRecorderAddress,
Jungshik Jang12e5dce2014-07-24 15:27:44 +090068 ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION);
Jungshik Jangb6591b82014-07-23 16:10:23 +090069 finish();
70 return;
71 }
Jungshik Jangb6591b82014-07-23 16:10:23 +090072 }
73 });
Jungshik Jang5352081c2014-09-22 15:14:49 +090074 mState = STATE_WAITING_FOR_RECORD_STATUS;
75 addTimer(mState, RECORD_STATUS_TIMEOUT_MS);
Jungshik Janga6b2a7a2014-07-16 18:04:49 +090076 }
77
Jungshik Jang3dcdd712014-07-07 13:44:07 +090078 @Override
79 boolean processCommand(HdmiCecMessage cmd) {
Jungshik Jang5352081c2014-09-22 15:14:49 +090080 if (mState != STATE_WAITING_FOR_RECORD_STATUS || mRecorderAddress != cmd.getSource()) {
Jungshik Jangb6591b82014-07-23 16:10:23 +090081 return false;
82 }
83
84 switch (cmd.getOpcode()) {
85 case Constants.MESSAGE_RECORD_STATUS:
86 return handleRecordStatus(cmd);
Jungshik Jangb6591b82014-07-23 16:10:23 +090087 }
Jungshik Jang3dcdd712014-07-07 13:44:07 +090088 return false;
89 }
90
Jungshik Jangb6591b82014-07-23 16:10:23 +090091 private boolean handleRecordStatus(HdmiCecMessage cmd) {
92 // Only handle message coming from original recorder.
93 if (cmd.getSource() != mRecorderAddress) {
94 return false;
95 }
96
97 int recordStatus = cmd.getParams()[0];
Jungshik Jang326aef02014-11-05 12:50:35 +090098 tv().announceOneTouchRecordResult(mRecorderAddress, recordStatus);
Jungshik Jangb6591b82014-07-23 16:10:23 +090099 Slog.i(TAG, "Got record status:" + recordStatus + " from " + cmd.getSource());
100
Jungshik Jangb6591b82014-07-23 16:10:23 +0900101 // If recording started successfully, change state and keep this action until <Record Off>
102 // received. Otherwise, finish action.
Jungshik Jang12e5dce2014-07-24 15:27:44 +0900103 switch (recordStatus) {
104 case ONE_TOUCH_RECORD_RECORDING_CURRENTLY_SELECTED_SOURCE:
105 case ONE_TOUCH_RECORD_RECORDING_DIGITAL_SERVICE:
106 case ONE_TOUCH_RECORD_RECORDING_ANALOGUE_SERVICE:
107 case ONE_TOUCH_RECORD_RECORDING_EXTERNAL_INPUT:
Jungshik Jangb6591b82014-07-23 16:10:23 +0900108 mState = STATE_RECORDING_IN_PROGRESS;
109 mActionTimer.clearTimerMessage();
110 break;
111 default:
112 finish();
113 break;
114 }
115 return true;
116 }
117
Jungshik Jang3dcdd712014-07-07 13:44:07 +0900118 @Override
119 void handleTimerEvent(int state) {
Jungshik Jangb6591b82014-07-23 16:10:23 +0900120 if (mState != state) {
121 Slog.w(TAG, "Timeout in invalid state:[Expected:" + mState + ", Actual:" + state + "]");
122 return;
123 }
124
Jungshik Jang326aef02014-11-05 12:50:35 +0900125 tv().announceOneTouchRecordResult(mRecorderAddress,
126 ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION);
Jungshik Jangb6591b82014-07-23 16:10:23 +0900127 finish();
Jungshik Jang3dcdd712014-07-07 13:44:07 +0900128 }
129
130 int getRecorderAddress() {
131 return mRecorderAddress;
132 }
133}