From 80bf47493e5c9cc10c1510125f4a5fa9099e0963 Mon Sep 17 00:00:00 2001 From: TobiGr Date: Fri, 22 Oct 2021 21:24:22 +0200 Subject: [PATCH] Fix check wether the app's notifications are disabled via system settings Add comments Rename a few methods --- .../feed/notifications/NotificationHelper.kt | 30 +++++++++++++++---- .../feed/notifications/NotificationWorker.kt | 2 +- .../settings/NotificationsSettingsFragment.kt | 6 ++-- .../schabi/newpipe/util/NavigationHelper.java | 8 ++--- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationHelper.kt b/app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationHelper.kt index fac3b8f72..fa26be37a 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationHelper.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationHelper.kt @@ -101,39 +101,57 @@ class NotificationHelper(val context: Context) { companion object { /** - * Check whether notifications are not disabled by user via system settings. + * Check whether notifications are enabled on the device. + * Users can disable them via the system settings for a single app. + * If this is the case, the app cannot create any notifications + * and display them to the user. + *
+ * On Android 26 and above, notification channels are used by NewPipe. + * These can be configured by the user, too. + * The notification channel for new streams is also checked by this method. * * @param context Context - * @return true if notifications are allowed, false otherwise + * @return true if notifications are allowed and can be displayed; + * false otherwise */ - fun isNotificationsEnabledNative(context: Context): Boolean { + fun areNotificationsEnabledOnDevice(context: Context): Boolean { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channelId = context.getString(R.string.streams_notification_channel_id) val manager = context.getSystemService( Context.NOTIFICATION_SERVICE ) as NotificationManager + val enabled = manager.areNotificationsEnabled() val channel = manager.getNotificationChannel(channelId) - channel != null && channel.importance != NotificationManager.IMPORTANCE_NONE + val importance = channel?.importance + enabled && channel != null && importance != NotificationManager.IMPORTANCE_NONE } else { NotificationManagerCompat.from(context).areNotificationsEnabled() } } @JvmStatic + /** + * Whether the user enabled the notifications for new streams in the app settings. + */ fun areNewStreamsNotificationsEnabled(context: Context): Boolean { return ( PreferenceManager.getDefaultSharedPreferences(context) .getBoolean(context.getString(R.string.enable_streams_notifications), false) && - isNotificationsEnabledNative(context) + areNotificationsEnabledOnDevice(context) ) } - fun openNativeSettingsScreen(context: Context) { + /** + * Open the system's notification settings for NewPipe on Android O (API 26) and later. + * Open the system's app settings for NewPipe on previous Android versions. + */ + fun openNewPipeSystemNotificationSettings(context: Context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channelId = context.getString(R.string.streams_notification_channel_id) val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS) .putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName) .putExtra(Settings.EXTRA_CHANNEL_ID, channelId) + .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) context.startActivity(intent) } else { val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationWorker.kt b/app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationWorker.kt index daae52fdd..afdeee7f4 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationWorker.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationWorker.kt @@ -75,7 +75,7 @@ class NotificationWorker( .getBoolean( context.getString(R.string.enable_streams_notifications), false - ) && NotificationHelper.isNotificationsEnabledNative(context) + ) && NotificationHelper.areNotificationsEnabledOnDevice(context) } fun schedule(context: Context, options: ScheduleOptions, force: Boolean = false) { 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 50fb95450..938b7ff8d 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/NotificationsSettingsFragment.kt +++ b/app/src/main/java/org/schabi/newpipe/settings/NotificationsSettingsFragment.kt @@ -47,7 +47,7 @@ class NotificationsSettingsFragment : BasePreferenceFragment(), OnSharedPreferen override fun onResume() { super.onResume() - val enabled = NotificationHelper.isNotificationsEnabledNative(requireContext()) + val enabled = NotificationHelper.areNotificationsEnabledOnDevice(requireContext()) preferenceScreen.isEnabled = enabled if (!enabled) { if (notificationWarningSnackbar == null) { @@ -56,8 +56,8 @@ class NotificationsSettingsFragment : BasePreferenceFragment(), OnSharedPreferen R.string.notifications_disabled, Snackbar.LENGTH_INDEFINITE ).apply { - setAction(R.string.settings) { v -> - NotificationHelper.openNativeSettingsScreen(v.context) + setAction(R.string.settings) { + activity?.let { NotificationHelper.openNewPipeSystemNotificationSettings(it) } } setActionTextColor(Color.YELLOW) addCallback(object : Snackbar.Callback() { diff --git a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java index 80267a9dd..f70002409 100644 --- a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java @@ -1,5 +1,7 @@ package org.schabi.newpipe.util; +import static org.schabi.newpipe.util.external_communication.ShareUtils.installApp; + import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; @@ -18,6 +20,8 @@ import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; +import com.jakewharton.processphoenix.ProcessPhoenix; + import org.schabi.newpipe.MainActivity; import org.schabi.newpipe.NewPipeDatabase; import org.schabi.newpipe.R; @@ -57,10 +61,6 @@ import org.schabi.newpipe.util.external_communication.ShareUtils; import java.util.ArrayList; -import static org.schabi.newpipe.util.external_communication.ShareUtils.installApp; - -import com.jakewharton.processphoenix.ProcessPhoenix; - public final class NavigationHelper { public static final String MAIN_FRAGMENT_TAG = "main_fragment_tag"; public static final String SEARCH_FRAGMENT_TAG = "search_fragment_tag";