From 4ff4263af321821cf94ac92fc731ac09299e2e87 Mon Sep 17 00:00:00 2001 From: olly Date: Thu, 16 Sep 2021 23:10:59 +0100 Subject: [PATCH] DownloadService: Minor improvements * Avoid ActivityManager log spam by only calling startForeground once, and subsequently updating the notification via NotificationManager. * Tweak demo app service to make it a tiny bit easier to swap the Scheduler. PiperOrigin-RevId: 397179398 --- .../exoplayer2/demo/DemoDownloadService.java | 3 ++- .../android/exoplayer2/offline/DownloadService.java | 13 +++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoDownloadService.java b/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoDownloadService.java index 87d45148fe..e32f72f39f 100644 --- a/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoDownloadService.java +++ b/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoDownloadService.java @@ -26,6 +26,7 @@ import com.google.android.exoplayer2.offline.DownloadManager; import com.google.android.exoplayer2.offline.DownloadService; import com.google.android.exoplayer2.scheduler.PlatformScheduler; import com.google.android.exoplayer2.scheduler.Requirements; +import com.google.android.exoplayer2.scheduler.Scheduler; import com.google.android.exoplayer2.ui.DownloadNotificationHelper; import com.google.android.exoplayer2.util.NotificationUtil; import com.google.android.exoplayer2.util.Util; @@ -61,7 +62,7 @@ public class DemoDownloadService extends DownloadService { } @Override - protected PlatformScheduler getScheduler() { + protected Scheduler getScheduler() { return Util.SDK_INT >= 21 ? new PlatformScheduler(this, JOB_ID) : null; } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java index 0bccbb31b3..f0f22af1ac 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java @@ -18,6 +18,7 @@ package com.google.android.exoplayer2.offline; import static com.google.android.exoplayer2.offline.Download.STOP_REASON_NONE; import android.app.Notification; +import android.app.NotificationManager; import android.app.Service; import android.content.Context; import android.content.Intent; @@ -889,8 +890,16 @@ public abstract class DownloadService extends Service { List downloads = Assertions.checkNotNull(downloadManager).getCurrentDownloads(); @Requirements.RequirementFlags int notMetRequirements = downloadManager.getNotMetRequirements(); - startForeground(notificationId, getForegroundNotification(downloads, notMetRequirements)); - notificationDisplayed = true; + Notification notification = getForegroundNotification(downloads, notMetRequirements); + if (!notificationDisplayed) { + startForeground(notificationId, notification); + notificationDisplayed = true; + } else { + // Update the notification via NotificationManager rather than by repeatedly calling + // startForeground, since the latter can cause ActivityManager log spam. + ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)) + .notify(notificationId, notification); + } if (periodicUpdatesStarted) { handler.removeCallbacksAndMessages(null); handler.postDelayed(this::update, updateInterval);