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

Commit 33550599 authored by William Escande's avatar William Escande
Browse files

ScanManager: Use provided AdapterService for context

Avoid the duplicating context / AdapterService since both are the same.
By pasisng the adapter in parameter we also remove the need to set a
"testing overlay" of the adapter service and make the test more reliable

Bug: 330956986
Fix: 330956986
Bug: 380539632
Bug: 380510617
Bug: 380510672
Bug: 380536618
Bug: 380510672
Bug: 380510672
Bug: 380536573
Bug: 378542424
Bug: 346028665
Bug: 347474468
Test: atest ScanManagerTest --rerun-until-failure 3000
Flag: TEST_ONLY || The change in app are no-op
Change-Id: Ibb5c7c315ca89dc907be655aad35609b336201ea
parent 174926bb
Loading
Loading
Loading
Loading
+13 −6
Original line number Diff line number Diff line
@@ -6649,14 +6649,14 @@ public class AdapterService extends Service {
    }

    /** Returns scan upgrade duration in millis. */
    public long getScanUpgradeDurationMillis() {
    public int getScanUpgradeDurationMillis() {
        synchronized (mDeviceConfigLock) {
            return mScanUpgradeDurationMillis;
        }
    }

    /** Returns scan downgrade duration in millis. */
    public long getScanDowngradeDurationMillis() {
    public int getScanDowngradeDurationMillis() {
        synchronized (mDeviceConfigLock) {
            return mScanDowngradeDurationMillis;
        }
@@ -6690,7 +6690,8 @@ public class AdapterService extends Service {
        }
    }

    private class DeviceConfigListener implements DeviceConfig.OnPropertiesChangedListener {
    @VisibleForTesting
    public class DeviceConfigListener implements DeviceConfig.OnPropertiesChangedListener {
        private static final String LOCATION_DENYLIST_NAME = "location_denylist_name";
        private static final String LOCATION_DENYLIST_MAC = "location_denylist_mac";
        private static final String LOCATION_DENYLIST_ADVERTISING_DATA =
@@ -6720,9 +6721,15 @@ public class AdapterService extends Service {

        private static final int DEFAULT_SCAN_QUOTA_COUNT = 5;
        private static final long DEFAULT_SCAN_QUOTA_WINDOW_MILLIS = 30 * SECOND_IN_MILLIS;
        private static final long DEFAULT_SCAN_TIMEOUT_MILLIS = 10 * MINUTE_IN_MILLIS;
        private static final int DEFAULT_SCAN_UPGRADE_DURATION_MILLIS = (int) SECOND_IN_MILLIS * 6;
        private static final int DEFAULT_SCAN_DOWNGRADE_DURATION_BT_CONNECTING_MILLIS =

        @VisibleForTesting
        public static final long DEFAULT_SCAN_TIMEOUT_MILLIS = 10 * MINUTE_IN_MILLIS;

        @VisibleForTesting
        public static final int DEFAULT_SCAN_UPGRADE_DURATION_MILLIS = (int) SECOND_IN_MILLIS * 6;

        @VisibleForTesting
        public static final int DEFAULT_SCAN_DOWNGRADE_DURATION_BT_CONNECTING_MILLIS =
                (int) SECOND_IN_MILLIS * 6;

        public void start() {
+3 −4
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@ import android.annotation.Nullable;
import android.bluetooth.BluetoothProtoEnums;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanSettings;
import android.content.Context;
import android.os.BatteryStatsManager;
import android.os.Binder;
import android.os.WorkSource;
@@ -173,14 +172,15 @@ public class AppScanStats {
            String name,
            WorkSource source,
            ScannerMap map,
            Context context,
            AdapterService adapterService,
            TransitionalScanHelper scanHelper,
            TimeProvider timeProvider) {
        mAdapterService = requireNonNull(adapterService);
        mTimeProvider = requireNonNull(timeProvider);
        mAppName = name;
        mScannerMap = map;
        mScanHelper = scanHelper;
        mBatteryStatsManager = context.getSystemService(BatteryStatsManager.class);
        mBatteryStatsManager = adapterService.getSystemService(BatteryStatsManager.class);

        if (source == null) {
            // Bill the caller if the work source isn't passed through
@@ -188,7 +188,6 @@ public class AppScanStats {
        }
        mWorkSource = source;
        mWorkSourceUtil = new WorkSourceUtil(source);
        mAdapterService = Objects.requireNonNull(AdapterService.getAdapterService());
    }

    public synchronized void addResult(int scannerId) {
+13 −23
Original line number Diff line number Diff line
@@ -161,9 +161,8 @@ public class ScanManager {
    }

    public ScanManager(
            Context context,
            TransitionalScanHelper scanHelper,
            AdapterService adapterService,
            TransitionalScanHelper scanHelper,
            BluetoothAdapterProxy bluetoothAdapterProxy,
            Looper looper,
            TimeProvider timeProvider) {
@@ -172,7 +171,7 @@ public class ScanManager {
        mBatchClients = Collections.newSetFromMap(new ConcurrentHashMap<ScanClient, Boolean>());
        mSuspendedScanClients =
                Collections.newSetFromMap(new ConcurrentHashMap<ScanClient, Boolean>());
        mContext = context;
        mContext = adapterService;
        mScanHelper = scanHelper;
        mAdapterService = adapterService;
        mTimeProvider = timeProvider;
@@ -1609,8 +1608,7 @@ public class ScanManager {
                            if (client.stats != null) {
                                client.stats.recordTrackingHwFilterNotAvailableCountMetrics(
                                        client.scannerId,
                                        AdapterService.getAdapterService()
                                                .getTotalNumOfTrackableAdvertisements());
                                        mAdapterService.getTotalNumOfTrackableAdvertisements());
                            }
                            try {
                                mScanHelper.onScanManagerErrorCallback(
@@ -1711,8 +1709,7 @@ public class ScanManager {
                if (client.stats != null) {
                    client.stats.recordHwFilterNotAvailableCountMetrics(
                            client.scannerId,
                            AdapterService.getAdapterService()
                                    .getNumOfOffloadedScanFilterSupported());
                            mAdapterService.getNumOfOffloadedScanFilterSupported());
                }
                return true;
            }
@@ -1720,8 +1717,7 @@ public class ScanManager {
        }

        private void initFilterIndexStack() {
            int maxFiltersSupported =
                    AdapterService.getAdapterService().getNumOfOffloadedScanFilterSupported();
            int maxFiltersSupported = mAdapterService.getNumOfOffloadedScanFilterSupported();
            if (!isFilteringSupported() && mIsMsftSupported) {
                // Hardcoded minimum number of hardware adv monitor slots, because this value
                // cannot be queried from the controller for MSFT enabled devices
@@ -1933,7 +1929,7 @@ public class ScanManager {
            }
            int val = 0;
            int maxTotalTrackableAdvertisements =
                    AdapterService.getAdapterService().getTotalNumOfTrackableAdvertisements();
                    mAdapterService.getTotalNumOfTrackableAdvertisements();
            // controller based onfound onlost resources are scarce commodity; the
            // assignment of filters to num of beacons to track is configurable based
            // on hw capabilities. Apps give an intent and allocation of onfound
@@ -1960,7 +1956,7 @@ public class ScanManager {
        private boolean manageAllocationOfTrackingAdvertisement(
                int numOfTrackableAdvertisement, boolean allocate) {
            int maxTotalTrackableAdvertisements =
                    AdapterService.getAdapterService().getTotalNumOfTrackableAdvertisements();
                    mAdapterService.getTotalNumOfTrackableAdvertisements();
            synchronized (mCurUsedTrackableAdvertisementsLock) {
                int availableEntries =
                        maxTotalTrackableAdvertisements - mCurUsedTrackableAdvertisements;
@@ -2185,12 +2181,9 @@ public class ScanManager {
        }
        Log.d(
                TAG,
                "mProfilesConnecting "
                        + mProfilesConnecting
                        + ", mProfilesConnected "
                        + mProfilesConnected
                        + ", mProfilesDisconnecting "
                        + mProfilesDisconnecting);
                ("mProfilesConnecting " + mProfilesConnecting)
                        + (", mProfilesConnected " + mProfilesConnected)
                        + (", mProfilesDisconnecting " + mProfilesDisconnecting));
        return (mProfilesConnecting > 0);
    }

@@ -2231,12 +2224,9 @@ public class ScanManager {
            }
            Log.d(
                    TAG,
                    "uid "
                            + uid
                            + " isForeground "
                            + isForeground
                            + " scanMode "
                            + getScanModeString(client.settings.getScanMode()));
                    ("uid " + uid)
                            + (" isForeground " + isForeground)
                            + (" scanMode " + getScanModeString(client.settings.getScanMode())));
        }

        if (updatedScanParams) {
+3 −6
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@ package com.android.bluetooth.le_scan;

import static com.android.bluetooth.Utils.sSystemClock;

import android.content.Context;
import android.os.Looper;
import android.util.Log;

@@ -68,21 +67,19 @@ public class ScanObjectsFactory {
    /**
     * Create an instance of ScanManager
     *
     * @param context a Context instance
     * @param scanHelper a TransitionalScanHelper instance
     * @param adapterService an AdapterService instance
     * @param scanHelper a TransitionalScanHelper instance
     * @param bluetoothAdapterProxy a bluetoothAdapterProxy instance
     * @param looper the looper to be used for processing messages
     * @return the created ScanManager instance
     */
    public ScanManager createScanManager(
            Context context,
            TransitionalScanHelper scanHelper,
            AdapterService adapterService,
            TransitionalScanHelper scanHelper,
            BluetoothAdapterProxy bluetoothAdapterProxy,
            Looper looper) {
        return new ScanManager(
                context, scanHelper, adapterService bluetoothAdapterProxy, looper, sSystemClock);
                adapterService, scanHelper, bluetoothAdapterProxy, looper, sSystemClock);
    }

    public PeriodicScanManager createPeriodicScanManager(AdapterService adapterService) {
+10 −8
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ import static com.android.bluetooth.util.AttributionSourceUtil.getLastAttributio
import android.annotation.Nullable;
import android.bluetooth.le.IScannerCallback;
import android.content.AttributionSource;
import android.content.Context;
import android.os.Binder;
import android.os.IBinder;
import android.os.IInterface;
@@ -30,6 +29,8 @@ import android.os.UserHandle;
import android.os.WorkSource;
import android.util.Log;

import com.android.bluetooth.btservice.AdapterService;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@@ -54,9 +55,9 @@ public class ScannerMap {
            AttributionSource attributionSource,
            WorkSource workSource,
            IScannerCallback callback,
            Context context,
            AdapterService adapterService,
            TransitionalScanHelper scanHelper) {
        return add(uuid, attributionSource, workSource, callback, null, context, scanHelper);
        return add(uuid, attributionSource, workSource, callback, null, adapterService, scanHelper);
    }

    /** Add an entry to the application context list with a pending intent. */
@@ -64,9 +65,9 @@ public class ScannerMap {
            UUID uuid,
            AttributionSource attributionSource,
            TransitionalScanHelper.PendingIntentInfo piInfo,
            Context context,
            AdapterService adapterService,
            TransitionalScanHelper scanHelper) {
        return add(uuid, attributionSource, null, null, piInfo, context, scanHelper);
        return add(uuid, attributionSource, null, null, piInfo, adapterService, scanHelper);
    }

    private ScannerApp add(
@@ -75,7 +76,7 @@ public class ScannerMap {
            @Nullable WorkSource workSource,
            @Nullable IScannerCallback callback,
            @Nullable TransitionalScanHelper.PendingIntentInfo piInfo,
            Context context,
            AdapterService adapterService,
            TransitionalScanHelper scanHelper) {
        int appUid;
        String appName = null;
@@ -84,7 +85,7 @@ public class ScannerMap {
            appName = piInfo.callingPackage;
        } else {
            appUid = Binder.getCallingUid();
            appName = context.getPackageManager().getNameForUid(appUid);
            appName = adapterService.getPackageManager().getNameForUid(appUid);
        }
        if (appName == null) {
            // Assign an app name if one isn't found
@@ -93,7 +94,8 @@ public class ScannerMap {
        AppScanStats appScanStats = mAppScanStatsMap.get(appUid);
        if (appScanStats == null) {
            appScanStats =
                    new AppScanStats(appName, workSource, this, context, scanHelper, sSystemClock);
                    new AppScanStats(
                            appName, workSource, this, adapterService, scanHelper, sSystemClock);
            mAppScanStatsMap.put(appUid, appScanStats);
        }
        ScannerApp app =
Loading