HdmiControlManager.java revision c0c20d0522d7756d80f011e7a54bf3b51c78df41
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 android.hardware.hdmi;
18
19import android.annotation.Nullable;
20import android.annotation.SystemApi;
21import android.os.RemoteException;
22
23/**
24 * The {@link HdmiControlManager} class is used to send HDMI control messages
25 * to attached CEC devices.
26 *
27 * <p>Provides various HDMI client instances that represent HDMI-CEC logical devices
28 * hosted in the system. {@link #getTvClient()}, for instance will return an
29 * {@link HdmiTvClient} object if the system is configured to host one. Android system
30 * can host more than one logical CEC devices. If multiple types are configured they
31 * all work as if they were independent logical devices running in the system.
32 *
33 * @hide
34 */
35@SystemApi
36public final class HdmiControlManager {
37    @Nullable private final IHdmiControlService mService;
38
39    public static final int POWER_STATUS_UNKNOWN = -1;
40    public static final int POWER_STATUS_ON = 0;
41    public static final int POWER_STATUS_STANDBY = 1;
42    public static final int POWER_STATUS_TRANSIENT_TO_ON = 2;
43    public static final int POWER_STATUS_TRANSIENT_TO_STANDBY = 3;
44
45    public static final int RESULT_SUCCESS = 0;
46    public static final int RESULT_TIMEOUT = 1;
47    public static final int RESULT_SOURCE_NOT_AVAILABLE = 2;
48    public static final int RESULT_TARGET_NOT_AVAILABLE = 3;
49    public static final int RESULT_ALREADY_IN_PROGRESS = 4;
50    public static final int RESULT_EXCEPTION = 5;
51    public static final int RESULT_INCORRECT_MODE = 6;
52
53    // True if we have a logical device of type playback hosted in the system.
54    private final boolean mHasPlaybackDevice;
55    // True if we have a logical device of type TV hosted in the system.
56    private final boolean mHasTvDevice;
57
58    /**
59     * @hide - hide this constructor because it has a parameter of type
60     * IHdmiControlService, which is a system private class. The right way
61     * to create an instance of this class is using the factory
62     * Context.getSystemService.
63     */
64    public HdmiControlManager(IHdmiControlService service) {
65        mService = service;
66        int[] types = null;
67        if (mService != null) {
68            try {
69                types = mService.getSupportedTypes();
70            } catch (RemoteException e) {
71                // Do nothing.
72            }
73        }
74        mHasTvDevice = hasDeviceType(types, HdmiCecDeviceInfo.DEVICE_TV);
75        mHasPlaybackDevice = hasDeviceType(types, HdmiCecDeviceInfo.DEVICE_PLAYBACK);
76    }
77
78    private static boolean hasDeviceType(int[] types, int type) {
79        if (types == null) {
80            return false;
81        }
82        for (int t : types) {
83            if (t == type) {
84                return true;
85            }
86        }
87        return false;
88    }
89
90    /**
91     * Gets an object that represents a HDMI-CEC logical device of type playback on the system.
92     *
93     * <p>Used to send HDMI control messages to other devices like TV or audio amplifier through
94     * HDMI bus. It is also possible to communicate with other logical devices hosted in the same
95     * system if the system is configured to host more than one type of HDMI-CEC logical devices.
96     *
97     * @return {@link HdmiPlaybackClient} instance. {@code null} on failure.
98     */
99    @Nullable
100    public HdmiPlaybackClient getPlaybackClient() {
101        if (mService == null || !mHasPlaybackDevice) {
102            return null;
103        }
104        return new HdmiPlaybackClient(mService);
105    }
106
107    /**
108     * Gets an object that represents a HDMI-CEC logical device of type TV on the system.
109     *
110     * <p>Used to send HDMI control messages to other devices and manage them through
111     * HDMI bus. It is also possible to communicate with other logical devices hosted in the same
112     * system if the system is configured to host more than one type of HDMI-CEC logical devices.
113     *
114     * @return {@link HdmiTvClient} instance. {@code null} on failure.
115     */
116    @Nullable
117    public HdmiTvClient getTvClient() {
118        if (mService == null || !mHasTvDevice) {
119                return null;
120        }
121        return new HdmiTvClient(mService);
122    }
123
124    /**
125     * Listener used to get hotplug event from HDMI port.
126     */
127    public interface HotplugEventListener {
128        void onReceived(HdmiHotplugEvent event);
129    }
130
131    /**
132     * Adds a listener to get informed of {@link HdmiHotplugEvent}.
133     *
134     * <p>To stop getting the notification,
135     * use {@link #removeHotplugEventListener(HotplugEventListener)}.
136     *
137     * @param listener {@link HotplugEventListener} instance
138     * @see HdmiControlManager#removeHotplugEventListener(HotplugEventListener)
139     */
140    public void addHotplugEventListener(HotplugEventListener listener) {
141        if (mService == null) {
142            return;
143        }
144        try {
145            mService.addHotplugEventListener(getHotplugEventListenerWrapper(listener));
146        } catch (RemoteException e) {
147            // Do nothing.
148        }
149    }
150
151    /**
152     * Removes a listener to stop getting informed of {@link HdmiHotplugEvent}.
153     *
154     * @param listener {@link HotplugEventListener} instance to be removed
155     */
156    public void removeHotplugEventListener(HotplugEventListener listener) {
157        if (mService == null) {
158            return;
159        }
160        try {
161            mService.removeHotplugEventListener(getHotplugEventListenerWrapper(listener));
162        } catch (RemoteException e) {
163            // Do nothing.
164        }
165    }
166
167    private IHdmiHotplugEventListener getHotplugEventListenerWrapper(
168            final HotplugEventListener listener) {
169        return new IHdmiHotplugEventListener.Stub() {
170            public void onReceived(HdmiHotplugEvent event) {
171                listener.onReceived(event);;
172            }
173        };
174    }
175}
176