0
0
Fork 0
mirror of https://github.com/YTVanced/VancedMicroG synced 2024-11-24 20:25:14 +00:00

Do not use database.use (for pre-Q), ensure database stays open as needed

Related to #1115
This commit is contained in:
Marvin W 2020-08-04 11:42:15 +02:00
parent b898878f26
commit 761b6dfd47
No known key found for this signature in database
GPG key ID: 072E9235DB996F2A
3 changed files with 103 additions and 92 deletions

View file

@ -21,10 +21,25 @@ import java.util.concurrent.Future
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
import kotlin.math.roundToInt
class ExposureDatabase(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) {
private val refCount = AtomicInteger(0)
fun ref(): ExposureDatabase {
refCount.incrementAndGet()
return this
}
fun unref() {
val nu = refCount.decrementAndGet()
if (nu == 0) {
close()
} else if (nu < 0) {
throw RuntimeException("ref/unref mismatch")
}
}
override fun onCreate(db: SQLiteDatabase) {
onUpgrade(db, 0, DB_VERSION)
@ -149,7 +164,7 @@ class ExposureDatabase(context: Context) : SQLiteOpenHelper(context, DB_NAME, nu
}
}
fun applyDiagnosisKeySearchResult(packageName: String, token: String, key: TemporaryExposureKey, matched: Boolean) = writableDatabase.run {
fun applyDiagnosisKeySearchResult(key: TemporaryExposureKey, matched: Boolean) = writableDatabase.run {
insert(TABLE_TEK_CHECK, "NULL", ContentValues().apply {
put("keyData", key.keyData)
put("rollingStartNumber", key.rollingStartIntervalNumber)
@ -195,10 +210,10 @@ class ExposureDatabase(context: Context) : SQLiteOpenHelper(context, DB_NAME, nu
for (key in keys) {
if (oldestRpi == null || key.rollingStartIntervalNumber * ROLLING_WINDOW_LENGTH_MS - ALLOWED_KEY_OFFSET_MS < oldestRpi) {
// Early ignore because key is older than since we started scanning.
applyDiagnosisKeySearchResult(packageName, token, key, false)
applyDiagnosisKeySearchResult(key, false)
} else {
futures.add(executor.submit {
applyDiagnosisKeySearchResult(packageName, token, key, findMeasuredExposures(key).isNotEmpty())
applyDiagnosisKeySearchResult(key, findMeasuredExposures(key).isNotEmpty())
})
}
}
@ -207,7 +222,7 @@ class ExposureDatabase(context: Context) : SQLiteOpenHelper(context, DB_NAME, nu
}
val time = (System.currentTimeMillis() - start).toDouble() / 1000.0
executor.shutdown()
Log.d(TAG, "Processed ${keys.size} keys in ${System.currentTimeMillis() - start}s -> ${(keys.size.toDouble() / time * 1000).roundToInt().toDouble() / 1000.0} keys/s")
Log.d(TAG, "Processed ${keys.size} keys in ${time}s -> ${(keys.size.toDouble() / time * 1000).roundToInt().toDouble() / 1000.0} keys/s")
}
fun findAllMeasuredExposures(packageName: String, token: String): List<MeasuredExposure> {

View file

@ -22,13 +22,16 @@ class ExposureNotificationService : BaseService(TAG, GmsService.NEARBY_EXPOSURE)
override fun onDestroy() {
super.onDestroy()
database.close()
database.unref()
}
override fun onCreate() {
super.onCreate()
if (!this::database.isInitialized) {
database = ExposureDatabase(this)
}
database.ref()
}
override fun handleServiceRequest(callback: IGmsCallbacks, request: GetServiceRequest, service: GmsService) {
PackageUtils.getAndCheckCallingPackage(this, request.packageName)

View file

@ -59,11 +59,9 @@ class ExposureNotificationServiceImpl(private val context: Context, private val
if (resultCode == SUCCESS) {
ExposurePreferences(context).scannerEnabled = true
}
database.use {
it.noteAppAction(packageName, "start", JSONObject().apply {
database.noteAppAction(packageName, "start", JSONObject().apply {
put("result", resultCode)
}.toString())
}
try {
params.callback.onResult(Status(if (resultCode == SUCCESS) SUCCESS else FAILED_REJECTED_OPT_IN, resultData?.getString("message")))
} catch (e: Exception) {
@ -77,11 +75,9 @@ class ExposureNotificationServiceImpl(private val context: Context, private val
if (resultCode == SUCCESS) {
ExposurePreferences(context).scannerEnabled = false
}
database.use {
it.noteAppAction(packageName, "stop", JSONObject().apply {
database.noteAppAction(packageName, "stop", JSONObject().apply {
put("result", resultCode)
}.toString())
}
try {
params.callback.onResult(Status.SUCCESS)
} catch (e: Exception) {
@ -106,12 +102,10 @@ class ExposureNotificationServiceImpl(private val context: Context, private val
FAILED_REJECTED_OPT_IN to emptyList()
}
database.use {
it.noteAppAction(packageName, "getTemporaryExposureKeyHistory", JSONObject().apply {
database.noteAppAction(packageName, "getTemporaryExposureKeyHistory", JSONObject().apply {
put("result", resultCode)
put("response_keys_size", response.size)
}.toString())
}
try {
params.callback.onResult(Status(status, resultData?.getString("message")), response)
} catch (e: Exception) {
@ -140,7 +134,9 @@ class ExposureNotificationServiceImpl(private val context: Context, private val
}
override fun provideDiagnosisKeys(params: ProvideDiagnosisKeysParams) {
database.ref()
Thread(Runnable {
try {
if (params.configuration != null) {
database.storeConfiguration(packageName, params.token, params.configuration)
}
@ -183,7 +179,6 @@ class ExposureNotificationServiceImpl(private val context: Context, private val
}
Log.d(TAG, "$packageName/${params.token} provided $keys keys")
Handler(Looper.getMainLooper()).post {
database.noteAppAction(packageName, "provideDiagnosisKeys", JSONObject().apply {
put("request_token", params.token)
put("request_keys_size", params.keys?.size)
@ -191,6 +186,7 @@ class ExposureNotificationServiceImpl(private val context: Context, private val
put("request_keys_count", keys)
}.toString())
Handler(Looper.getMainLooper()).post {
try {
params.callback.onResult(Status.SUCCESS)
} catch (e: Exception) {
@ -198,10 +194,8 @@ class ExposureNotificationServiceImpl(private val context: Context, private val
}
}
val match = database.use {
it.finishMatching(packageName, params.token)
it.findAllMeasuredExposures(packageName, params.token).isNotEmpty()
}
database.finishMatching(packageName, params.token)
val match = database.findAllMeasuredExposures(packageName, params.token).isNotEmpty()
try {
val intent = Intent(if (match) ACTION_EXPOSURE_STATE_UPDATED else ACTION_EXPOSURE_NOT_FOUND)
@ -211,6 +205,9 @@ class ExposureNotificationServiceImpl(private val context: Context, private val
} catch (e: Exception) {
Log.w(TAG, "Callback failed", e)
}
} finally {
database.unref()
}
}).start()
}
@ -237,8 +234,7 @@ class ExposureNotificationServiceImpl(private val context: Context, private val
.setSummationRiskScore(exposures.map { it.getRiskScore(configuration) }.sum())
.build()
database.use {
it.noteAppAction(packageName, "getExposureSummary", JSONObject().apply {
database.noteAppAction(packageName, "getExposureSummary", JSONObject().apply {
put("request_token", params.token)
put("response_days_since", response.daysSinceLastExposure)
put("response_matched_keys", response.matchedKeyCount)
@ -248,7 +244,6 @@ class ExposureNotificationServiceImpl(private val context: Context, private val
})
put("response_summation_risk", response.summationRiskScore)
}.toString())
}
try {
params.callback.onResult(Status.SUCCESS, response)
} catch (e: Exception) {
@ -271,12 +266,10 @@ class ExposureNotificationServiceImpl(private val context: Context, private val
it.toExposureInformation(configuration)
}
database.use {
database.noteAppAction(packageName, "getExposureInformation", JSONObject().apply {
put("request_token", params.token)
put("response_size", response.size)
}.toString())
}
try {
params.callback.onResult(Status.SUCCESS, response)
} catch (e: Exception) {