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.annotation.TargetApi
|
|
|
|
import android.os.Bundle
|
2020-08-11 20:34:04 +00:00
|
|
|
import android.text.format.DateFormat
|
2020-08-03 16:07:06 +00:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
|
|
|
import androidx.preference.PreferenceCategory
|
|
|
|
import androidx.preference.PreferenceFragmentCompat
|
|
|
|
import com.db.williamchart.data.Scale
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.withContext
|
|
|
|
import org.microg.gms.nearby.exposurenotification.ExposureDatabase
|
|
|
|
import java.util.*
|
|
|
|
import kotlin.math.roundToInt
|
2020-10-12 19:25:34 +00:00
|
|
|
import kotlin.math.roundToLong
|
2020-08-03 16:07:06 +00:00
|
|
|
|
|
|
|
@TargetApi(21)
|
|
|
|
class ExposureNotificationsRpisFragment : PreferenceFragmentCompat() {
|
|
|
|
private lateinit var histogramCategory: PreferenceCategory
|
|
|
|
private lateinit var histogram: BarChartPreference
|
|
|
|
|
|
|
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
|
|
|
addPreferencesFromResource(R.xml.preferences_exposure_notifications_rpis)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBindPreferences() {
|
|
|
|
histogramCategory = preferenceScreen.findPreference("prefcat_exposure_rpi_histogram") ?: histogramCategory
|
|
|
|
histogram = preferenceScreen.findPreference("pref_exposure_rpi_histogram") ?: histogram
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
updateChart()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun updateChart() {
|
|
|
|
lifecycleScope.launchWhenResumed {
|
2020-10-12 19:25:34 +00:00
|
|
|
val (totalRpiCount, rpiHistogram) = ExposureDatabase.with(requireContext()) { database ->
|
|
|
|
val map = linkedMapOf<String, Float>()
|
|
|
|
val lowestDate = (System.currentTimeMillis() / 24 / 60 / 60 / 1000 - 13).toDouble().roundToLong() * 24 * 60 * 60 * 1000
|
|
|
|
for (i in 0..13) {
|
|
|
|
val date = Calendar.getInstance().apply { this.time = Date(lowestDate + i * 24 * 60 * 60 * 1000) }.get(Calendar.DAY_OF_MONTH)
|
|
|
|
val str = when (i) {
|
|
|
|
0, 13 -> DateFormat.format(DateFormat.getBestDateTimePattern(Locale.getDefault(), "MMMd"), lowestDate + i * 24 * 60 * 60 * 1000).toString()
|
|
|
|
else -> IntArray(date).joinToString("").replace("0", "\u200B")
|
2020-08-05 12:17:29 +00:00
|
|
|
}
|
2020-10-12 19:25:34 +00:00
|
|
|
map[str] = 0f
|
|
|
|
}
|
|
|
|
val refDateLow = Calendar.getInstance().apply { this.time = Date(lowestDate) }.get(Calendar.DAY_OF_MONTH)
|
|
|
|
val refDateHigh = Calendar.getInstance().apply { this.time = Date(lowestDate + 13 * 24 * 60 * 60 * 1000) }.get(Calendar.DAY_OF_MONTH)
|
|
|
|
for (entry in database.rpiHistogram) {
|
|
|
|
val time = Date(entry.key * 24 * 60 * 60 * 1000)
|
|
|
|
if (time.time < lowestDate) continue // Ignore old data
|
|
|
|
val date = Calendar.getInstance().apply { this.time = time }.get(Calendar.DAY_OF_MONTH)
|
|
|
|
val str = when (date) {
|
|
|
|
refDateLow, refDateHigh -> DateFormat.format(DateFormat.getBestDateTimePattern(Locale.getDefault(), "MMMd"), entry.key * 24 * 60 * 60 * 1000).toString()
|
|
|
|
else -> IntArray(date).joinToString("").replace("0", "\u200B")
|
2020-08-05 12:17:29 +00:00
|
|
|
}
|
2020-10-12 19:25:34 +00:00
|
|
|
map[str] = entry.value.toFloat()
|
2020-08-03 16:07:06 +00:00
|
|
|
}
|
2020-10-12 19:25:34 +00:00
|
|
|
val totalRpiCount = database.totalRpiCount
|
|
|
|
totalRpiCount to map
|
2020-08-03 16:07:06 +00:00
|
|
|
}
|
|
|
|
histogramCategory.title = getString(R.string.prefcat_exposure_rpis_histogram_title, totalRpiCount)
|
|
|
|
histogram.labelsFormatter = { it.roundToInt().toString() }
|
2020-09-27 13:27:21 +00:00
|
|
|
histogram.scale = Scale(0f, rpiHistogram.values.max()?.coerceAtLeast(0.1f) ?: 0.1f)
|
2020-08-03 16:07:06 +00:00
|
|
|
histogram.data = rpiHistogram
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|