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 
17 package android.hardware.hdmi;
18 
19 import android.annotation.Nullable;
20 import android.annotation.SdkConstant;
21 import android.annotation.SdkConstant.SdkConstantType;
22 import android.annotation.SystemApi;
23 import android.os.RemoteException;
24 import android.util.ArrayMap;
25 import android.util.Log;
26 
27 /**
28  * The {@link HdmiControlManager} class is used to send HDMI control messages
29  * to attached CEC devices.
30  *
31  * <p>Provides various HDMI client instances that represent HDMI-CEC logical devices
32  * hosted in the system. {@link #getTvClient()}, for instance will return an
33  * {@link HdmiTvClient} object if the system is configured to host one. Android system
34  * can host more than one logical CEC devices. If multiple types are configured they
35  * all work as if they were independent logical devices running in the system.
36  *
37  * @hide
38  */
39 @SystemApi
40 public final class HdmiControlManager {
41     private static final String TAG = "HdmiControlManager";
42 
43     @Nullable private final IHdmiControlService mService;
44 
45     /**
46      * Broadcast Action: Display OSD message.
47      * <p>Send when the service has a message to display on screen for events
48      * that need user's attention such as ARC status change.
49      * <p>Always contains the extra fields {@link #EXTRA_MESSAGE_ID}.
50      * <p>Requires {@link android.Manifest.permission#HDMI_CEC} to receive.
51      */
52     @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
53     public static final String ACTION_OSD_MESSAGE = "android.hardware.hdmi.action.OSD_MESSAGE";
54 
55     // --- Messages for ACTION_OSD_MESSAGE ---
56     /**
57      * Message that ARC enabled device is connected to invalid port (non-ARC port).
58      */
59     public static final int OSD_MESSAGE_ARC_CONNECTED_INVALID_PORT = 1;
60 
61     /**
62      * Message used by TV to receive volume status from Audio Receiver. It should check volume value
63      * that is retrieved from extra value with the key {@link #EXTRA_MESSAGE_EXTRA_PARAM1}. If the
64      * value is in range of [0,100], it is current volume of Audio Receiver. And there is another
65      * value, {@link #AVR_VOLUME_MUTED}, which is used to inform volume mute.
66      */
67     public static final int OSD_MESSAGE_AVR_VOLUME_CHANGED = 2;
68 
69     /**
70      * Used as an extra field in the intent {@link #ACTION_OSD_MESSAGE}. Contains the ID of
71      * the message to display on screen.
72      */
73     public static final String EXTRA_MESSAGE_ID = "android.hardware.hdmi.extra.MESSAGE_ID";
74     /**
75      * Used as an extra field in the intent {@link #ACTION_OSD_MESSAGE}. Contains the extra value
76      * of the message.
77      */
78     public static final String EXTRA_MESSAGE_EXTRA_PARAM1 =
79             "android.hardware.hdmi.extra.MESSAGE_EXTRA_PARAM1";
80 
81     /**
82      * Volume value for mute state.
83      */
84     public static final int AVR_VOLUME_MUTED = 101;
85 
86     public static final int POWER_STATUS_UNKNOWN = -1;
87     public static final int POWER_STATUS_ON = 0;
88     public static final int POWER_STATUS_STANDBY = 1;
89     public static final int POWER_STATUS_TRANSIENT_TO_ON = 2;
90     public static final int POWER_STATUS_TRANSIENT_TO_STANDBY = 3;
91 
92     public static final int RESULT_SUCCESS = 0;
93     public static final int RESULT_TIMEOUT = 1;
94     public static final int RESULT_SOURCE_NOT_AVAILABLE = 2;
95     public static final int RESULT_TARGET_NOT_AVAILABLE = 3;
96     public static final int RESULT_ALREADY_IN_PROGRESS = 4;
97     public static final int RESULT_EXCEPTION = 5;
98     public static final int RESULT_INCORRECT_MODE = 6;
99     public static final int RESULT_COMMUNICATION_FAILED = 7;
100 
101     public static final int DEVICE_EVENT_ADD_DEVICE = 1;
102     public static final int DEVICE_EVENT_REMOVE_DEVICE = 2;
103     public static final int DEVICE_EVENT_UPDATE_DEVICE = 3;
104 
105     // --- One Touch Recording success result
106     /** Recording currently selected source. Indicates the status of a recording. */
107     public static final int ONE_TOUCH_RECORD_RECORDING_CURRENTLY_SELECTED_SOURCE = 0x01;
108     /** Recording Digital Service. Indicates the status of a recording. */
109     public static final int ONE_TOUCH_RECORD_RECORDING_DIGITAL_SERVICE = 0x02;
110     /** Recording Analogue Service. Indicates the status of a recording. */
111     public static final int ONE_TOUCH_RECORD_RECORDING_ANALOGUE_SERVICE = 0x03;
112     /** Recording External input. Indicates the status of a recording. */
113     public static final int ONE_TOUCH_RECORD_RECORDING_EXTERNAL_INPUT = 0x04;
114 
115     // --- One Touch Record failure result
116     /** No recording – unable to record Digital Service. No suitable tuner. */
117     public static final int ONE_TOUCH_RECORD_UNABLE_DIGITAL_SERVICE = 0x05;
118     /** No recording – unable to record Analogue Service. No suitable tuner. */
119     public static final int ONE_TOUCH_RECORD_UNABLE_ANALOGUE_SERVICE = 0x06;
120     /**
121      * No recording – unable to select required service. as suitable tuner, but the requested
122      * parameters are invalid or out of range for that tuner.
123      */
124     public static final int ONE_TOUCH_RECORD_UNABLE_SELECTED_SERVICE = 0x07;
125     /** No recording – invalid External plug number */
126     public static final int ONE_TOUCH_RECORD_INVALID_EXTERNAL_PLUG_NUMBER = 0x09;
127     /** No recording – invalid External Physical Address */
128     public static final int ONE_TOUCH_RECORD_INVALID_EXTERNAL_PHYSICAL_ADDRESS = 0x0A;
129     /** No recording – CA system not supported */
130     public static final int ONE_TOUCH_RECORD_UNSUPPORTED_CA = 0x0B;
131     /** No Recording – No or Insufficient CA Entitlements” */
132     public static final int ONE_TOUCH_RECORD_NO_OR_INSUFFICIENT_CA_ENTITLEMENTS = 0x0C;
133     /** No recording – Not allowed to copy source. Source is “copy never”. */
134     public static final int ONE_TOUCH_RECORD_DISALLOW_TO_COPY = 0x0D;
135     /** No recording – No further copies allowed */
136     public static final int ONE_TOUCH_RECORD_DISALLOW_TO_FUTHER_COPIES = 0x0E;
137     /** No recording – No media */
138     public static final int ONE_TOUCH_RECORD_NO_MEDIA = 0x10;
139     /** No recording – playing */
140     public static final int ONE_TOUCH_RECORD_PLAYING = 0x11;
141     /** No recording – already recording */
142     public static final int ONE_TOUCH_RECORD_ALREADY_RECORDING = 0x12;
143     /** No recording – media protected */
144     public static final int ONE_TOUCH_RECORD_MEDIA_PROTECTED = 0x13;
145     /** No recording – no source signal */
146     public static final int ONE_TOUCH_RECORD_NO_SOURCE_SIGNAL = 0x14;
147     /** No recording – media problem */
148     public static final int ONE_TOUCH_RECORD_MEDIA_PROBLEM = 0x15;
149     /** No recording – not enough space available */
150     public static final int ONE_TOUCH_RECORD_NOT_ENOUGH_SPACE = 0x16;
151     /** No recording – Parental Lock On */
152     public static final int ONE_TOUCH_RECORD_PARENT_LOCK_ON = 0x17;
153     /** Recording terminated normally */
154     public static final int ONE_TOUCH_RECORD_RECORDING_TERMINATED_NORMALLY = 0x1A;
155     /** Recording has already terminated */
156     public static final int ONE_TOUCH_RECORD_RECORDING_ALREADY_TERMINATED = 0x1B;
157     /** No recording – other reason */
158     public static final int ONE_TOUCH_RECORD_OTHER_REASON = 0x1F;
159     // From here extra message for recording that is not mentioned in CEC spec
160     /** No recording. Previous recording request in progress. */
161     public static final int ONE_TOUCH_RECORD_PREVIOUS_RECORDING_IN_PROGRESS = 0x30;
162     /** No recording. Please check recorder and connection. */
163     public static final int ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION = 0x31;
164     /** Cannot record currently displayed source. */
165     public static final int ONE_TOUCH_RECORD_FAIL_TO_RECORD_DISPLAYED_SCREEN = 0x32;
166     /** CEC is disabled. */
167     public static final int ONE_TOUCH_RECORD_CEC_DISABLED = 0x33;
168 
169     // --- Types for timer recording
170     /** Timer recording type for digital service source. */
171     public static final int TIMER_RECORDING_TYPE_DIGITAL = 1;
172     /** Timer recording type for analogue service source. */
173     public static final int TIMER_RECORDING_TYPE_ANALOGUE = 2;
174     /** Timer recording type for external source. */
175     public static final int TIMER_RECORDING_TYPE_EXTERNAL = 3;
176 
177     // --- Timer Status Data
178     /** [Timer Status Data/Media Info] - Media present and not protected. */
179     public static final int TIMER_STATUS_MEDIA_INFO_PRESENT_NOT_PROTECTED = 0x0;
180     /** [Timer Status Data/Media Info] - Media present, but protected. */
181     public static final int TIMER_STATUS_MEDIA_INFO_PRESENT_PROTECTED = 0x1;
182     /** [Timer Status Data/Media Info] - Media not present. */
183     public static final int TIMER_STATUS_MEDIA_INFO_NOT_PRESENT = 0x2;
184 
185     /** [Timer Status Data/Programmed Info] - Enough space available for recording. */
186     public static final int TIMER_STATUS_PROGRAMMED_INFO_ENOUGH_SPACE = 0x8;
187     /** [Timer Status Data/Programmed Info] - Not enough space available for recording. */
188     public static final int TIMER_STATUS_PROGRAMMED_INFO_NOT_ENOUGH_SPACE = 0x9;
189     /** [Timer Status Data/Programmed Info] - Might not enough space available for recording. */
190     public static final int TIMER_STATUS_PROGRAMMED_INFO_MIGHT_NOT_ENOUGH_SPACE = 0xB;
191     /** [Timer Status Data/Programmed Info] - No media info available. */
192     public static final int TIMER_STATUS_PROGRAMMED_INFO_NO_MEDIA_INFO = 0xA;
193 
194     /** [Timer Status Data/Not Programmed Error Info] - No free timer available. */
195     public static final int TIMER_STATUS_NOT_PROGRAMMED_NO_FREE_TIME = 0x1;
196     /** [Timer Status Data/Not Programmed Error Info] - Date out of range. */
197     public static final int TIMER_STATUS_NOT_PROGRAMMED_DATE_OUT_OF_RANGE = 0x2;
198     /** [Timer Status Data/Not Programmed Error Info] - Recording Sequence error. */
199     public static final int TIMER_STATUS_NOT_PROGRAMMED_INVALID_SEQUENCE = 0x3;
200     /** [Timer Status Data/Not Programmed Error Info] - Invalid External Plug Number. */
201     public static final int TIMER_STATUS_NOT_PROGRAMMED_INVALID_EXTERNAL_PLUG_NUMBER = 0x4;
202     /** [Timer Status Data/Not Programmed Error Info] - Invalid External Physical Address. */
203     public static final int TIMER_STATUS_NOT_PROGRAMMED_INVALID_EXTERNAL_PHYSICAL_NUMBER = 0x5;
204     /** [Timer Status Data/Not Programmed Error Info] - CA system not supported. */
205     public static final int TIMER_STATUS_NOT_PROGRAMMED_CA_NOT_SUPPORTED = 0x6;
206     /** [Timer Status Data/Not Programmed Error Info] - No or insufficient CA Entitlements. */
207     public static final int TIMER_STATUS_NOT_PROGRAMMED_NO_CA_ENTITLEMENTS = 0x7;
208     /** [Timer Status Data/Not Programmed Error Info] - Does not support resolution. */
209     public static final int TIMER_STATUS_NOT_PROGRAMMED_UNSUPPORTED_RESOLUTION = 0x8;
210     /** [Timer Status Data/Not Programmed Error Info] - Parental Lock On. */
211     public static final int TIMER_STATUS_NOT_PROGRAMMED_PARENTAL_LOCK_ON= 0x9;
212     /** [Timer Status Data/Not Programmed Error Info] - Clock Failure. */
213     public static final int TIMER_STATUS_NOT_PROGRAMMED_CLOCK_FAILURE = 0xA;
214     /** [Timer Status Data/Not Programmed Error Info] - Duplicate: already programmed. */
215     public static final int TIMER_STATUS_NOT_PROGRAMMED_DUPLICATED = 0xE;
216 
217     // --- Extra result value for timer recording.
218     /** No extra error. */
219     public static final int TIMER_RECORDING_RESULT_EXTRA_NO_ERROR = 0x00;
220     /** No timer recording - check recorder and connection. */
221     public static final int TIMER_RECORDING_RESULT_EXTRA_CHECK_RECORDER_CONNECTION = 0x01;
222     /** No timer recording - cannot record selected source. */
223     public static final int TIMER_RECORDING_RESULT_EXTRA_FAIL_TO_RECORD_SELECTED_SOURCE = 0x02;
224     /** CEC is disabled. */
225     public static final int TIMER_RECORDING_RESULT_EXTRA_CEC_DISABLED = 0x03;
226 
227     // -- Timer cleared status data code used for result of onClearTimerRecordingResult.
228     /** Timer not cleared – recording. */
229     public static final int CLEAR_TIMER_STATUS_TIMER_NOT_CLEARED_RECORDING = 0x00;
230     /** Timer not cleared – no matching. */
231     public static final int CLEAR_TIMER_STATUS_TIMER_NOT_CLEARED_NO_MATCHING = 0x01;
232     /** Timer not cleared – no info available. */
233     public static final int CLEAR_TIMER_STATUS_TIMER_NOT_CLEARED_NO_INFO_AVAILABLE = 0x02;
234     /** Timer cleared. */
235     public static final int CLEAR_TIMER_STATUS_TIMER_CLEARED = 0x80;
236     /** Clear timer error - check recorder and connection. */
237     public static final int CLEAR_TIMER_STATUS_CHECK_RECORDER_CONNECTION = 0xA0;
238     /** Clear timer error - cannot clear timer for selected source. */
239     public static final int CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE = 0xA1;
240     /** Clear timer error - CEC is disabled. */
241     public static final int CLEAR_TIMER_STATUS_CEC_DISABLE = 0xA2;
242 
243     /** The HdmiControlService is started. */
244     public static final int CONTROL_STATE_CHANGED_REASON_START = 0;
245     /** The state of HdmiControlService is changed by changing of settings. */
246     public static final int CONTROL_STATE_CHANGED_REASON_SETTING = 1;
247     /** The HdmiControlService is enabled to wake up. */
248     public static final int CONTROL_STATE_CHANGED_REASON_WAKEUP = 2;
249     /** The HdmiControlService will be disabled to standby. */
250     public static final int CONTROL_STATE_CHANGED_REASON_STANDBY = 3;
251 
252     // True if we have a logical device of type playback hosted in the system.
253     private final boolean mHasPlaybackDevice;
254     // True if we have a logical device of type TV hosted in the system.
255     private final boolean mHasTvDevice;
256 
257     /**
258      * {@hide} - hide this constructor because it has a parameter of type IHdmiControlService,
259      * which is a system private class. The right way to create an instance of this class is
260      * using the factory Context.getSystemService.
261      */
HdmiControlManager(IHdmiControlService service)262     public HdmiControlManager(IHdmiControlService service) {
263         mService = service;
264         int[] types = null;
265         if (mService != null) {
266             try {
267                 types = mService.getSupportedTypes();
268             } catch (RemoteException e) {
269                 // Do nothing.
270             }
271         }
272         mHasTvDevice = hasDeviceType(types, HdmiDeviceInfo.DEVICE_TV);
273         mHasPlaybackDevice = hasDeviceType(types, HdmiDeviceInfo.DEVICE_PLAYBACK);
274     }
275 
hasDeviceType(int[] types, int type)276     private static boolean hasDeviceType(int[] types, int type) {
277         if (types == null) {
278             return false;
279         }
280         for (int t : types) {
281             if (t == type) {
282                 return true;
283             }
284         }
285         return false;
286     }
287 
288     /**
289      * Gets an object that represents an HDMI-CEC logical device of a specified type.
290      *
291      * @param type CEC device type
292      * @return {@link HdmiClient} instance. {@code null} on failure.
293      * See {@link HdmiDeviceInfo#DEVICE_PLAYBACK}
294      * See {@link HdmiDeviceInfo#DEVICE_TV}
295      */
296     @Nullable
getClient(int type)297     public HdmiClient getClient(int type) {
298         if (mService == null) {
299             return null;
300         }
301         switch (type) {
302             case HdmiDeviceInfo.DEVICE_TV:
303                 return mHasTvDevice ? new HdmiTvClient(mService) : null;
304             case HdmiDeviceInfo.DEVICE_PLAYBACK:
305                 return mHasPlaybackDevice ? new HdmiPlaybackClient(mService) : null;
306             default:
307                 return null;
308         }
309     }
310 
311     /**
312      * Gets an object that represents an HDMI-CEC logical device of type playback on the system.
313      *
314      * <p>Used to send HDMI control messages to other devices like TV or audio amplifier through
315      * HDMI bus. It is also possible to communicate with other logical devices hosted in the same
316      * system if the system is configured to host more than one type of HDMI-CEC logical devices.
317      *
318      * @return {@link HdmiPlaybackClient} instance. {@code null} on failure.
319      */
320     @Nullable
getPlaybackClient()321     public HdmiPlaybackClient getPlaybackClient() {
322         return (HdmiPlaybackClient) getClient(HdmiDeviceInfo.DEVICE_PLAYBACK);
323     }
324 
325     /**
326      * Gets an object that represents an HDMI-CEC logical device of type TV on the system.
327      *
328      * <p>Used to send HDMI control messages to other devices and manage them through
329      * HDMI bus. It is also possible to communicate with other logical devices hosted in the same
330      * system if the system is configured to host more than one type of HDMI-CEC logical devices.
331      *
332      * @return {@link HdmiTvClient} instance. {@code null} on failure.
333      */
334     @Nullable
getTvClient()335     public HdmiTvClient getTvClient() {
336         return (HdmiTvClient) getClient(HdmiDeviceInfo.DEVICE_TV);
337     }
338 
339     /**
340      * Listener used to get hotplug event from HDMI port.
341      */
342     public interface HotplugEventListener {
onReceived(HdmiHotplugEvent event)343         void onReceived(HdmiHotplugEvent event);
344     }
345 
346     private final ArrayMap<HotplugEventListener, IHdmiHotplugEventListener>
347             mHotplugEventListeners = new ArrayMap<>();
348 
349     /**
350      * Listener used to get vendor-specific commands.
351      */
352     public interface VendorCommandListener {
353         /**
354          * Called when a vendor command is received.
355          *
356          * @param srcAddress source logical address
357          * @param destAddress destination logical address
358          * @param params vendor-specific parameters
359          * @param hasVendorId {@code true} if the command is &lt;Vendor Command
360          *        With ID&gt;. The first 3 bytes of params is vendor id.
361          */
onReceived(int srcAddress, int destAddress, byte[] params, boolean hasVendorId)362         void onReceived(int srcAddress, int destAddress, byte[] params, boolean hasVendorId);
363 
364         /**
365          * The callback is called:
366          * <ul>
367          *     <li> before HdmiControlService is disabled.
368          *     <li> after HdmiControlService is enabled and the local address is assigned.
369          * </ul>
370          * The client shouldn't hold the thread too long since this is a blocking call.
371          *
372          * @param enabled {@code true} if HdmiControlService is enabled.
373          * @param reason the reason code why the state of HdmiControlService is changed.
374          * @see #CONTROL_STATE_CHANGED_REASON_START
375          * @see #CONTROL_STATE_CHANGED_REASON_SETTING
376          * @see #CONTROL_STATE_CHANGED_REASON_WAKEUP
377          * @see #CONTROL_STATE_CHANGED_REASON_STANDBY
378          */
onControlStateChanged(boolean enabled, int reason)379         void onControlStateChanged(boolean enabled, int reason);
380     }
381 
382     /**
383      * Adds a listener to get informed of {@link HdmiHotplugEvent}.
384      *
385      * <p>To stop getting the notification,
386      * use {@link #removeHotplugEventListener(HotplugEventListener)}.
387      *
388      * @param listener {@link HotplugEventListener} instance
389      * @see HdmiControlManager#removeHotplugEventListener(HotplugEventListener)
390      */
addHotplugEventListener(HotplugEventListener listener)391     public void addHotplugEventListener(HotplugEventListener listener) {
392         if (mService == null) {
393             Log.e(TAG, "HdmiControlService is not available");
394             return;
395         }
396         if (mHotplugEventListeners.containsKey(listener)) {
397             Log.e(TAG, "listener is already registered");
398             return;
399         }
400         IHdmiHotplugEventListener wrappedListener = getHotplugEventListenerWrapper(listener);
401         mHotplugEventListeners.put(listener, wrappedListener);
402         try {
403             mService.addHotplugEventListener(wrappedListener);
404         } catch (RemoteException e) {
405             Log.e(TAG, "failed to add hotplug event listener: ", e);
406         }
407     }
408 
409     /**
410      * Removes a listener to stop getting informed of {@link HdmiHotplugEvent}.
411      *
412      * @param listener {@link HotplugEventListener} instance to be removed
413      */
removeHotplugEventListener(HotplugEventListener listener)414     public void removeHotplugEventListener(HotplugEventListener listener) {
415         if (mService == null) {
416             Log.e(TAG, "HdmiControlService is not available");
417             return;
418         }
419         IHdmiHotplugEventListener wrappedListener = mHotplugEventListeners.remove(listener);
420         if (wrappedListener == null) {
421             Log.e(TAG, "tried to remove not-registered listener");
422             return;
423         }
424         try {
425             mService.removeHotplugEventListener(wrappedListener);
426         } catch (RemoteException e) {
427             Log.e(TAG, "failed to remove hotplug event listener: ", e);
428         }
429     }
430 
getHotplugEventListenerWrapper( final HotplugEventListener listener)431     private IHdmiHotplugEventListener getHotplugEventListenerWrapper(
432             final HotplugEventListener listener) {
433         return new IHdmiHotplugEventListener.Stub() {
434             @Override
435             public void onReceived(HdmiHotplugEvent event) {
436                 listener.onReceived(event);;
437             }
438         };
439     }
440 }
441