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 17 package com.android.server.hdmi; 18 19 import android.annotation.Nullable; 20 import android.hardware.hdmi.IHdmiControlCallback; 21 import android.hardware.hdmi.HdmiDeviceInfo; 22 import android.hardware.hdmi.HdmiControlManager; 23 import android.os.RemoteException; 24 import android.util.Slog; 25 26 import 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 */ 34 final 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 tv.updateActiveSource(newActive); 72 boolean notifyInputChange = (mCallback == null); 73 tv.updateActiveInput(newActive.physicalAddress, notifyInputChange); 74 invokeCallback(HdmiControlManager.RESULT_SUCCESS); 75 } else { 76 // TV is in a mode that should keep its current source/input from 77 // being changed for its operation. Reclaim the active source 78 // or switch the port back to the one used for the current mode. 79 ActiveSource current = tv.getActiveSource(); 80 if (current.logicalAddress == getSourceAddress()) { 81 HdmiCecMessage activeSourceCommand = HdmiCecMessageBuilder.buildActiveSource( 82 current.logicalAddress, current.physicalAddress); 83 mService.sendCecCommand(activeSourceCommand); 84 tv.updateActiveSource(current); 85 invokeCallback(HdmiControlManager.RESULT_SUCCESS); 86 } else { 87 tv.startRoutingControl(newActive.physicalAddress, current.physicalAddress, true, 88 mCallback); 89 } 90 } 91 } 92 93 private final int getSourceAddress() { 94 return mSource.getDeviceInfo().getLogicalAddress(); 95 } 96 97 private void invokeCallback(int result) { 98 if (mCallback == null) { 99 return; 100 } 101 try { 102 mCallback.onComplete(result); 103 } catch (RemoteException e) { 104 Slog.e(TAG, "Callback failed:" + e); 105 } 106 } 107 } 108