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