HdmiCecLocalDevice.java revision 47927f756a0f694358567cec845b53ab3fc980e9
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;
22
23/**
24 * Class that models a logical CEC device hosted in this system. Handles initialization,
25 * CEC commands that call for actions customized per device type.
26 */
27abstract class HdmiCecLocalDevice {
28
29    protected final HdmiCecController mController;
30    protected final int mDeviceType;
31    protected int mAddress;
32    protected int mPreferredAddress;
33
34    protected HdmiCecLocalDevice(HdmiCecController controller, int deviceType) {
35        mController = controller;
36        mDeviceType = deviceType;
37        mAddress = HdmiCec.ADDR_UNREGISTERED;
38    }
39
40    // Factory method that returns HdmiCecLocalDevice of corresponding type.
41    static HdmiCecLocalDevice create(HdmiCecController controller, int deviceType) {
42        switch (deviceType) {
43        case HdmiCec.DEVICE_TV:
44            return new HdmiCecLocalDeviceTv(controller);
45        case HdmiCec.DEVICE_PLAYBACK:
46            return new HdmiCecLocalDevicePlayback(controller);
47        default:
48            return null;
49        }
50    }
51
52    abstract void init();
53
54    protected void allocateAddress(int type) {
55        mController.allocateLogicalAddress(type, mPreferredAddress,
56                new AllocateLogicalAddressCallback() {
57            @Override
58            public void onAllocated(int deviceType, int logicalAddress) {
59                mAddress = mPreferredAddress = logicalAddress;
60                mController.addLogicalAddress(logicalAddress);
61            }
62        });
63    }
64
65    // Returns true if the logical address is same as the argument.
66    boolean isAddressOf(int addr) {
67        return addr == mAddress;
68    }
69
70    // Resets the logical address to unregistered(15), meaning the logical device is invalid.
71    void clearAddress() {
72        mAddress = HdmiCec.ADDR_UNREGISTERED;
73    }
74
75    void setPreferredAddress(int addr) {
76        mPreferredAddress = addr;
77    }
78}
79