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 {
* Stop the foreground service that is running.
*/
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
......
......@@ -197,28 +197,38 @@ public class DownloadForegroundServiceManager {
// This does not happen for API >= 24.
if (mPinnedNotificationId != INVALID_NOTIFICATION_ID
&& mPinnedNotificationId != notificationId && Build.VERSION.SDK_INT < 24) {
NotificationManager notificationManager =
(NotificationManager) ContextUtils.getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
notificationManager.notify(mPinnedNotificationId,
mDownloadUpdateQueue.get(mPinnedNotificationId).mNotification);
relaunchPinnedNotification();
}
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. */
@VisibleForTesting
void stopAndUnbindService(boolean isCancelled) {
mIsServiceBound = false;
mPinnedNotificationId = INVALID_NOTIFICATION_ID;
if (mBoundService != null) {
stopAndUnbindServiceInternal(isCancelled);
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
......
......@@ -320,16 +320,16 @@ public class DownloadNotificationService2 {
Notification notification = DownloadNotificationFactory.buildNotification(
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 (hasUserGesture) {
updateNotification(notificationId, notification);
return;
}
updateNotification(notificationId, notification, id,
new DownloadSharedPreferenceEntry(id, notificationId, isOffTheRecord,
canDownloadWhileMetered, fileName, isAutoResumable, isTransient));
mDownloadForegroundServiceManager.updateDownloadStatus(context,
DownloadForegroundServiceManager.DownloadStatus.PAUSE, notificationId,
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