Commit 5467a02e authored by Joy Ming's avatar Joy Ming Committed by Commit Bot

Fix issue of not being able to swipe away paused downloads.

This is part of a larger refactor to make downloads a foreground
service on all versions of Android. There was a bug that made it so
that paused downloads were not swipeable (and therefore not
cancellable). This CL fixes that bug.

Bug: 747563
Change-Id: If67d34523e0352aaf3b8f5c78967fd1ec3a697a7
Reviewed-on: https://chromium-review.googlesource.com/661940Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Commit-Queue: Joy Ming <jming@chromium.org>
Cr-Commit-Position: refs/heads/master@{#501450}
parent 205cc831
...@@ -47,7 +47,14 @@ public class DownloadForegroundService extends Service { ...@@ -47,7 +47,14 @@ public class DownloadForegroundService extends Service {
* Stop the foreground service that is running. * Stop the foreground service that is running.
*/ */
public void stopDownloadForegroundService(boolean isCancelled) { public void stopDownloadForegroundService(boolean isCancelled) {
stopForeground(isCancelled /* kill notification if cancelled */); // If it's not cancelled, just detach the notification from the service, if possible.
if (!isCancelled && Build.VERSION.SDK_INT >= 24) {
stopForeground(STOP_FOREGROUND_DETACH);
return;
}
// Otherwise, just stop the foreground and correct it elsewhere.
stopForeground(true);
} }
@Override @Override
......
...@@ -197,28 +197,38 @@ public class DownloadForegroundServiceManager { ...@@ -197,28 +197,38 @@ public class DownloadForegroundServiceManager {
// This does not happen for API >= 24. // This does not happen for API >= 24.
if (mPinnedNotificationId != INVALID_NOTIFICATION_ID if (mPinnedNotificationId != INVALID_NOTIFICATION_ID
&& mPinnedNotificationId != notificationId && Build.VERSION.SDK_INT < 24) { && mPinnedNotificationId != notificationId && Build.VERSION.SDK_INT < 24) {
NotificationManager notificationManager = relaunchPinnedNotification();
(NotificationManager) ContextUtils.getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
notificationManager.notify(mPinnedNotificationId,
mDownloadUpdateQueue.get(mPinnedNotificationId).mNotification);
} }
mPinnedNotificationId = notificationId; mPinnedNotificationId = notificationId;
} }
} }
private void relaunchPinnedNotification() {
NotificationManager notificationManager =
(NotificationManager) ContextUtils.getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
notificationManager.notify(mPinnedNotificationId,
mDownloadUpdateQueue.get(mPinnedNotificationId).mNotification);
}
/** Helper code to stop and unbind service. */ /** Helper code to stop and unbind service. */
@VisibleForTesting @VisibleForTesting
void stopAndUnbindService(boolean isCancelled) { void stopAndUnbindService(boolean isCancelled) {
mIsServiceBound = false; mIsServiceBound = false;
mPinnedNotificationId = INVALID_NOTIFICATION_ID;
if (mBoundService != null) { if (mBoundService != null) {
stopAndUnbindServiceInternal(isCancelled); stopAndUnbindServiceInternal(isCancelled);
mBoundService = null; mBoundService = null;
} }
// If the download isn't cancelled, need to relaunch the notification so it is no longer
// pinned to the foreground service.
if (!isCancelled && Build.VERSION.SDK_INT < 24) {
relaunchPinnedNotification();
}
mPinnedNotificationId = INVALID_NOTIFICATION_ID;
} }
@VisibleForTesting @VisibleForTesting
......
...@@ -320,16 +320,16 @@ public class DownloadNotificationService2 { ...@@ -320,16 +320,16 @@ public class DownloadNotificationService2 {
Notification notification = DownloadNotificationFactory.buildNotification( Notification notification = DownloadNotificationFactory.buildNotification(
context, DownloadNotificationFactory.DownloadStatus.PAUSED, downloadUpdate); context, DownloadNotificationFactory.DownloadStatus.PAUSED, downloadUpdate);
updateNotification(notificationId, notification, id,
new DownloadSharedPreferenceEntry(id, notificationId, isOffTheRecord,
canDownloadWhileMetered, fileName, isAutoResumable, isTransient));
// If called from DownloadBroadcastManager, only update notification, not tracking. // If called from DownloadBroadcastManager, only update notification, not tracking.
if (hasUserGesture) { if (hasUserGesture) {
updateNotification(notificationId, notification); updateNotification(notificationId, notification);
return; return;
} }
updateNotification(notificationId, notification, id,
new DownloadSharedPreferenceEntry(id, notificationId, isOffTheRecord,
canDownloadWhileMetered, fileName, isAutoResumable, isTransient));
mDownloadForegroundServiceManager.updateDownloadStatus(context, mDownloadForegroundServiceManager.updateDownloadStatus(context,
DownloadForegroundServiceManager.DownloadStatus.PAUSE, notificationId, DownloadForegroundServiceManager.DownloadStatus.PAUSE, notificationId,
notification); notification);
......
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 to comment