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

Commit e2ff8436 authored by Kihong Seong's avatar Kihong Seong
Browse files

Fix permission issue in GattService.clientConnect

Using `ActivityManager.getPackageImportance` instead of
`ActivityManager.getUidImportance` to fix permission issue when checking
foreground information during BLE connection. Also changing for server
connect.

Bug: 306525416
Test: atest BluetoothInstrumentationTests
Change-Id: I86e2cfd1d4503c9509922f5d0573d1543bcc1280
parent ce7714ba
Loading
Loading
Loading
Loading
+61 −46
Original line number Diff line number Diff line
@@ -61,6 +61,7 @@ import android.companion.CompanionDeviceManager;
import android.content.AttributionSource;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.PackageManager.PackageInfoFlags;
import android.content.res.Resources;
@@ -294,6 +295,7 @@ public class GattService extends ProfileService {
    private String mExposureNotificationPackage;
    private Handler mTestModeHandler;
    private ActivityManager mActivityManager;
    private PackageManager mPackageManager;
    private final Object mTestModeLock = new Object();

    public GattService(Context ctx) {
@@ -367,6 +369,7 @@ public class GattService extends ProfileService {
                .createDistanceMeasurementManager(mAdapterService);

        mActivityManager = getSystemService(ActivityManager.class);
        mPackageManager = mAdapterService.getPackageManager();

        return true;
    }
@@ -3646,32 +3649,7 @@ public class GattService extends ProfileService {
        }
        statsLogAppPackage(address, attributionSource.getUid(), clientIf);

        boolean isForegroundService =
                mActivityManager.getUidImportance(attributionSource.getUid())
                        == IMPORTANCE_FOREGROUND_SERVICE;

        if (isDirect) {
            MetricsLogger.getInstance().count(BluetoothProtoEnums.GATT_CLIENT_CONNECT_IS_DIRECT, 1);
            MetricsLogger.getInstance()
                    .count(
                            isForegroundService
                                    ? BluetoothProtoEnums
                                            .GATT_CLIENT_CONNECT_IS_DIRECT_IN_FOREGROUND
                                    : BluetoothProtoEnums
                                            .GATT_CLIENT_CONNECT_IS_DIRECT_NOT_IN_FOREGROUND,
                            1);
        } else {
            MetricsLogger.getInstance()
                    .count(BluetoothProtoEnums.GATT_CLIENT_CONNECT_IS_AUTOCONNECT, 1);
            MetricsLogger.getInstance()
                    .count(
                            isForegroundService
                                    ? BluetoothProtoEnums
                                            .GATT_CLIENT_CONNECT_IS_AUTOCONNECT_IN_FOREGROUND
                                    : BluetoothProtoEnums
                                            .GATT_CLIENT_CONNECT_IS_AUTOCONNECT_NOT_IN_FOREGROUND,
                            1);
        }
        logClientForegroundInfo(attributionSource.getUid(), isDirect);

        statsLogGattConnectionStateChange(
                BluetoothProfile.GATT, address, clientIf,
@@ -4567,26 +4545,7 @@ public class GattService extends ProfileService {
            Log.d(TAG, "serverConnect() - address=" + address);
        }

        int importance = mActivityManager.getUidImportance(attributionSource.getUid());
        if (importance == IMPORTANCE_FOREGROUND_SERVICE) {
            MetricsLogger.getInstance()
                    .count(
                            isDirect
                                    ? BluetoothProtoEnums
                                            .GATT_SERVER_CONNECT_IS_DIRECT_IN_FOREGROUND
                                    : BluetoothProtoEnums
                                            .GATT_SERVER_CONNECT_IS_AUTOCONNECT_IN_FOREGROUND,
                            1);
        } else {
            MetricsLogger.getInstance()
                    .count(
                            isDirect
                                    ? BluetoothProtoEnums
                                            .GATT_SERVER_CONNECT_IS_DIRECT_NOT_IN_FOREGROUND
                                    : BluetoothProtoEnums
                                            .GATT_SERVER_CONNECT_IS_AUTOCONNECT_NOT_IN_FOREGROUND,
                            1);
        }
        logServerForegroundInfo(attributionSource.getUid(), isDirect);

        mNativeInterface.gattServerConnect(serverIf, address, isDirect, transport);
    }
@@ -4903,6 +4862,62 @@ public class GattService extends ProfileService {
        }
    }

    private void logClientForegroundInfo(int uid, boolean isDirect) {
        if (mPackageManager == null) {
            return;
        }

        String packageName = mPackageManager.getPackagesForUid(uid)[0];
        int importance = mActivityManager.getPackageImportance(packageName);
        if (importance == IMPORTANCE_FOREGROUND_SERVICE) {
            MetricsLogger.getInstance()
                    .count(
                            isDirect
                                    ? BluetoothProtoEnums
                                            .GATT_CLIENT_CONNECT_IS_DIRECT_IN_FOREGROUND
                                    : BluetoothProtoEnums
                                            .GATT_CLIENT_CONNECT_IS_AUTOCONNECT_IN_FOREGROUND,
                            1);
        } else {
            MetricsLogger.getInstance()
                    .count(
                            isDirect
                                    ? BluetoothProtoEnums
                                            .GATT_CLIENT_CONNECT_IS_DIRECT_NOT_IN_FOREGROUND
                                    : BluetoothProtoEnums
                                            .GATT_CLIENT_CONNECT_IS_AUTOCONNECT_NOT_IN_FOREGROUND,
                            1);
        }
    }

    private void logServerForegroundInfo(int uid, boolean isDirect) {
        if (mPackageManager == null) {
            return;
        }

        String packageName = mPackageManager.getPackagesForUid(uid)[0];
        int importance = mActivityManager.getPackageImportance(packageName);
        if (importance == IMPORTANCE_FOREGROUND_SERVICE) {
            MetricsLogger.getInstance()
                    .count(
                            isDirect
                                    ? BluetoothProtoEnums
                                            .GATT_SERVER_CONNECT_IS_DIRECT_IN_FOREGROUND
                                    : BluetoothProtoEnums
                                            .GATT_SERVER_CONNECT_IS_AUTOCONNECT_IN_FOREGROUND,
                            1);
        } else {
            MetricsLogger.getInstance()
                    .count(
                            isDirect
                                    ? BluetoothProtoEnums
                                            .GATT_SERVER_CONNECT_IS_DIRECT_NOT_IN_FOREGROUND
                                    : BluetoothProtoEnums
                                            .GATT_SERVER_CONNECT_IS_AUTOCONNECT_NOT_IN_FOREGROUND,
                            1);
        }
    }

    /**
     * Ensures the report delay is either 0 or at least the floor value (5000ms)
     *