@@ -2,7 +2,6 @@ package org.schabi.newpipe.local.feed.notifications
22
33import android.content.Context
44import androidx.core.app.NotificationCompat
5- import androidx.preference.PreferenceManager
65import androidx.work.BackoffPolicy
76import androidx.work.Constraints
87import androidx.work.ExistingPeriodicWorkPolicy
@@ -15,6 +14,7 @@ import androidx.work.WorkerParameters
1514import androidx.work.rxjava3.RxWorker
1615import io.reactivex.rxjava3.core.Observable
1716import io.reactivex.rxjava3.core.Single
17+ import org.schabi.newpipe.App
1818import org.schabi.newpipe.R
1919import org.schabi.newpipe.local.feed.service.FeedLoadManager
2020import org.schabi.newpipe.local.feed.service.FeedLoadService
@@ -51,7 +51,11 @@ class NotificationWorker(
5151 .flatMapCompletable { x -> notificationHelper.displayNewStreamsNotification(x) }
5252 .toSingleDefault(Result .success())
5353 .onErrorReturnItem(Result .failure())
54- } else Single .just(Result .success())
54+ } else {
55+ // Can be the case when the user disables notifications for NewPipe
56+ // in the device's app settings.
57+ Single .just(Result .success())
58+ }
5559
5660 private fun createForegroundInfo (): ForegroundInfo {
5761 val notification = NotificationCompat .Builder (
@@ -69,16 +73,32 @@ class NotificationWorker(
6973
7074 companion object {
7175
72- private const val TAG = " streams_notifications"
76+ private const val TAG = App .PACKAGE_NAME + " _streams_notifications"
77+
78+ private fun isEnabled (context : Context ) =
79+ NotificationHelper .areNewStreamsNotificationsEnabled(context) &&
80+ NotificationHelper .areNotificationsEnabledOnDevice(context)
7381
74- private fun isEnabled (context : Context ): Boolean {
75- return PreferenceManager .getDefaultSharedPreferences(context)
76- .getBoolean(
77- context.getString(R .string.enable_streams_notifications),
78- false
79- ) && NotificationHelper .areNotificationsEnabledOnDevice(context)
82+ /* *
83+ * Schedules a task for the [NotificationWorker]
84+ * if the (device and in-app) notifications are enabled,
85+ * otherwise [cancel]s all scheduled tasks.
86+ */
87+ @JvmStatic
88+ fun initialize (context : Context ) {
89+ if (isEnabled(context)) {
90+ schedule(context)
91+ } else {
92+ cancel(context)
93+ }
8094 }
8195
96+ /* *
97+ * @param context the context to use
98+ * @param options configuration options for the scheduler
99+ * @param force Force the scheduler to use the new options
100+ * by replacing the previously used worker.
101+ */
82102 fun schedule (context : Context , options : ScheduleOptions , force : Boolean = false) {
83103 val constraints = Constraints .Builder ()
84104 .setRequiredNetworkType(
@@ -113,12 +133,23 @@ class NotificationWorker(
113133 @JvmStatic
114134 fun schedule (context : Context ) = schedule(context, ScheduleOptions .from(context))
115135
136+ /* *
137+ * Check for new streams immediately
138+ */
116139 @JvmStatic
117140 fun runNow (context : Context ) {
118141 val request = OneTimeWorkRequestBuilder <NotificationWorker >()
119142 .addTag(TAG )
120143 .build()
121144 WorkManager .getInstance(context).enqueue(request)
122145 }
146+
147+ /* *
148+ * Cancels all current work related to the [NotificationWorker].
149+ */
150+ @JvmStatic
151+ fun cancel (context : Context ) {
152+ WorkManager .getInstance(context).cancelAllWorkByTag(TAG )
153+ }
123154 }
124155}
0 commit comments