diff --git a/app/src/main/java/org/schabi/newpipe/MainActivity.java b/app/src/main/java/org/schabi/newpipe/MainActivity.java index 2e3eec6c1..bbf45e035 100644 --- a/app/src/main/java/org/schabi/newpipe/MainActivity.java +++ b/app/src/main/java/org/schabi/newpipe/MainActivity.java @@ -165,8 +165,9 @@ public class MainActivity extends AppCompatActivity { } openMiniPlayerUponPlayerStarted(); - // schedule worker for checking for new streams and creating corresponding notifications - NotificationWorker.schedule(this); + // Schedule worker for checking for new streams and creating corresponding notifications + // if this is enabled by the user. + NotificationWorker.initialize(this); } @Override 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 df1ddd5c9..48525864b 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 @@ -2,7 +2,6 @@ package org.schabi.newpipe.local.feed.notifications import android.content.Context import androidx.core.app.NotificationCompat -import androidx.preference.PreferenceManager import androidx.work.BackoffPolicy import androidx.work.Constraints import androidx.work.ExistingPeriodicWorkPolicy @@ -15,6 +14,7 @@ import androidx.work.WorkerParameters import androidx.work.rxjava3.RxWorker import io.reactivex.rxjava3.core.Observable import io.reactivex.rxjava3.core.Single +import org.schabi.newpipe.App import org.schabi.newpipe.R import org.schabi.newpipe.local.feed.service.FeedLoadManager import org.schabi.newpipe.local.feed.service.FeedLoadService @@ -51,7 +51,11 @@ class NotificationWorker( .flatMapCompletable { x -> notificationHelper.displayNewStreamsNotification(x) } .toSingleDefault(Result.success()) .onErrorReturnItem(Result.failure()) - } else Single.just(Result.success()) + } else { + // Can be the case when the user disables notifications for NewPipe + // in the device's app settings. + Single.just(Result.success()) + } private fun createForegroundInfo(): ForegroundInfo { val notification = NotificationCompat.Builder( @@ -69,16 +73,32 @@ class NotificationWorker( companion object { - private const val TAG = "streams_notifications" + private const val TAG = App.PACKAGE_NAME + "_streams_notifications" - private fun isEnabled(context: Context): Boolean { - return PreferenceManager.getDefaultSharedPreferences(context) - .getBoolean( - context.getString(R.string.enable_streams_notifications), - false - ) && NotificationHelper.areNotificationsEnabledOnDevice(context) + private fun isEnabled(context: Context) = + NotificationHelper.areNewStreamsNotificationsEnabled(context) && + NotificationHelper.areNotificationsEnabledOnDevice(context) + + /** + * Schedules a task for the [NotificationWorker] + * if the (device and in-app) notifications are enabled, + * otherwise [cancel]s all scheduled tasks. + */ + @JvmStatic + fun initialize(context: Context) { + if (isEnabled(context)) { + schedule(context) + } else { + cancel(context) + } } + /** + * @param context the context to use + * @param options configuration options for the scheduler + * @param force Force the scheduler to use the new options + * by replacing the previously used worker. + */ fun schedule(context: Context, options: ScheduleOptions, force: Boolean = false) { val constraints = Constraints.Builder() .setRequiredNetworkType( @@ -113,6 +133,9 @@ class NotificationWorker( @JvmStatic fun schedule(context: Context) = schedule(context, ScheduleOptions.from(context)) + /** + * Check for new streams immediately + */ @JvmStatic fun runNow(context: Context) { val request = OneTimeWorkRequestBuilder() @@ -120,5 +143,13 @@ class NotificationWorker( .build() WorkManager.getInstance(context).enqueue(request) } + + /** + * Cancels all current work related to the [NotificationWorker]. + */ + @JvmStatic + fun cancel(context: Context) { + WorkManager.getInstance(context).cancelAllWorkByTag(TAG) + } } } diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadManager.kt b/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadManager.kt index aa4b40f5b..dea498675 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadManager.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadManager.kt @@ -261,7 +261,7 @@ class FeedLoadManager(private val context: Context) { companion object { /** - * + * Constant used to check for updates of subscriptions with [NotificationMode.ENABLED]. */ const val GROUP_NOTIFICATION_ENABLED = -2L 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 04f5a9b56..2cea04dc3 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/NotificationsSettingsFragment.kt +++ b/app/src/main/java/org/schabi/newpipe/settings/NotificationsSettingsFragment.kt @@ -40,14 +40,29 @@ class NotificationsSettingsFragment : BasePreferenceFragment(), OnSharedPreferen override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) { val context = context ?: return - if (key == getString(R.string.streams_notifications_interval_key) || key == getString(R.string.streams_notifications_network_key)) { + if (key == getString(R.string.streams_notifications_interval_key) || + key == getString(R.string.streams_notifications_network_key) + ) { + // apply new configuration NotificationWorker.schedule(context, ScheduleOptions.from(context), true) + } else if (key == getString(R.string.enable_streams_notifications)) { + if (NotificationHelper.areNewStreamsNotificationsEnabled(context)) { + // Start the worker, because notifications were disabled previously. + NotificationWorker.schedule(context) + } else { + // The user disabled the notifications. Cancel the worker to save energy. + // A new one will be created once the notifications are enabled again. + NotificationWorker.cancel(context) + } } } override fun onResume() { super.onResume() + // Check whether the notifications are disabled in the device's app settings. + // 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 if (!enabled) {