HdmiTvClient.java revision 5008486b09c588bf3409b70d9ee29225e8593c64
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.NonNull;
19import android.annotation.SystemApi;
20import android.hardware.hdmi.HdmiRecordSources.RecordSource;
21import android.hardware.hdmi.HdmiTimerRecordSources.TimerRecordSource;
22import android.os.RemoteException;
23import android.util.Log;
24
25import libcore.util.EmptyArray;
26
27/**
28 * HdmiTvClient represents HDMI-CEC logical device of type TV in the Android system
29 * which acts as TV/Display. It provides with methods that manage, interact with other
30 * devices on the CEC bus.
31 *
32 * @hide
33 */
34@SystemApi
35public final class HdmiTvClient extends HdmiClient {
36    private static final String TAG = "HdmiTvClient";
37
38    HdmiTvClient(IHdmiControlService service) {
39        super(service);
40    }
41
42    // Factory method for HdmiTvClient.
43    // Declared package-private. Accessed by HdmiControlManager only.
44    static HdmiTvClient create(IHdmiControlService service) {
45        return new HdmiTvClient(service);
46    }
47
48    @Override
49    public int getDeviceType() {
50        return HdmiDeviceInfo.DEVICE_TV;
51    }
52
53    /**
54     * Callback interface used to get the result of {@link #deviceSelect}.
55     */
56    public interface SelectCallback {
57        /**
58         * Called when the operation is finished.
59         *
60         * @param result the result value of {@link #deviceSelect}
61         */
62        void onComplete(int result);
63    }
64
65    /**
66     * Select a CEC logical device to be a new active source.
67     *
68     * @param logicalAddress logical address of the device to select
69     * @param callback callback to get the result with
70     * @throws {@link IllegalArgumentException} if the {@code callback} is null
71     */
72    public void deviceSelect(int logicalAddress, @NonNull SelectCallback callback) {
73        if (callback == null) {
74            throw new IllegalArgumentException("callback must not be null.");
75        }
76        try {
77            mService.deviceSelect(logicalAddress, getCallbackWrapper(callback));
78        } catch (RemoteException e) {
79            Log.e(TAG, "failed to select device: ", e);
80        }
81    }
82
83    /**
84     * Select a HDMI port to be a new route path.
85     *
86     * @param portId HDMI port to select
87     * @param callback callback to get the result with
88     * @throws {@link IllegalArgumentException} if the {@code callback} is null
89     */
90    public void portSelect(int portId, @NonNull SelectCallback callback) {
91        if (callback == null) {
92            throw new IllegalArgumentException("Callback must not be null");
93        }
94        try {
95            mService.portSelect(portId, getCallbackWrapper(callback));
96        } catch (RemoteException e) {
97            Log.e(TAG, "failed to select port: ", e);
98        }
99    }
100
101    /**
102     * Set system audio volume
103     *
104     * @param oldIndex current volume index
105     * @param newIndex volume index to be set
106     * @param maxIndex maximum volume index
107     */
108    public void setSystemAudioVolume(int oldIndex, int newIndex, int maxIndex) {
109        try {
110            mService.setSystemAudioVolume(oldIndex, newIndex, maxIndex);
111        } catch (RemoteException e) {
112            Log.e(TAG, "failed to set volume: ", e);
113        }
114    }
115
116    /**
117     * Set system audio mute status
118     *
119     * @param mute {@code true} if muted; otherwise, {@code false}
120     */
121    public void setSystemAudioMute(boolean mute) {
122        try {
123            mService.setSystemAudioMute(mute);
124        } catch (RemoteException e) {
125            Log.e(TAG, "failed to set mute: ", e);
126        }
127    }
128
129    /**
130     * Set record listener
131     *
132     * @param listener
133     */
134    public void setRecordListener(@NonNull HdmiRecordListener listener) {
135        if (listener == null) {
136            throw new IllegalArgumentException("listener must not be null.");
137        }
138        try {
139            mService.setHdmiRecordListener(getListenerWrapper(listener));
140        } catch (RemoteException e) {
141            Log.e(TAG, "failed to set record listener.", e);
142        }
143    }
144
145    /**
146     * Start one touch recording with the given recorder address and recorder source.
147     * <p>
148     * Usage
149     * <pre>
150     * HdmiTvClient tvClient = ....;
151     * // for own source.
152     * OwnSource ownSource = ownHdmiRecordSources.ownSource();
153     * tvClient.startOneTouchRecord(recorderAddress, ownSource);
154     * </pre>
155     */
156    public void startOneTouchRecord(int recorderAddress, @NonNull RecordSource source) {
157        if (source == null) {
158            throw new IllegalArgumentException("source must not be null.");
159        }
160
161        try {
162            byte[] data = new byte[source.getDataSize(true)];
163            source.toByteArray(true, data, 0);
164            mService.startOneTouchRecord(recorderAddress, data);
165        } catch (RemoteException e) {
166            Log.e(TAG, "failed to start record: ", e);
167        }
168    }
169
170    /**
171     * Stop one touch record.
172     *
173     * @param recorderAddress recorder address where recoding will be stopped
174     */
175    public void stopOneTouchRecord(int recorderAddress) {
176        try {
177            mService.stopOneTouchRecord(recorderAddress);
178        } catch (RemoteException e) {
179            Log.e(TAG, "failed to stop record: ", e);
180        }
181    }
182
183    /**
184     * Start timer recording with the given recoder address and recorder source.
185     * <p>
186     * Usage
187     * <pre>
188     * HdmiTvClient tvClient = ....;
189     * // create timer info
190     * TimerInfo timerInfo = HdmiTimerRecourdSources.timerInfoOf(...);
191     * // for digital source.
192     * DigitalServiceSource recordSource = HdmiRecordSources.ofDigitalService(...);
193     * // create timer recording source.
194     * TimerRecordSource source = HdmiTimerRecourdSources.ofDigitalSource(timerInfo, recordSource);
195     * tvClient.startTimerRecording(recorderAddress, source);
196     * </pre>
197     *
198     * @param recorderAddress target recorder address
199     * @param sourceType type of record source. It should be one of
200     *          {@link HdmiControlManager#TIMER_RECORDING_TYPE_DIGITAL},
201     *          {@link HdmiControlManager#TIMER_RECORDING_TYPE_ANALOGUE},
202     *          {@link HdmiControlManager#TIMER_RECORDING_TYPE_EXTERNAL}.
203     * @param source record source to be used
204     */
205    public void startTimerRecording(int recorderAddress, int sourceType, TimerRecordSource source) {
206        if (source == null) {
207            throw new IllegalArgumentException("source must not be null.");
208        }
209
210        checkTimerRecordingSourceType(sourceType);
211
212        try {
213            byte[] data = new byte[source.getDataSize()];
214            source.toByteArray(data, 0);
215            mService.startTimerRecording(recorderAddress, sourceType, data);
216        } catch (RemoteException e) {
217            Log.e(TAG, "failed to start record: ", e);
218        }
219    }
220
221    private void checkTimerRecordingSourceType(int sourceType) {
222        switch (sourceType) {
223            case HdmiControlManager.TIMER_RECORDING_TYPE_DIGITAL:
224            case HdmiControlManager.TIMER_RECORDING_TYPE_ANALOGUE:
225            case HdmiControlManager.TIMER_RECORDING_TYPE_EXTERNAL:
226                break;
227            default:
228                throw new IllegalArgumentException("Invalid source type:" + sourceType);
229        }
230    }
231
232    /**
233     * Clear timer recording with the given recorder address and recording source.
234     * For more details, please refer {@link #startTimerRecording(int, int, TimerRecordSource)}.
235     */
236    public void clearTimerRecording(int recorderAddress, int sourceType, TimerRecordSource source) {
237        if (source == null) {
238            throw new IllegalArgumentException("source must not be null.");
239        }
240
241        checkTimerRecordingSourceType(sourceType);
242        try {
243            byte[] data = new byte[source.getDataSize()];
244            source.toByteArray(data, 0);
245            mService.clearTimerRecording(recorderAddress, sourceType, data);
246        } catch (RemoteException e) {
247            Log.e(TAG, "failed to start record: ", e);
248        }
249    }
250
251    private static IHdmiControlCallback getCallbackWrapper(final SelectCallback callback) {
252        return new IHdmiControlCallback.Stub() {
253            @Override
254            public void onComplete(int result) {
255                callback.onComplete(result);
256            }
257        };
258    }
259
260    private static IHdmiRecordListener getListenerWrapper(final HdmiRecordListener callback) {
261        return new IHdmiRecordListener.Stub() {
262            @Override
263            public byte[] getOneTouchRecordSource(int recorderAddress) {
264                HdmiRecordSources.RecordSource source =
265                        callback.getOneTouchRecordSource(recorderAddress);
266                if (source == null) {
267                    return EmptyArray.BYTE;
268                }
269                byte[] data = new byte[source.getDataSize(true)];
270                source.toByteArray(true, data, 0);
271                return data;
272            }
273
274            @Override
275            public void onOneTouchRecordResult(int result) {
276                callback.onOneTouchRecordResult(result);
277            }
278
279            @Override
280            public void onTimerRecordingResult(int result) {
281                callback.onTimerRecordingResult(
282                        HdmiRecordListener.TimerStatusData.parseFrom(result));
283            }
284
285            @Override
286            public void onClearTimerRecordingResult(int result) {
287                callback.onClearTimerRecordingResult(result);
288            }
289        };
290    }
291}
292