Skip to content

Commit 19fd7bc

Browse files
committed
Reduce power consumption
Only schedule the chek for new streams if the user enaled the check. Cancel the worker when the user disables the notifications.
1 parent 779d3dc commit 19fd7bc

4 files changed

Lines changed: 60 additions & 13 deletions

File tree

app/src/main/java/org/schabi/newpipe/MainActivity.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,9 @@ protected void onCreate(final Bundle savedInstanceState) {
165165
}
166166
openMiniPlayerUponPlayerStarted();
167167

168-
// schedule worker for checking for new streams and creating corresponding notifications
169-
NotificationWorker.schedule(this);
168+
// Schedule worker for checking for new streams and creating corresponding notifications
169+
// if this is enabled by the user.
170+
NotificationWorker.initialize(this);
170171
}
171172

172173
@Override

app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationWorker.kt

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package org.schabi.newpipe.local.feed.notifications
22

33
import android.content.Context
44
import androidx.core.app.NotificationCompat
5-
import androidx.preference.PreferenceManager
65
import androidx.work.BackoffPolicy
76
import androidx.work.Constraints
87
import androidx.work.ExistingPeriodicWorkPolicy
@@ -15,6 +14,7 @@ import androidx.work.WorkerParameters
1514
import androidx.work.rxjava3.RxWorker
1615
import io.reactivex.rxjava3.core.Observable
1716
import io.reactivex.rxjava3.core.Single
17+
import org.schabi.newpipe.App
1818
import org.schabi.newpipe.R
1919
import org.schabi.newpipe.local.feed.service.FeedLoadManager
2020
import 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
}

app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ class FeedLoadManager(private val context: Context) {
261261
companion object {
262262

263263
/**
264-
*
264+
* Constant used to check for updates of subscriptions with [NotificationMode.ENABLED].
265265
*/
266266
const val GROUP_NOTIFICATION_ENABLED = -2L
267267

app/src/main/java/org/schabi/newpipe/settings/NotificationsSettingsFragment.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,29 @@ class NotificationsSettingsFragment : BasePreferenceFragment(), OnSharedPreferen
4040

4141
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
4242
val context = context ?: return
43-
if (key == getString(R.string.streams_notifications_interval_key) || key == getString(R.string.streams_notifications_network_key)) {
43+
if (key == getString(R.string.streams_notifications_interval_key) ||
44+
key == getString(R.string.streams_notifications_network_key)
45+
) {
46+
// apply new configuration
4447
NotificationWorker.schedule(context, ScheduleOptions.from(context), true)
48+
} else if (key == getString(R.string.enable_streams_notifications)) {
49+
if (NotificationHelper.areNewStreamsNotificationsEnabled(context)) {
50+
// Start the worker, because notifications were disabled previously.
51+
NotificationWorker.schedule(context)
52+
} else {
53+
// The user disabled the notifications. Cancel the worker to save energy.
54+
// A new one will be created once the notifications are enabled again.
55+
NotificationWorker.cancel(context)
56+
}
4557
}
4658
}
4759

4860
override fun onResume() {
4961
super.onResume()
5062

63+
// Check whether the notifications are disabled in the device's app settings.
64+
// If they are disabled, show a snackbar informing the user about that
65+
// while allowing them to open the device's app settings.
5166
val enabled = NotificationHelper.areNotificationsEnabledOnDevice(requireContext())
5267
preferenceScreen.isEnabled = enabled
5368
if (!enabled) {

0 commit comments

Comments
 (0)