EN: Display last exposure report in settings

This commit is contained in:
Marvin W 2020-09-04 10:37:02 +02:00
parent 876e32acd5
commit fd6d915f0a
No known key found for this signature in database
GPG Key ID: 072E9235DB996F2A
6 changed files with 40 additions and 20 deletions

View File

@ -8,10 +8,13 @@ package org.microg.gms.ui
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.text.format.DateUtils import android.text.format.DateUtils
import android.util.JsonReader
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import com.google.android.gms.R import com.google.android.gms.R
import org.json.JSONObject
import org.microg.gms.nearby.exposurenotification.ExposureDatabase import org.microg.gms.nearby.exposurenotification.ExposureDatabase
import java.util.concurrent.TimeUnit
class ExposureNotificationsAppPreferencesFragment : PreferenceFragmentCompat() { class ExposureNotificationsAppPreferencesFragment : PreferenceFragmentCompat() {
private lateinit var open: Preference private lateinit var open: Preference
@ -53,6 +56,19 @@ class ExposureNotificationsAppPreferencesFragment : PreferenceFragmentCompat() {
if (lastCheckTime != null && lastCheckTime != 0L) { if (lastCheckTime != null && lastCheckTime != 0L) {
str += "\n" + getString(R.string.pref_exposure_app_last_check_summary, DateUtils.getRelativeDateTimeString(context, lastCheckTime, DateUtils.MINUTE_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, DateUtils.FORMAT_SHOW_TIME)) str += "\n" + getString(R.string.pref_exposure_app_last_check_summary, DateUtils.getRelativeDateTimeString(context, lastCheckTime, DateUtils.MINUTE_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, DateUtils.FORMAT_SHOW_TIME))
} }
val lastExposureSummaryTime = database.lastMethodCall(packageName, "getExposureSummary")
val lastExposureSummary = database.lastMethodCallArgs(packageName, "getExposureSummary")
if (lastExposureSummaryTime != null && lastExposureSummary != null && System.currentTimeMillis() - lastExposureSummaryTime <= TimeUnit.DAYS.toMillis(1)) {
try {
val json = JSONObject(lastExposureSummary)
val matchedKeys = json.optInt("response_matched_keys")
val daysSince = json.optInt("response_days_since", -1)
if (matchedKeys > 0 && daysSince >= 0) {
str += "\n" + resources.getQuantityString(R.plurals.pref_exposure_app_last_report_summary, matchedKeys, matchedKeys, daysSince)
}
} catch (ignored: Exception) {
}
}
checks.summary = str checks.summary = str
} }
} }

View File

@ -32,4 +32,8 @@
<item quantity="one">Request missing permission</item> <item quantity="one">Request missing permission</item>
<item quantity="other">Request missing permissions</item> <item quantity="other">Request missing permissions</item>
</plurals> </plurals>
</resources> <plurals name="pref_exposure_app_last_report_summary">
<item quantity="one">Last report: <xliff:g example="1">%1$d</xliff:g> match, <xliff:g example="3">%2$d</xliff:g> days ago</item>
<item quantity="other">Last report: <xliff:g example="2">%1$d</xliff:g> matches, latest <xliff:g example="3">%2$d</xliff:g> days ago</item>
</plurals>
</resources>

View File

@ -16,6 +16,6 @@
<Preference <Preference
android:key="pref_exposure_app_checks" android:key="pref_exposure_app_checks"
android:selectable="false" android:selectable="false"
tools:summary="7 checks in past 14 days\nLast check: 3 hours ago" /> tools:summary="7 checks in past 14 days\nLast check: 3 hours ago\nLast report: 2 matches, latest 3 days ago" />
</PreferenceCategory> </PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>

View File

@ -444,6 +444,16 @@ class ExposureDatabase private constructor(private val context: Context) : SQLit
} }
} }
fun lastMethodCallArgs(packageName: String, method: String): String? = readableDatabase.run {
query(TABLE_APP_LOG, arrayOf("args"), "package = ? AND method = ?", arrayOf(packageName, method), null, null, "timestamp DESC", "1").use { cursor ->
if (cursor.moveToNext()) {
cursor.getString(0)
} else {
null
}
}
}
private val currentTemporaryExposureKey: TemporaryExposureKey private val currentTemporaryExposureKey: TemporaryExposureKey
get() = findOwnKeyAt(currentRollingStartNumber.toInt()) get() = findOwnKeyAt(currentRollingStartNumber.toInt())
?: storeOwnKey(generateCurrentTemporaryExposureKey()) ?: storeOwnKey(generateCurrentTemporaryExposureKey())

View File

@ -72,24 +72,14 @@ class ExposureNotificationServiceImpl(private val context: Context, private val
} }
override fun stop(params: StopParams) { override fun stop(params: StopParams) {
if (!ExposurePreferences(context).enabled) { ExposurePreferences(context).enabled = false
params.callback.onResult(Status.SUCCESS) ExposureDatabase.with(context) { database ->
return database.noteAppAction(packageName, "stop")
} }
confirm(CONFIRM_ACTION_STOP) { resultCode, _ -> try {
if (resultCode == SUCCESS) { params.callback.onResult(Status.SUCCESS)
ExposurePreferences(context).enabled = false } catch (e: Exception) {
} Log.w(TAG, "Callback failed", e)
ExposureDatabase.with(context) { database ->
database.noteAppAction(packageName, "stop", JSONObject().apply {
put("result", resultCode)
}.toString())
}
try {
params.callback.onResult(Status.SUCCESS)
} catch (e: Exception) {
Log.w(TAG, "Callback failed", e)
}
} }
} }

View File

@ -55,7 +55,7 @@ data class MergedExposure internal constructor(val key: TemporaryExposureKey, va
get() = TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis() - timestamp) get() = TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis() - timestamp)
val attenuation val attenuation
get() = subs.map { it.attenuation }.min()!! get() = (subs.map { it.attenuation * it.duration }.sum().toDouble() / subs.map { it.duration }.sum().toDouble()).toInt()
fun getAttenuationRiskScore(configuration: ExposureConfiguration): Int { fun getAttenuationRiskScore(configuration: ExposureConfiguration): Int {
return when { return when {