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

Commit 997f1ef0 authored by Wei Wang's avatar Wei Wang Committed by Tom Turney
Browse files

Add opportunistic ble scan mode. (2/2)

Bug:19003667
Change-Id: I51c609cf9941143637f0a5b4060705adc50ace72
(cherry picked from commit e90cdc4a5d2ea50178931e459c0e57e4116b55be)
parent 5630b097
Loading
Loading
Loading
Loading
+33 −10
Original line number Original line Diff line number Diff line
@@ -210,16 +210,20 @@ public class ScanManager {
            } else {
            } else {
                mRegularScanClients.add(client);
                mRegularScanClients.add(client);
                mScanNative.startRegularScan(client);
                mScanNative.startRegularScan(client);
                if (!mScanNative.isOpportunisticScanClient(client)) {
                    mScanNative.configureRegularScanParams();
                    mScanNative.configureRegularScanParams();
                }
                }
            }
            }
        }


        void handleStopScan(ScanClient client) {
        void handleStopScan(ScanClient client) {
            Utils.enforceAdminPermission(mService);
            Utils.enforceAdminPermission(mService);
            if (client == null) return;
            if (client == null) return;
            if (mRegularScanClients.contains(client)) {
            if (mRegularScanClients.contains(client)) {
                mScanNative.stopRegularScan(client);
                mScanNative.stopRegularScan(client);
                if (!mScanNative.isOpportunisticScanClient(client)) {
                    mScanNative.configureRegularScanParams();
                    mScanNative.configureRegularScanParams();
                }
            } else {
            } else {
                mScanNative.stopBatchScan(client);
                mScanNative.stopBatchScan(client);
            }
            }
@@ -390,7 +394,8 @@ public class ScanManager {
            logd("configureRegularScanParams() - ScanSetting Scan mode=" + curScanSetting +
            logd("configureRegularScanParams() - ScanSetting Scan mode=" + curScanSetting +
                    " mLastConfiguredScanSetting=" + mLastConfiguredScanSetting);
                    " mLastConfiguredScanSetting=" + mLastConfiguredScanSetting);


            if (curScanSetting != Integer.MIN_VALUE) {
            if (curScanSetting != Integer.MIN_VALUE &&
                    curScanSetting != ScanSettings.SCAN_MODE_OPPORTUNISTIC) {
                if (curScanSetting != mLastConfiguredScanSetting) {
                if (curScanSetting != mLastConfiguredScanSetting) {
                    int scanWindow, scanInterval;
                    int scanWindow, scanInterval;
                    switch (curScanSetting) {
                    switch (curScanSetting) {
@@ -441,27 +446,43 @@ public class ScanManager {
        }
        }


        void startRegularScan(ScanClient client) {
        void startRegularScan(ScanClient client) {
            if (isFilteringSupported() && mFilterIndexStack.isEmpty() &&
            if (isFilteringSupported() && mFilterIndexStack.isEmpty()
                    mClientFilterIndexMap.isEmpty()) {
                    && mClientFilterIndexMap.isEmpty()) {
                initFilterIndexStack();
                initFilterIndexStack();
            }
            }
            if (isFilteringSupported()) {
            if (isFilteringSupported()) {
                configureScanFilters(client);
                configureScanFilters(client);
            }
            }
            // Start scan native only for the first client.
            // Start scan native only for the first client.
            if (mRegularScanClients.size() == 1) {
            if (numRegularScanClients() == 1) {
                gattClientScanNative(true);
                gattClientScanNative(true);
            }
            }
        }
        }


        private int numRegularScanClients() {
            int num = 0;
            for (ScanClient client: mRegularScanClients) {
                if (client.settings.getScanMode() != ScanSettings.SCAN_MODE_OPPORTUNISTIC) {
                    num++;
                }
            }
            return num;
        }

        void startBatchScan(ScanClient client) {
        void startBatchScan(ScanClient client) {
            if (mFilterIndexStack.isEmpty() && isFilteringSupported()) {
            if (mFilterIndexStack.isEmpty() && isFilteringSupported()) {
                initFilterIndexStack();
                initFilterIndexStack();
            }
            }
            configureScanFilters(client);
            configureScanFilters(client);
            if (!isOpportunisticScanClient(client)) {
                // Reset batch scan. May need to stop the existing batch scan and update scan params.
                // Reset batch scan. May need to stop the existing batch scan and update scan params.
                resetBatchScan(client);
                resetBatchScan(client);
            }
            }
        }

        private boolean isOpportunisticScanClient(ScanClient client) {
            return client.settings.getScanMode() == ScanSettings.SCAN_MODE_OPPORTUNISTIC;
        }


        private void resetBatchScan(ScanClient client) {
        private void resetBatchScan(ScanClient client) {
            int clientIf = client.clientIf;
            int clientIf = client.clientIf;
@@ -579,7 +600,7 @@ public class ScanManager {
            // Remove scan filters and recycle filter indices.
            // Remove scan filters and recycle filter indices.
            removeScanFilters(client.clientIf);
            removeScanFilters(client.clientIf);
            mRegularScanClients.remove(client);
            mRegularScanClients.remove(client);
            if (mRegularScanClients.isEmpty()) {
            if (numRegularScanClients() == 0) {
                logd("stop scan");
                logd("stop scan");
                gattClientScanNative(false);
                gattClientScanNative(false);
            }
            }
@@ -588,8 +609,10 @@ public class ScanManager {
        void stopBatchScan(ScanClient client) {
        void stopBatchScan(ScanClient client) {
            mBatchClients.remove(client);
            mBatchClients.remove(client);
            removeScanFilters(client.clientIf);
            removeScanFilters(client.clientIf);
            if (!isOpportunisticScanClient(client)) {
                resetBatchScan(client);
                resetBatchScan(client);
            }
            }
        }


        void flushBatchResults(int clientIf) {
        void flushBatchResults(int clientIf) {
            logd("flushPendingBatchResults - clientIf = " + clientIf);
            logd("flushPendingBatchResults - clientIf = " + clientIf);