Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 09ff3dd6 authored by Himanshu Rawat's avatar Himanshu Rawat
Browse files

HidHostService: Move case handling to individual message handlers

Bug: 324094542
Test: mmm packages/modules/Bluetooth
Flag: EXEMPT no op
Change-Id: I88de5a7f1a0ab3f6b0310388439bc677e2e253cb
parent 77649861
Loading
Loading
Loading
Loading
+229 −203
Original line number Diff line number Diff line
@@ -195,120 +195,141 @@ public class HidHostService extends ProfileService {
                    if (DBG) Log.v(TAG, "handleMessage(): msg.what=" + msg.what);

                    switch (msg.what) {
                        case MESSAGE_CONNECT: {
                                BluetoothDevice device = (BluetoothDevice) msg.obj;
                                // TODO: b/324094542 Use the preferred transport
                                if (!mNativeInterface.connectHid(
                                        getByteAddress(device),
                                        BluetoothDevice.ADDRESS_TYPE_PUBLIC,
                                        BluetoothDevice.TRANSPORT_AUTO)) {
                                    broadcastConnectionState(
                                            device, BluetoothProfile.STATE_DISCONNECTING);
                                    broadcastConnectionState(
                                            device, BluetoothProfile.STATE_DISCONNECTED);
                        case MESSAGE_CONNECT:
                            handleMessageConnect(msg);
                            break;
                        case MESSAGE_DISCONNECT:
                            handleMessageDisconnect(msg);
                            break;
                        case MESSAGE_CONNECT_STATE_CHANGED:
                            handleMessageConnectStateChanged(msg);
                            break;
                        case MESSAGE_GET_PROTOCOL_MODE:
                            handleMessageGetProtocolMode(msg);
                            break;
                        case MESSAGE_ON_GET_PROTOCOL_MODE:
                            handleMessageOnGetProtocolMode(msg);
                            break;
                        case MESSAGE_VIRTUAL_UNPLUG:
                            handleMessageVirtualUnplug(msg);
                            break;
                        case MESSAGE_SET_PROTOCOL_MODE:
                            handleMessageSetProtocolMode(msg);
                            break;
                        case MESSAGE_GET_REPORT:
                            handleMessageGetReport(msg);
                            break;
                        case MESSAGE_ON_GET_REPORT:
                            handleMessageOnGetReport(msg);
                            break;
                        case MESSAGE_ON_HANDSHAKE:
                            handleMessageOnHandshake(msg);
                            break;
                        case MESSAGE_SET_REPORT:
                            handleMessageSetProtocol(msg);
                            break;
                        case MESSAGE_ON_VIRTUAL_UNPLUG:
                            handleMessageOnVirtualUnplug(msg);
                            break;
                        case MESSAGE_GET_IDLE_TIME:
                            handleMessageGetIdleTime(msg);
                            break;
                        case MESSAGE_ON_GET_IDLE_TIME:
                            handleMessageOnGetIdleTime(msg);
                            break;
                        case MESSAGE_SET_IDLE_TIME:
                            handleMessageSetIdleTime(msg);
                            break;
                    }
                                mTargetDevice = device;
                }
                            break;
                        case MESSAGE_DISCONNECT: {
            };

    private void handleMessageSetIdleTime(Message msg) {
        BluetoothDevice device = (BluetoothDevice) msg.obj;
                                boolean reconnectAllowed = false;
                                if (getConnectionPolicy(device)
                                        == BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
                                    reconnectAllowed = true;
                                }
        Bundle data = msg.getData();
        byte idleTime = data.getByte(BluetoothHidHost.EXTRA_IDLE_TIME);
        // TODO: b/324094542 Use the preferred transport
                                if (!mNativeInterface.disconnectHid(
        if (!mNativeInterface.setIdleTime(
                getByteAddress(device),
                BluetoothDevice.ADDRESS_TYPE_PUBLIC,
                BluetoothDevice.TRANSPORT_AUTO,
                                        reconnectAllowed)) {
                                    broadcastConnectionState(
                                            device, BluetoothProfile.STATE_DISCONNECTING);
                                    broadcastConnectionState(
                                            device, BluetoothProfile.STATE_DISCONNECTED);
                                    break;
                                }
                            }
                            break;
                        case MESSAGE_CONNECT_STATE_CHANGED: {
                                BluetoothDevice device =
                                        mAdapterService.getDeviceFromByte((byte[]) msg.obj);
                                int state = msg.arg1;
                                Integer prevStateInteger = mInputDevices.get(device);
                                int prevState =
                                        (prevStateInteger == null)
                                                ? BluetoothProfile.STATE_DISCONNECTED
                                                : prevStateInteger;
                                if (DBG) {
                                    Log.d(
                                            TAG,
                                            "MESSAGE_CONNECT_STATE_CHANGED"
                                                    + (" newState=" + state)
                                                    + (" prevState=" + prevState));
                idleTime)) {
            Log.e(TAG, "Error: get idle time native returns false");
        }
                                if (state == STATE_ACCEPTING) {
                                    // TODO: b/324094542 save the preferred transport
                                    state = BluetoothProfile.STATE_DISCONNECTED;
    }
                                if (state == BluetoothProfile.STATE_CONNECTED
                                        && prevState == BluetoothProfile.STATE_DISCONNECTED
                                        && (!okToConnect(device))) {
                                    if (DBG) {
                                        Log.d(TAG, "Incoming HID connection rejected");

    private void handleMessageOnGetIdleTime(Message msg) {
        BluetoothDevice device = mAdapterService.getDeviceFromByte((byte[]) msg.obj);
        int idleTime = msg.arg1;
        broadcastIdleTime(device, idleTime);
    }

    private void handleMessageGetIdleTime(Message msg) {
        BluetoothDevice device = (BluetoothDevice) msg.obj;
        // TODO: b/324094542 Use the preferred transport
                                    mNativeInterface.virtualUnPlug(
        if (!mNativeInterface.getIdleTime(
                getByteAddress(device),
                BluetoothDevice.ADDRESS_TYPE_PUBLIC,
                                            BluetoothDevice.TRANSPORT_AUTO);
                                } else {
                                    broadcastConnectionState(device, state);
                BluetoothDevice.TRANSPORT_AUTO)) {
            Log.e(TAG, "Error: get idle time native returns false");
        }
                                if (state == BluetoothProfile.STATE_CONNECTED
                                        && (mTargetDevice != null
                                                && mTargetDevice.equals(device))) {
                                    mTargetDevice = null;
                                    // local device originated connection to hid device, move out
                                    // of quiet mode
                                    AdapterService adapterService =
                                            AdapterService.getAdapterService();
                                    adapterService.enable(false);
    }

    private void handleMessageOnVirtualUnplug(Message msg) {
        BluetoothDevice device = mAdapterService.getDeviceFromByte((byte[]) msg.obj);
        int status = msg.arg1;
        broadcastVirtualUnplugStatus(device, status);
    }
                            break;
                        case MESSAGE_GET_PROTOCOL_MODE: {

    private void handleMessageSetProtocol(Message msg) {
        BluetoothDevice device = (BluetoothDevice) msg.obj;
        Bundle data = msg.getData();
        byte reportType = data.getByte(BluetoothHidHost.EXTRA_REPORT_TYPE);
        String report = data.getString(BluetoothHidHost.EXTRA_REPORT);
        // TODO: b/324094542 Use the preferred transport
                                if (!mNativeInterface.getProtocolMode(
        if (!mNativeInterface.setReport(
                getByteAddress(device),
                BluetoothDevice.ADDRESS_TYPE_PUBLIC,
                                        BluetoothDevice.TRANSPORT_AUTO)) {
                                    Log.e(TAG, "Error: get protocol mode native returns false");
                BluetoothDevice.TRANSPORT_AUTO,
                reportType,
                report)) {
            Log.e(TAG, "Error: set report native returns false");
        }
    }
                            break;

                        case MESSAGE_ON_GET_PROTOCOL_MODE: {
                                BluetoothDevice device =
                                        mAdapterService.getDeviceFromByte((byte[]) msg.obj);
                                int protocolMode = msg.arg1;
                                broadcastProtocolMode(device, protocolMode);
    private void handleMessageOnHandshake(Message msg) {
        BluetoothDevice device = mAdapterService.getDeviceFromByte((byte[]) msg.obj);
        int status = msg.arg1;
        broadcastHandshake(device, status);
    }
                            break;
                        case MESSAGE_VIRTUAL_UNPLUG: {

    private void handleMessageOnGetReport(Message msg) {
        BluetoothDevice device = mAdapterService.getDeviceFromByte((byte[]) msg.obj);
        Bundle data = msg.getData();
        byte[] report = data.getByteArray(BluetoothHidHost.EXTRA_REPORT);
        int bufferSize = data.getInt(BluetoothHidHost.EXTRA_REPORT_BUFFER_SIZE);
        broadcastReport(device, report, bufferSize);
    }

    private void handleMessageGetReport(Message msg) {
        BluetoothDevice device = (BluetoothDevice) msg.obj;
        Bundle data = msg.getData();
        byte reportType = data.getByte(BluetoothHidHost.EXTRA_REPORT_TYPE);
        byte reportId = data.getByte(BluetoothHidHost.EXTRA_REPORT_ID);
        int bufferSize = data.getInt(BluetoothHidHost.EXTRA_REPORT_BUFFER_SIZE);
        // TODO: b/324094542 Use the preferred transport
                                if (!mNativeInterface.virtualUnPlug(
        if (!mNativeInterface.getReport(
                getByteAddress(device),
                BluetoothDevice.ADDRESS_TYPE_PUBLIC,
                                        BluetoothDevice.TRANSPORT_AUTO)) {
                                    Log.e(TAG, "Error: virtual unplug native returns false");
                BluetoothDevice.TRANSPORT_AUTO,
                reportType,
                reportId,
                bufferSize)) {
            Log.e(TAG, "Error: get report native returns false");
        }
    }
                            break;
                        case MESSAGE_SET_PROTOCOL_MODE: {

    private void handleMessageSetProtocolMode(Message msg) {
        BluetoothDevice device = (BluetoothDevice) msg.obj;
        byte protocolMode = (byte) msg.arg1;
        Log.d(TAG, "sending set protocol mode(" + protocolMode + ")");
@@ -321,101 +342,106 @@ public class HidHostService extends ProfileService {
            Log.e(TAG, "Error: set protocol mode native returns false");
        }
    }
                            break;
                        case MESSAGE_GET_REPORT: {

    private void handleMessageVirtualUnplug(Message msg) {
        BluetoothDevice device = (BluetoothDevice) msg.obj;
                                Bundle data = msg.getData();
                                byte reportType = data.getByte(BluetoothHidHost.EXTRA_REPORT_TYPE);
                                byte reportId = data.getByte(BluetoothHidHost.EXTRA_REPORT_ID);
                                int bufferSize =
                                        data.getInt(BluetoothHidHost.EXTRA_REPORT_BUFFER_SIZE);
        // TODO: b/324094542 Use the preferred transport
                                if (!mNativeInterface.getReport(
        if (!mNativeInterface.virtualUnPlug(
                getByteAddress(device),
                BluetoothDevice.ADDRESS_TYPE_PUBLIC,
                                        BluetoothDevice.TRANSPORT_AUTO,
                                        reportType,
                                        reportId,
                                        bufferSize)) {
                                    Log.e(TAG, "Error: get report native returns false");
                                }
                BluetoothDevice.TRANSPORT_AUTO)) {
            Log.e(TAG, "Error: virtual unplug native returns false");
        }
                            break;
                        case MESSAGE_ON_GET_REPORT: {
                                BluetoothDevice device =
                                        mAdapterService.getDeviceFromByte((byte[]) msg.obj);
                                Bundle data = msg.getData();
                                byte[] report = data.getByteArray(BluetoothHidHost.EXTRA_REPORT);
                                int bufferSize =
                                        data.getInt(BluetoothHidHost.EXTRA_REPORT_BUFFER_SIZE);
                                broadcastReport(device, report, bufferSize);
    }
                            break;
                        case MESSAGE_ON_HANDSHAKE: {
                                BluetoothDevice device =
                                        mAdapterService.getDeviceFromByte((byte[]) msg.obj);
                                int status = msg.arg1;
                                broadcastHandshake(device, status);

    private void handleMessageOnGetProtocolMode(Message msg) {
        BluetoothDevice device = mAdapterService.getDeviceFromByte((byte[]) msg.obj);
        int protocolMode = msg.arg1;
        broadcastProtocolMode(device, protocolMode);
    }
                            break;
                        case MESSAGE_SET_REPORT: {

    private void handleMessageGetProtocolMode(Message msg) {
        BluetoothDevice device = (BluetoothDevice) msg.obj;
                                Bundle data = msg.getData();
                                byte reportType = data.getByte(BluetoothHidHost.EXTRA_REPORT_TYPE);
                                String report = data.getString(BluetoothHidHost.EXTRA_REPORT);
        // TODO: b/324094542 Use the preferred transport
                                if (!mNativeInterface.setReport(
        if (!mNativeInterface.getProtocolMode(
                getByteAddress(device),
                BluetoothDevice.ADDRESS_TYPE_PUBLIC,
                                        BluetoothDevice.TRANSPORT_AUTO,
                                        reportType,
                                        report)) {
                                    Log.e(TAG, "Error: set report native returns false");
                BluetoothDevice.TRANSPORT_AUTO)) {
            Log.e(TAG, "Error: get protocol mode native returns false");
        }
    }
                            break;
                        case MESSAGE_ON_VIRTUAL_UNPLUG: {
                                BluetoothDevice device =
                                        mAdapterService.getDeviceFromByte((byte[]) msg.obj);
                                int status = msg.arg1;
                                broadcastVirtualUnplugStatus(device, status);

    private void handleMessageConnectStateChanged(Message msg) {
        BluetoothDevice device = mAdapterService.getDeviceFromByte((byte[]) msg.obj);
        int state = msg.arg1;
        Integer prevStateInteger = mInputDevices.get(device);
        int prevState =
                (prevStateInteger == null) ? BluetoothProfile.STATE_DISCONNECTED : prevStateInteger;
        if (DBG) {
            Log.d(
                    TAG,
                    "MESSAGE_CONNECT_STATE_CHANGED"
                            + (" newState=" + state)
                            + (" prevState=" + prevState));
        }
        if (state == STATE_ACCEPTING) {
            // TODO: b/324094542 save the preferred transport
            state = BluetoothProfile.STATE_DISCONNECTED;
        }
        if (state == BluetoothProfile.STATE_CONNECTED
                && prevState == BluetoothProfile.STATE_DISCONNECTED
                && (!okToConnect(device))) {
            if (DBG) {
                Log.d(TAG, "Incoming HID connection rejected");
            }
                            break;
                        case MESSAGE_GET_IDLE_TIME: {
                                BluetoothDevice device = (BluetoothDevice) msg.obj;
            // TODO: b/324094542 Use the preferred transport
                                if (!mNativeInterface.getIdleTime(
            mNativeInterface.virtualUnPlug(
                    getByteAddress(device),
                    BluetoothDevice.ADDRESS_TYPE_PUBLIC,
                                        BluetoothDevice.TRANSPORT_AUTO)) {
                                    Log.e(TAG, "Error: get idle time native returns false");
                    BluetoothDevice.TRANSPORT_AUTO);
        } else {
            broadcastConnectionState(device, state);
        }
        if (state == BluetoothProfile.STATE_CONNECTED
                && (mTargetDevice != null && mTargetDevice.equals(device))) {
            mTargetDevice = null;
            // local device originated connection to hid device, move out
            // of quiet mode
            AdapterService adapterService = AdapterService.getAdapterService();
            adapterService.enable(false);
        }
                            break;
                        case MESSAGE_ON_GET_IDLE_TIME: {
                                BluetoothDevice device =
                                        mAdapterService.getDeviceFromByte((byte[]) msg.obj);
                                int idleTime = msg.arg1;
                                broadcastIdleTime(device, idleTime);
    }
                            break;
                        case MESSAGE_SET_IDLE_TIME: {

    private void handleMessageDisconnect(Message msg) {
        BluetoothDevice device = (BluetoothDevice) msg.obj;
                                Bundle data = msg.getData();
                                byte idleTime = data.getByte(BluetoothHidHost.EXTRA_IDLE_TIME);
        boolean reconnectAllowed = false;
        if (getConnectionPolicy(device) == BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
            reconnectAllowed = true;
        }
        // TODO: b/324094542 Use the preferred transport
                                if (!mNativeInterface.setIdleTime(
        if (!mNativeInterface.disconnectHid(
                getByteAddress(device),
                BluetoothDevice.ADDRESS_TYPE_PUBLIC,
                BluetoothDevice.TRANSPORT_AUTO,
                                        idleTime)) {
                                    Log.e(TAG, "Error: get idle time native returns false");
                reconnectAllowed)) {
            broadcastConnectionState(device, BluetoothProfile.STATE_DISCONNECTING);
            broadcastConnectionState(device, BluetoothProfile.STATE_DISCONNECTED);
        }
    }
                            break;

    private void handleMessageConnect(Message msg) {
        BluetoothDevice device = (BluetoothDevice) msg.obj;
        // TODO: b/324094542 Use the preferred transport
        if (!mNativeInterface.connectHid(
                getByteAddress(device),
                BluetoothDevice.ADDRESS_TYPE_PUBLIC,
                BluetoothDevice.TRANSPORT_AUTO)) {
            broadcastConnectionState(device, BluetoothProfile.STATE_DISCONNECTING);
            broadcastConnectionState(device, BluetoothProfile.STATE_DISCONNECTED);
            return;
        }
        mTargetDevice = device;
    }
            };

    /**
     * Handlers for incoming service calls