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

Commit 986f57e8 authored by William Escande's avatar William Escande
Browse files

BluetoothManager: override context deviceId

Changing the device association invalidates the context's attribution
source. Caching it causes a crash.

The BluetoothAdapter passes its cached attribution source to
PermissionManager and an exception is thrown because the given context
and attribution source have different device associations.
At this point there's not much that PermissionManager can do to handle
this gracefully.

The correct fix is to:
* In the bluetooth stack never cache an attribution source directly,
  use a Context instance instead, so its attribution source is always
  up to date.

But because of the BluetoothDevice and BluetoothAdapter implementation,
and because of robolectric that prevent us to update the
BluetoothAdapter constructor, we cannot make those change.

Instead createDeviceContext will pin the deviceId and thuss, the
AttributionSource will still match its Context

Bug: 349657939
Bug: 343121936
Test: None
Flag: com.android.bluetooth.flags.override_context_to_specify_device_id
Change-Id: I239af84013f5571a50ef0322f8b71fc1f33e40d7
parent 1a7bcca7
Loading
Loading
Loading
Loading
+6 −8
Original line number Diff line number Diff line
@@ -21,9 +21,7 @@ java_defaults {
    name: "BluetoothShimsDefaults",
    libs: [
        "androidx.annotation_annotation",
    ],
    static_libs : [
        "modules-utils-build_system"
        "modules-utils-build",
    ],
    apex_available: [
        "com.android.btservices",
@@ -40,7 +38,7 @@ java_library {
        "BluetoothShimsDefaults",
    ],
    srcs: ["common/**/*.java"],
    sdk_version: "system_current",
    sdk_version: "module_current",
    visibility: ["//visibility:private"],
}

@@ -70,10 +68,10 @@ java_library {
        "34/**/*.java",
    ],
    libs: [
        "BluetoothShimsCommon",
        "BluetoothApi33Shims",
        "BluetoothShimsCommon",
    ],
    sdk_version: "module_current",
    sdk_version: "module_34",
    visibility: ["//visibility:private"],
}

@@ -83,9 +81,9 @@ java_library {
        "BluetoothShimsDefaults",
    ],
    static_libs: [
        "BluetoothShimsCommon",
        "BluetoothApi33Shims",
        "BluetoothApi34Shims",
        "BluetoothShimsCommon",
    ],
    visibility: [
        "//packages/modules/Bluetooth/android/app",
+1 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ java_defaults {
    static_libs: [
        "PlatformProperties",
        "bluetooth_flags_java_lib",
        "modules-utils-build",
        "modules-utils-expresslog",
        "service-bluetooth-binder-aidl",
    ],
+12 −4
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package android.bluetooth;

import static com.android.modules.utils.build.SdkLevel.isAtLeastU;

import android.annotation.RequiresFeature;
import android.annotation.RequiresNoPermission;
import android.annotation.RequiresPermission;
@@ -56,14 +58,20 @@ public final class BluetoothManager {

    /** @hide */
    public BluetoothManager(Context context) {
        mAdapter = BluetoothAdapter.createAdapter(context.getAttributionSource());
        if (com.android.bluetooth.flags.Flags.overrideContextToSpecifyDeviceId() && isAtLeastU()) {
            // Pin the context DeviceId prevent the associated attribution source to be obsolete
            // TODO: b/343739429 -- pass the context to BluetoothAdapter constructor instead
            mContext = context.createDeviceContext(Context.DEVICE_ID_DEFAULT);
        } else {
            mContext = context;
        }
        mAdapter = BluetoothAdapter.createAdapter(mContext.getAttributionSource());
    }

    /**
     * Get the BLUETOOTH Adapter for this device.
     * Get the BluetoothAdapter for this device.
     *
     * @return the BLUETOOTH Adapter
     * @return the BluetoothAdapter
     */
    @RequiresNoPermission
    public BluetoothAdapter getAdapter() {