HdmiClient.java revision f424932cfb1b16b01a37500d09e295912700a51d
1package android.hardware.hdmi;
2
3import android.annotation.NonNull;
4import android.annotation.SystemApi;
5import android.hardware.hdmi.HdmiControlManager.VendorCommandListener;
6import android.hardware.hdmi.IHdmiVendorCommandListener;
7import android.os.RemoteException;
8import android.util.Log;
9
10/**
11 * Parent for classes of various HDMI-CEC device type used to access
12 * {@link HdmiControlService}. Contains methods and data used in common.
13 *
14 * @hide
15 */
16@SystemApi
17public abstract class HdmiClient {
18    private static final String TAG = "HdmiClient";
19
20    protected final IHdmiControlService mService;
21
22    protected abstract int getDeviceType();
23
24    public HdmiClient(IHdmiControlService service) {
25        mService = service;
26    }
27
28    /**
29     * Returns the active source information.
30     *
31     * @return {@link HdmiDeviceInfo} object that describes the active source
32     *         or active routing path
33     */
34    public HdmiDeviceInfo getActiveSource() {
35        try {
36            return mService.getActiveSource();
37        } catch (RemoteException e) {
38            Log.e(TAG, "getActiveSource threw exception ", e);
39        }
40        return null;
41    }
42
43    /**
44     * Send a key event to other logical device.
45     *
46     * @param keyCode key code to send. Defined in {@link android.view.KeyEvent}.
47     * @param isPressed true if this is key press event
48     */
49    public void sendKeyEvent(int keyCode, boolean isPressed) {
50        try {
51            mService.sendKeyEvent(getDeviceType(), keyCode, isPressed);
52        } catch (RemoteException e) {
53            Log.e(TAG, "sendKeyEvent threw exception ", e);
54        }
55    }
56
57    /**
58     * Send vendor-specific command.
59     *
60     * @param targetAddress address of the target device
61     * @param params vendor-specific parameter. For <Vendor Command With ID> do not
62     *               include the first 3 bytes (vendor ID).
63     * @param hasVendorId {@code true} if the command type will be <Vendor Command With ID>.
64     *                    {@code false} if the command will be <Vendor Command>
65     */
66    public void sendVendorCommand(int targetAddress, byte[] params, boolean hasVendorId) {
67        try {
68            mService.sendVendorCommand(getDeviceType(), targetAddress, params, hasVendorId);
69        } catch (RemoteException e) {
70            Log.e(TAG, "failed to send vendor command: ", e);
71        }
72    }
73
74    /**
75     * Add a listener used to receive incoming vendor-specific command.
76     *
77     * @param listener listener object
78     */
79    public void addVendorCommandListener(@NonNull VendorCommandListener listener) {
80        if (listener == null) {
81            throw new IllegalArgumentException("listener cannot be null");
82        }
83        try {
84            mService.addVendorCommandListener(getListenerWrapper(listener), getDeviceType());
85        } catch (RemoteException e) {
86            Log.e(TAG, "failed to add vendor command listener: ", e);
87        }
88    }
89
90    private static IHdmiVendorCommandListener getListenerWrapper(
91            final VendorCommandListener listener) {
92        return new IHdmiVendorCommandListener.Stub() {
93            @Override
94            public void onReceived(int srcAddress, byte[] params, boolean hasVendorId) {
95                listener.onReceived(srcAddress, params, hasVendorId);
96            }
97        };
98    }
99}
100