| 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.DownloadManager; |
| 8 import android.content.Context; | 9 import android.content.Context; |
| 10 import android.content.Intent; |
| 9 | 11 |
| 10 import org.chromium.base.ApplicationStatus; | 12 import org.chromium.base.ApplicationStatus; |
| 11 import org.chromium.base.BuildInfo; | 13 import org.chromium.base.BuildInfo; |
| 12 import org.chromium.chrome.R; | 14 import org.chromium.chrome.R; |
| 13 import org.chromium.chrome.browser.customtabs.CustomTabActivity; | 15 import org.chromium.chrome.browser.customtabs.CustomTabActivity; |
| 14 import org.chromium.chrome.browser.offlinepages.downloads.OfflinePageDownloadBri
dge; | 16 import org.chromium.chrome.browser.offlinepages.downloads.OfflinePageDownloadBri
dge; |
| 15 import org.chromium.chrome.browser.snackbar.Snackbar; | 17 import org.chromium.chrome.browser.snackbar.Snackbar; |
| 16 import org.chromium.chrome.browser.snackbar.SnackbarManager; | 18 import org.chromium.chrome.browser.snackbar.SnackbarManager; |
| 17 | 19 |
| 18 /** | 20 /** |
| 19 * Class for displaying a snackbar when a download completes. | 21 * Class for displaying a snackbar when a download completes. |
| 20 */ | 22 */ |
| 21 public class DownloadSnackbarController implements SnackbarManager.SnackbarContr
oller { | 23 public class DownloadSnackbarController implements SnackbarManager.SnackbarContr
oller { |
| 22 public static final int INVALID_NOTIFICATION_ID = -1; | 24 public static final int INVALID_NOTIFICATION_ID = -1; |
| 23 private static final int SNACKBAR_DURATION_IN_MILLISECONDS = 5000; | 25 private static final int SNACKBAR_DURATION_IN_MILLISECONDS = 5000; |
| 24 private final Context mContext; | 26 private final Context mContext; |
| 25 | 27 |
| 26 private static class ActionDataInfo { | 28 private static class ActionDataInfo { |
| 27 public final DownloadInfo downloadInfo; | 29 public final DownloadInfo downloadInfo; |
| 28 public final int notificationId; | 30 public final int notificationId; |
| 29 public final long systemDownloadId; | 31 public final long systemDownloadId; |
| 32 public final boolean usesAndroidDownloadManager; |
| 30 | 33 |
| 31 ActionDataInfo(DownloadInfo downloadInfo, int notificationId, long syste
mDownloadId) { | 34 ActionDataInfo(DownloadInfo downloadInfo, int notificationId, long syste
mDownloadId, |
| 35 boolean usesAndroidDownloadManager) { |
| 32 this.downloadInfo = downloadInfo; | 36 this.downloadInfo = downloadInfo; |
| 33 this.notificationId = notificationId; | 37 this.notificationId = notificationId; |
| 34 this.systemDownloadId = systemDownloadId; | 38 this.systemDownloadId = systemDownloadId; |
| 39 this.usesAndroidDownloadManager = usesAndroidDownloadManager; |
| 35 } | 40 } |
| 36 } | 41 } |
| 37 | 42 |
| 38 public DownloadSnackbarController(Context context) { | 43 public DownloadSnackbarController(Context context) { |
| 39 mContext = context; | 44 mContext = context; |
| 40 } | 45 } |
| 41 | 46 |
| 42 @Override | 47 @Override |
| 43 public void onAction(Object actionData) { | 48 public void onAction(Object actionData) { |
| 44 if (!(actionData instanceof ActionDataInfo)) { | 49 if (!(actionData instanceof ActionDataInfo)) { |
| 45 DownloadManagerService.openDownloadsPage(mContext); | 50 DownloadManagerService.openDownloadsPage(mContext); |
| 46 return; | 51 return; |
| 47 } | 52 } |
| 48 final ActionDataInfo download = (ActionDataInfo) actionData; | 53 final ActionDataInfo download = (ActionDataInfo) actionData; |
| 49 if (download.downloadInfo.isOfflinePage()) { | 54 if (download.downloadInfo.isOfflinePage()) { |
| 50 OfflinePageDownloadBridge.openDownloadedPage(download.downloadInfo.g
etDownloadGuid()); | 55 OfflinePageDownloadBridge.openDownloadedPage(download.downloadInfo.g
etDownloadGuid()); |
| 51 return; | 56 return; |
| 52 } | 57 } |
| 58 if (download.usesAndroidDownloadManager) { |
| 59 mContext.startActivity(new Intent(DownloadManager.ACTION_VIEW_DOWNLO
ADS).addFlags( |
| 60 Intent.FLAG_ACTIVITY_NEW_TASK)); |
| 61 return; |
| 62 } |
| 53 DownloadManagerService manager = DownloadManagerService.getDownloadManag
erService(mContext); | 63 DownloadManagerService manager = DownloadManagerService.getDownloadManag
erService(mContext); |
| 54 manager.openDownloadedContent(download.downloadInfo, download.systemDown
loadId); | 64 manager.openDownloadedContent(download.downloadInfo, download.systemDown
loadId); |
| 55 if (download.notificationId != INVALID_NOTIFICATION_ID) { | 65 if (download.notificationId != INVALID_NOTIFICATION_ID) { |
| 56 manager.getDownloadNotifier().removeDownloadNotification( | 66 manager.getDownloadNotifier().removeDownloadNotification( |
| 57 download.notificationId, download.downloadInfo); | 67 download.notificationId, download.downloadInfo); |
| 58 } | 68 } |
| 59 } | 69 } |
| 60 | 70 |
| 61 @Override | 71 @Override |
| 62 public void onDismissNoAction(Object actionData) { | 72 public void onDismissNoAction(Object actionData) { |
| 63 } | 73 } |
| 64 | 74 |
| 65 /** | 75 /** |
| 66 * Called to display the download succeeded snackbar. | 76 * Called to display the download succeeded snackbar. |
| 67 * | 77 * |
| 68 * @param downloadInfo Info of the download. | 78 * @param downloadInfo Info of the download. |
| 69 * @param notificationId Notification Id of the successful download. | 79 * @param notificationId Notification Id of the successful download. |
| 70 * @param downloadId Id of the download from Android DownloadManager. | 80 * @param downloadId Id of the download from Android DownloadManager. |
| 71 * @param canBeResolved Whether the download can be resolved to any activity
. | 81 * @param canBeResolved Whether the download can be resolved to any activity
. |
| 82 * @param usesAndroidDownloadManager Whether the download uses Android Downl
oadManager. |
| 72 */ | 83 */ |
| 73 public void onDownloadSucceeded( | 84 public void onDownloadSucceeded( |
| 74 DownloadInfo downloadInfo, int notificationId, long downloadId, bool
ean canBeResolved) { | 85 DownloadInfo downloadInfo, int notificationId, long downloadId, bool
ean canBeResolved, |
| 86 boolean usesAndroidDownloadManager) { |
| 75 if (getSnackbarManager() == null) return; | 87 if (getSnackbarManager() == null) return; |
| 76 Snackbar snackbar; | 88 Snackbar snackbar; |
| 77 if (getActivity() instanceof CustomTabActivity) { | 89 if (getActivity() instanceof CustomTabActivity) { |
| 78 String packageLabel = BuildInfo.getPackageLabel(getActivity()); | 90 String packageLabel = BuildInfo.getPackageLabel(getActivity()); |
| 79 snackbar = Snackbar.make(mContext.getString(R.string.download_succee
ded_message, | 91 snackbar = Snackbar.make(mContext.getString(R.string.download_succee
ded_message, |
| 80 downloadInfo.getFileName(), packageLabel), | 92 downloadInfo.getFileName(), packageLabel), |
| 81 this, Snackbar.TYPE_NOTIFICATION, Snackbar.UMA_DOWNLOAD_SUCC
EEDED); | 93 this, Snackbar.TYPE_NOTIFICATION, Snackbar.UMA_DOWNLOAD_SUCC
EEDED); |
| 82 } else { | 94 } else { |
| 83 snackbar = Snackbar.make(mContext.getString(R.string.download_succee
ded_message_default, | 95 snackbar = Snackbar.make(mContext.getString(R.string.download_succee
ded_message_default, |
| 84 downloadInfo.getFileName()), | 96 downloadInfo.getFileName()), |
| 85 this, Snackbar.TYPE_NOTIFICATION, Snackbar.UMA_DOWNLOAD_SUCC
EEDED); | 97 this, Snackbar.TYPE_NOTIFICATION, Snackbar.UMA_DOWNLOAD_SUCC
EEDED); |
| 86 } | 98 } |
| 87 // TODO(qinmin): Coalesce snackbars if multiple downloads finish at the
same time. | 99 // TODO(qinmin): Coalesce snackbars if multiple downloads finish at the
same time. |
| 88 snackbar.setDuration(SNACKBAR_DURATION_IN_MILLISECONDS).setSingleLine(fa
lse); | 100 snackbar.setDuration(SNACKBAR_DURATION_IN_MILLISECONDS).setSingleLine(fa
lse); |
| 89 ActionDataInfo info = null; | 101 ActionDataInfo info = null; |
| 90 if (canBeResolved || downloadInfo.isOfflinePage()) { | 102 if (canBeResolved || downloadInfo.isOfflinePage() || usesAndroidDownload
Manager) { |
| 91 info = new ActionDataInfo(downloadInfo, notificationId, downloadId); | 103 info = new ActionDataInfo(downloadInfo, notificationId, downloadId, |
| 104 usesAndroidDownloadManager); |
| 92 } | 105 } |
| 93 // Show downloads app if the download cannot be resolved to any activity
. | 106 // Show downloads app if the download cannot be resolved to any activity
. |
| 94 snackbar.setAction( | 107 snackbar.setAction( |
| 95 mContext.getString(R.string.open_downloaded_label), info); | 108 mContext.getString(R.string.open_downloaded_label), info); |
| 96 getSnackbarManager().showSnackbar(snackbar); | 109 getSnackbarManager().showSnackbar(snackbar); |
| 97 } | 110 } |
| 98 | 111 |
| 99 /** | 112 /** |
| 100 * Called to display the download failed snackbar. | 113 * Called to display the download failed snackbar. |
| 101 * | 114 * |
| (...skipping 25 matching lines...) Expand all Loading... |
| 127 } | 140 } |
| 128 | 141 |
| 129 public SnackbarManager getSnackbarManager() { | 142 public SnackbarManager getSnackbarManager() { |
| 130 Activity activity = getActivity(); | 143 Activity activity = getActivity(); |
| 131 if (activity != null && activity instanceof SnackbarManager.SnackbarMana
geable) { | 144 if (activity != null && activity instanceof SnackbarManager.SnackbarMana
geable) { |
| 132 return ((SnackbarManager.SnackbarManageable) activity).getSnackbarMa
nager(); | 145 return ((SnackbarManager.SnackbarManageable) activity).getSnackbarMa
nager(); |
| 133 } | 146 } |
| 134 return null; | 147 return null; |
| 135 } | 148 } |
| 136 } | 149 } |
| OLD | NEW |