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

Commit 803d3778 authored by Kihong Seong's avatar Kihong Seong Committed by Gerrit Code Review
Browse files

Merge "Ensure scan timeout is reset if new scan started" into main

parents d08f27b5 19712f2e
Loading
Loading
Loading
Loading
+5 −2
Original line number Original line Diff line number Diff line
@@ -22,6 +22,7 @@ import android.util.Log;
import com.android.bluetooth.Utils;
import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.BluetoothAdapterProxy;
import com.android.bluetooth.btservice.BluetoothAdapterProxy;
import com.android.bluetooth.flags.FeatureFlags;


/**
/**
 * Factory class for object initialization to help with unit testing
 * Factory class for object initialization to help with unit testing
@@ -82,8 +83,10 @@ public class GattObjectsFactory {
            GattService service,
            GattService service,
            AdapterService adapterService,
            AdapterService adapterService,
            BluetoothAdapterProxy bluetoothAdapterProxy,
            BluetoothAdapterProxy bluetoothAdapterProxy,
            Looper looper) {
            Looper looper,
        return new ScanManager(service, adapterService, bluetoothAdapterProxy, looper);
            FeatureFlags featureFlags) {
        return new ScanManager(
                service, adapterService, bluetoothAdapterProxy, looper, featureFlags);
    }
    }


    public PeriodicScanManager createPeriodicScanManager(AdapterService adapterService) {
    public PeriodicScanManager createPeriodicScanManager(AdapterService adapterService) {
+9 −1
Original line number Original line Diff line number Diff line
@@ -92,6 +92,8 @@ import com.android.bluetooth.btservice.BluetoothAdapterProxy;
import com.android.bluetooth.btservice.CompanionManager;
import com.android.bluetooth.btservice.CompanionManager;
import com.android.bluetooth.btservice.MetricsLogger;
import com.android.bluetooth.btservice.MetricsLogger;
import com.android.bluetooth.btservice.ProfileService;
import com.android.bluetooth.btservice.ProfileService;
import com.android.bluetooth.flags.FeatureFlags;
import com.android.bluetooth.flags.FeatureFlagsImpl;
import com.android.bluetooth.util.NumberUtils;
import com.android.bluetooth.util.NumberUtils;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.annotations.VisibleForTesting;
import com.android.modules.utils.SynchronousResultReceiver;
import com.android.modules.utils.SynchronousResultReceiver;
@@ -284,6 +286,7 @@ public class GattService extends ProfileService {
     */
     */
    private final HashMap<String, Integer> mPermits = new HashMap<>();
    private final HashMap<String, Integer> mPermits = new HashMap<>();


    private FeatureFlags mFeatureFlags;
    private AdapterService mAdapterService;
    private AdapterService mAdapterService;
    private BluetoothAdapterProxy mBluetoothAdapterProxy;
    private BluetoothAdapterProxy mBluetoothAdapterProxy;
    AdvertiseManager mAdvertiseManager;
    AdvertiseManager mAdvertiseManager;
