| /* |
| * Copyright (C) 2014 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package com.android.server.hdmi; |
| |
| import android.hardware.hdmi.HdmiCec; |
| import android.hardware.hdmi.HdmiCecMessage; |
| import android.util.Slog; |
| |
| /** |
| * Base feature action class for <Request ARC Initiation>/<Request ARC Termination>. |
| */ |
| abstract class RequestArcAction extends FeatureAction { |
| private static final String TAG = "RequestArcAction"; |
| |
| // State in which waits for ARC response. |
| protected static final int STATE_WATING_FOR_REQUEST_ARC_REQUEST_RESPONSE = 1; |
| |
| // Logical address of AV Receiver. |
| protected final int mAvrAddress; |
| |
| /** |
| * @Constructor |
| * |
| * @param service {@link HdmiControlService} instance |
| * @param sourceAddress logical address to be used as source address. It should |
| * TV type |
| * @param avrAddress address of AV receiver. It should be AUDIO_SYSTEM type |
| * @throw IllegalArugmentException if device type of sourceAddress and avrAddress |
| * is invalid |
| */ |
| RequestArcAction(HdmiControlService service, int sourceAddress, int avrAddress) { |
| super(service, sourceAddress); |
| verifyAddressType(sourceAddress, HdmiCec.DEVICE_TV); |
| verifyAddressType(avrAddress, HdmiCec.DEVICE_AUDIO_SYSTEM); |
| mAvrAddress = avrAddress; |
| } |
| |
| private static void verifyAddressType(int logicalAddress, int deviceType) { |
| int actualDeviceType = HdmiCec.getTypeFromAddress(logicalAddress); |
| if (actualDeviceType != deviceType) { |
| throw new IllegalArgumentException("Device type missmatch:[Expected:" + deviceType |
| + ", Actual:" + actualDeviceType); |
| } |
| } |
| |
| @Override |
| boolean processCommand(HdmiCecMessage cmd) { |
| if (mState != STATE_WATING_FOR_REQUEST_ARC_REQUEST_RESPONSE) { |
| return false; |
| } |
| |
| int src = cmd.getSource(); |
| if (src != mAvrAddress) { |
| Slog.w(TAG, "Invalid source [Expected:" + mAvrAddress + ", Actual:" + src + "]"); |
| return false; |
| } |
| int opcode = cmd.getOpcode(); |
| switch (opcode) { |
| // Handles only <Feature Abort> here and, both <Initiate ARC> and <Terminate ARC> |
| // are handled in HdmiControlService itself because both can be |
| // received wihtout <Request ARC Initiation> or <Request ARC Termination>. |
| case HdmiCec.MESSAGE_FEATURE_ABORT: |
| disableArcTransmission(); |
| finish(); |
| return true; |
| default: |
| Slog.w(TAG, "Unsupported opcode:" + cmd.toString()); |
| } |
| return false; |
| } |
| |
| protected final void disableArcTransmission() { |
| // Start Set ARC Transmission State action. |
| SetArcTransmissionStateAction action = new SetArcTransmissionStateAction(mService, |
| mSourceAddress, mAvrAddress, false); |
| mService.addAndStartAction(action); |
| } |
| |
| @Override |
| final void handleTimerEvent(int state) { |
| if (mState != state || state != STATE_WATING_FOR_REQUEST_ARC_REQUEST_RESPONSE) { |
| return; |
| } |
| disableArcTransmission(); |
| finish(); |
| } |
| } |