HdmiCecLocalDeviceTv.java revision 092b445ef898e3c1e5b2918b554480940f0f5a28
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.HdmiCecMessage;
21import android.util.Slog;
22
23import java.util.Locale;
24
25/**
26 * Represent a logical device of type TV residing in Android system.
27 */
28final class HdmiCecLocalDeviceTv extends HdmiCecLocalDevice {
29    private static final String TAG = "HdmiCecLocalDeviceTv";
30
31    HdmiCecLocalDeviceTv(HdmiControlService service) {
32        super(service, HdmiCec.DEVICE_TV);
33    }
34
35    @Override
36    protected void onAddressAllocated(int logicalAddress) {
37        // TODO: vendor-specific initialization here.
38
39        mService.sendCecCommand(HdmiCecMessageBuilder.buildReportPhysicalAddressCommand(
40                mAddress, mService.getPhysicalAddress(), mDeviceType));
41        mService.sendCecCommand(HdmiCecMessageBuilder.buildDeviceVendorIdCommand(
42                mAddress, mService.getVendorId()));
43
44        mService.launchDeviceDiscovery(mAddress);
45        // TODO: Start routing control action, device discovery action.
46    }
47
48    @Override
49    protected boolean onMessage(HdmiCecMessage message) {
50        switch (message.getOpcode()) {
51            case HdmiCec.MESSAGE_REPORT_PHYSICAL_ADDRESS:
52                return handleReportPhysicalAddress(message);
53            default:
54                return super.onMessage(message);
55        }
56    }
57
58    @Override
59    protected boolean handleGetMenuLanguage(HdmiCecMessage message) {
60        HdmiCecMessage command = HdmiCecMessageBuilder.buildSetMenuLanguageCommand(
61                mAddress, Locale.getDefault().getISO3Language());
62        // TODO: figure out how to handle failed to get language code.
63        if (command != null) {
64            mService.sendCecCommand(command);
65        } else {
66            Slog.w(TAG, "Failed to respond to <Get Menu Language>: " + message.toString());
67        }
68        return true;
69    }
70
71    private boolean handleReportPhysicalAddress(HdmiCecMessage message) {
72        // Ignore if [Device Discovery Action] is going on.
73        if (mService.hasAction(DeviceDiscoveryAction.class)) {
74            Slog.i(TAG, "Ignore unrecognizable <Report Physical Address> "
75                    + "because Device Discovery Action is on-going:" + message);
76            return true;
77        }
78
79        int physicalAddress = HdmiUtils.twoBytesToInt(message.getParams());
80        mService.addAndStartAction(new NewDeviceAction(mService,
81                mAddress, message.getSource(), physicalAddress));
82
83        return true;
84    }
85}
86