Shubang | ec66896 | 2018-07-13 19:03:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | package com.android.server.hdmi; |
| 17 | |
| 18 | import android.hardware.tv.cec.V1_0.SendMessageResult; |
| 19 | |
| 20 | import com.android.server.hdmi.HdmiCecLocalDeviceAudioSystem.TvSystemAudioModeSupportedCallback; |
| 21 | |
| 22 | /** |
| 23 | * Feature action that detects if TV supports system audio control. |
| 24 | */ |
| 25 | public class DetectTvSystemAudioModeSupportAction extends HdmiCecFeatureAction { |
| 26 | |
| 27 | // State that waits for <Active Source> once send <Request Active Source>. |
| 28 | private static final int STATE_WAITING_FOR_FEATURE_ABORT = 1; |
| 29 | |
| 30 | private TvSystemAudioModeSupportedCallback mCallback; |
| 31 | private int mState; |
| 32 | |
| 33 | DetectTvSystemAudioModeSupportAction(HdmiCecLocalDevice source, |
| 34 | TvSystemAudioModeSupportedCallback callback) { |
| 35 | super(source); |
| 36 | mCallback = callback; |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | boolean start() { |
| 41 | mState = STATE_WAITING_FOR_FEATURE_ABORT; |
| 42 | addTimer(mState, HdmiConfig.TIMEOUT_MS); |
| 43 | sendSetSystemAudioMode(); |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | boolean processCommand(HdmiCecMessage cmd) { |
| 49 | if (cmd.getOpcode() == Constants.MESSAGE_FEATURE_ABORT) { |
| 50 | if (mState != STATE_WAITING_FOR_FEATURE_ABORT) { |
| 51 | return false; |
| 52 | } |
Amy | f4b66f6 | 2018-11-09 09:52:50 -0800 | [diff] [blame] | 53 | if ((cmd.getParams()[0] & 0xFF) == Constants.MESSAGE_SET_SYSTEM_AUDIO_MODE) { |
| 54 | finishAction(false); |
| 55 | return true; |
| 56 | } |
Shubang | ec66896 | 2018-07-13 19:03:19 -0700 | [diff] [blame] | 57 | } |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | void handleTimerEvent(int state) { |
| 63 | if (mState != state) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | switch (mState) { |
| 68 | case STATE_WAITING_FOR_FEATURE_ABORT: |
| 69 | finishAction(true); |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | protected void sendSetSystemAudioMode() { |
| 75 | sendCommand( |
| 76 | HdmiCecMessageBuilder.buildSetSystemAudioMode(getSourceAddress(),Constants.ADDR_TV, |
| 77 | true), |
| 78 | result -> { |
| 79 | if (result != SendMessageResult.SUCCESS) { |
| 80 | finishAction(false); |
| 81 | } |
| 82 | }); |
| 83 | } |
| 84 | |
| 85 | private void finishAction(boolean supported) { |
| 86 | mCallback.onResult(supported); |
| 87 | audioSystem().setTvSystemAudioModeSupport(supported); |
| 88 | finish(); |
| 89 | } |
| 90 | } |