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

Commit 97011c3d authored by Chienyuan's avatar Chienyuan Committed by Chienyuan Huang
Browse files

HID HOST: Choose proper address for native command/callback

Tag: #refactor
Bug: 197044261
Test: manual

Change-Id: I32f910abbe1030bc837508482c0e07f22fa356d7
parent 10cc7a73
Loading
Loading
Loading
Loading
+28 −24
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ import static android.Manifest.permission.BLUETOOTH_CONNECT;
import static com.android.bluetooth.Utils.enforceBluetoothPrivilegedPermission;

import android.annotation.RequiresPermission;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHidHost;
import android.bluetooth.BluetoothProfile;
@@ -66,6 +65,7 @@ public class HidHostService extends ProfileService {
    private BluetoothDevice mTargetDevice = null;

    private DatabaseManager mDatabaseManager;
    private AdapterService mAdapterService;

    private static final int MESSAGE_CONNECT = 1;
    private static final int MESSAGE_DISCONNECT = 2;
@@ -96,6 +96,8 @@ public class HidHostService extends ProfileService {
    protected boolean start() {
        mDatabaseManager = Objects.requireNonNull(AdapterService.getAdapterService().getDatabase(),
                "DatabaseManager cannot be null when HidHostService starts");
        mAdapterService = Objects.requireNonNull(AdapterService.getAdapterService(),
                "AdapterService cannot be null when HidHostService starts");

        mInputDevices = Collections.synchronizedMap(new HashMap<BluetoothDevice, Integer>());
        initializeNative();
@@ -133,6 +135,14 @@ public class HidHostService extends ProfileService {
        setHidHostService(null);
    }

    private BluetoothDevice getDevice(byte[] address) {
        return mAdapterService.getDeviceFromByte(address);
    }

    private byte[] getByteAddress(BluetoothDevice device) {
        return mAdapterService.getByteIdentityAddress(device);
    }

    public static synchronized HidHostService getHidHostService() {
        if (sHidHostService == null) {
            Log.w(TAG, "getHidHostService(): service is null");
@@ -161,7 +171,7 @@ public class HidHostService extends ProfileService {
            switch (msg.what) {
                case MESSAGE_CONNECT: {
                    BluetoothDevice device = (BluetoothDevice) msg.obj;
                    if (!connectHidNative(Utils.getByteAddress(device))) {
                    if (!connectHidNative(getByteAddress(device))) {
                        broadcastConnectionState(device, BluetoothProfile.STATE_DISCONNECTING);
                        broadcastConnectionState(device, BluetoothProfile.STATE_DISCONNECTED);
                        break;
@@ -171,7 +181,7 @@ public class HidHostService extends ProfileService {
                break;
                case MESSAGE_DISCONNECT: {
                    BluetoothDevice device = (BluetoothDevice) msg.obj;
                    if (!disconnectHidNative(Utils.getByteAddress(device))) {
                    if (!disconnectHidNative(getByteAddress(device))) {
                        broadcastConnectionState(device, BluetoothProfile.STATE_DISCONNECTING);
                        broadcastConnectionState(device, BluetoothProfile.STATE_DISCONNECTED);
                        break;
@@ -179,8 +189,7 @@ public class HidHostService extends ProfileService {
                }
                break;
                case MESSAGE_CONNECT_STATE_CHANGED: {
                    BluetoothDevice device = BluetoothAdapter.getDefaultAdapter()
                            .getRemoteDevice((byte[]) msg.obj);
                    BluetoothDevice device = mAdapterService.getDeviceFromByte((byte[]) msg.obj);
                    int halState = msg.arg1;
                    Integer prevStateInteger = mInputDevices.get(device);
                    int prevState =
@@ -196,7 +205,7 @@ public class HidHostService extends ProfileService {
                        if (DBG) {
                            Log.d(TAG, "Incoming HID connection rejected");
                        }
                        virtualUnPlugNative(Utils.getByteAddress(device));
                        virtualUnPlugNative(getByteAddress(device));
                    } else {
                        broadcastConnectionState(device, convertHalState(halState));
                    }
@@ -212,22 +221,21 @@ public class HidHostService extends ProfileService {
                break;
                case MESSAGE_GET_PROTOCOL_MODE: {
                    BluetoothDevice device = (BluetoothDevice) msg.obj;
                    if (!getProtocolModeNative(Utils.getByteAddress(device))) {
                    if (!getProtocolModeNative(getByteAddress(device))) {
                        Log.e(TAG, "Error: get protocol mode native returns false");
                    }
                }
                break;

                case MESSAGE_ON_GET_PROTOCOL_MODE: {
                    BluetoothDevice device = BluetoothAdapter.getDefaultAdapter()
                            .getRemoteDevice((byte[]) msg.obj);
                    BluetoothDevice device = mAdapterService.getDeviceFromByte((byte[]) msg.obj);
                    int protocolMode = msg.arg1;
                    broadcastProtocolMode(device, protocolMode);
                }
                break;
                case MESSAGE_VIRTUAL_UNPLUG: {
                    BluetoothDevice device = (BluetoothDevice) msg.obj;
                    if (!virtualUnPlugNative(Utils.getByteAddress(device))) {
                    if (!virtualUnPlugNative(getByteAddress(device))) {
                        Log.e(TAG, "Error: virtual unplug native returns false");
                    }
                }
@@ -236,7 +244,7 @@ public class HidHostService extends ProfileService {
                    BluetoothDevice device = (BluetoothDevice) msg.obj;
                    byte protocolMode = (byte) msg.arg1;
                    Log.d(TAG, "sending set protocol mode(" + protocolMode + ")");
                    if (!setProtocolModeNative(Utils.getByteAddress(device), protocolMode)) {
                    if (!setProtocolModeNative(getByteAddress(device), protocolMode)) {
                        Log.e(TAG, "Error: set protocol mode native returns false");
                    }
                }
@@ -247,15 +255,14 @@ public class HidHostService extends ProfileService {
                    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);
                    if (!getReportNative(Utils.getByteAddress(device), reportType, reportId,
                    if (!getReportNative(getByteAddress(device), reportType, reportId,
                            bufferSize)) {
                        Log.e(TAG, "Error: get report native returns false");
                    }
                }
                break;
                case MESSAGE_ON_GET_REPORT: {
                    BluetoothDevice device = BluetoothAdapter.getDefaultAdapter()
                            .getRemoteDevice((byte[]) msg.obj);
                    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);
@@ -263,8 +270,7 @@ public class HidHostService extends ProfileService {
                }
                break;
                case MESSAGE_ON_HANDSHAKE: {
                    BluetoothDevice device = BluetoothAdapter.getDefaultAdapter()
                            .getRemoteDevice((byte[]) msg.obj);
                    BluetoothDevice device = mAdapterService.getDeviceFromByte((byte[]) msg.obj);
                    int status = msg.arg1;
                    broadcastHandshake(device, status);
                }
@@ -274,28 +280,26 @@ public class HidHostService extends ProfileService {
                    Bundle data = msg.getData();
                    byte reportType = data.getByte(BluetoothHidHost.EXTRA_REPORT_TYPE);
                    String report = data.getString(BluetoothHidHost.EXTRA_REPORT);
                    if (!setReportNative(Utils.getByteAddress(device), reportType, report)) {
                    if (!setReportNative(getByteAddress(device), reportType, report)) {
                        Log.e(TAG, "Error: set report native returns false");
                    }
                }
                break;
                case MESSAGE_ON_VIRTUAL_UNPLUG: {
                    BluetoothDevice device = BluetoothAdapter.getDefaultAdapter()
                            .getRemoteDevice((byte[]) msg.obj);
                    BluetoothDevice device = mAdapterService.getDeviceFromByte((byte[]) msg.obj);
                    int status = msg.arg1;
                    broadcastVirtualUnplugStatus(device, status);
                }
                break;
                case MESSAGE_GET_IDLE_TIME: {
                    BluetoothDevice device = (BluetoothDevice) msg.obj;
                    if (!getIdleTimeNative(Utils.getByteAddress(device))) {
                    if (!getIdleTimeNative(getByteAddress(device))) {
                        Log.e(TAG, "Error: get idle time native returns false");
                    }
                }
                break;
                case MESSAGE_ON_GET_IDLE_TIME: {
                    BluetoothDevice device = BluetoothAdapter.getDefaultAdapter()
                            .getRemoteDevice((byte[]) msg.obj);
                    BluetoothDevice device = mAdapterService.getDeviceFromByte((byte[]) msg.obj);
                    int idleTime = msg.arg1;
                    broadcastIdleTime(device, idleTime);
                }
@@ -304,7 +308,7 @@ public class HidHostService extends ProfileService {
                    BluetoothDevice device = (BluetoothDevice) msg.obj;
                    Bundle data = msg.getData();
                    byte idleTime = data.getByte(BluetoothHidHost.EXTRA_IDLE_TIME);
                    if (!setIdleTimeNative(Utils.getByteAddress(device), idleTime)) {
                    if (!setIdleTimeNative(getByteAddress(device), idleTime)) {
                        Log.e(TAG, "Error: get idle time native returns false");
                    }
                }
@@ -778,7 +782,7 @@ public class HidHostService extends ProfileService {
            return false;
        }

        return sendDataNative(Utils.getByteAddress(device), report);
        return sendDataNative(getByteAddress(device), report);
    }

    boolean getIdleTime(BluetoothDevice device) {