HdmiCecLocalDevice.java revision 1a4485dcd25ed036fb8de1a271b37121d8135f4e
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 int mAddress;
33    protected int mPreferredAddress;
34    protected HdmiCecDeviceInfo mDeviceInfo;
35
36    protected HdmiCecLocalDevice(HdmiCecController controller, int deviceType) {
37        mController = controller;
38        mDeviceType = deviceType;
39        mAddress = HdmiCec.ADDR_UNREGISTERED;
40    }
41
42    // Factory method that returns HdmiCecLocalDevice of corresponding type.
43    static HdmiCecLocalDevice create(HdmiCecController controller, int deviceType) {
44        switch (deviceType) {
45        case HdmiCec.DEVICE_TV:
46            return new HdmiCecLocalDeviceTv(controller);
47        case HdmiCec.DEVICE_PLAYBACK:
48            return new HdmiCecLocalDevicePlayback(controller);
49        default:
50            return null;
51        }
52    }
53
54    abstract void init();
55
56    protected void allocateAddress(int type) {
57        mController.allocateLogicalAddress(type, mPreferredAddress,
58                new AllocateLogicalAddressCallback() {
59            @Override
60            public void onAllocated(int deviceType, int logicalAddress) {
61                mAddress = mPreferredAddress = logicalAddress;
62
63                // Create and set device info.
64                HdmiCecDeviceInfo deviceInfo = createDeviceInfo(mAddress, deviceType);
65                setDeviceInfo(deviceInfo);
66                mController.addDeviceInfo(deviceInfo);
67
68                mController.addLogicalAddress(logicalAddress);
69            }
70        });
71    }
72
73    private final HdmiCecDeviceInfo createDeviceInfo(int logicalAddress, int deviceType) {
74        int vendorId = mController.getVendorId();
75        int physicalAddress = mController.getPhysicalAddress();
76        // TODO: get device name read from system configuration.
77        String displayName = HdmiCec.getDefaultDeviceName(logicalAddress);
78        return new HdmiCecDeviceInfo(logicalAddress,
79                physicalAddress, deviceType, vendorId, displayName);
80    }
81
82    HdmiCecDeviceInfo getDeviceInfo() {
83        return mDeviceInfo;
84    }
85
86    void setDeviceInfo(HdmiCecDeviceInfo info) {
87        mDeviceInfo = info;
88    }
89
90    // Returns true if the logical address is same as the argument.
91    boolean isAddressOf(int addr) {
92        return addr == mAddress;
93    }
94
95    // Resets the logical address to unregistered(15), meaning the logical device is invalid.
96    void clearAddress() {
97        mAddress = HdmiCec.ADDR_UNREGISTERED;
98    }
99
100    void setPreferredAddress(int addr) {
101        mPreferredAddress = addr;
102    }
103}
104