0
0
Fork 0
mirror of https://github.com/YTVanced/VancedMicroG synced 2024-12-03 16:27:26 +00:00

EN: Don't scan persistently

This commit is contained in:
Marvin W 2020-09-09 20:17:26 +02:00
parent f9cc9249d5
commit 4663d61007
No known key found for this signature in database
GPG key ID: 072E9235DB996F2A
2 changed files with 31 additions and 12 deletions

View file

@ -11,6 +11,11 @@ import java.util.*
const val TAG = "ExposureNotification" const val TAG = "ExposureNotification"
val SERVICE_UUID = ParcelUuid(UUID.fromString("0000FD6F-0000-1000-8000-00805F9B34FB")) val SERVICE_UUID = ParcelUuid(UUID.fromString("0000FD6F-0000-1000-8000-00805F9B34FB"))
const val SCANNING_INTERVAL = 3 * 60 // Google uses 5m, but we use a slightly different scanning and reporting system
const val SCANNING_INTERVAL_MS = SCANNING_INTERVAL * 1000L
const val SCANNING_TIME = 20 // Google uses 4s + 13s (if Bluetooth is used by something else)
const val SCANNING_TIME_MS = SCANNING_TIME * 1000L
const val ROLLING_WINDOW_LENGTH = 10 * 60 const val ROLLING_WINDOW_LENGTH = 10 * 60
const val ROLLING_WINDOW_LENGTH_MS = ROLLING_WINDOW_LENGTH * 1000 const val ROLLING_WINDOW_LENGTH_MS = ROLLING_WINDOW_LENGTH * 1000
const val ROLLING_PERIOD = 144 const val ROLLING_PERIOD = 144

View file

@ -14,7 +14,9 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.os.Build import android.os.Build
import android.os.Handler
import android.os.IBinder import android.os.IBinder
import android.os.Looper
import android.util.Log import android.util.Log
import org.microg.gms.common.ForegroundServiceContext import org.microg.gms.common.ForegroundServiceContext
import java.io.FileDescriptor import java.io.FileDescriptor
@ -23,8 +25,8 @@ import java.util.*
@TargetApi(21) @TargetApi(21)
class ScannerService : Service() { class ScannerService : Service() {
private var started = false private var scanning = false
private var startTime = 0L private var lastStartTime = 0L
private var seenAdvertisements = 0L private var seenAdvertisements = 0L
private var lastAdvertisement = 0L private var lastAdvertisement = 0L
private lateinit var database: ExposureDatabase private lateinit var database: ExposureDatabase
@ -55,6 +57,9 @@ class ScannerService : Service() {
} }
} }
} }
private val handler = Handler(Looper.getMainLooper())
private val stopLaterRunnable = Runnable { stopScan() }
private val startLaterRunnable = Runnable { startScan() }
private val scanner: BluetoothLeScanner? private val scanner: BluetoothLeScanner?
get() = getDefaultAdapter()?.bluetoothLeScanner get() = getDefaultAdapter()?.bluetoothLeScanner
@ -101,9 +106,11 @@ class ScannerService : Service() {
@Synchronized @Synchronized
private fun startScan() { private fun startScan() {
if (started) return if (scanning) return
val scanner = scanner ?: return val scanner = scanner ?: return
Log.d(TAG, "Starting scanner for service $SERVICE_UUID") Log.d(TAG, "Starting scanner for service $SERVICE_UUID")
handler.removeCallbacks(startLaterRunnable)
seenAdvertisements = 0
scanner.startScan( scanner.startScan(
listOf(ScanFilter.Builder() listOf(ScanFilter.Builder()
.setServiceUuid(SERVICE_UUID) .setServiceUuid(SERVICE_UUID)
@ -114,25 +121,32 @@ class ScannerService : Service() {
.build(), .build(),
callback callback
) )
started = true scanning = true
startTime = System.currentTimeMillis() lastStartTime = System.currentTimeMillis()
handler.postDelayed(stopLaterRunnable, SCANNING_TIME_MS)
} }
@Synchronized @Synchronized
private fun stopScan() { private fun stopScan() {
if (!started) return if (!scanning) return
Log.d(TAG, "Stopping scanner for service $SERVICE_UUID") Log.d(TAG, "Stopping scanner for service $SERVICE_UUID")
started = false handler.removeCallbacks(stopLaterRunnable)
scanning = false
scanner?.stopScan(callback) scanner?.stopScan(callback)
if (ExposurePreferences(this).enabled) {
handler.postDelayed(startLaterRunnable, ((lastStartTime + SCANNING_INTERVAL_MS) - System.currentTimeMillis()).coerceIn(0, SCANNING_INTERVAL_MS))
}
} }
override fun dump(fd: FileDescriptor?, writer: PrintWriter?, args: Array<out String>?) { override fun dump(fd: FileDescriptor?, writer: PrintWriter?, args: Array<out String>?) {
writer?.println("Started: $started") writer?.println("Scanning now: $scanning")
if (started) { writer?.println("Last scan start: ${Date(lastStartTime)}")
writer?.println("Since ${Date(startTime)}") if (Build.VERSION.SDK_INT >= 29) {
writer?.println("Seen advertisements: $seenAdvertisements") writer?.println("Scan start pending: ${handler.hasCallbacks(startLaterRunnable)}")
writer?.println("Last advertisement: ${Date(lastAdvertisement)}") writer?.println("Scan stop pending: ${handler.hasCallbacks(stopLaterRunnable)}")
} }
writer?.println("Seen advertisements since last scan start: $seenAdvertisements")
writer?.println("Last advertisement seen: ${Date(lastAdvertisement)}")
} }
companion object { companion object {