@@ -348,6 +351,7 @@ public class GattService extends ProfileService {
        mNativeInterface = GattObjectsFactory.getInstance().getNativeInterface();
        mNativeInterface = GattObjectsFactory.getInstance().getNativeInterface();
        mNativeInterface.init(this);
        mNativeInterface.init(this);
        mAdapterService = AdapterService.getAdapterService();
        mAdapterService = AdapterService.getAdapterService();
        mFeatureFlags = new FeatureFlagsImpl();
        mBluetoothAdapterProxy = BluetoothAdapterProxy.getInstance();
        mBluetoothAdapterProxy = BluetoothAdapterProxy.getInstance();
        mCompanionManager = getSystemService(CompanionDeviceManager.class);
        mCompanionManager = getSystemService(CompanionDeviceManager.class);
        mAppOps = getSystemService(AppOpsManager.class);
        mAppOps = getSystemService(AppOpsManager.class);
@@ -363,7 +367,11 @@ public class GattService extends ProfileService {
        mScanManager =
        mScanManager =
                GattObjectsFactory.getInstance()
                GattObjectsFactory.getInstance()
                        .createScanManager(
                        .createScanManager(
                                this, mAdapterService, mBluetoothAdapterProxy, thread.getLooper());
                                this,
                                mAdapterService,
                                mBluetoothAdapterProxy,
                                thread.getLooper(),
                                mFeatureFlags);


        mPeriodicScanManager = GattObjectsFactory.getInstance()
        mPeriodicScanManager = GattObjectsFactory.getInstance()
                .createPeriodicScanManager(mAdapterService);
                .createPeriodicScanManager(mAdapterService);
+9 −1
Original line number Original line Diff line number Diff line
@@ -45,6 +45,7 @@ import android.view.Display;
import com.android.bluetooth.Utils;
import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.BluetoothAdapterProxy;
import com.android.bluetooth.btservice.BluetoothAdapterProxy;
import com.android.bluetooth.flags.FeatureFlags;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.annotations.VisibleForTesting;


@@ -116,6 +117,8 @@ public class ScanManager {
    private final Object mCurUsedTrackableAdvertisementsLock = new Object();
    private final Object mCurUsedTrackableAdvertisementsLock = new Object();
    @GuardedBy("mCurUsedTrackableAdvertisementsLock")
    @GuardedBy("mCurUsedTrackableAdvertisementsLock")
    private int mCurUsedTrackableAdvertisements = 0;
    private int mCurUsedTrackableAdvertisements = 0;

    private final FeatureFlags mFeatureFlags;
    private final GattService mService;
    private final GattService mService;
    private final AdapterService mAdapterService;
    private final AdapterService mAdapterService;
    private BroadcastReceiver mBatchAlarmReceiver;
    private BroadcastReceiver mBatchAlarmReceiver;
@@ -160,7 +163,8 @@ public class ScanManager {
            GattService service,
            GattService service,
            AdapterService adapterService,
            AdapterService adapterService,
            BluetoothAdapterProxy bluetoothAdapterProxy,
            BluetoothAdapterProxy bluetoothAdapterProxy,
            Looper looper) {
            Looper looper,
            FeatureFlags featureFlags) {
        mRegularScanClients =
        mRegularScanClients =
                Collections.newSetFromMap(new ConcurrentHashMap<ScanClient, Boolean>());
                Collections.newSetFromMap(new ConcurrentHashMap<ScanClient, Boolean>());
        mBatchClients = Collections.newSetFromMap(new ConcurrentHashMap<ScanClient, Boolean>());
        mBatchClients = Collections.newSetFromMap(new ConcurrentHashMap<ScanClient, Boolean>());
@@ -169,6 +173,7 @@ public class ScanManager {
        mService = service;
        mService = service;
        mAdapterService = adapterService;
        mAdapterService = adapterService;
        mScanNative = new ScanNative();
        mScanNative = new ScanNative();
        mFeatureFlags = featureFlags;
        mDm = mService.getSystemService(DisplayManager.class);
        mDm = mService.getSystemService(DisplayManager.class);
        mActivityManager = mService.getSystemService(ActivityManager.class);
        mActivityManager = mService.getSystemService(ActivityManager.class);
        mLocationManager = mAdapterService.getSystemService(LocationManager.class);
        mLocationManager = mAdapterService.getSystemService(LocationManager.class);
@@ -445,6 +450,9 @@ public class ScanManager {
                        Message msg = obtainMessage(MSG_SCAN_TIMEOUT);
                        Message msg = obtainMessage(MSG_SCAN_TIMEOUT);
                        msg.obj = client;
                        msg.obj = client;
                        // Only one timeout message should exist at any time
                        // Only one timeout message should exist at any time
                        if (mFeatureFlags.scanTimeoutReset()) {
                            removeMessages(MSG_SCAN_TIMEOUT, client);
                        }
                        sendMessageDelayed(msg, mAdapterService.getScanTimeoutMillis());
                        sendMessageDelayed(msg, mAdapterService.getScanTimeoutMillis());
                        if (DBG) {
                        if (DBG) {
                            Log.d(TAG,
                            Log.d(TAG,
+1 −1
Original line number Original line Diff line number Diff line
@@ -119,7 +119,7 @@ public class GattServiceTest {


        GattObjectsFactory.setInstanceForTesting(mFactory);
        GattObjectsFactory.setInstanceForTesting(mFactory);
        doReturn(mNativeInterface).when(mFactory).getNativeInterface();
        doReturn(mNativeInterface).when(mFactory).getNativeInterface();
        doReturn(mScanManager).when(mFactory).createScanManager(any(), any(), any(), any());
        doReturn(mScanManager).when(mFactory).createScanManager(any(), any(), any(), any(), any());
        doReturn(mPeriodicScanManager).when(mFactory).createPeriodicScanManager(any());
        doReturn(mPeriodicScanManager).when(mFactory).createPeriodicScanManager(any());
        doReturn(mDistanceMeasurementManager).when(mFactory)
        doReturn(mDistanceMeasurementManager).when(mFactory)
                .createDistanceMeasurementManager(any());
                .createDistanceMeasurementManager(any());
+42 −4
Original line number Original line Diff line number Diff line
@@ -68,6 +68,8 @@ import com.android.bluetooth.TestUtils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.BluetoothAdapterProxy;
import com.android.bluetooth.btservice.BluetoothAdapterProxy;
import com.android.bluetooth.btservice.MetricsLogger;
import com.android.bluetooth.btservice.MetricsLogger;
import com.android.bluetooth.flags.FakeFeatureFlagsImpl;
import com.android.bluetooth.flags.Flags;
import com.android.internal.app.IBatteryStats;
import com.android.internal.app.IBatteryStats;


import org.junit.After;
import org.junit.After;
@@ -116,6 +118,7 @@ public class ScanManagerTest {
    final BatteryStatsManager mBatteryStatsManager =
    final BatteryStatsManager mBatteryStatsManager =
            new BatteryStatsManager(mock(IBatteryStats.class));
            new BatteryStatsManager(mock(IBatteryStats.class));


    private FakeFeatureFlagsImpl mFakeFlagsImpl;
    @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule();
    @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule();
    @Mock private AdapterService mAdapterService;
    @Mock private AdapterService mAdapterService;
    @Mock private GattService mMockGattService;
    @Mock private GattService mMockGattService;
@@ -183,6 +186,8 @@ public class ScanManagerTest {
        doReturn(mTargetContext.getUser()).when(mMockGattService).getUser();
        doReturn(mTargetContext.getUser()).when(mMockGattService).getUser();
        doReturn(mTargetContext.getPackageName()).when(mMockGattService).getPackageName();
        doReturn(mTargetContext.getPackageName()).when(mMockGattService).getPackageName();


        mFakeFlagsImpl = new FakeFeatureFlagsImpl();
        mFakeFlagsImpl.setFlag(Flags.FLAG_SCAN_TIMEOUT_RESET, true);
        mTestLooper = new TestLooper();
        mTestLooper = new TestLooper();
        mTestLooper.startAutoDispatch();
        mTestLooper.startAutoDispatch();
        mScanManager =
        mScanManager =
@@ -190,7 +195,8 @@ public class ScanManagerTest {
                        mMockGattService,
                        mMockGattService,
                        mAdapterService,
                        mAdapterService,
                        mBluetoothAdapterProxy,
                        mBluetoothAdapterProxy,
                        mTestLooper.getLooper());
                        mTestLooper.getLooper(),
                        mFakeFlagsImpl);


        mHandler = mScanManager.getClientHandler();
        mHandler = mScanManager.getClientHandler();
        assertThat(mHandler).isNotNull();
        assertThat(mHandler).isNotNull();
@@ -581,7 +587,7 @@ public class ScanManagerTest {
            // Turn on screen
            // Turn on screen
            sendMessageWaitForProcessed(createScreenOnOffMessage(true));
            sendMessageWaitForProcessed(createScreenOnOffMessage(true));
            assertThat(client.settings.getScanMode()).isEqualTo(expectedScanMode);
            assertThat(client.settings.getScanMode()).isEqualTo(expectedScanMode);
            // Set as backgournd app
            // Set as background app
            sendMessageWaitForProcessed(createImportanceMessage(false));
            sendMessageWaitForProcessed(createImportanceMessage(false));
            assertThat(client.settings.getScanMode()).isEqualTo(expectedScanMode);
            assertThat(client.settings.getScanMode()).isEqualTo(expectedScanMode);
            // Set as foreground app
            // Set as foreground app
@@ -636,6 +642,38 @@ public class ScanManagerTest {
        }
        }
    }
    }


    @Test
    public void testScanTimeoutResetForNewScan() {
        mTestLooper.stopAutoDispatchAndIgnoreExceptions();
        // Set filtered scan flag
        final boolean isFiltered = false;
        when(mAdapterService.getScanTimeoutMillis()).thenReturn((long) DELAY_SCAN_TIMEOUT_MS);
        // Turn on screen
        mHandler.sendMessage(createScreenOnOffMessage(true));
        mTestLooper.dispatchAll();
        // Create scan client
        ScanClient client = createScanClient(0, isFiltered, SCAN_MODE_LOW_POWER);

        // Put a timeout message in the queue to emulate the scan being started already
        Message timeoutMessage = mHandler.obtainMessage(ScanManager.MSG_SCAN_TIMEOUT, client);
        mHandler.sendMessageDelayed(timeoutMessage, DELAY_SCAN_TIMEOUT_MS / 2);
        mHandler.sendMessage(createStartStopScanMessage(true, client));

        // Dispatching all messages only runs start scan
        assertThat(mTestLooper.dispatchAll()).isEqualTo(1);
        mTestLooper.moveTimeForward(DELAY_SCAN_TIMEOUT_MS / 2);
        assertThat(mHandler.hasMessages(ScanManager.MSG_SCAN_TIMEOUT, client)).isTrue();

        // After restarting the scan, we can check that the initial timeout message is not triggered
        assertThat(mTestLooper.dispatchAll()).isEqualTo(0);

        // After timeout, the next message that is run should be a timeout message
        mTestLooper.moveTimeForward(DELAY_SCAN_TIMEOUT_MS / 2 + 1);
        Message nextMessage = mTestLooper.nextMessage();
        assertThat(nextMessage.what).isEqualTo(ScanManager.MSG_SCAN_TIMEOUT);
        assertThat(nextMessage.obj).isEqualTo(client);
    }

    @Test
    @Test
    public void testSwitchForeBackgroundUnfilteredScan() {
    public void testSwitchForeBackgroundUnfilteredScan() {
        // Set filtered scan flag
        // Set filtered scan flag
@@ -662,7 +700,7 @@ public class ScanManagerTest {
            assertThat(mScanManager.getRegularScanQueue().contains(client)).isTrue();
            assertThat(mScanManager.getRegularScanQueue().contains(client)).isTrue();
            assertThat(mScanManager.getSuspendedScanQueue().contains(client)).isFalse();
            assertThat(mScanManager.getSuspendedScanQueue().contains(client)).isFalse();
            assertThat(client.settings.getScanMode()).isEqualTo(ScanMode);
            assertThat(client.settings.getScanMode()).isEqualTo(ScanMode);
            // Set as backgournd app
            // Set as background app
            sendMessageWaitForProcessed(createImportanceMessage(false));
            sendMessageWaitForProcessed(createImportanceMessage(false));
            assertThat(mScanManager.getRegularScanQueue().contains(client)).isTrue();
            assertThat(mScanManager.getRegularScanQueue().contains(client)).isTrue();
            assertThat(mScanManager.getSuspendedScanQueue().contains(client)).isFalse();
            assertThat(mScanManager.getSuspendedScanQueue().contains(client)).isFalse();
@@ -701,7 +739,7 @@ public class ScanManagerTest {
            assertThat(mScanManager.getRegularScanQueue().contains(client)).isTrue();
            assertThat(mScanManager.getRegularScanQueue().contains(client)).isTrue();
            assertThat(mScanManager.getSuspendedScanQueue().contains(client)).isFalse();
            assertThat(mScanManager.getSuspendedScanQueue().contains(client)).isFalse();
            assertThat(client.settings.getScanMode()).isEqualTo(ScanMode);
            assertThat(client.settings.getScanMode()).isEqualTo(ScanMode);
            // Set as backgournd app
            // Set as background app
            sendMessageWaitForProcessed(createImportanceMessage(false));
            sendMessageWaitForProcessed(createImportanceMessage(false));
            assertThat(mScanManager.getRegularScanQueue().contains(client)).isTrue();
            assertThat(mScanManager.getRegularScanQueue().contains(client)).isTrue();
            assertThat(mScanManager.getSuspendedScanQueue().contains(client)).isFalse();
            assertThat(mScanManager.getSuspendedScanQueue().contains(client)).isFalse();