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

Commit 6bdfb0db authored by Oli Lan's avatar Oli Lan
Browse files

Decouple screen and location checks in ScanManager.

This separates checks in the BT LE ScanManager for the screen off
and location off conditions. Previously the two were connected so
that location is checked when the screen is turned off, and vice
versa.

This also adds a hasDisavowedLocation property to ScanClient, and
includes it in checks but the property is not yet populated and
is always false currently.

Bug: 183203469
Test: atest GattServiceTest
Change-Id: I85cf6db4df160f2d6b0247db618e0d70baf5de6e
parent 06348117
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ import java.util.Objects;
    public boolean hasNetworkSettingsPermission;
    public boolean hasNetworkSetupWizardPermission;
    public boolean hasScanWithoutLocationPermission;
    public boolean hasDisavowedLocation;
    public List<String> associatedDevices;

    public AppScanStats stats = null;
+22 −10
Original line number Diff line number Diff line
@@ -301,7 +301,6 @@ public class ScanManager {

        void handleStartScan(ScanClient client) {
            Utils.enforceAdminPermission(mService);
            boolean isFiltered = (client.filters != null) && !client.filters.isEmpty();
            if (DBG) {
                Log.d(TAG, "handling starting scan");
            }
@@ -316,7 +315,7 @@ public class ScanManager {
                return;
            }

            if (!mScanNative.isOpportunisticScanClient(client) && !isScreenOn() && !isFiltered) {
            if (requiresScreenOn(client) && !isScreenOn()) {
                Log.w(TAG, "Cannot start unfiltered scan in screen-off. This scan will be resumed "
                        + "later: " + client.scannerId);
                mSuspendedScanClients.add(client);
@@ -327,7 +326,7 @@ public class ScanManager {
            }

            final boolean locationEnabled = mLocationManager.isLocationEnabled();
            if (!locationEnabled && !isFiltered) {
            if (requiresLocationOn(client) && !locationEnabled) {
                Log.i(TAG, "Cannot start unfiltered scan in location-off. This scan will be"
                        + " resumed when location is on: " + client.scannerId);
                mSuspendedScanClients.add(client);
@@ -357,6 +356,16 @@ public class ScanManager {
            }
        }

        private boolean requiresScreenOn(ScanClient client) {
            boolean isFiltered = (client.filters != null) && !client.filters.isEmpty();
            return !mScanNative.isOpportunisticScanClient(client) && !isFiltered;
        }

        private boolean requiresLocationOn(ScanClient client) {
            boolean isFiltered = (client.filters != null) && !client.filters.isEmpty();
            return !client.hasDisavowedLocation && !isFiltered;
        }

        void handleStopScan(ScanClient client) {
            Utils.enforceAdminPermission(mService);
            if (client == null) {
@@ -419,8 +428,8 @@ public class ScanManager {

        void handleSuspendScans() {
            for (ScanClient client : mRegularScanClients) {
                if (!mScanNative.isOpportunisticScanClient(client) && (client.filters == null
                        || client.filters.isEmpty())) {
                if ((requiresScreenOn(client) && !isScreenOn())
                        || (requiresLocationOn(client) && !mLocationManager.isLocationEnabled())) {
                    /*Suspend unfiltered scans*/
                    if (client.stats != null) {
                        client.stats.recordScanSuspend(client.scannerId);
@@ -433,11 +442,14 @@ public class ScanManager {

        void handleResumeScans() {
            for (ScanClient client : mSuspendedScanClients) {
                if ((!requiresScreenOn(client) || isScreenOn())
                        && (!requiresLocationOn(client) || mLocationManager.isLocationEnabled())) {
                    if (client.stats != null) {
                        client.stats.recordScanResume(client.scannerId);
                    }
                    handleStartScan(client);
                }
            }
            mSuspendedScanClients.clear();
        }
    }
@@ -1328,7 +1340,7 @@ public class ScanManager {

                @Override
                public void onDisplayChanged(int displayId) {
                    if (isScreenOn() && mLocationManager.isLocationEnabled()) {
                    if (isScreenOn()) {
                        sendMessage(MSG_RESUME_SCANS, null);
                    } else {
                        sendMessage(MSG_SUSPEND_SCANS, null);
@@ -1356,7 +1368,7 @@ public class ScanManager {
                    String action = intent.getAction();
                    if (LocationManager.MODE_CHANGED_ACTION.equals(action)) {
                        final boolean locationEnabled = mLocationManager.isLocationEnabled();
                        if (locationEnabled && isScreenOn()) {
                        if (locationEnabled) {
                            sendMessage(MSG_RESUME_SCANS, null);
                        } else {
                            sendMessage(MSG_SUSPEND_SCANS, null);