116 lines
4.9 KiB
Kotlin
116 lines
4.9 KiB
Kotlin
package se.ajpanton.notificationsmaster
|
|
|
|
import android.Manifest
|
|
import android.os.Bundle
|
|
import android.view.View
|
|
import android.widget.Toast
|
|
import androidx.activity.result.contract.ActivityResultContracts
|
|
import androidx.fragment.app.Fragment
|
|
import com.google.android.material.switchmaterial.SwitchMaterial
|
|
import se.ajpanton.notificationsmaster.databinding.FragmentMiscellaneousBinding
|
|
import se.ajpanton.notificationsmaster.debug.DebugNotifications
|
|
import se.ajpanton.notificationsmaster.visibility.SystemUiMiscSettings
|
|
import se.ajpanton.notificationsmaster.visibility.VisibilityPolicyStore
|
|
|
|
class MiscellaneousFragment : Fragment(R.layout.fragment_miscellaneous) {
|
|
private var binding: FragmentMiscellaneousBinding? = null
|
|
private lateinit var store: VisibilityPolicyStore
|
|
private var pendingCount: Int? = null
|
|
private var pendingCycle = false
|
|
private var updatingCycle = false
|
|
private val permissionRequest = registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
|
|
val context = context ?: return@registerForActivityResult
|
|
if (granted) {
|
|
pendingCount?.let { DebugNotifications.setCount(context, it) }
|
|
if (pendingCycle) DebugNotifications.setCycling(context, true)
|
|
} else {
|
|
Toast.makeText(context, R.string.debug_notification_permission_required, Toast.LENGTH_SHORT).show()
|
|
}
|
|
pendingCount = null
|
|
pendingCycle = false
|
|
refreshDebugNotifications()
|
|
}
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
binding = FragmentMiscellaneousBinding.bind(view)
|
|
store = VisibilityPolicyStore(requireContext())
|
|
val settings = store.load().systemUiMisc
|
|
bind(binding!!.keepAodVisibleDuringCalls, settings.keepAodVisibleDuringCalls) { current, checked ->
|
|
current.copy(keepAodVisibleDuringCalls = checked)
|
|
}
|
|
bind(binding!!.showLockscreenBatteryPercent, settings.showLockscreenBatteryPercentWhenUnplugged) {
|
|
current, checked ->
|
|
current.copy(showLockscreenBatteryPercentWhenUnplugged = checked)
|
|
}
|
|
bind(binding!!.showAodBatteryPercent, settings.showAodBatteryPercentWhenUnplugged) { current, checked ->
|
|
current.copy(showAodBatteryPercentWhenUnplugged = checked)
|
|
}
|
|
if (!DeviceCapabilities.isOneUi) {
|
|
binding!!.oneUiOnlyNotice.visibility = View.VISIBLE
|
|
binding!!.oneUiMiscControls.alpha = DISABLED_CONTROL_ALPHA
|
|
binding!!.oneUiMiscControls.disableRecursively()
|
|
}
|
|
binding!!.debugMinus10.setOnClickListener { changeDebugCount(-10) }
|
|
binding!!.debugMinus1.setOnClickListener { changeDebugCount(-1) }
|
|
binding!!.debugPlus1.setOnClickListener { changeDebugCount(1) }
|
|
binding!!.debugPlus10.setOnClickListener { changeDebugCount(10) }
|
|
binding!!.debugReset.setOnClickListener { setDebugCount(0) }
|
|
binding!!.debugCycle.setOnCheckedChangeListener { _, enabled ->
|
|
if (updatingCycle) return@setOnCheckedChangeListener
|
|
if (enabled && !DebugNotifications.hasPermission(requireContext())) {
|
|
pendingCycle = true
|
|
permissionRequest.launch(Manifest.permission.POST_NOTIFICATIONS)
|
|
} else {
|
|
DebugNotifications.setCycling(requireContext(), enabled)
|
|
}
|
|
}
|
|
}
|
|
|
|
override fun onResume() {
|
|
super.onResume()
|
|
activity?.title = getString(R.string.page_miscellaneous)
|
|
DebugNotifications.apply(requireContext())
|
|
refreshDebugNotifications()
|
|
}
|
|
|
|
override fun onDestroyView() {
|
|
binding = null
|
|
super.onDestroyView()
|
|
}
|
|
|
|
private fun bind(
|
|
toggle: SwitchMaterial,
|
|
initial: Boolean,
|
|
update: (SystemUiMiscSettings, Boolean) -> SystemUiMiscSettings,
|
|
) {
|
|
toggle.isChecked = initial
|
|
toggle.setOnCheckedChangeListener { _, checked ->
|
|
store.update { policy -> policy.copy(systemUiMisc = update(policy.systemUiMisc, checked)) }
|
|
}
|
|
}
|
|
|
|
private fun changeDebugCount(delta: Int) = setDebugCount(DebugNotifications.count(requireContext()) + delta)
|
|
|
|
private fun setDebugCount(count: Int) {
|
|
val desired = count.coerceIn(0, DebugNotifications.MAX_COUNT)
|
|
if (desired > 0 && !DebugNotifications.hasPermission(requireContext())) {
|
|
pendingCount = desired
|
|
permissionRequest.launch(Manifest.permission.POST_NOTIFICATIONS)
|
|
return
|
|
}
|
|
DebugNotifications.setCount(requireContext(), desired)
|
|
refreshDebugNotifications()
|
|
}
|
|
|
|
private fun refreshDebugNotifications() {
|
|
val context = context ?: return
|
|
val count = DebugNotifications.count(context)
|
|
binding?.debugCount?.text = count.toString()
|
|
binding?.debugCycle?.isEnabled = count > 0
|
|
updatingCycle = true
|
|
binding?.debugCycle?.isChecked = DebugNotifications.isCycling(context)
|
|
updatingCycle = false
|
|
}
|
|
|
|
}
|