HdmiClient.java revision 0608b9328b1c2f804ffb2d4165c34383d34bde2a
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 * {@link HdmiControlService}. 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    protected final IHdmiControlService mService;
20
21    protected abstract int getDeviceType();
22
23    public HdmiClient(IHdmiControlService service) {
24        mService = service;
25    }
26
27    /**
28     * Returns the active source information.
29     *
30     * @return {@link HdmiDeviceInfo} object that describes the active source
31     *         or active routing path
32     */
33    public HdmiDeviceInfo getActiveSource() {
34        try {
35            return mService.getActiveSource();
36        } catch (RemoteException e) {
37            Log.e(TAG, "getActiveSource threw exception ", e);
38        }
39        return null;
40    }
41
42    /**
43     * Send a key event to other logical device.
44     *
45     * @param keyCode key code to send. Defined in {@link android.view.KeyEvent}.
46     * @param isPressed true if this is key press event
47     */
48    public void sendKeyEvent(int keyCode, boolean isPressed) {
49        try {
50            mService.sendKeyEvent(getDeviceType(), keyCode, isPressed);
51        } catch (RemoteException e) {
52            Log.e(TAG, "sendKeyEvent threw exception ", e);
53        }
54    }
55
56    /**
57     * Send vendor-specific command.
58     *
59     * @param targetAddress address of the target device
60     * @param params vendor-specific parameter. For <Vendor Command With ID> do not
61     *               include the first 3 bytes (vendor ID).
62     * @param hasVendorId {@code true} if the command type will be <Vendor Command With ID>.
63     *                    {@code false} if the command will be <Vendor Command>
64     */
65    public void sendVendorCommand(int targetAddress, byte[] params, boolean hasVendorId) {
66        try {
67            mService.sendVendorCommand(getDeviceType(), targetAddress, params, hasVendorId);
68        } catch (RemoteException e) {
69            Log.e(TAG, "failed to send vendor command: ", e);
70        }
71    }
72
73    /**
74     * Add a listener used to receive incoming vendor-specific command.
75     *
76     * @param listener listener object
77     */
78    public void addVendorCommandListener(@NonNull VendorCommandListener listener) {
79        if (listener == null) {
80            throw new IllegalArgumentException("listener cannot be null");
81        }
82        try {
83            mService.addVendorCommandListener(getListenerWrapper(listener), getDeviceType());
84        } catch (RemoteException e) {
85            Log.e(TAG, "failed to add vendor command listener: ", e);
86        }
87    }
88
89    private static IHdmiVendorCommandListener getListenerWrapper(
90            final VendorCommandListener listener) {
91        return new IHdmiVendorCommandListener.Stub() {
92            @Override
93            public void onReceived(int srcAddress, int destAddress, byte[] params,
94                    boolean hasVendorId) {
95                listener.onReceived(srcAddress, destAddress, params, hasVendorId);
96            }
97            @Override
98            public void onControlStateChanged(boolean enabled, int reason) {
99                listener.onControlStateChanged(enabled, reason);
100            }
101        };
102    }
103}
104