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

Commit 4f40b765 authored by Android (Google) Code Review's avatar Android (Google) Code Review
Browse files

Merge change 9602

* changes:
  Add incoming connections to the cache and change authorization check.
parents c93da2c4 9488cbd0
Loading
Loading
Loading
Loading
+12 −8
Original line number Diff line number Diff line
@@ -375,6 +375,10 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub {
    }

    private synchronized void onSinkPropertyChanged(String path, String []propValues) {
        if (!mBluetoothService.isEnabled()) {
            return;
        }

        String name = propValues[0];
        String address = mBluetoothService.getAddressFromObjectPath(path);
        if (address == null) {
@@ -382,17 +386,18 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub {
            return;
        }

        if (mAudioDevices.get(address) == null) {
            // Ignore this state change, since it means we have got it after
            // bluetooth has been disabled.
            return;
        }
        if (name.equals(PROPERTY_STATE)) {
            int state = convertBluezSinkStringtoState(propValues[1]);
            if (mAudioDevices.get(address) == null) {
                // This is for an incoming connection for a device not known to us.
                // We have authorized it and bluez state has changed.
                addAudioSink(address);
            } else {
                int prevState = mAudioDevices.get(address);
                handleSinkStateChange(address, prevState, state);
            }
        }
    }

    private void handleSinkStateChange(String address, int prevState, int state) {
        if (state != prevState) {
@@ -437,7 +442,6 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub {
        return sinks;
    }


    @Override
    protected synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
        if (mAudioDevices.isEmpty()) return;
+10 −2
Original line number Diff line number Diff line
@@ -804,6 +804,15 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub {
        return mBondState.getBondState(address.toUpperCase());
    }

    /*package*/ boolean isRemoteDeviceInCache(String address) {
        return (mRemoteDeviceProperties.get(address) != null);
    }

    /*package*/ String[] getRemoteDeviceProperties(String address) {
        String objectPath = getObjectPathFromAddress(address);
        return (String [])getDevicePropertiesNative(objectPath);
    }

    /*package*/ synchronized String getRemoteDeviceProperty(String address, String property) {
        Map<String, String> properties = mRemoteDeviceProperties.get(address);
        if (properties != null) {
@@ -812,8 +821,7 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub {
            // Query for remote device properties, again.
            // We will need to reload the cache when we switch Bluetooth on / off
            // or if we crash.
            String objectPath = getObjectPathFromAddress(address);
            String propValues[] = (String [])getDevicePropertiesNative(objectPath);
            String[] propValues = getRemoteDeviceProperties(address);
            if (propValues != null) {
                addRemoteDeviceProperties(address, propValues);
                return getRemoteDeviceProperty(address, property);
+37 −14
Original line number Diff line number Diff line
@@ -122,33 +122,41 @@ class BluetoothEventLoop {
        return isEventLoopRunningNative();
    }

    private void onDeviceFound(String address, String[] properties) {
        if (properties == null) {
            Log.e(TAG, "ERROR: Remote device properties are null");
            return;
        }
    private void addDevice(String address, String[] properties) {
        mBluetoothService.addRemoteDeviceProperties(address, properties);
        String rssi = mBluetoothService.getRemoteDeviceProperty(address, "RSSI");
        String classValue = mBluetoothService.getRemoteDeviceProperty(address, "Class");
        String name = mBluetoothService.getRemoteDeviceProperty(address, "Name");

        if (rssi != null && classValue != null) {
        short rssiValue;
        // For incoming connections, we don't get the RSSI value. Use a default of MIN_VALUE.
        // If we accept the pairing, we will automatically show it at the top of the list.
        if (rssi != null) {
            rssiValue = (short)Integer.valueOf(rssi).intValue();
        } else {
            rssiValue = Short.MIN_VALUE;
        }
        if (classValue != null) {
            Intent intent = new Intent(BluetoothIntent.REMOTE_DEVICE_FOUND_ACTION);
            intent.putExtra(BluetoothIntent.ADDRESS, address);
            intent.putExtra(BluetoothIntent.CLASS, Integer.valueOf(classValue));
            intent.putExtra(BluetoothIntent.RSSI, (short)Integer.valueOf(rssi).intValue());
            intent.putExtra(BluetoothIntent.RSSI, rssiValue);
            intent.putExtra(BluetoothIntent.NAME, name);

            mContext.sendBroadcast(intent, BLUETOOTH_PERM);
        } else {
            log ("RSSI: " + rssi + " or ClassValue: " + classValue +
                    " for remote device: " + address + " is null");
            log ("ClassValue: " + classValue + " for remote device: " + address + " is null");
        }
    }

    private void onDeviceDisappeared(String address) {
        mBluetoothService.removeRemoteDeviceProperties(address);
    private void onDeviceFound(String address, String[] properties) {
        if (properties == null) {
            Log.e(TAG, "ERROR: Remote device properties are null");
            return;
        }
        addDevice(address, properties);
    }

    private void onDeviceDisappeared(String address) {
        Intent intent = new Intent(BluetoothIntent.REMOTE_DEVICE_DISAPPEARED_ACTION);
        intent.putExtra(BluetoothIntent.ADDRESS, address);
        mContext.sendBroadcast(intent, BLUETOOTH_PERM);
@@ -208,7 +216,14 @@ class BluetoothEventLoop {
    }

    private void onDeviceCreated(String deviceObjectPath) {
        // do nothing.
        String address = mBluetoothService.getAddressFromObjectPath(deviceObjectPath);
        if (!mBluetoothService.isRemoteDeviceInCache(address)) {
            // Incoming connection, we haven't seen this device, add to cache.
            String[] properties = mBluetoothService.getRemoteDeviceProperties(address);
            if (properties != null) {
                addDevice(address, properties);
            }
        }
        return;
    }

@@ -316,6 +331,13 @@ class BluetoothEventLoop {
                }
            }
            mBluetoothService.setRemoteDeviceProperty(address, name, uuid);
        } else if (name.equals("Paired")) {
            if (propValues[1].equals("true")) {
                mBluetoothService.getBondState().setBondState(address, BluetoothDevice.BOND_BONDED);
            } else {
                mBluetoothService.getBondState().setBondState(address,
                        BluetoothDevice.BOND_NOT_BONDED);
            }
        }
    }

@@ -403,7 +425,8 @@ class BluetoothEventLoop {
        boolean authorized = false;
        UUID uuid = UUID.fromString(deviceUuid);
        if (mBluetoothService.isEnabled() &&
                (BluetoothUuid.isAudioSink(uuid) || BluetoothUuid.isAvrcpController(uuid))) {
                (BluetoothUuid.isAudioSink(uuid) || BluetoothUuid.isAvrcpController(uuid)
                        || BluetoothUuid.isAdvAudioDist(uuid))) {
            BluetoothA2dp a2dp = new BluetoothA2dp(mContext);
            authorized = a2dp.getSinkPriority(address) > BluetoothA2dp.PRIORITY_OFF;
            if (authorized) {