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

Commit a1850699 authored by Hui Peng's avatar Hui Peng
Browse files

[redact_log] Mark the change in BluetoothDevice#toString

Return redacted mac address in BluetoothDevice#toString

This is part of fixes for b/174487588: handling address redaction
in Java layer

Test method:

1. flash a user debug image (API level 33) with this CL on a device

2. Confirm that the change is included by runing the following command

    ```
    adb shell dumpsys platform_compat | grep 265103382
    # expected output
    ChangeId(265103382; name=CHANGE_TO_STRING_REDACTED; enableSinceTargetSdk=10000...
    # if there is no output, it means that change is not included
    ```

3. Use a test app that logs bluetooth mac addresses. In this test,
   the following app is used

   https://github.com/android/connectivity-samples/tree/main/BluetoothChat is used

4. Before enabling this API change, check the logs of bluetooth address in logcat:
   the bluetooth address should be shown as non-redacted

5. After enabling this API change (with the following command), the bluetooth address
   should be shown as redacted in logcat

    ```
    adb shell am compat enable 265103382 com.example.android.bluetoothchat
    ```

Bug: 265103382
Test: see above
Change-Id: I6ae13f5feb3c8136f37d8211b640695a6732fc52
parent 3dd3c107
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -20,7 +20,10 @@ apex {
    manifest: "apex_manifest.json",
    bootclasspath_fragments: ["com.android.btservices-bootclasspath-fragment"],
    systemserverclasspath_fragments: ["com.android.btservices-systemserverclasspath-fragment"],
    compat_configs: ["bluetooth-compat-config"],
    compat_configs: [
        "bluetooth-compat-config",
        "framework-bluetooth-compat-config",
    ],
    apps: ["Bluetooth"],

    multilib: {
+8 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ java_defaults {
    libs: [
        "framework-tethering.stubs.module_lib",
        "unsupportedappusage", // for android.compat.annotation.UnsupportedAppUsage
        "app-compat-annotations",
    ],
    stub_only_libs: ["framework-tethering.stubs.module_lib"],
    srcs: [
@@ -102,6 +103,7 @@ java_defaults {

        // if sdk_version="" this gets automatically included, but here we need to add manually.
        "framework-res",
        "app-compat-annotations",
    ],
    defaults_visibility: ["//visibility:public"],
}
@@ -119,3 +121,9 @@ java_api_contribution {
        "//build/orchestrator/apis",
    ],
}

platform_compat_config
{
    name: "framework-bluetooth-compat-config",
    src: ":framework-bluetooth",
}
+26 −5
Original line number Diff line number Diff line
@@ -27,12 +27,15 @@ import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
import android.annotation.SuppressLint;
import android.annotation.SystemApi;
import android.app.compat.CompatChanges;
import android.bluetooth.annotations.RequiresBluetoothConnectPermission;
import android.bluetooth.annotations.RequiresBluetoothLocationPermission;
import android.bluetooth.annotations.RequiresBluetoothScanPermission;
import android.bluetooth.annotations.RequiresLegacyBluetoothAdminPermission;
import android.bluetooth.annotations.RequiresLegacyBluetoothPermission;
import android.companion.AssociationRequest;
import android.compat.annotation.ChangeId;
import android.compat.annotation.EnabledSince;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.AttributionSource;
import android.content.Context;
@@ -856,6 +859,15 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    public static final String ACTION_PAIRING_REQUEST =
            "android.bluetooth.device.action.PAIRING_REQUEST";

    /**
     * Starting from {@link android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE},
     * the return value of {@link BluetoothDevice#toString()} has changed
     * to improve privacy.
     */
    @ChangeId
    @EnabledSince(targetSdkVersion = android.os.Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
    private static final long CHANGE_TO_STRING_REDACTED = 265103382L;

    /**
     * Broadcast Action: This intent is used to broadcast PAIRING CANCEL
     *
@@ -1422,17 +1434,26 @@ public final class BluetoothDevice implements Parcelable, Attributable {

    /**
     * Returns a string representation of this BluetoothDevice.
     * <p>Currently this is the Bluetooth hardware address, for example
     * "00:11:22:AA:BB:CC". However, you should always use {@link #getAddress}
     * if you explicitly require the Bluetooth hardware address in case the
     * {@link #toString} representation changes in the future.
     * <p> For apps targeting {@link android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE}
     * (API level 34) or higher, this returns the MAC address of the device redacted
     * by replacing the hexadecimal digits of leftmost 4 bytes (in big endian order)
     * with "XX", e.g., "XX:XX:XX:XX:12:34". For apps targeting earlier versions,
     * the MAC address is returned without redaction.
     *
     * Warning: The return value of {@link #toString()} may change in the future.
     * It is intended to be used in logging statements. Thus apps should never rely
     * on the return value of {@link #toString()} in their logic. Always use other
     * appropriate APIs instead (e.g., use {@link #getAddress()} to get the MAC address).
     *
     * @return string representation of this BluetoothDevice
     */
    @Override
    public String toString() {
        if (!CompatChanges.isChangeEnabled(CHANGE_TO_STRING_REDACTED)) {
            return mAddress;
        }
        return toStringForLogging();
    }

    private static boolean shouldLogBeRedacted() {
        boolean defaultValue = true;