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.SystemApi;
20 import android.os.RemoteException;
21 import android.util.Log;
22 
23 /**
24  * HdmiPlaybackClient represents HDMI-CEC logical device of type Playback
25  * in the Android system which acts as a playback device such as set-top box.
26  *
27  * <p>HdmiPlaybackClient provides methods that control, get information from TV/Display device
28  * connected through HDMI bus.
29  *
30  * @hide
31  */
32 @SystemApi
33 public final class HdmiPlaybackClient extends HdmiClient {
34     private static final String TAG = "HdmiPlaybackClient";
35 
36     // Logical address of TV. The secondary TV is not handled.
37     private static final int ADDR_TV = 0;
38 
39     /**
40      * Listener used by the client to get the result of one touch play operation.
41      */
42     public interface OneTouchPlayCallback {
43         /**
44          * Called when the result of the feature one touch play is returned.
45          *
46          * @param result the result of the operation. {@link HdmiControlManager#RESULT_SUCCESS}
47          *         if successful.
48          */
onComplete(int result)49         public void onComplete(int result);
50     }
51 
52     /**
53      * Listener used by the client to get display device status.
54      */
55     public interface DisplayStatusCallback {
56         /**
57          * Called when display device status is reported.
58          *
59          * @param status display device status. It should be one of the following values.
60          *            <ul>
61          *            <li>{@link HdmiControlManager#POWER_STATUS_ON}
62          *            <li>{@link HdmiControlManager#POWER_STATUS_STANDBY}
63          *            <li>{@link HdmiControlManager#POWER_STATUS_TRANSIENT_TO_ON}
64          *            <li>{@link HdmiControlManager#POWER_STATUS_TRANSIENT_TO_STANDBY}
65          *            <li>{@link HdmiControlManager#POWER_STATUS_UNKNOWN}
66          *            </ul>
67          */
onComplete(int status)68         public void onComplete(int status);
69     }
70 
HdmiPlaybackClient(IHdmiControlService service)71     /* package */ HdmiPlaybackClient(IHdmiControlService service) {
72         super(service);
73     }
74 
75     /**
76      * Performs the feature 'one touch play' from playback device to turn on display and claim
77      * to be the streaming source.
78      *
79      * <p>Client side is responsible to send out intent to choose whichever internal
80      * source on the current device it would like to switch to. Otherwise the current
81      * device will remain on the current input.
82      *
83      * @param callback {@link OneTouchPlayCallback} object to get informed
84      *         of the result
85      */
oneTouchPlay(OneTouchPlayCallback callback)86     public void oneTouchPlay(OneTouchPlayCallback callback) {
87         try {
88             mService.oneTouchPlay(getCallbackWrapper(callback));
89         } catch (RemoteException e) {
90             Log.e(TAG, "oneTouchPlay threw exception ", e);
91         }
92     }
93 
94     @Override
getDeviceType()95     public int getDeviceType() {
96         return HdmiDeviceInfo.DEVICE_PLAYBACK;
97     }
98 
99     /**
100      * Gets the status of display device connected through HDMI bus.
101      *
102      * @param callback {@link DisplayStatusCallback} object to get informed
103      *         of the result
104      */
queryDisplayStatus(DisplayStatusCallback callback)105     public void queryDisplayStatus(DisplayStatusCallback callback) {
106         try {
107             mService.queryDisplayStatus(getCallbackWrapper(callback));
108         } catch (RemoteException e) {
109             Log.e(TAG, "queryDisplayStatus threw exception ", e);
110         }
111     }
112 
113     /**
114      * Sends a &lt;Standby&gt; command to TV.
115      */
sendStandby()116     public void sendStandby() {
117         try {
118             mService.sendStandby(getDeviceType(), HdmiDeviceInfo.idForCecDevice(ADDR_TV));
119         } catch (RemoteException e) {
120             Log.e(TAG, "sendStandby threw exception ", e);
121         }
122     }
123 
getCallbackWrapper(final OneTouchPlayCallback callback)124     private IHdmiControlCallback getCallbackWrapper(final OneTouchPlayCallback callback) {
125         if (callback == null) {
126             throw new IllegalArgumentException("OneTouchPlayCallback cannot be null.");
127         }
128         return new IHdmiControlCallback.Stub() {
129             @Override
130             public void onComplete(int result) {
131                 callback.onComplete(result);
132             }
133         };
134     }
135 
136     private IHdmiControlCallback getCallbackWrapper(final DisplayStatusCallback callback) {
137         if (callback == null) {
138             throw new IllegalArgumentException("DisplayStatusCallback cannot be null.");
139         }
140         return new IHdmiControlCallback.Stub() {
141             @Override
142             public void onComplete(int status) {
143                 callback.onComplete(status);
144             }
145         };
146     }
147 }
148