2020-08-03 16:07:06 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2020, microG Project Team
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-10-07 16:14:41 +00:00
|
|
|
package org.microg.gms.nearby.core.ui
|
2020-08-03 16:07:06 +00:00
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.os.Handler
|
|
|
|
import androidx.core.os.bundleOf
|
|
|
|
import androidx.lifecycle.lifecycleScope
|
|
|
|
import androidx.navigation.fragment.findNavController
|
|
|
|
import androidx.preference.Preference
|
|
|
|
import androidx.preference.PreferenceCategory
|
|
|
|
import androidx.preference.PreferenceFragmentCompat
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.withContext
|
|
|
|
import org.microg.gms.nearby.exposurenotification.ExposureDatabase
|
2020-09-02 19:41:15 +00:00
|
|
|
import org.microg.gms.nearby.exposurenotification.getExposureNotificationsServiceInfo
|
2020-10-07 16:14:41 +00:00
|
|
|
import org.microg.gms.ui.AppIconPreference
|
|
|
|
import org.microg.gms.ui.getApplicationInfoIfExists
|
|
|
|
import org.microg.gms.ui.navigate
|
2020-08-03 16:07:06 +00:00
|
|
|
|
|
|
|
class ExposureNotificationsPreferencesFragment : PreferenceFragmentCompat() {
|
|
|
|
private lateinit var exposureEnableInfo: Preference
|
|
|
|
private lateinit var exposureApps: PreferenceCategory
|
|
|
|
private lateinit var exposureAppsNone: Preference
|
|
|
|
private lateinit var collectedRpis: Preference
|
|
|
|
private lateinit var advertisingId: Preference
|
|
|
|
private val handler = Handler()
|
|
|
|
private val updateStatusRunnable = Runnable { updateStatus() }
|
|
|
|
private val updateContentRunnable = Runnable { updateContent() }
|
|
|
|
|
|
|
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
|
|
|
addPreferencesFromResource(R.xml.preferences_exposure_notifications)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBindPreferences() {
|
|
|
|
exposureEnableInfo = preferenceScreen.findPreference("pref_exposure_enable_info") ?: exposureEnableInfo
|
|
|
|
exposureApps = preferenceScreen.findPreference("prefcat_exposure_apps") ?: exposureApps
|
|
|
|
exposureAppsNone = preferenceScreen.findPreference("pref_exposure_apps_none") ?: exposureAppsNone
|
|
|
|
collectedRpis = preferenceScreen.findPreference("pref_exposure_collected_rpis") ?: collectedRpis
|
|
|
|
advertisingId = preferenceScreen.findPreference("pref_exposure_advertising_id") ?: advertisingId
|
|
|
|
collectedRpis.onPreferenceClickListener = Preference.OnPreferenceClickListener {
|
2020-08-05 13:48:51 +00:00
|
|
|
findNavController().navigate(requireContext(), R.id.openExposureRpis)
|
2020-08-03 16:07:06 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
2020-08-05 12:17:29 +00:00
|
|
|
|
2020-08-03 16:07:06 +00:00
|
|
|
updateStatus()
|
|
|
|
updateContent()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPause() {
|
|
|
|
super.onPause()
|
2020-08-05 12:17:29 +00:00
|
|
|
|
2020-08-03 16:07:06 +00:00
|
|
|
handler.removeCallbacks(updateStatusRunnable)
|
|
|
|
handler.removeCallbacks(updateContentRunnable)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun updateStatus() {
|
|
|
|
lifecycleScope.launchWhenResumed {
|
|
|
|
handler.postDelayed(updateStatusRunnable, UPDATE_STATUS_INTERVAL)
|
2020-09-02 19:41:15 +00:00
|
|
|
val enabled = getExposureNotificationsServiceInfo(requireContext()).configuration.enabled
|
|
|
|
exposureEnableInfo.isVisible = !enabled
|
|
|
|
advertisingId.isVisible = enabled
|
2020-08-03 16:07:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun updateContent() {
|
|
|
|
lifecycleScope.launchWhenResumed {
|
|
|
|
handler.postDelayed(updateContentRunnable, UPDATE_CONTENT_INTERVAL)
|
|
|
|
val context = requireContext()
|
2020-10-12 19:25:34 +00:00
|
|
|
val (apps, lastHourKeys, currentId) = ExposureDatabase.with(context) { database ->
|
|
|
|
val apps = database.appList.map { packageName ->
|
|
|
|
context.packageManager.getApplicationInfoIfExists(packageName)
|
|
|
|
}.filterNotNull().mapIndexed { idx, applicationInfo ->
|
|
|
|
val pref = AppIconPreference(context)
|
|
|
|
pref.order = idx
|
|
|
|
pref.title = applicationInfo.loadLabel(context.packageManager)
|
|
|
|
pref.icon = applicationInfo.loadIcon(context.packageManager)
|
|
|
|
pref.onPreferenceClickListener = Preference.OnPreferenceClickListener {
|
|
|
|
findNavController().navigate(requireContext(), R.id.openExposureAppDetails, bundleOf(
|
|
|
|
"package" to applicationInfo.packageName
|
|
|
|
))
|
|
|
|
true
|
2020-08-03 16:07:06 +00:00
|
|
|
}
|
2020-10-12 19:25:34 +00:00
|
|
|
pref.key = "pref_exposure_app_" + applicationInfo.packageName
|
|
|
|
pref
|
2020-08-03 16:07:06 +00:00
|
|
|
}
|
2020-10-12 19:25:34 +00:00
|
|
|
val lastHourKeys = database.hourRpiCount
|
|
|
|
val currentId = database.currentRpiId
|
|
|
|
Triple(apps, lastHourKeys, currentId)
|
2020-08-03 16:07:06 +00:00
|
|
|
}
|
|
|
|
collectedRpis.summary = getString(R.string.pref_exposure_collected_rpis_summary, lastHourKeys)
|
2020-09-08 22:50:16 +00:00
|
|
|
if (currentId != null) {
|
|
|
|
advertisingId.isVisible = true
|
|
|
|
advertisingId.summary = currentId.toString()
|
|
|
|
} else {
|
|
|
|
advertisingId.isVisible = false
|
|
|
|
}
|
2020-08-03 16:07:06 +00:00
|
|
|
exposureApps.removeAll()
|
|
|
|
if (apps.isEmpty()) {
|
|
|
|
exposureApps.addPreference(exposureAppsNone)
|
|
|
|
} else {
|
|
|
|
for (app in apps) {
|
|
|
|
exposureApps.addPreference(app)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
private const val UPDATE_STATUS_INTERVAL = 1000L
|
|
|
|
private const val UPDATE_CONTENT_INTERVAL = 60000L
|
|
|
|
}
|
|
|
|
}
|