diff --git a/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/AdvertiserService.kt b/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/AdvertiserService.kt index 87373f1c..231356a7 100644 --- a/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/AdvertiserService.kt +++ b/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/AdvertiserService.kt @@ -35,7 +35,6 @@ class AdvertiserService : Service() { get() = BluetoothAdapter.getDefaultAdapter()?.bluetoothLeAdvertiser private val alarmManager: AlarmManager get() = getSystemService(Context.ALARM_SERVICE) as AlarmManager - private lateinit var database: ExposureDatabase private val callback: AdvertiseCallback = object : AdvertiseCallback() { override fun onStartSuccess(settingsInEffect: AdvertiseSettings?) { Log.d(TAG, "Advertising active for ${settingsInEffect?.timeout}ms") @@ -64,7 +63,6 @@ class AdvertiserService : Service() { override fun onCreate() { super.onCreate() - database = ExposureDatabase.ref(this) registerReceiver(trigger, IntentFilter().also { it.addAction("android.bluetooth.adapter.action.STATE_CHANGED") }) } @@ -84,7 +82,6 @@ class AdvertiserService : Service() { unregisterReceiver(trigger) stopOrRestartAdvertising() handler.removeCallbacks(startLaterRunnable) - database.unref() } override fun onBind(intent: Intent?): IBinder? { @@ -123,7 +120,9 @@ class AdvertiserService : Service() { else -> return } var nextSend = nextKeyMillis.coerceAtLeast(10000) - val payload = database.generateCurrentPayload(aemBytes) + val payload = ExposureDatabase.with(this@AdvertiserService) { database -> + database.generateCurrentPayload(aemBytes) + } val data = AdvertiseData.Builder().addServiceUuid(SERVICE_UUID).addServiceData(SERVICE_UUID, payload).build() val (uuid, _) = ByteBuffer.wrap(payload).let { UUID(it.long, it.long) to it.int } Log.i(TAG, "Starting advertiser for RPI $uuid") diff --git a/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/ScannerService.kt b/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/ScannerService.kt index fdbeed2b..4df0159a 100644 --- a/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/ScannerService.kt +++ b/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/ScannerService.kt @@ -29,7 +29,6 @@ class ScannerService : Service() { private var lastStartTime = 0L private var seenAdvertisements = 0L private var lastAdvertisement = 0L - private lateinit var database: ExposureDatabase private val callback = object : ScanCallback() { override fun onScanResult(callbackType: Int, result: ScanResult?) { result?.let { onScanResult(it) } @@ -82,7 +81,9 @@ class ScannerService : Service() { fun onScanResult(result: ScanResult) { val data = result.scanRecord?.serviceData?.get(SERVICE_UUID) ?: return if (data.size < 16) return // Ignore invalid advertisements - database.noteAdvertisement(data.sliceArray(0..15), data.drop(16).toByteArray(), result.rssi) + ExposureDatabase.with(this) { database -> + database.noteAdvertisement(data.sliceArray(0..15), data.drop(16).toByteArray(), result.rssi) + } seenAdvertisements++ lastAdvertisement = System.currentTimeMillis() } @@ -97,7 +98,6 @@ class ScannerService : Service() { override fun onCreate() { super.onCreate() - database = ExposureDatabase.ref(this) registerReceiver(trigger, IntentFilter().also { it.addAction("android.bluetooth.adapter.action.STATE_CHANGED") }) } @@ -105,7 +105,6 @@ class ScannerService : Service() { super.onDestroy() unregisterReceiver(trigger) stopScan() - database.unref() } override fun onBind(intent: Intent?): IBinder? {