Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always allow changing player notification preferences on Android 13+ #11738

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading