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

Commit 9e78435c authored by Fyodor Kupolov's avatar Fyodor Kupolov Committed by Android (Google) Code Review
Browse files

Merge "Do not return devices when caller has no location permission" into mnc-dev

parents a933175c d2fc8cbd
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -57,6 +57,7 @@
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
    <uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
    <uses-permission android:name="android.permission.UPDATE_APP_OPS_STATS" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.DEVICE_POWER" />

+24 −0
Original line number Diff line number Diff line
@@ -18,10 +18,12 @@ package com.android.bluetooth;

import android.app.ActivityManager;
import android.app.ActivityThread;
import android.app.AppOpsManager;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.pm.PackageManager;
import android.content.pm.UserInfo;
import android.os.Binder;
import android.os.ParcelUuid;
@@ -266,6 +268,28 @@ final public class Utils {
                "Need BLUETOOTH_ADMIN permission");
    }

    /**
     * Checks that calling process has android.Manifest.permission.ACCESS_COARSE_LOCATION or
     * android.Manifest.permission.ACCESS_FINE_LOCATION and a corresponding app op is allowed
     */
    public static boolean checkCallerHasLocationPermission(Context context, AppOpsManager appOps,
            String callingPackage) {
        if (context.checkCallingOrSelfPermission(android.Manifest.permission.
                ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                && isAppOppAllowed(appOps, AppOpsManager.OP_FINE_LOCATION, callingPackage)) {
            return true;
        }

        return context.checkCallingOrSelfPermission(android.Manifest.permission.
                ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
                && isAppOppAllowed(appOps, AppOpsManager.OP_COARSE_LOCATION, callingPackage);
    }

    private static boolean isAppOppAllowed(AppOpsManager appOps, int op, String callingPackage) {
        return appOps.noteOp(op, Binder.getCallingUid(), callingPackage)
                == AppOpsManager.MODE_ALLOWED;
    }

    /**
     * Converts {@code millisecond} to unit. Each unit is 0.625 millisecond.
     */
+13 −5
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.bluetooth.gatt;

import android.app.AppOpsManager;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
@@ -148,6 +149,7 @@ public class GattService extends ProfileService {

    private AdvertiseManager mAdvertiseManager;
    private ScanManager mScanManager;
    private AppOpsManager mAppOps;

    /**
     * Reliable write queue
@@ -169,6 +171,7 @@ public class GattService extends ProfileService {
    protected boolean start() {
        if (DBG) Log.d(TAG, "start()");
        initializeNative();
        mAppOps = getSystemService(AppOpsManager.class);
        mAdvertiseManager = new AdvertiseManager(this, AdapterService.getAdapterService());
        mAdvertiseManager.start();

@@ -303,10 +306,10 @@ public class GattService extends ProfileService {

        @Override
        public void startScan(int appIf, boolean isServer, ScanSettings settings,
                List<ScanFilter> filters, List storages) {
                List<ScanFilter> filters, List storages, String callingPackage) {
            GattService service = getService();
            if (service == null) return;
            service.startScan(appIf, isServer, settings, filters, storages);
            service.startScan(appIf, isServer, settings, filters, storages, callingPackage);
        }

        public void stopScan(int appIf, boolean isServer) {
@@ -599,7 +602,7 @@ public class GattService extends ProfileService {
                            .getRemoteDevice(address);
                    ScanResult result = new ScanResult(device, ScanRecord.parseFromBytes(adv_data),
                            rssi, SystemClock.elapsedRealtimeNanos());
                    if (matchesFilters(client, result)) {
                    if (client.hasLocationPermission && matchesFilters(client, result)) {
                        try {
                            ScanSettings settings = client.settings;
                            if ((settings.getCallbackType() &
@@ -1350,13 +1353,18 @@ public class GattService extends ProfileService {
    }

    void startScan(int appIf, boolean isServer, ScanSettings settings,
            List<ScanFilter> filters, List<List<ResultStorageDescriptor>> storages) {
            List<ScanFilter> filters, List<List<ResultStorageDescriptor>> storages,
            String callingPackage) {
        if (DBG) Log.d(TAG, "start scan with filters");
        enforceAdminPermission();
        if (needsPrivilegedPermissionForScan(settings)) {
            enforcePrivilegedPermission();
        }
        mScanManager.startScan(new ScanClient(appIf, isServer, settings, filters, storages));
        boolean hasLocationPermission = Utils.checkCallerHasLocationPermission(this,
                mAppOps, callingPackage);
        final ScanClient scanClient = new ScanClient(appIf, isServer, settings, filters, storages);
        scanClient.hasLocationPermission = hasLocationPermission;
        mScanManager.startScan(scanClient);
    }

    void flushPendingBatchResults(int clientIf, boolean isServer) {
+1 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import java.util.UUID;
    List<List<ResultStorageDescriptor>> storages;
    // App associated with the scan client died.
    boolean appDied;
    boolean hasLocationPermission;

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