blob: 512d5378a64a249d14029b0ab6e4e43cdf84d3c5 [file] [log] [blame]
Jungshik Jang187d0172014-06-17 17:48:42 +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 Jang187d0172014-06-17 17:48:42 +090019import com.android.server.hdmi.HdmiControlService.SendMessageCallback;
20
21/**
22 * Action to initiate system audio once AVR is detected on Device discovery action.
23 */
Yuncheol Heoc516d652014-07-11 18:23:24 +090024// Seq #27
Jungshik Jangb509c2e2014-08-07 13:45:01 +090025final class SystemAudioAutoInitiationAction extends HdmiCecFeatureAction {
Jungshik Jang187d0172014-06-17 17:48:42 +090026 private final int mAvrAddress;
27
28 // State that waits for <System Audio Mode Status> once send
29 // <Give System Audio Mode Status> to AV Receiver.
30 private static final int STATE_WAITING_FOR_SYSTEM_AUDIO_MODE_STATUS = 1;
31
32 SystemAudioAutoInitiationAction(HdmiCecLocalDevice source, int avrAddress) {
33 super(source);
34 mAvrAddress = avrAddress;
35 }
36
37 @Override
38 boolean start() {
39 mState = STATE_WAITING_FOR_SYSTEM_AUDIO_MODE_STATUS;
40
Jinsuk Kim5fba96d2014-07-11 11:51:34 +090041 addTimer(mState, HdmiConfig.TIMEOUT_MS);
Jungshik Jang187d0172014-06-17 17:48:42 +090042 sendGiveSystemAudioModeStatus();
43 return true;
44 }
45
46 private void sendGiveSystemAudioModeStatus() {
47 sendCommand(HdmiCecMessageBuilder.buildGiveSystemAudioModeStatus(getSourceAddress(),
48 mAvrAddress), new SendMessageCallback() {
49 @Override
50 public void onSendCompleted(int error) {
Jinsuk Kimc0c20d02014-07-04 14:34:31 +090051 if (error != Constants.SEND_RESULT_SUCCESS) {
Jinsuk Kim7ecfbae2014-07-11 14:16:29 +090052 tv().setSystemAudioMode(false, true);
Jungshik Jang187d0172014-06-17 17:48:42 +090053 finish();
54 }
55 }
56 });
57 }
58
59 @Override
60 boolean processCommand(HdmiCecMessage cmd) {
Jungshik Jang5352081c2014-09-22 15:14:49 +090061 if (mState != STATE_WAITING_FOR_SYSTEM_AUDIO_MODE_STATUS
62 || mAvrAddress != cmd.getSource()) {
Jungshik Jang187d0172014-06-17 17:48:42 +090063 return false;
64 }
65
Jungshik Jang5352081c2014-09-22 15:14:49 +090066 if (cmd.getOpcode() == Constants.MESSAGE_SYSTEM_AUDIO_MODE_STATUS) {
67 handleSystemAudioModeStatusMessage();
68 return true;
Jungshik Jang187d0172014-06-17 17:48:42 +090069 }
Jungshik Jang5352081c2014-09-22 15:14:49 +090070 return false;
Jungshik Jang187d0172014-06-17 17:48:42 +090071 }
72
73 private void handleSystemAudioModeStatusMessage() {
Jungshik Jang7fb8e7f2014-11-06 14:03:15 +090074 if (!canChangeSystemAudio()) {
75 HdmiLogger.debug("Cannot change system audio mode in auto initiation action.");
76 finish();
77 return;
Jungshik Jang187d0172014-06-17 17:48:42 +090078 }
Jungshik Jang7fb8e7f2014-11-06 14:03:15 +090079
80 boolean systemAudioModeSetting = tv().getSystemAudioModeSetting();
81 // Update AVR's system audio mode regardless of AVR's status.
82 addAndStartAction(new SystemAudioActionFromTv(tv(), mAvrAddress, systemAudioModeSetting,
83 null));
Jungshik Jang187d0172014-06-17 17:48:42 +090084 finish();
85 }
86
87 @Override
88 void handleTimerEvent(int state) {
89 if (mState != state) {
90 return;
91 }
92
93 switch (mState) {
94 case STATE_WAITING_FOR_SYSTEM_AUDIO_MODE_STATUS:
95 handleSystemAudioModeStatusTimeout();
96 break;
97 }
98 }
99
100 private void handleSystemAudioModeStatusTimeout() {
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900101 if (tv().getSystemAudioModeSetting()) {
Jungshik Jang187d0172014-06-17 17:48:42 +0900102 if (canChangeSystemAudio()) {
Jungshik Jang7f0a1c52014-06-23 16:00:07 +0900103 addAndStartAction(new SystemAudioActionFromTv(tv(), mAvrAddress, true, null));
Jungshik Jang187d0172014-06-17 17:48:42 +0900104 }
105 } else {
Jinsuk Kim7ecfbae2014-07-11 14:16:29 +0900106 tv().setSystemAudioMode(false, true);
Jungshik Jang187d0172014-06-17 17:48:42 +0900107 }
108 finish();
109 }
110
111 private boolean canChangeSystemAudio() {
Jungshik Janga858d222014-06-23 17:17:47 +0900112 return !(tv().hasAction(SystemAudioActionFromTv.class)
113 || tv().hasAction(SystemAudioActionFromAvr.class));
Jungshik Jang187d0172014-06-17 17:48:42 +0900114 }
115}