HdmiTvClient.java revision a6b2a7a59ab79b2d91412c1095d1c49b8dc9d507
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 */
16package android.hardware.hdmi;
17
18import android.annotation.SystemApi;
19import android.os.RemoteException;
20import android.util.Log;
21
22import libcore.util.EmptyArray;
23
24/**
25 * HdmiTvClient represents HDMI-CEC logical device of type TV in the Android system
26 * which acts as TV/Display. It provides with methods that manage, interact with other
27 * devices on the CEC bus.
28 *
29 * @hide
30 */
31@SystemApi
32public final class HdmiTvClient extends HdmiClient {
33    private static final String TAG = "HdmiTvClient";
34
35    // Definitions used for setOption(). These should be in sync with the definition
36    // in hardware/libhardware/include/hardware/{hdmi_cec.h,mhl.h}.
37
38    /**
39     * TV gets turned on by incoming <Text/Image View On>. {@code ENABLED} by default.
40     * If set to {@code DISABLED}, TV won't turn on automatically.
41     */
42    public static final int OPTION_CEC_AUTO_WAKEUP = 1;
43
44    /**
45     * If set to {@code DISABLED}, all CEC commands are discarded.
46     *
47     * <p> This option is for internal use only, not supposed to be used by other components.
48     * @hide
49     */
50    public static final int OPTION_CEC_ENABLE = 2;
51
52    /**
53     * If set to {@code DISABLED}, system service yields control of CEC to sub-microcontroller.
54     * If {@code ENABLED}, it take the control back.
55     *
56     * <p> This option is for internal use only, not supposed to be used by other components.
57     * @hide
58     */
59    public static final int OPTION_CEC_SERVICE_CONTROL = 3;
60
61    /**
62     * Put other devices to standby when TV goes to standby. {@code ENABLED} by default.
63     * If set to {@code DISABLED}, TV doesn't send &lt;Standby&gt; to other devices.
64     */
65    public static final int OPTION_CEC_AUTO_DEVICE_OFF = 4;
66
67    /** If set to {@code DISABLED}, TV does not switch ports when mobile device is connected. */
68    public static final int OPTION_MHL_INPUT_SWITCHING = 101;
69
70    /** If set to {@code ENABLED}, TV disables power charging for mobile device. */
71    public static final int OPTION_MHL_POWER_CHARGE = 102;
72
73    /**
74     * If set to {@code DISABLED}, all MHL commands are discarded.
75     *
76     * <p> This option is for internal use only, not supposed to be used by other components.
77     * @hide
78     */
79    public static final int OPTION_MHL_ENABLE = 103;
80
81    public static final int DISABLED = 0;
82    public static final int ENABLED = 1;
83
84    HdmiTvClient(IHdmiControlService service) {
85        super(service);
86    }
87
88    // Factory method for HdmiTvClient.
89    // Declared package-private. Accessed by HdmiControlManager only.
90    static HdmiTvClient create(IHdmiControlService service) {
91        return new HdmiTvClient(service);
92    }
93
94    @Override
95    public int getDeviceType() {
96        return HdmiCecDeviceInfo.DEVICE_TV;
97    }
98
99    /**
100     * Callback interface used to get the result of {@link #deviceSelect}.
101     */
102    public interface SelectCallback {
103        /**
104         * Called when the operation is finished.
105         *
106         * @param result the result value of {@link #deviceSelect}
107         */
108        void onComplete(int result);
109    }
110
111    /**
112     * Select a CEC logical device to be a new active source.
113     *
114     * @param logicalAddress
115     * @param callback
116     */
117    public void deviceSelect(int logicalAddress, SelectCallback callback) {
118        // TODO: Replace SelectCallback with PartialResult.
119        try {
120            mService.deviceSelect(logicalAddress, getCallbackWrapper(callback));
121        } catch (RemoteException e) {
122            Log.e(TAG, "failed to select device: ", e);
123        }
124    }
125
126    /**
127     * Set system audio volume
128     *
129     * @param oldIndex current volume index
130     * @param newIndex volume index to be set
131     * @param maxIndex maximum volume index
132     */
133    public void setSystemAudioVolume(int oldIndex, int newIndex, int maxIndex) {
134        try {
135            mService.setSystemAudioVolume(oldIndex, newIndex, maxIndex);
136        } catch (RemoteException e) {
137            Log.e(TAG, "failed to set volume: ", e);
138        }
139    }
140
141    /**
142     * Set system audio mute status
143     *
144     * @param mute {@code true} if muted; otherwise, {@code false}
145     */
146    public void setSystemAudioMute(boolean mute) {
147        try {
148            mService.setSystemAudioMute(mute);
149        } catch (RemoteException e) {
150            Log.e(TAG, "failed to set mute: ", e);
151        }
152    }
153
154    /**
155     * Callback interface to used to get notified when a record request from recorder device.
156     */
157    public interface RecordRequestListener {
158        /**
159         * Called when tv receives request request from recorder device. When it's called,
160         * it should return record source in byte array so that hdmi control service
161         * can start recording with the given source info.
162         *
163         * @return {@link HdmiRecordSources} to be used to set recording info
164         */
165        HdmiRecordSources.RecordSource onRecordRequestReceived(int recorderAddress);
166    }
167
168    /**
169     * Set {@link RecordRequestListener} to hdmi control service.
170     */
171    public void setRecordRequestListener(RecordRequestListener listener) {
172        try {
173            mService.setRecordRequestListener(getCallbackWrapper(listener));
174        } catch (RemoteException e) {
175            Log.e(TAG, "failed to set record request listener: ", e);
176        }
177    }
178
179    /**
180     * Start recording with the given recorder address and recorder source.
181     * <p>Usage
182     * <pre>
183     * HdmiTvClient tvClient = ....;
184     * // for own source.
185     * OwnSource ownSource = ownHdmiRecordSources.ownSource();
186     * tvClient.startRecord(recorderAddress, ownSource);
187     * </pre>
188     */
189    public void startRecord(int recorderAddress, HdmiRecordSources.RecordSource source) {
190        try {
191            byte[] data = new byte[source.getDataSize(true)];
192            source.toByteArray(true, data, 0);
193            mService.startRecord(recorderAddress, data);
194        } catch (RemoteException e) {
195            Log.e(TAG, "failed to start record: ", e);
196        }
197    }
198
199    private static IHdmiControlCallback getCallbackWrapper(final SelectCallback callback) {
200        return new IHdmiControlCallback.Stub() {
201            @Override
202            public void onComplete(int result) {
203                callback.onComplete(result);
204            }
205        };
206    }
207
208    private static IHdmiRecordRequestListener getCallbackWrapper(
209            final RecordRequestListener listener) {
210        return new IHdmiRecordRequestListener.Stub() {
211            @Override
212            public byte[] onRecordRequestReceived(int recorderAddress) throws RemoteException {
213                HdmiRecordSources.RecordSource source =
214                        listener.onRecordRequestReceived(recorderAddress);
215                if (source == null) {
216                    return EmptyArray.BYTE;
217                }
218                byte[] data = new byte[source.getDataSize(true)];
219                source.toByteArray(true, data, 0);
220                return data;
221            }
222        };
223    }
224}
225