Skip to content

Commit

Permalink
Merge pull request #11738 from cillyvms/a13-player-notifs
Browse files Browse the repository at this point in the history
Always allow changing player notification preferences on Android 13+
  • Loading branch information
Stypox authored Nov 27, 2024
2 parents e4b0245 + 213f49f commit 52942ff
Showing 1 changed file with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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<SubscriptionEntity>) {
val notified = subscriptions.count { it.notificationMode != NotificationMode.DISABLED }
val preference = findPreference<Preference>(getString(R.string.streams_notifications_channels_key))
Expand Down

0 comments on commit 52942ff

Please sign in to comment.