Allow changing player notification preferences regardless of system settings on Android 13 and above.

This commit is contained in:
cillyvms 2024-11-22 14:21:46 +01:00
parent 14081505cd
commit 213f49f5c4
No known key found for this signature in database

View file

@ -3,8 +3,10 @@ package org.schabi.newpipe.settings
import android.content.SharedPreferences import android.content.SharedPreferences
import android.content.SharedPreferences.OnSharedPreferenceChangeListener import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import android.graphics.Color import android.graphics.Color
import android.os.Build
import android.os.Bundle import android.os.Bundle
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.SwitchPreference
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.disposables.Disposable import io.reactivex.rxjava3.disposables.Disposable
@ -21,15 +23,17 @@ import org.schabi.newpipe.local.subscription.SubscriptionManager
class NotificationsSettingsFragment : BasePreferenceFragment(), OnSharedPreferenceChangeListener { class NotificationsSettingsFragment : BasePreferenceFragment(), OnSharedPreferenceChangeListener {
private var streamsNotificationsPreference: SwitchPreference? = null
private var notificationWarningSnackbar: Snackbar? = null private var notificationWarningSnackbar: Snackbar? = null
private var loader: Disposable? = null private var loader: Disposable? = null
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.notifications_settings) addPreferencesFromResource(R.xml.notifications_settings)
streamsNotificationsPreference =
findPreference(getString(R.string.enable_streams_notifications))
// main check is done in onResume, but also do it here to prevent flickering // main check is done in onResume, but also do it here to prevent flickering
preferenceScreen.isEnabled = updateEnabledState(NotificationHelper.areNotificationsEnabledOnDevice(requireContext()))
NotificationHelper.areNotificationsEnabledOnDevice(requireContext())
} }
override fun onStart() { override fun onStart() {
@ -68,7 +72,7 @@ class NotificationsSettingsFragment : BasePreferenceFragment(), OnSharedPreferen
// If they are disabled, show a snackbar informing the user about that // If they are disabled, show a snackbar informing the user about that
// while allowing them to open the device's app settings. // while allowing them to open the device's app settings.
val enabled = NotificationHelper.areNotificationsEnabledOnDevice(requireContext()) val enabled = NotificationHelper.areNotificationsEnabledOnDevice(requireContext())
preferenceScreen.isEnabled = enabled // it is disabled by default, see the xml updateEnabledState(enabled)
if (!enabled) { if (!enabled) {
if (notificationWarningSnackbar == null) { if (notificationWarningSnackbar == null) {
notificationWarningSnackbar = Snackbar.make( notificationWarningSnackbar = Snackbar.make(
@ -109,6 +113,16 @@ class NotificationsSettingsFragment : BasePreferenceFragment(), OnSharedPreferen
super.onPause() super.onPause()
} }
private fun updateEnabledState(enabled: Boolean) {
// On Android 13 player notifications are exempt from notification settings
// so the preferences in app should always be available.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
streamsNotificationsPreference?.isEnabled = enabled
} else {
preferenceScreen.isEnabled = enabled
}
}
private fun updateSubscriptions(subscriptions: List<SubscriptionEntity>) { private fun updateSubscriptions(subscriptions: List<SubscriptionEntity>) {
val notified = subscriptions.count { it.notificationMode != NotificationMode.DISABLED } val notified = subscriptions.count { it.notificationMode != NotificationMode.DISABLED }
val preference = findPreference<Preference>(getString(R.string.streams_notifications_channels_key)) val preference = findPreference<Preference>(getString(R.string.streams_notifications_channels_key))