| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.download; | 5 package org.chromium.chrome.browser.download; |
| 6 | 6 |
| 7 import android.app.Activity; | 7 import android.app.Activity; |
| 8 import android.app.NotificationManager; |
| 8 import android.content.Context; | 9 import android.content.Context; |
| 9 import android.util.Pair; | |
| 10 | 10 |
| 11 import org.chromium.base.ApplicationStatus; | 11 import org.chromium.base.ApplicationStatus; |
| 12 import org.chromium.chrome.R; | 12 import org.chromium.chrome.R; |
| 13 import org.chromium.chrome.browser.snackbar.Snackbar; | 13 import org.chromium.chrome.browser.snackbar.Snackbar; |
| 14 import org.chromium.chrome.browser.snackbar.SnackbarManager; | 14 import org.chromium.chrome.browser.snackbar.SnackbarManager; |
| 15 import org.chromium.content.browser.DownloadInfo; | 15 import org.chromium.content.browser.DownloadInfo; |
| 16 | 16 |
| 17 /** | 17 /** |
| 18 * Class for displaying a snackbar when a download completes. | 18 * Class for displaying a snackbar when a download completes. |
| 19 */ | 19 */ |
| 20 public class DownloadSnackbarController implements SnackbarManager.SnackbarContr
oller { | 20 public class DownloadSnackbarController implements SnackbarManager.SnackbarContr
oller { |
| 21 public static final int INVALID_NOTIFICATION_ID = -1; |
| 21 private static final int SNACKBAR_DURATION_IN_MILLISECONDS = 5000; | 22 private static final int SNACKBAR_DURATION_IN_MILLISECONDS = 5000; |
| 22 private final Context mContext; | 23 private final Context mContext; |
| 23 | 24 |
| 25 private static class ActionDataInfo { |
| 26 public final DownloadInfo downloadInfo; |
| 27 public final int notificationId; |
| 28 public final long systemDownloadId; |
| 29 |
| 30 ActionDataInfo(DownloadInfo downloadInfo, int notificationId, long syste
mDownloadId) { |
| 31 this.downloadInfo = downloadInfo; |
| 32 this.notificationId = notificationId; |
| 33 this.systemDownloadId = systemDownloadId; |
| 34 } |
| 35 } |
| 36 |
| 24 public DownloadSnackbarController(Context context) { | 37 public DownloadSnackbarController(Context context) { |
| 25 mContext = context; | 38 mContext = context; |
| 26 } | 39 } |
| 27 | 40 |
| 28 @Override | 41 @Override |
| 29 @SuppressWarnings("unchecked") | 42 @SuppressWarnings("unchecked") |
| 30 public void onAction(Object actionData) { | 43 public void onAction(Object actionData) { |
| 31 if (actionData == null) { | 44 if (actionData == null) { |
| 32 DownloadManagerService.openDownloadsPage(mContext); | 45 DownloadManagerService.openDownloadsPage(mContext); |
| 33 return; | 46 return; |
| 34 } | 47 } |
| 35 Pair<DownloadInfo, Long> download = (Pair<DownloadInfo, Long>) actionDat
a; | 48 ActionDataInfo download = (ActionDataInfo) actionData; |
| 36 DownloadManagerService manager = DownloadManagerService.getDownloadManag
erService(mContext); | 49 DownloadManagerService manager = DownloadManagerService.getDownloadManag
erService(mContext); |
| 37 manager.openDownloadedContent(download.second); | 50 manager.openDownloadedContent(download.systemDownloadId); |
| 38 manager.cancelNotification( | 51 if (download.notificationId != INVALID_NOTIFICATION_ID) { |
| 39 download.first.getNotificationId(), download.first.getDownloadGu
id()); | 52 NotificationManager notificationManager = |
| 53 (NotificationManager) mContext.getSystemService(Context.NOTI
FICATION_SERVICE); |
| 54 notificationManager.cancel( |
| 55 DownloadNotificationService.NOTIFICATION_NAMESPACE, download
.notificationId); |
| 56 } |
| 40 } | 57 } |
| 41 | 58 |
| 42 @Override | 59 @Override |
| 43 public void onDismissNoAction(Object actionData) { | 60 public void onDismissNoAction(Object actionData) { |
| 44 } | 61 } |
| 45 | 62 |
| 46 /** | 63 /** |
| 47 * Called to display the download succeeded snackbar. | 64 * Called to display the download succeeded snackbar. |
| 48 * | 65 * |
| 49 * @param downloadInfo Info of the download. | 66 * @param downloadInfo Info of the download. |
| 67 * @param notificationId Notification Id of the successful download. |
| 50 * @param downloadId Id of the download from Android DownloadManager. | 68 * @param downloadId Id of the download from Android DownloadManager. |
| 51 * @param canBeResolved Whether the download can be resolved to any activity
. | 69 * @param canBeResolved Whether the download can be resolved to any activity
. |
| 52 */ | 70 */ |
| 53 public void onDownloadSucceeded( | 71 public void onDownloadSucceeded( |
| 54 DownloadInfo downloadInfo, final long downloadId, boolean canBeResol
ved) { | 72 DownloadInfo downloadInfo, int notificationId, long downloadId, bool
ean canBeResolved) { |
| 55 if (getSnackbarManager() == null) return; | 73 if (getSnackbarManager() == null) return; |
| 56 Snackbar snackbar = Snackbar.make( | 74 Snackbar snackbar = Snackbar.make( |
| 57 mContext.getString(R.string.download_succeeded_message, download
Info.getFileName()), | 75 mContext.getString(R.string.download_succeeded_message, download
Info.getFileName()), |
| 58 this, Snackbar.TYPE_NOTIFICATION); | 76 this, Snackbar.TYPE_NOTIFICATION); |
| 59 // TODO(qinmin): Coalesce snackbars if multiple downloads finish at the
same time. | 77 // TODO(qinmin): Coalesce snackbars if multiple downloads finish at the
same time. |
| 60 snackbar.setDuration(SNACKBAR_DURATION_IN_MILLISECONDS).setSingleLine(fa
lse); | 78 snackbar.setDuration(SNACKBAR_DURATION_IN_MILLISECONDS).setSingleLine(fa
lse); |
| 61 Pair<DownloadInfo, Long> actionData = null; | 79 ActionDataInfo info = null; |
| 62 if (canBeResolved) { | 80 if (canBeResolved) { |
| 63 actionData = Pair.create(downloadInfo, downloadId); | 81 info = new ActionDataInfo(downloadInfo, notificationId, downloadId); |
| 64 } | 82 } |
| 65 // Show downloads app if the download cannot be resolved to any activity
. | 83 // Show downloads app if the download cannot be resolved to any activity
. |
| 66 snackbar.setAction( | 84 snackbar.setAction( |
| 67 mContext.getString(R.string.open_downloaded_label), actionData); | 85 mContext.getString(R.string.open_downloaded_label), info); |
| 68 getSnackbarManager().showSnackbar(snackbar); | 86 getSnackbarManager().showSnackbar(snackbar); |
| 69 } | 87 } |
| 70 | 88 |
| 71 /** | 89 /** |
| 72 * Called to display the download failed snackbar. | 90 * Called to display the download failed snackbar. |
| 73 * | 91 * |
| 74 * @param filename File name of the failed download. | 92 * @param filename File name of the failed download. |
| 75 * @param whether to show all downloads in case the failure is caused by dup
licated files. | 93 * @param whether to show all downloads in case the failure is caused by dup
licated files. |
| 76 */ | 94 */ |
| 77 public void onDownloadFailed(String errorMessage, boolean showAllDownloads)
{ | 95 public void onDownloadFailed(String errorMessage, boolean showAllDownloads)
{ |
| (...skipping 12 matching lines...) Expand all Loading... |
| 90 | 108 |
| 91 public SnackbarManager getSnackbarManager() { | 109 public SnackbarManager getSnackbarManager() { |
| 92 Activity activity = ApplicationStatus.getLastTrackedFocusedActivity(); | 110 Activity activity = ApplicationStatus.getLastTrackedFocusedActivity(); |
| 93 if (activity != null && ApplicationStatus.hasVisibleActivities() | 111 if (activity != null && ApplicationStatus.hasVisibleActivities() |
| 94 && activity instanceof SnackbarManager.SnackbarManageable) { | 112 && activity instanceof SnackbarManager.SnackbarManageable) { |
| 95 return ((SnackbarManager.SnackbarManageable) activity).getSnackbarMa
nager(); | 113 return ((SnackbarManager.SnackbarManageable) activity).getSnackbarMa
nager(); |
| 96 } | 114 } |
| 97 return null; | 115 return null; |
| 98 } | 116 } |
| 99 } | 117 } |
| OLD | NEW |