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

Commit 9013d987 authored by Fyodor Kupolov's avatar Fyodor Kupolov
Browse files

Fix foreground scans for pre-M apps when location is disabled

Foreground scans were still broken even for L-or-earlier apps when the
location was disabled.

Bug: 24203266
Change-Id: Iaad8d7828a4b383b6cc0a033e94c7d2cd8c9328b
parent 9c3aff47
Loading
Loading
Loading
Loading
+15 −8
Original line number Diff line number Diff line
@@ -288,14 +288,7 @@ final public class Utils {
            return true;
        }
        // Enforce location permission for apps targeting M and later versions
        boolean enforceLocationPermission = true;
        try {
            enforceLocationPermission = context.getPackageManager().getApplicationInfo(
                    callingPackage, 0).targetSdkVersion >= Build.VERSION_CODES.M;
        } catch (PackageManager.NameNotFoundException e) {
            // In case of exception, enforce permission anyway
        }
        if (enforceLocationPermission) {
        if (isMApp(context, callingPackage)) {
            throw new SecurityException("Need ACCESS_COARSE_LOCATION or "
                    + "ACCESS_FINE_LOCATION permission to get scan results");
        } else {
@@ -317,6 +310,20 @@ final public class Utils {
                android.Manifest.permission.PEERS_MAC_ADDRESS) == PackageManager.PERMISSION_GRANTED;
    }

    public static boolean isLegacyForegroundApp(Context context, String pkgName) {
        return !isMApp(context, pkgName) && isForegroundApp(context, pkgName);
    }

    private static boolean isMApp(Context context, String pkgName) {
        try {
            return context.getPackageManager().getApplicationInfo(pkgName, 0)
                    .targetSdkVersion >= Build.VERSION_CODES.M;
        } catch (PackageManager.NameNotFoundException e) {
            // In case of exception, assume M app
        }
        return true;
    }

    /**
     * Return true if the specified package name is a foreground app.
     *
+8 −7
Original line number Diff line number Diff line
@@ -641,12 +641,13 @@ public class GattService extends ProfileService {
    private boolean hasScanResultPermission(final ScanClient client) {
        final boolean requiresLocationEnabled =
                getResources().getBoolean(R.bool.strict_location_check);
        final boolean locationEnabled = Settings.Secure.getInt(getContentResolver(),
        final boolean locationEnabledSetting = Settings.Secure.getInt(getContentResolver(),
                Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF)
                != Settings.Secure.LOCATION_MODE_OFF;

        return (client.hasPeersMacAddressPermission ||
                (client.hasLocationPermission && (!requiresLocationEnabled || locationEnabled)));
        final boolean locationEnabled = !requiresLocationEnabled || locationEnabledSetting
                || client.legacyForegroundApp;
        return (client.hasPeersMacAddressPermission
                || (client.hasLocationPermission && locationEnabled));
    }

    // Check if a scan record matches a specific filters.
@@ -1378,12 +1379,12 @@ public class GattService extends ProfileService {
        if (needsPrivilegedPermissionForScan(settings)) {
            enforcePrivilegedPermission();
        }
        boolean hasLocationPermission = Utils.checkCallerHasLocationPermission(this,
                mAppOps, callingPackage);
        final ScanClient scanClient = new ScanClient(appIf, isServer, settings, filters, storages);
        scanClient.hasLocationPermission = hasLocationPermission;
        scanClient.hasLocationPermission = Utils.checkCallerHasLocationPermission(this, mAppOps,
                callingPackage);
        scanClient.hasPeersMacAddressPermission = Utils.checkCallerHasPeersMacAddressPermission(
                this);
        scanClient.legacyForegroundApp = Utils.isLegacyForegroundApp(this, callingPackage);
        mScanManager.startScan(scanClient);
    }

+2 −0
Original line number Diff line number Diff line
@@ -40,6 +40,8 @@ import java.util.UUID;
    boolean appDied;
    boolean hasLocationPermission;
    boolean hasPeersMacAddressPermission;
    // Pre-M apps are allowed to get scan results even if location is disabled
    boolean legacyForegroundApp;

    private static final ScanSettings DEFAULT_SCAN_SETTINGS = new ScanSettings.Builder()
            .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();