VancedMicroG/play-services-core/src/main/kotlin/org/microg/gms/ui/PushNotificationPreferences...

129 lines
5.1 KiB
Kotlin
Raw Normal View History

2020-07-26 09:59:27 +00:00
/*
* SPDX-FileCopyrightText: 2020, microG Project Team
* SPDX-License-Identifier: Apache-2.0
*/
2020-10-20 23:39:47 +00:00
@file:Suppress("DEPRECATION")
2020-07-26 09:59:27 +00:00
package org.microg.gms.ui
2022-01-29 20:23:50 +00:00
import android.annotation.SuppressLint
2020-07-26 09:59:27 +00:00
import android.os.Bundle
import android.os.Handler
import android.text.format.DateUtils
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
2020-07-29 05:47:50 +00:00
import com.mgoogle.android.gms.R
2020-07-26 09:59:27 +00:00
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.microg.gms.gcm.GcmDatabase
2020-09-11 08:11:10 +00:00
import org.microg.gms.gcm.getGcmServiceInfo
2020-07-26 09:59:27 +00:00
class PushNotificationPreferencesFragment : PreferenceFragmentCompat() {
private lateinit var pushStatusCategory: PreferenceCategory
private lateinit var pushStatus: Preference
private lateinit var pushApps: PreferenceCategory
private lateinit var pushAppsAll: Preference
private lateinit var pushAppsNone: Preference
private lateinit var database: GcmDatabase
private val handler = Handler()
private val updateRunnable = Runnable { updateStatus() }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
database = GcmDatabase(context)
}
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.preferences_push_notifications)
}
2022-01-29 20:23:50 +00:00
@SuppressLint("RestrictedApi")
2020-07-26 09:59:27 +00:00
override fun onBindPreferences() {
pushStatusCategory = preferenceScreen.findPreference("prefcat_push_status") ?: pushStatusCategory
pushStatus = preferenceScreen.findPreference("pref_push_status") ?: pushStatus
pushApps = preferenceScreen.findPreference("prefcat_push_apps") ?: pushApps
pushAppsAll = preferenceScreen.findPreference("pref_push_apps_all") ?: pushAppsAll
pushAppsNone = preferenceScreen.findPreference("pref_push_apps_none") ?: pushAppsNone
pushAppsAll.onPreferenceClickListener = Preference.OnPreferenceClickListener {
2020-08-07 16:57:36 +00:00
findNavController().navigate(requireContext(), R.id.openAllGcmApps)
2020-07-26 09:59:27 +00:00
true
}
}
override fun onResume() {
super.onResume()
updateStatus()
updateContent()
}
override fun onPause() {
super.onPause()
database.close()
handler.removeCallbacks(updateRunnable)
}
private fun updateStatus() {
2020-10-20 23:39:47 +00:00
try {
handler.postDelayed(updateRunnable, UPDATE_INTERVAL)
val appContext = requireContext().applicationContext
2020-10-20 23:39:47 +00:00
lifecycleScope.launchWhenStarted {
val statusInfo = getGcmServiceInfo(appContext)
2020-10-20 23:39:47 +00:00
pushStatusCategory.isVisible = statusInfo.configuration.enabled
pushStatus.summary = if (statusInfo.connected) {
getString(R.string.gcm_network_state_connected, DateUtils.getRelativeTimeSpanString(statusInfo.startTimestamp, System.currentTimeMillis(), 0))
} else {
getString(R.string.gcm_network_state_disconnected)
}
2020-09-11 08:11:10 +00:00
}
2020-10-20 23:39:47 +00:00
} catch (e: Exception) {}
2020-07-26 09:59:27 +00:00
}
private fun updateContent() {
val appContext = requireContext().applicationContext
val context = requireContext()
2020-07-26 09:59:27 +00:00
lifecycleScope.launchWhenResumed {
val (apps, showAll) = withContext(Dispatchers.IO) {
val apps = database.appList.sortedByDescending { it.lastMessageTimestamp }
val res = apps.map { app ->
app to appContext.packageManager.getApplicationInfoIfExists(app.packageName)
2020-08-04 23:57:23 +00:00
}.mapNotNull { (app, info) ->
if (info == null) null else app to info
}.take(3).mapIndexed { idx, (app, applicationInfo) ->
val pref = AppIconPreference(appContext)
2020-07-26 09:59:27 +00:00
pref.order = idx
pref.title = applicationInfo.loadLabel(appContext.packageManager)
pref.icon = applicationInfo.loadIcon(appContext.packageManager)
2020-07-26 09:59:27 +00:00
pref.onPreferenceClickListener = Preference.OnPreferenceClickListener {
findNavController().navigate(context, R.id.openGcmAppDetails, bundleOf(
2020-07-26 09:59:27 +00:00
"package" to app.packageName
))
true
}
pref.key = "pref_push_app_" + app.packageName
pref
}.let { it to (it.size < apps.size) }
database.close()
res
}
pushAppsAll.isVisible = showAll
pushApps.removeAll()
for (app in apps) {
pushApps.addPreference(app)
}
if (showAll) {
pushApps.addPreference(pushAppsAll)
} else if (apps.isEmpty()) {
pushApps.addPreference(pushAppsNone)
}
}
}
companion object {
private const val UPDATE_INTERVAL = 1000L
}
}