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