mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
Add DownloadService.invalidateForegroundNotification
ISSUE: #4563 PiperOrigin-RevId: 243616444
This commit is contained in:
parent
18dd3fdb46
commit
ba91501751
1 changed files with 32 additions and 17 deletions
|
|
@ -159,6 +159,7 @@ public abstract class DownloadService extends Service {
|
||||||
private int lastStartId;
|
private int lastStartId;
|
||||||
private boolean startedInForeground;
|
private boolean startedInForeground;
|
||||||
private boolean taskRemoved;
|
private boolean taskRemoved;
|
||||||
|
private boolean isDestroyed;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a DownloadService.
|
* Creates a DownloadService.
|
||||||
|
|
@ -468,6 +469,7 @@ public abstract class DownloadService extends Service {
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
logd("onDestroy");
|
logd("onDestroy");
|
||||||
|
isDestroyed = true;
|
||||||
DownloadManagerHelper downloadManagerHelper = downloadManagerListeners.get(getClass());
|
DownloadManagerHelper downloadManagerHelper = downloadManagerListeners.get(getClass());
|
||||||
boolean unschedule = !downloadManager.isWaitingForRequirements();
|
boolean unschedule = !downloadManager.isWaitingForRequirements();
|
||||||
downloadManagerHelper.detachService(this, unschedule);
|
downloadManagerHelper.detachService(this, unschedule);
|
||||||
|
|
@ -517,6 +519,16 @@ public abstract class DownloadService extends Service {
|
||||||
*/
|
*/
|
||||||
protected abstract Notification getForegroundNotification(Download[] downloads);
|
protected abstract Notification getForegroundNotification(Download[] downloads);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invalidates the current foreground notification and causes {@link
|
||||||
|
* #getForegroundNotification(Download[])} to be invoked again if the service isn't stopped.
|
||||||
|
*/
|
||||||
|
protected final void invalidateForegroundNotification() {
|
||||||
|
if (foregroundNotificationUpdater != null && !isDestroyed) {
|
||||||
|
foregroundNotificationUpdater.invalidate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when the state of a download changes. The default implementation is a no-op.
|
* Called when the state of a download changes. The default implementation is a no-op.
|
||||||
*
|
*
|
||||||
|
|
@ -543,7 +555,7 @@ public abstract class DownloadService extends Service {
|
||||||
|| download.state == Download.STATE_RESTARTING) {
|
|| download.state == Download.STATE_RESTARTING) {
|
||||||
foregroundNotificationUpdater.startPeriodicUpdates();
|
foregroundNotificationUpdater.startPeriodicUpdates();
|
||||||
} else {
|
} else {
|
||||||
foregroundNotificationUpdater.update();
|
foregroundNotificationUpdater.invalidate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -551,7 +563,7 @@ public abstract class DownloadService extends Service {
|
||||||
private void notifyDownloadRemoved(Download download) {
|
private void notifyDownloadRemoved(Download download) {
|
||||||
onDownloadRemoved(download);
|
onDownloadRemoved(download);
|
||||||
if (foregroundNotificationUpdater != null) {
|
if (foregroundNotificationUpdater != null) {
|
||||||
foregroundNotificationUpdater.update();
|
foregroundNotificationUpdater.invalidate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -583,11 +595,12 @@ public abstract class DownloadService extends Service {
|
||||||
return new Intent(context, clazz).setAction(action);
|
return new Intent(context, clazz).setAction(action);
|
||||||
}
|
}
|
||||||
|
|
||||||
private final class ForegroundNotificationUpdater implements Runnable {
|
private final class ForegroundNotificationUpdater {
|
||||||
|
|
||||||
private final int notificationId;
|
private final int notificationId;
|
||||||
private final long updateInterval;
|
private final long updateInterval;
|
||||||
private final Handler handler;
|
private final Handler handler;
|
||||||
|
private final Runnable callback;
|
||||||
|
|
||||||
private boolean periodicUpdatesStarted;
|
private boolean periodicUpdatesStarted;
|
||||||
private boolean notificationDisplayed;
|
private boolean notificationDisplayed;
|
||||||
|
|
@ -596,6 +609,7 @@ public abstract class DownloadService extends Service {
|
||||||
this.notificationId = notificationId;
|
this.notificationId = notificationId;
|
||||||
this.updateInterval = updateInterval;
|
this.updateInterval = updateInterval;
|
||||||
this.handler = new Handler(Looper.getMainLooper());
|
this.handler = new Handler(Looper.getMainLooper());
|
||||||
|
this.callback = this::update;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startPeriodicUpdates() {
|
public void startPeriodicUpdates() {
|
||||||
|
|
@ -605,17 +619,7 @@ public abstract class DownloadService extends Service {
|
||||||
|
|
||||||
public void stopPeriodicUpdates() {
|
public void stopPeriodicUpdates() {
|
||||||
periodicUpdatesStarted = false;
|
periodicUpdatesStarted = false;
|
||||||
handler.removeCallbacks(this);
|
handler.removeCallbacks(callback);
|
||||||
}
|
|
||||||
|
|
||||||
public void update() {
|
|
||||||
Download[] downloads = downloadManager.getCurrentDownloads();
|
|
||||||
startForeground(notificationId, getForegroundNotification(downloads));
|
|
||||||
notificationDisplayed = true;
|
|
||||||
if (periodicUpdatesStarted) {
|
|
||||||
handler.removeCallbacks(this);
|
|
||||||
handler.postDelayed(this, updateInterval);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showNotificationIfNotAlready() {
|
public void showNotificationIfNotAlready() {
|
||||||
|
|
@ -624,9 +628,20 @@ public abstract class DownloadService extends Service {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public void invalidate() {
|
||||||
public void run() {
|
if (notificationDisplayed) {
|
||||||
update();
|
update();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void update() {
|
||||||
|
Download[] downloads = downloadManager.getCurrentDownloads();
|
||||||
|
startForeground(notificationId, getForegroundNotification(downloads));
|
||||||
|
notificationDisplayed = true;
|
||||||
|
if (periodicUpdatesStarted) {
|
||||||
|
handler.removeCallbacks(callback);
|
||||||
|
handler.postDelayed(callback, updateInterval);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue