1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-24 00:50:32 +00:00

Fix check wether the app's notifications are disabled via system settings

Add comments
Rename a few methods
This commit is contained in:
TobiGr 2021-10-22 21:24:22 +02:00
parent 7d4c7718aa
commit 80bf47493e
4 changed files with 32 additions and 14 deletions

View File

@ -101,39 +101,57 @@ class NotificationHelper(val context: Context) {
companion object { 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.
* <br>
* 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 * @param context Context
* @return true if notifications are allowed, false otherwise * @return <code>true</code> if notifications are allowed and can be displayed;
* <code>false</code> otherwise
*/ */
fun isNotificationsEnabledNative(context: Context): Boolean { fun areNotificationsEnabledOnDevice(context: Context): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channelId = context.getString(R.string.streams_notification_channel_id) val channelId = context.getString(R.string.streams_notification_channel_id)
val manager = context.getSystemService( val manager = context.getSystemService(
Context.NOTIFICATION_SERVICE Context.NOTIFICATION_SERVICE
) as NotificationManager ) as NotificationManager
val enabled = manager.areNotificationsEnabled()
val channel = manager.getNotificationChannel(channelId) val channel = manager.getNotificationChannel(channelId)
channel != null && channel.importance != NotificationManager.IMPORTANCE_NONE val importance = channel?.importance
enabled && channel != null && importance != NotificationManager.IMPORTANCE_NONE
} else { } else {
NotificationManagerCompat.from(context).areNotificationsEnabled() NotificationManagerCompat.from(context).areNotificationsEnabled()
} }
} }
@JvmStatic @JvmStatic
/**
* Whether the user enabled the notifications for new streams in the app settings.
*/
fun areNewStreamsNotificationsEnabled(context: Context): Boolean { fun areNewStreamsNotificationsEnabled(context: Context): Boolean {
return ( return (
PreferenceManager.getDefaultSharedPreferences(context) PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(context.getString(R.string.enable_streams_notifications), false) && .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) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channelId = context.getString(R.string.streams_notification_channel_id) val channelId = context.getString(R.string.streams_notification_channel_id)
val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS) val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
.putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName) .putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
.putExtra(Settings.EXTRA_CHANNEL_ID, channelId) .putExtra(Settings.EXTRA_CHANNEL_ID, channelId)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent) context.startActivity(intent)
} else { } else {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)

View File

@ -75,7 +75,7 @@ class NotificationWorker(
.getBoolean( .getBoolean(
context.getString(R.string.enable_streams_notifications), context.getString(R.string.enable_streams_notifications),
false false
) && NotificationHelper.isNotificationsEnabledNative(context) ) && NotificationHelper.areNotificationsEnabledOnDevice(context)
} }
fun schedule(context: Context, options: ScheduleOptions, force: Boolean = false) { fun schedule(context: Context, options: ScheduleOptions, force: Boolean = false) {

View File

@ -47,7 +47,7 @@ class NotificationsSettingsFragment : BasePreferenceFragment(), OnSharedPreferen
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
val enabled = NotificationHelper.isNotificationsEnabledNative(requireContext()) val enabled = NotificationHelper.areNotificationsEnabledOnDevice(requireContext())
preferenceScreen.isEnabled = enabled preferenceScreen.isEnabled = enabled
if (!enabled) { if (!enabled) {
if (notificationWarningSnackbar == null) { if (notificationWarningSnackbar == null) {
@ -56,8 +56,8 @@ class NotificationsSettingsFragment : BasePreferenceFragment(), OnSharedPreferen
R.string.notifications_disabled, R.string.notifications_disabled,
Snackbar.LENGTH_INDEFINITE Snackbar.LENGTH_INDEFINITE
).apply { ).apply {
setAction(R.string.settings) { v -> setAction(R.string.settings) {
NotificationHelper.openNativeSettingsScreen(v.context) activity?.let { NotificationHelper.openNewPipeSystemNotificationSettings(it) }
} }
setActionTextColor(Color.YELLOW) setActionTextColor(Color.YELLOW)
addCallback(object : Snackbar.Callback() { addCallback(object : Snackbar.Callback() {

View File

@ -1,5 +1,7 @@
package org.schabi.newpipe.util; package org.schabi.newpipe.util;
import static org.schabi.newpipe.util.external_communication.ShareUtils.installApp;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
@ -18,6 +20,8 @@ import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction; import androidx.fragment.app.FragmentTransaction;
import com.jakewharton.processphoenix.ProcessPhoenix;
import org.schabi.newpipe.MainActivity; import org.schabi.newpipe.MainActivity;
import org.schabi.newpipe.NewPipeDatabase; import org.schabi.newpipe.NewPipeDatabase;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
@ -57,10 +61,6 @@ import org.schabi.newpipe.util.external_communication.ShareUtils;
import java.util.ArrayList; import java.util.ArrayList;
import static org.schabi.newpipe.util.external_communication.ShareUtils.installApp;
import com.jakewharton.processphoenix.ProcessPhoenix;
public final class NavigationHelper { public final class NavigationHelper {
public static final String MAIN_FRAGMENT_TAG = "main_fragment_tag"; public static final String MAIN_FRAGMENT_TAG = "main_fragment_tag";
public static final String SEARCH_FRAGMENT_TAG = "search_fragment_tag"; public static final String SEARCH_FRAGMENT_TAG = "search_fragment_tag";