HdmiCecLocalDevice.java revision 3ee65720e91c7f92ad5a034d7052122a606aa8d5
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.hardware.hdmi.HdmiCec;
20import android.hardware.hdmi.HdmiCecDeviceInfo;
21
22/**
23 * Class that models a logical CEC device hosted in this system. Handles initialization,
24 * CEC commands that call for actions customized per device type.
25 */
26abstract class HdmiCecLocalDevice {
27
28    protected final HdmiControlService mService;
29    protected final int mDeviceType;
30    protected int mAddress;
31    protected int mPreferredAddress;
32    protected HdmiCecDeviceInfo mDeviceInfo;
33
34    protected HdmiCecLocalDevice(HdmiControlService service, int deviceType) {
35        mService = service;
36        mDeviceType = deviceType;
37        mAddress = HdmiCec.ADDR_UNREGISTERED;
38    }
39
40    // Factory method that returns HdmiCecLocalDevice of corresponding type.
41    static HdmiCecLocalDevice create(HdmiControlService service, int deviceType) {
42        switch (deviceType) {
43        case HdmiCec.DEVICE_TV:
44            return new HdmiCecLocalDeviceTv(service);
45        case HdmiCec.DEVICE_PLAYBACK:
46            return new HdmiCecLocalDevicePlayback(service);
47        default:
48            return null;
49        }
50    }
51
52    void init() {
53        mPreferredAddress = HdmiCec.ADDR_UNREGISTERED;
54        // TODO: load preferred address from permanent storage.
55    }
56
57    /**
58     * Called once a logical address of the local device is allocated.
59     */
60    protected abstract void onAddressAllocated(int logicalAddress);
61
62    final void handleAddressAllocated(int logicalAddress) {
63        mAddress = mPreferredAddress = logicalAddress;
64        onAddressAllocated(logicalAddress);
65    }
66
67    HdmiCecDeviceInfo getDeviceInfo() {
68        return mDeviceInfo;
69    }
70
71    void setDeviceInfo(HdmiCecDeviceInfo info) {
72        mDeviceInfo = info;
73    }
74
75    // Returns true if the logical address is same as the argument.
76    boolean isAddressOf(int addr) {
77        return addr == mAddress;
78    }
79
80    // Resets the logical address to unregistered(15), meaning the logical device is invalid.
81    void clearAddress() {
82        mAddress = HdmiCec.ADDR_UNREGISTERED;
83    }
84
85    void setPreferredAddress(int addr) {
86        mPreferredAddress = addr;
87    }
88
89    int getPreferredAddress() {
90        return mPreferredAddress;
91    }
92}
93