HdmiCecLocalDevice.java revision 3ef57d99b3b1b751097d58c6c1b98db123d5ccc5
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 com.android.server.hdmi.HdmiCecController.AllocateLogicalAddressCallback; 20 21import android.hardware.hdmi.HdmiCec; 22import android.hardware.hdmi.HdmiCecDeviceInfo; 23 24/** 25 * Class that models a logical CEC device hosted in this system. Handles initialization, 26 * CEC commands that call for actions customized per device type. 27 */ 28abstract class HdmiCecLocalDevice { 29 30 protected final HdmiCecController mController; 31 protected final int mDeviceType; 32 protected final AddressAllocationCallback mAllocationCallback; 33 protected int mAddress; 34 protected int mPreferredAddress; 35 protected HdmiCecDeviceInfo mDeviceInfo; 36 37 /** 38 * Callback interface to notify newly allocated logical address of the given 39 * local device. 40 */ 41 interface AddressAllocationCallback { 42 /** 43 * Called when a logical address of the given device is allocated. 44 * 45 * @param deviceType original device type 46 * @param logicalAddress newly allocated logical address 47 */ 48 void onAddressAllocated(int deviceType, int logicalAddress); 49 } 50 51 protected HdmiCecLocalDevice(HdmiCecController controller, int deviceType, 52 AddressAllocationCallback callback) { 53 mController = controller; 54 mDeviceType = deviceType; 55 mAllocationCallback = callback; 56 mAddress = HdmiCec.ADDR_UNREGISTERED; 57 } 58 59 // Factory method that returns HdmiCecLocalDevice of corresponding type. 60 static HdmiCecLocalDevice create(HdmiCecController controller, int deviceType, 61 AddressAllocationCallback callback) { 62 switch (deviceType) { 63 case HdmiCec.DEVICE_TV: 64 return new HdmiCecLocalDeviceTv(controller, callback); 65 case HdmiCec.DEVICE_PLAYBACK: 66 return new HdmiCecLocalDevicePlayback(controller, callback); 67 default: 68 return null; 69 } 70 } 71 72 abstract void init(); 73 74 /** 75 * Called when a logical address of the local device is allocated. 76 * Note that internal variables are updated before it's called. 77 */ 78 protected abstract void onAddressAllocated(int logicalAddress); 79 80 protected void allocateAddress(int type) { 81 mController.allocateLogicalAddress(type, mPreferredAddress, 82 new AllocateLogicalAddressCallback() { 83 @Override 84 public void onAllocated(int deviceType, int logicalAddress) { 85 mAddress = mPreferredAddress = logicalAddress; 86 87 // Create and set device info. 88 HdmiCecDeviceInfo deviceInfo = createDeviceInfo(mAddress, deviceType); 89 setDeviceInfo(deviceInfo); 90 mController.addDeviceInfo(deviceInfo); 91 92 mController.addLogicalAddress(logicalAddress); 93 onAddressAllocated(logicalAddress); 94 if (mAllocationCallback != null) { 95 mAllocationCallback.onAddressAllocated(deviceType, logicalAddress); 96 } 97 } 98 }); 99 } 100 101 private final HdmiCecDeviceInfo createDeviceInfo(int logicalAddress, int deviceType) { 102 int vendorId = mController.getVendorId(); 103 int physicalAddress = mController.getPhysicalAddress(); 104 // TODO: get device name read from system configuration. 105 String displayName = HdmiCec.getDefaultDeviceName(logicalAddress); 106 return new HdmiCecDeviceInfo(logicalAddress, 107 physicalAddress, deviceType, vendorId, displayName); 108 } 109 110 HdmiCecDeviceInfo getDeviceInfo() { 111 return mDeviceInfo; 112 } 113 114 void setDeviceInfo(HdmiCecDeviceInfo info) { 115 mDeviceInfo = info; 116 } 117 118 // Returns true if the logical address is same as the argument. 119 boolean isAddressOf(int addr) { 120 return addr == mAddress; 121 } 122 123 // Resets the logical address to unregistered(15), meaning the logical device is invalid. 124 void clearAddress() { 125 mAddress = HdmiCec.ADDR_UNREGISTERED; 126 } 127 128 void setPreferredAddress(int addr) { 129 mPreferredAddress = addr; 130 } 131} 132