SystemAudioAction.java revision 5fba96df30b6b50b3cb9fe1d783320b1cc3bd6ea
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 android.annotation.Nullable; 20import android.hardware.hdmi.HdmiCecDeviceInfo; 21import android.hardware.hdmi.HdmiControlManager; 22import android.hardware.hdmi.IHdmiControlCallback; 23import android.os.RemoteException; 24import android.util.Slog; 25 26/** 27 * Base feature action class for SystemAudioActionFromTv and SystemAudioActionFromAvr. 28 */ 29abstract class SystemAudioAction extends FeatureAction { 30 private static final String TAG = "SystemAudioAction"; 31 32 // State in which waits for <SetSystemAudioMode>. 33 private static final int STATE_WAIT_FOR_SET_SYSTEM_AUDIO_MODE = 1; 34 35 private static final int MAX_SEND_RETRY_COUNT = 2; 36 37 private static final int ON_TIMEOUT_MS = 5000; 38 private static final int OFF_TIMEOUT_MS = HdmiConfig.TIMEOUT_MS; 39 40 // Logical address of AV Receiver. 41 protected final int mAvrLogicalAddress; 42 43 // The target audio status of the action, whether to enable the system audio mode or not. 44 protected boolean mTargetAudioStatus; 45 46 @Nullable private final IHdmiControlCallback mCallback; 47 48 private int mSendRetryCount = 0; 49 50 /** 51 * Constructor 52 * 53 * @param source {@link HdmiCecLocalDevice} instance 54 * @param avrAddress logical address of AVR device 55 * @param targetStatus Whether to enable the system audio mode or not 56 * @param callback callback interface to be notified when it's done 57 * @throw IllegalArugmentException if device type of sourceAddress and avrAddress is invalid 58 */ 59 SystemAudioAction(HdmiCecLocalDevice source, int avrAddress, boolean targetStatus, 60 IHdmiControlCallback callback) { 61 super(source); 62 HdmiUtils.verifyAddressType(avrAddress, HdmiCecDeviceInfo.DEVICE_AUDIO_SYSTEM); 63 mAvrLogicalAddress = avrAddress; 64 mTargetAudioStatus = targetStatus; 65 mCallback = callback; 66 } 67 68 protected void sendSystemAudioModeRequest() { 69 int avrPhysicalAddress = tv().getAvrDeviceInfo().getPhysicalAddress(); 70 HdmiCecMessage command = HdmiCecMessageBuilder.buildSystemAudioModeRequest( 71 getSourceAddress(), 72 mAvrLogicalAddress, avrPhysicalAddress, mTargetAudioStatus); 73 sendCommand(command, new HdmiControlService.SendMessageCallback() { 74 @Override 75 public void onSendCompleted(int error) { 76 if (error == Constants.SEND_RESULT_SUCCESS) { 77 mState = STATE_WAIT_FOR_SET_SYSTEM_AUDIO_MODE; 78 addTimer(mState, mTargetAudioStatus ? ON_TIMEOUT_MS : OFF_TIMEOUT_MS); 79 } else { 80 setSystemAudioMode(false); 81 finishWithCallback(HdmiControlManager.RESULT_EXCEPTION); 82 } 83 } 84 }); 85 } 86 87 private void handleSendSystemAudioModeRequestTimeout() { 88 if (!mTargetAudioStatus // Don't retry for Off case. 89 || mSendRetryCount++ >= MAX_SEND_RETRY_COUNT) { 90 setSystemAudioMode(false); 91 finishWithCallback(HdmiControlManager.RESULT_TIMEOUT); 92 return; 93 } 94 sendSystemAudioModeRequest(); 95 } 96 97 protected void setSystemAudioMode(boolean mode) { 98 tv().setSystemAudioMode(mode); 99 } 100 101 @Override 102 final boolean processCommand(HdmiCecMessage cmd) { 103 switch (mState) { 104 case STATE_WAIT_FOR_SET_SYSTEM_AUDIO_MODE: 105 // TODO: Handle <FeatureAbort> of <SystemAudioModeRequest> 106 if (cmd.getOpcode() != Constants.MESSAGE_SET_SYSTEM_AUDIO_MODE 107 || !HdmiUtils.checkCommandSource(cmd, mAvrLogicalAddress, TAG)) { 108 return false; 109 } 110 boolean receivedStatus = HdmiUtils.parseCommandParamSystemAudioStatus(cmd); 111 if (receivedStatus == mTargetAudioStatus) { 112 setSystemAudioMode(receivedStatus); 113 startAudioStatusAction(); 114 return true; 115 } else { 116 // Unexpected response, consider the request is newly initiated by AVR. 117 // To return 'false' will initiate new SystemAudioActionFromAvr by the control 118 // service. 119 finishWithCallback(HdmiControlManager.RESULT_EXCEPTION); 120 return false; 121 } 122 default: 123 return false; 124 } 125 } 126 127 protected void startAudioStatusAction() { 128 addAndStartAction(new SystemAudioStatusAction(tv(), mAvrLogicalAddress, mCallback)); 129 finish(); 130 } 131 132 protected void removeSystemAudioActionInProgress() { 133 removeActionExcept(SystemAudioActionFromTv.class, this); 134 removeActionExcept(SystemAudioActionFromAvr.class, this); 135 } 136 137 @Override 138 final void handleTimerEvent(int state) { 139 if (mState != state) { 140 return; 141 } 142 switch (mState) { 143 case STATE_WAIT_FOR_SET_SYSTEM_AUDIO_MODE: 144 handleSendSystemAudioModeRequestTimeout(); 145 return; 146 } 147 } 148 149 // TODO: if IHdmiControlCallback is general to other FeatureAction, 150 // move it into FeatureAction. 151 protected void finishWithCallback(int returnCode) { 152 if (mCallback != null) { 153 try { 154 mCallback.onComplete(returnCode); 155 } catch (RemoteException e) { 156 Slog.e(TAG, "Failed to invoke callback.", e); 157 } 158 } 159 finish(); 160 } 161} 162