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

Commit 318a9308 authored by Ajay Panicker's avatar Ajay Panicker
Browse files

Switch GATT scan timekeeping to use a different clock

System.currentTimeMillis() could cause a scan to have a negative scan
time if the system time changed during a scan. Switch to
SystemClock.elapsedRealtime which is guaranteed to be monotonic.

Test: Start a scan and change system time
Bug: 62057540
Change-Id: Ie4d88df96c0a133955f46c355660756e587d29c5
parent 2df98dca
Loading
Loading
Loading
Loading
+10 −8
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ import android.bluetooth.le.ScanSettings;
import android.os.Binder;
import android.os.WorkSource;
import android.os.ServiceManager;
import android.os.SystemClock;
import android.os.RemoteException;
import com.android.internal.app.IBatteryStats;
import java.text.DateFormat;
@@ -117,7 +118,7 @@ import com.android.bluetooth.btservice.BluetoothProto;

        this.scansStarted++;
        isScanning = true;
        startTime = System.currentTimeMillis();
        startTime = SystemClock.elapsedRealtime();

        LastScan scan = new LastScan(startTime, 0, false, false, filtered);
        if (settings != null) {
@@ -146,7 +147,7 @@ import com.android.bluetooth.btservice.BluetoothProto;

        this.scansStopped++;
        isScanning = false;
        stopTime = System.currentTimeMillis();
        stopTime = SystemClock.elapsedRealtime();
        long scanDuration = stopTime - startTime;

        minScanTime = Math.min(scanDuration, minScanTime);
@@ -189,8 +190,8 @@ import com.android.bluetooth.btservice.BluetoothProto;
            return false;
        }

        return (System.currentTimeMillis() - lastScans.get(0).timestamp) <
            EXCESSIVE_SCANNING_PERIOD_MS;
        return (SystemClock.elapsedRealtime() - lastScans.get(0).timestamp)
                < EXCESSIVE_SCANNING_PERIOD_MS;
    }

    synchronized boolean isScanningTooLong() {
@@ -198,7 +199,7 @@ import com.android.bluetooth.btservice.BluetoothProto;
            return false;
        }

        return (System.currentTimeMillis() - startTime) > SCAN_TIMEOUT_MS;
        return (SystemClock.elapsedRealtime() - startTime) > SCAN_TIMEOUT_MS;
    }

    // This function truncates the app name for privacy reasons. Apps with
@@ -222,7 +223,7 @@ import com.android.bluetooth.btservice.BluetoothProto;
    }

    synchronized void dumpToString(StringBuilder sb) {
        long currTime = System.currentTimeMillis();
        long currTime = SystemClock.elapsedRealtime();
        long maxScan = maxScanTime;
        long minScan = minScanTime;
        long scanDuration = 0;
@@ -273,7 +274,8 @@ import com.android.bluetooth.btservice.BluetoothProto;

            for (int i = 0; i < lastScansSize; i++) {
                LastScan scan = lastScans.get(i);
                Date timestamp = new Date(scan.timestamp);
                Date timestamp = new Date(System.currentTimeMillis() - SystemClock.elapsedRealtime()
                        + scan.timestamp);
                sb.append("    " + dateFormat.format(timestamp) + " - ");
                sb.append(scan.duration + "ms ");
                if (scan.opportunistic) sb.append("Opp ");
@@ -305,7 +307,7 @@ import com.android.bluetooth.btservice.BluetoothProto;
            Iterator<ContextMap.Connection> ii = connections.iterator();
            while(ii.hasNext()) {
                ContextMap.Connection connection = ii.next();
                long connectionTime = System.currentTimeMillis() - connection.startTime;
                long connectionTime = SystemClock.elapsedRealtime() - connection.startTime;
                sb.append("    " + connection.connId + ": " +
                          connection.address + " " + connectionTime + "ms\n");
            }