Commit 4ff4263a by olly Committed by Oliver Woodman

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
parent 6edf9c31
...@@ -26,6 +26,7 @@ import com.google.android.exoplayer2.offline.DownloadManager; ...@@ -26,6 +26,7 @@ import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.offline.DownloadService; import com.google.android.exoplayer2.offline.DownloadService;
import com.google.android.exoplayer2.scheduler.PlatformScheduler; import com.google.android.exoplayer2.scheduler.PlatformScheduler;
import com.google.android.exoplayer2.scheduler.Requirements; 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.ui.DownloadNotificationHelper;
import com.google.android.exoplayer2.util.NotificationUtil; import com.google.android.exoplayer2.util.NotificationUtil;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
...@@ -61,7 +62,7 @@ public class DemoDownloadService extends DownloadService { ...@@ -61,7 +62,7 @@ public class DemoDownloadService extends DownloadService {
} }
@Override @Override
protected PlatformScheduler getScheduler() { protected Scheduler getScheduler() {
return Util.SDK_INT >= 21 ? new PlatformScheduler(this, JOB_ID) : null; return Util.SDK_INT >= 21 ? new PlatformScheduler(this, JOB_ID) : null;
} }
......
...@@ -18,6 +18,7 @@ package com.google.android.exoplayer2.offline; ...@@ -18,6 +18,7 @@ package com.google.android.exoplayer2.offline;
import static com.google.android.exoplayer2.offline.Download.STOP_REASON_NONE; import static com.google.android.exoplayer2.offline.Download.STOP_REASON_NONE;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service; import android.app.Service;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
...@@ -889,8 +890,16 @@ public abstract class DownloadService extends Service { ...@@ -889,8 +890,16 @@ public abstract class DownloadService extends Service {
List<Download> downloads = Assertions.checkNotNull(downloadManager).getCurrentDownloads(); List<Download> downloads = Assertions.checkNotNull(downloadManager).getCurrentDownloads();
@Requirements.RequirementFlags @Requirements.RequirementFlags
int notMetRequirements = downloadManager.getNotMetRequirements(); int notMetRequirements = downloadManager.getNotMetRequirements();
startForeground(notificationId, getForegroundNotification(downloads, notMetRequirements)); Notification notification = getForegroundNotification(downloads, notMetRequirements);
if (!notificationDisplayed) {
startForeground(notificationId, notification);
notificationDisplayed = true; 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) { if (periodicUpdatesStarted) {
handler.removeCallbacksAndMessages(null); handler.removeCallbacksAndMessages(null);
handler.postDelayed(this::update, updateInterval); handler.postDelayed(this::update, updateInterval);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment