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