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

Commit 0ba75ae1 authored by Rishab's avatar Rishab
Browse files

Improve Bluetooth logging to include all remote devices interacting with Bluetooth

Bug: 378971581
Test: m com.android.btservices and statsd_testdrive
Flag: EXEMPT, Metrics Logging
Change-Id: I82cc34f2501dc39a05b65c65c2775bfb4e29892d
parent 910db1be
Loading
Loading
Loading
Loading
+4 −19
Original line number Diff line number Diff line
@@ -75,7 +75,6 @@ import com.android.bluetooth.BluetoothStatsLog;
import com.android.bluetooth.BtRestrictedStatsLog;
import com.android.bluetooth.Utils;
import com.android.bluetooth.bass_client.BassConstants;
import com.android.modules.utils.build.SdkLevel;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Ascii;
@@ -343,18 +342,11 @@ public class MetricsLogger {
        BluetoothDevice device = connIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        int state = connIntent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1);
        int metricId = mAdapterService.getMetricId(device);
        boolean includeMedicalDevices = false;
        byte[] remoteDeviceInfoBytes = getRemoteDeviceInfoProto(device, includeMedicalDevices);
        if (state == BluetoothProfile.STATE_CONNECTING) {
            String deviceName = mRemoteDevices.getName(device);
            BluetoothStatsLog.write(
                    BluetoothStatsLog.BLUETOOTH_DEVICE_NAME_REPORTED, metricId, deviceName);
            BluetoothStatsLog.write(
                    BluetoothStatsLog.REMOTE_DEVICE_INFORMATION_WITH_METRIC_ID,
                    metricId,
                    remoteDeviceInfoBytes);

            logAllowlistedDeviceNameHash(metricId, deviceName, true);
            logAllowlistedDeviceNameHash(metricId, deviceName);
        }
        BluetoothStatsLog.write(
                BluetoothStatsLog.BLUETOOTH_CONNECTION_STATE_CHANGED,
@@ -605,7 +597,7 @@ public class MetricsLogger {
        return Integer.parseInt(device.getAddress().replace(":", "").substring(0, 6), 16);
    }

    private List<String> getWordBreakdownList(String deviceName) {
    protected List<String> getWordBreakdownList(String deviceName) {
        if (deviceName == null) {
            return Collections.emptyList();
        }
@@ -634,7 +626,7 @@ public class MetricsLogger {
    }

    @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
    private void uploadRestrictedBluetothDeviceName(List<String> wordBreakdownList) {
    protected void uploadRestrictedBluetothDeviceName(List<String> wordBreakdownList) {
        for (String word : wordBreakdownList) {
            BtRestrictedStatsLog.write(RESTRICTED_BLUETOOTH_DEVICE_NAME_REPORTED, word);
        }
@@ -759,17 +751,10 @@ public class MetricsLogger {
        return getSha256String(matchedString);
    }

    protected String logAllowlistedDeviceNameHash(
            int metricId, String deviceName, boolean logRestrictedNames) {
    protected String logAllowlistedDeviceNameHash(int metricId, String deviceName) {
        List<String> wordBreakdownList = getWordBreakdownList(deviceName);
        boolean includeMedicalDevices = false;
        String matchedString = getMatchedString(wordBreakdownList, includeMedicalDevices);
        if (logRestrictedNames) {
            // Log the restricted bluetooth device name
            if (SdkLevel.isAtLeastU()) {
                uploadRestrictedBluetothDeviceName(wordBreakdownList);
            }
        }
        if (!matchedString.isEmpty()) {
            statslogBluetoothDeviceNames(metricId, matchedString);
        }
+14 −0
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ import com.android.bluetooth.bas.BatteryService;
import com.android.bluetooth.flags.Flags;
import com.android.bluetooth.hfp.HeadsetHalConstants;
import com.android.internal.annotations.VisibleForTesting;
import com.android.modules.utils.build.SdkLevel;

import java.nio.charset.StandardCharsets;
import java.util.ArrayDeque;
@@ -946,6 +947,12 @@ public class RemoteDevices {
                                break;
                            }
                            deviceProperties.setName(newName);
                            List<String> wordBreakdownList =
                                    MetricsLogger.getInstance().getWordBreakdownList(newName);
                            if (SdkLevel.isAtLeastU()) {
                                MetricsLogger.getInstance()
                                        .uploadRestrictedBluetothDeviceName(wordBreakdownList);
                            }
                            intent = new Intent(BluetoothDevice.ACTION_NAME_CHANGED);
                            intent.putExtra(BluetoothDevice.EXTRA_DEVICE, bdDevice);
                            intent.putExtra(BluetoothDevice.EXTRA_NAME, deviceProperties.getName());
@@ -1295,6 +1302,13 @@ public class RemoteDevices {
                getBluetoothClass(device),
                metricId);

        byte[] remoteDeviceInfoBytes = MetricsLogger.getInstance().getRemoteDeviceInfoProto(device);

        BluetoothStatsLog.write(
                BluetoothStatsLog.REMOTE_DEVICE_INFORMATION_WITH_METRIC_ID,
                metricId,
                remoteDeviceInfoBytes);

        if (intent == null) {
            Log.e(TAG, "aclStateChangeCallback intent is null. BondState: " + getBondState(device));
            return;
+2 −2
Original line number Diff line number Diff line
@@ -246,7 +246,7 @@ public class MetricsLoggerTest {
            Assert.assertEquals(
                    deviceName,
                    sha256,
                    mTestableMetricsLogger.logAllowlistedDeviceNameHash(1, deviceName, true));
                    mTestableMetricsLogger.logAllowlistedDeviceNameHash(1, deviceName));
        }
    }

@@ -294,7 +294,7 @@ public class MetricsLoggerTest {
    @Test
    public void uploadEmptyDeviceName() {
        initTestingBloomfilter();
        Assert.assertEquals("", mTestableMetricsLogger.logAllowlistedDeviceNameHash(1, "", true));
        Assert.assertEquals("", mTestableMetricsLogger.logAllowlistedDeviceNameHash(1, ""));
    }

    private void initTestingBloomfilter() {