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

Commit aa88322e authored by Kai Shi's avatar Kai Shi Committed by Automerger Merge Worker
Browse files

Merge "BLE scan time optimization" am: b6ba66bc am: 73c9ebec am: df1fb92e

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Bluetooth/+/1956456

Change-Id: Idc93a75347709d15a0a3a0420ecdce74dec322fb
parents a3031f2f df1fb92e
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -4012,6 +4012,9 @@ public class AdapterService extends Service {
    private long mScanQuotaWindowMillis = DeviceConfigListener.DEFAULT_SCAN_QUOTA_WINDOW_MILLIS;
    @GuardedBy("mDeviceConfigLock")
    private long mScanTimeoutMillis = DeviceConfigListener.DEFAULT_SCAN_TIMEOUT_MILLIS;
    @GuardedBy("mDeviceConfigLock")
    private int mScanUpgradeDurationMillis =
            DeviceConfigListener.DEFAULT_SCAN_UPGRADE_DURATION_MILLIS;

    public @NonNull Predicate<String> getLocationDenylistName() {
        synchronized (mDeviceConfigLock) {
@@ -4049,6 +4052,15 @@ public class AdapterService extends Service {
        }
    }

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

    private final DeviceConfigListener mDeviceConfigListener = new DeviceConfigListener();

    private class DeviceConfigListener implements DeviceConfig.OnPropertiesChangedListener {
@@ -4064,6 +4076,8 @@ public class AdapterService extends Service {
                "scan_quota_window_millis";
        private static final String SCAN_TIMEOUT_MILLIS =
                "scan_timeout_millis";
        private static final String SCAN_UPGRADE_DURATION_MILLIS =
                "scan_upgrade_duration_millis";

        /**
         * Default denylist which matches Eddystone and iBeacon payloads.
@@ -4074,6 +4088,7 @@ 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 = 30 * MINUTE_IN_MILLIS;
        private static final int DEFAULT_SCAN_UPGRADE_DURATION_MILLIS = (int) SECOND_IN_MILLIS * 6;

        public void start() {
            DeviceConfig.addOnPropertiesChangedListener(DeviceConfig.NAMESPACE_BLUETOOTH,
@@ -4099,6 +4114,8 @@ public class AdapterService extends Service {
                        DEFAULT_SCAN_QUOTA_WINDOW_MILLIS);
                mScanTimeoutMillis = properties.getLong(SCAN_TIMEOUT_MILLIS,
                        DEFAULT_SCAN_TIMEOUT_MILLIS);
                mScanUpgradeDurationMillis = properties.getInt(SCAN_UPGRADE_DURATION_MILLIS,
                        DEFAULT_SCAN_UPGRADE_DURATION_MILLIS);
            }
        }
    }
+16 −0
Original line number Diff line number Diff line
@@ -54,6 +54,8 @@ import java.util.Objects;
    static final int BALANCED_WEIGHT = 25;
    static final int LOW_LATENCY_WEIGHT = 100;

    static final int LARGE_SCAN_TIME_GAP_MS = 24000;

    // ContextMap here is needed to grab Apps and Connections
    ContextMap mContextMap;

@@ -119,6 +121,11 @@ import java.util.Objects;
        return AdapterService.getAdapterService().getScanTimeoutMillis();
    }

    // Scan mode upgrade duration after scanStart()
    static long getScanUpgradeDurationMillis() {
        return AdapterService.getAdapterService().getScanUpgradeDurationMillis();
    }

    public String appName;
    public WorkSource mWorkSource; // Used for BatteryStatsManager
    public final WorkSourceUtil mWorkSourceUtil; // Used for BluetoothStatsLog
@@ -363,6 +370,15 @@ import java.util.Objects;
        return (SystemClock.elapsedRealtime() - mScanStartTime) > getScanTimeoutMillis();
    }

    synchronized boolean hasRecentScan() {
        if (!isScanning() || mLastScans.isEmpty()) {
            return false;
        }
        LastScan lastScan = mLastScans.get(mLastScans.size() - 1);
        return ((SystemClock.elapsedRealtime() - lastScan.duration - lastScan.timestamp)
                < LARGE_SCAN_TIME_GAP_MS);
    }

    // This function truncates the app name for privacy reasons. Apps with
    // four part package names or more get truncated to three parts, and apps
    // with three part package names names get truncated to two. Apps with two
+36 −2
Original line number Diff line number Diff line
@@ -32,7 +32,8 @@ import java.util.Objects;
/* package */class ScanClient {
    public int scannerId;
    public ScanSettings settings;
    public ScanSettings passiveSettings;
    public int scanModeApp;
    public boolean started = false;
    public int appUid;
    public List<ScanFilter> filters;
    // App associated with the scan client died.
@@ -59,7 +60,7 @@ import java.util.Objects;
    ScanClient(int scannerId, ScanSettings settings, List<ScanFilter> filters) {
        this.scannerId = scannerId;
        this.settings = settings;
        this.passiveSettings = null;
        this.scanModeApp = settings.getScanMode();
        this.filters = filters;
        this.appUid = Binder.getCallingUid();
    }
@@ -80,4 +81,37 @@ import java.util.Objects;
    public int hashCode() {
        return Objects.hash(scannerId);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(" [ScanClient")
                .append(" scanModeApp ").append(scanModeApp)
                .append(" scanModeUsed ").append(settings.getScanMode());
        if (stats != null && stats.appName != null) {
            sb.append(" [appScanStats ").append(stats.appName).append("]");
        }
        sb.append("]");
        return sb.toString();
    }

    /**
     * Update scan settings with the new scan mode.
     * @param newScanMode
     * @return true if scan settings are updated, false otherwise.
     */
    public boolean updateScanMode(int newScanMode) {
        if (settings.getScanMode() == newScanMode) {
            return false;
        }

        ScanSettings.Builder builder = new ScanSettings.Builder();
        settings = builder.setScanMode(newScanMode)
                .setCallbackType(settings.getCallbackType())
                .setScanResultType(settings.getScanResultType())
                .setReportDelay(settings.getReportDelayMillis())
                .setNumOfMatches(settings.getNumOfMatches())
                .build();
        return true;
    }
}
+261 −84

File changed.

Preview size limit exceeded, changes collapsed.

+19 −0
Original line number Diff line number Diff line
@@ -61,6 +61,23 @@ public final class ScanSettings implements Parcelable {
    @SystemApi
    public static final int SCAN_MODE_AMBIENT_DISCOVERY = 3;

    /**
     * Default Bluetooth LE scan mode when the screen is off.
     * This mode has the low duty cycle and long scan interval which results in the lowest
     * power consumption among all modes. It is for the framework internal use only.
     *
     * @hide
     */
    public static final int SCAN_MODE_SCREEN_OFF = 4;

    /**
     * Balanced Bluetooth LE scan mode for foreground service when the screen is off.
     * It is for the framework internal use only.
     *
     * @hide
     */
    public static final int SCAN_MODE_SCREEN_OFF_BALANCED = 5;

    /**
     * Trigger a callback for every Bluetooth advertisement found that matches the filter criteria.
     * If no filter is active, all advertisement packets are reported.
@@ -292,6 +309,8 @@ public final class ScanSettings implements Parcelable {
                case SCAN_MODE_BALANCED:
                case SCAN_MODE_LOW_LATENCY:
                case SCAN_MODE_AMBIENT_DISCOVERY:
                case SCAN_MODE_SCREEN_OFF:
                case SCAN_MODE_SCREEN_OFF_BALANCED:
                    mScanMode = scanMode;
                    break;
                default: