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.IHdmiControlCallback; 21import android.hardware.hdmi.HdmiDeviceInfo; 22import android.hardware.hdmi.HdmiControlManager; 23import android.os.RemoteException; 24import android.util.Slog; 25 26import com.android.server.hdmi.HdmiCecLocalDevice.ActiveSource; 27 28/** 29 * Handles CEC command <Active Source>. 30 * <p> 31 * Used by feature actions that need to handle the command in their flow. Only for TV 32 * local device. 33 */ 34final class ActiveSourceHandler { 35 private static final String TAG = "ActiveSourceHandler"; 36 37 private final HdmiCecLocalDeviceTv mSource; 38 private final HdmiControlService mService; 39 @Nullable 40 private final IHdmiControlCallback mCallback; 41 42 static ActiveSourceHandler create(HdmiCecLocalDeviceTv source, IHdmiControlCallback callback) { 43 if (source == null) { 44 Slog.e(TAG, "Wrong arguments"); 45 return null; 46 } 47 return new ActiveSourceHandler(source, callback); 48 } 49 50 private ActiveSourceHandler(HdmiCecLocalDeviceTv source, IHdmiControlCallback callback) { 51 mSource = source; 52 mService = mSource.getService(); 53 mCallback = callback; 54 } 55 56 /** 57 * Handles the incoming active source command. 58 * 59 * @param newActive new active source information 60 * @param deviceType device type of the new active source 61 */ 62 void process(ActiveSource newActive, int deviceType) { 63 // Seq #17 64 HdmiCecLocalDeviceTv tv = mSource; 65 HdmiDeviceInfo device = mService.getDeviceInfo(newActive.logicalAddress); 66 if (device == null) { 67 tv.startNewDeviceAction(newActive, deviceType); 68 } 69 70 if (!tv.isProhibitMode()) { 71 ActiveSource old = ActiveSource.of(tv.getActiveSource()); 72 tv.updateActiveSource(newActive); 73 boolean notifyInputChange = (mCallback == null); 74 if (!old.equals(newActive)) { 75 tv.setPrevPortId(tv.getActivePortId()); 76 } 77 tv.updateActiveInput(newActive.physicalAddress, notifyInputChange); 78 invokeCallback(HdmiControlManager.RESULT_SUCCESS); 79 } else { 80 // TV is in a mode that should keep its current source/input from 81 // being changed for its operation. Reclaim the active source 82 // or switch the port back to the one used for the current mode. 83 ActiveSource current = tv.getActiveSource(); 84 if (current.logicalAddress == getSourceAddress()) { 85 HdmiCecMessage activeSourceCommand = HdmiCecMessageBuilder.buildActiveSource( 86 current.logicalAddress, current.physicalAddress); 87 mService.sendCecCommand(activeSourceCommand); 88 tv.updateActiveSource(current); 89 invokeCallback(HdmiControlManager.RESULT_SUCCESS); 90 } else { 91 tv.startRoutingControl(newActive.physicalAddress, current.physicalAddress, true, 92 mCallback); 93 } 94 } 95 } 96 97 private final int getSourceAddress() { 98 return mSource.getDeviceInfo().getLogicalAddress(); 99 } 100 101 private void invokeCallback(int result) { 102 if (mCallback == null) { 103 return; 104 } 105 try { 106 mCallback.onComplete(result); 107 } catch (RemoteException e) { 108 Slog.e(TAG, "Callback failed:" + e); 109 } 110 } 111} 112