From 213f49f5c422ab52d79c70279234b6fcad26f7c3 Mon Sep 17 00:00:00 2001 From: cillyvms Date: Fri, 22 Nov 2024 14:21:46 +0100 Subject: [PATCH] Allow changing player notification preferences regardless of system settings on Android 13 and above. --- .../settings/NotificationsSettingsFragment.kt | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/settings/NotificationsSettingsFragment.kt b/app/src/main/java/org/schabi/newpipe/settings/NotificationsSettingsFragment.kt index fcc9abf73..2d3344c09 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/NotificationsSettingsFragment.kt +++ b/app/src/main/java/org/schabi/newpipe/settings/NotificationsSettingsFragment.kt @@ -3,8 +3,10 @@ package org.schabi.newpipe.settings import android.content.SharedPreferences import android.content.SharedPreferences.OnSharedPreferenceChangeListener import android.graphics.Color +import android.os.Build import android.os.Bundle import androidx.preference.Preference +import androidx.preference.SwitchPreference import com.google.android.material.snackbar.Snackbar import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers import io.reactivex.rxjava3.disposables.Disposable @@ -21,15 +23,17 @@ import org.schabi.newpipe.local.subscription.SubscriptionManager class NotificationsSettingsFragment : BasePreferenceFragment(), OnSharedPreferenceChangeListener { + private var streamsNotificationsPreference: SwitchPreference? = null private var notificationWarningSnackbar: Snackbar? = null private var loader: Disposable? = null override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { 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 - preferenceScreen.isEnabled = - NotificationHelper.areNotificationsEnabledOnDevice(requireContext()) + updateEnabledState(NotificationHelper.areNotificationsEnabledOnDevice(requireContext())) } override fun onStart() { @@ -68,7 +72,7 @@ class NotificationsSettingsFragment : BasePreferenceFragment(), OnSharedPreferen // If they are disabled, show a snackbar informing the user about that // while allowing them to open the device's app settings. val enabled = NotificationHelper.areNotificationsEnabledOnDevice(requireContext()) - preferenceScreen.isEnabled = enabled // it is disabled by default, see the xml + updateEnabledState(enabled) if (!enabled) { if (notificationWarningSnackbar == null) { notificationWarningSnackbar = Snackbar.make( @@ -109,6 +113,16 @@ class NotificationsSettingsFragment : BasePreferenceFragment(), OnSharedPreferen 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) { val notified = subscriptions.count { it.notificationMode != NotificationMode.DISABLED } val preference = findPreference(getString(R.string.streams_notifications_channels_key))