1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-06-24 06:03:25 +00:00

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.
This commit is contained in:
TobiGr 2021-12-10 23:52:28 +01:00
parent 779d3dce6f
commit 19fd7bc37e
4 changed files with 60 additions and 13 deletions

View File

@ -165,8 +165,9 @@ public class MainActivity extends AppCompatActivity {
} }
openMiniPlayerUponPlayerStarted(); openMiniPlayerUponPlayerStarted();
// schedule worker for checking for new streams and creating corresponding notifications // Schedule worker for checking for new streams and creating corresponding notifications
NotificationWorker.schedule(this); // if this is enabled by the user.
NotificationWorker.initialize(this);
} }
@Override @Override

View File

@ -2,7 +2,6 @@ package org.schabi.newpipe.local.feed.notifications
import android.content.Context import android.content.Context
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.preference.PreferenceManager
import androidx.work.BackoffPolicy import androidx.work.BackoffPolicy
import androidx.work.Constraints import androidx.work.Constraints
import androidx.work.ExistingPeriodicWorkPolicy import androidx.work.ExistingPeriodicWorkPolicy
@ -15,6 +14,7 @@ import androidx.work.WorkerParameters
import androidx.work.rxjava3.RxWorker import androidx.work.rxjava3.RxWorker
import io.reactivex.rxjava3.core.Observable import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.core.Single import io.reactivex.rxjava3.core.Single
import org.schabi.newpipe.App
import org.schabi.newpipe.R import org.schabi.newpipe.R
import org.schabi.newpipe.local.feed.service.FeedLoadManager import org.schabi.newpipe.local.feed.service.FeedLoadManager
import org.schabi.newpipe.local.feed.service.FeedLoadService import org.schabi.newpipe.local.feed.service.FeedLoadService
@ -51,7 +51,11 @@ class NotificationWorker(
.flatMapCompletable { x -> notificationHelper.displayNewStreamsNotification(x) } .flatMapCompletable { x -> notificationHelper.displayNewStreamsNotification(x) }
.toSingleDefault(Result.success()) .toSingleDefault(Result.success())
.onErrorReturnItem(Result.failure()) .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 { private fun createForegroundInfo(): ForegroundInfo {
val notification = NotificationCompat.Builder( val notification = NotificationCompat.Builder(
@ -69,16 +73,32 @@ class NotificationWorker(
companion object { companion object {
private const val TAG = "streams_notifications" private const val TAG = App.PACKAGE_NAME + "_streams_notifications"
private fun isEnabled(context: Context): Boolean { private fun isEnabled(context: Context) =
return PreferenceManager.getDefaultSharedPreferences(context) NotificationHelper.areNewStreamsNotificationsEnabled(context) &&
.getBoolean( NotificationHelper.areNotificationsEnabledOnDevice(context)
context.getString(R.string.enable_streams_notifications),
false /**
) && 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) { fun schedule(context: Context, options: ScheduleOptions, force: Boolean = false) {
val constraints = Constraints.Builder() val constraints = Constraints.Builder()
.setRequiredNetworkType( .setRequiredNetworkType(
@ -113,6 +133,9 @@ class NotificationWorker(
@JvmStatic @JvmStatic
fun schedule(context: Context) = schedule(context, ScheduleOptions.from(context)) fun schedule(context: Context) = schedule(context, ScheduleOptions.from(context))
/**
* Check for new streams immediately
*/
@JvmStatic @JvmStatic
fun runNow(context: Context) { fun runNow(context: Context) {
val request = OneTimeWorkRequestBuilder<NotificationWorker>() val request = OneTimeWorkRequestBuilder<NotificationWorker>()
@ -120,5 +143,13 @@ class NotificationWorker(
.build() .build()
WorkManager.getInstance(context).enqueue(request) WorkManager.getInstance(context).enqueue(request)
} }
/**
* Cancels all current work related to the [NotificationWorker].
*/
@JvmStatic
fun cancel(context: Context) {
WorkManager.getInstance(context).cancelAllWorkByTag(TAG)
}
} }
} }

View File

@ -261,7 +261,7 @@ class FeedLoadManager(private val context: Context) {
companion object { companion object {
/** /**
* * Constant used to check for updates of subscriptions with [NotificationMode.ENABLED].
*/ */
const val GROUP_NOTIFICATION_ENABLED = -2L const val GROUP_NOTIFICATION_ENABLED = -2L

View File

@ -40,14 +40,29 @@ class NotificationsSettingsFragment : BasePreferenceFragment(), OnSharedPreferen
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) { override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
val context = context ?: return 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) 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() { override fun onResume() {
super.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()) val enabled = NotificationHelper.areNotificationsEnabledOnDevice(requireContext())
preferenceScreen.isEnabled = enabled preferenceScreen.isEnabled = enabled
if (!enabled) { if (!enabled) {