SystemAudioAutoInitiationAction.java revision 5352081c662299b618335bf3024058fa04ef2dfd
1f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian/*
2f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * Copyright (C) 2014 The Android Open Source Project
3f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian *
4f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
5f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * you may not use this file except in compliance with the License.
6f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * You may obtain a copy of the License at
7f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian *
8f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
9f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian *
10f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * Unless required by applicable law or agreed to in writing, software
11f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
12f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * See the License for the specific language governing permissions and
14f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * limitations under the License.
15f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian */
16f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
17f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianpackage com.android.server.hdmi;
18f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
19f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianimport com.android.server.hdmi.HdmiControlService.SendMessageCallback;
20f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
21f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian/**
22f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * Action to initiate system audio once AVR is detected on Device discovery action.
23f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian */
24f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian// Seq #27
25f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianfinal class SystemAudioAutoInitiationAction extends HdmiCecFeatureAction {
26f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    private final int mAvrAddress;
27f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
28984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian    // State that waits for <System Audio Mode Status> once send
29984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian    // <Give System Audio Mode Status> to AV Receiver.
30984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian    private static final int STATE_WAITING_FOR_SYSTEM_AUDIO_MODE_STATUS = 1;
31f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
32f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    SystemAudioAutoInitiationAction(HdmiCecLocalDevice source, int avrAddress) {
33f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        super(source);
34f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        mAvrAddress = avrAddress;
35f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    }
36f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
37984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian    @Override
38f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    boolean start() {
39f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        mState = STATE_WAITING_FOR_SYSTEM_AUDIO_MODE_STATUS;
40984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
41f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        addTimer(mState, HdmiConfig.TIMEOUT_MS);
42f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        sendGiveSystemAudioModeStatus();
43f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        return true;
44f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    }
45f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
46f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    private void sendGiveSystemAudioModeStatus() {
47f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        sendCommand(HdmiCecMessageBuilder.buildGiveSystemAudioModeStatus(getSourceAddress(),
48f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                mAvrAddress), new SendMessageCallback() {
493301542828febc768e1df42892cfac4992c35474Mathias Agopian            @Override
503301542828febc768e1df42892cfac4992c35474Mathias Agopian            public void onSendCompleted(int error) {
513301542828febc768e1df42892cfac4992c35474Mathias Agopian                if (error != Constants.SEND_RESULT_SUCCESS) {
523301542828febc768e1df42892cfac4992c35474Mathias Agopian                    tv().setSystemAudioMode(false, true);
533301542828febc768e1df42892cfac4992c35474Mathias Agopian                    finish();
543301542828febc768e1df42892cfac4992c35474Mathias Agopian                }
553301542828febc768e1df42892cfac4992c35474Mathias Agopian            }
563301542828febc768e1df42892cfac4992c35474Mathias Agopian        });
573301542828febc768e1df42892cfac4992c35474Mathias Agopian    }
583301542828febc768e1df42892cfac4992c35474Mathias Agopian
593301542828febc768e1df42892cfac4992c35474Mathias Agopian    @Override
603301542828febc768e1df42892cfac4992c35474Mathias Agopian    boolean processCommand(HdmiCecMessage cmd) {
613301542828febc768e1df42892cfac4992c35474Mathias Agopian        if (mState != STATE_WAITING_FOR_SYSTEM_AUDIO_MODE_STATUS
623301542828febc768e1df42892cfac4992c35474Mathias Agopian                || mAvrAddress != cmd.getSource()) {
63f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            return false;
64f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        }
65f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
66f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        if (cmd.getOpcode() == Constants.MESSAGE_SYSTEM_AUDIO_MODE_STATUS) {
67            handleSystemAudioModeStatusMessage();
68            return true;
69        }
70        return false;
71    }
72
73    private void handleSystemAudioModeStatusMessage() {
74        // If the last setting is system audio, turn on system audio whatever AVR status is.
75        if (tv().getSystemAudioModeSetting()) {
76            if (canChangeSystemAudio()) {
77                addAndStartAction(new SystemAudioActionFromTv(tv(), mAvrAddress, true, null));
78            }
79        } else {
80            // If the last setting is non-system audio, turn off system audio mode
81            // and update system audio status (volume or mute).
82            tv().setSystemAudioMode(false, true);
83            if (canChangeSystemAudio()) {
84                addAndStartAction(new SystemAudioStatusAction(tv(), mAvrAddress, null));
85            }
86        }
87        finish();
88    }
89
90    @Override
91    void handleTimerEvent(int state) {
92        if (mState != state) {
93            return;
94        }
95
96        switch (mState) {
97            case STATE_WAITING_FOR_SYSTEM_AUDIO_MODE_STATUS:
98                handleSystemAudioModeStatusTimeout();
99                break;
100        }
101    }
102
103    private void handleSystemAudioModeStatusTimeout() {
104        if (tv().getSystemAudioModeSetting()) {
105            if (canChangeSystemAudio()) {
106                addAndStartAction(new SystemAudioActionFromTv(tv(), mAvrAddress, true, null));
107            }
108        } else {
109            tv().setSystemAudioMode(false, true);
110        }
111        finish();
112    }
113
114    private boolean canChangeSystemAudio() {
115        return !(tv().hasAction(SystemAudioActionFromTv.class)
116               || tv().hasAction(SystemAudioActionFromAvr.class));
117    }
118}
119