Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(544)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/infobar/DuplicateDownloadInfoBar.java

Issue 2478583004: implementation for new duplicate download UI (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/infobar/DuplicateDownloadInfoBar.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/infobar/DuplicateDownloadInfoBar.java b/chrome/android/java/src/org/chromium/chrome/browser/infobar/DuplicateDownloadInfoBar.java
new file mode 100644
index 0000000000000000000000000000000000000000..eb5fea64b31fac5cc25cc7ac17aa8e97cfd87731
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/infobar/DuplicateDownloadInfoBar.java
@@ -0,0 +1,148 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.chrome.browser.infobar;
+
+import android.content.Context;
+import android.content.Intent;
+import android.graphics.Typeface;
+import android.net.Uri;
+import android.text.Spannable;
+import android.text.SpannableString;
+import android.text.TextUtils;
+import android.text.style.ClickableSpan;
+import android.text.style.StyleSpan;
+import android.view.View;
+import android.webkit.MimeTypeMap;
+
+import org.chromium.base.annotations.CalledByNative;
+import org.chromium.chrome.R;
+import org.chromium.chrome.browser.IntentHandler;
+import org.chromium.chrome.browser.download.DownloadManagerService;
+import org.chromium.chrome.browser.download.DownloadUtils;
+
+import java.io.File;
+
+/**
+ * An infobar to ask whether to proceed downloading a file that already exists locally.
+ */
+public class DuplicateDownloadInfoBar extends InfoBar {
gone 2016/11/07 19:45:55 This should be extending ConfirmInfoBar.
qinmin 2016/11/08 00:30:49 Done.
+ private static final String TAG = "DuplicateDownloadInfoBar";
+ private final String mFilePath;
+ private final boolean mIsOfflinePage;
+ private final String mPageUrl;
+
+ @CalledByNative
+ private static InfoBar createInfoBar(String filePath, boolean isOfflinePage, String pageUrl) {
+ return new DuplicateDownloadInfoBar(filePath, isOfflinePage, pageUrl);
+ }
+
+ /**
+ * Constructs DuplicateDownloadInfoBar.
+ * @param filePath The file path.
+ */
+ private DuplicateDownloadInfoBar(String filePath, boolean isOfflinePage, String pageUrl) {
+ super(R.drawable.infobar_downloading, null, null);
+ mFilePath = filePath;
+ mIsOfflinePage = isOfflinePage;
+ mPageUrl = pageUrl;
+ }
+
+ /**
+ * Gets the infobar text for regular downloads.
+ * @param context Context to be used.
+ * @param template Template of the text to be displayed.
+ */
+ private CharSequence getDownloadMessageText(final Context context, final String template) {
+ final File file = new File(mFilePath);
+ final Uri fileUri = Uri.fromFile(file);
+ final String mimeType = getMimeTypeFromFile(fileUri);
+ final String filename = file.getName();
+ return getMessageText(template, filename, new ClickableSpan() {
+ @Override
+ public void onClick(View view) {
gone 2016/11/07 19:45:55 Can you just call DownloadUtils.openFile()? I'd r
qinmin 2016/11/08 00:30:50 Thanks, I didn't saw this function in my previous
+ if (DownloadManagerService.isSupportedMimeType(mimeType)) {
+ final Uri shareUri = DownloadUtils.getUriForItem(file);
+ Intent intent = DownloadUtils.getMediaViewerIntentForDownloadItem(
+ fileUri, shareUri, mimeType);
+ IntentHandler.startActivityForTrustedIntent(intent, context);
+ } else {
+ Intent intent = DownloadUtils.createViewIntentForDownloadItem(
+ fileUri, mimeType);
+ context.startActivity(intent);
+ }
+ }
+ });
+ }
+
+ /**
+ * Gets the infobar text for offline page downloads.
+ * @param context Context to be used.
+ * @param template Template of the text to be displayed.
+ */
+ private CharSequence getOfflinePageMessageText(final Context context, final String template) {
+ return getMessageText(template, mFilePath, new ClickableSpan() {
+ @Override
+ public void onClick(View view) {
+ // TODO(qinmin): open the offline page on local storage instead of opening the url.
+ // However, there could be multiple stored offline pages for the same url, need to
+ // figure out which one to use.
+ Intent intent = new Intent(Intent.ACTION_VIEW);
+ intent.setData(Uri.parse(mPageUrl));
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ intent.setPackage(context.getPackageName());
+ context.startActivity(intent);
+ }
+ });
+ }
+
+ /**
+ * Helper method to get the text to be displayed on the infobar.
+ * @param template Message template.
+ * @param fileName Name of the file.
+ * @param clickableSpan Action to perform when clicking on the file name.
+ * @return message to be displayed on the infobar.
+ */
+ private CharSequence getMessageText(final String template, final String fileName,
+ final ClickableSpan clickableSpan) {
+ final SpannableString formattedFilePath = new SpannableString(fileName);
+ formattedFilePath.setSpan(new StyleSpan(Typeface.BOLD), 0, fileName.length(),
+ Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
+ formattedFilePath.setSpan(clickableSpan, 0, fileName.length(),
+ Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
+ return TextUtils.expandTemplate(template, formattedFilePath);
+ }
+
+ /**
+ * Retrieve the mime type based on the given file.
+ * @param fileUri Uri of the file
+ * @return Possible mime type of the file.
+ */
+ private static String getMimeTypeFromFile(Uri fileUri) {
gone 2016/11/07 19:45:55 FromUri because you're passing in a Uri.
qinmin 2016/11/08 00:30:50 Done.
+ String extension = MimeTypeMap.getSingleton().getFileExtensionFromUrl(fileUri.toString());
+ return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
+ }
+
+ @Override
+ public void createContent(InfoBarLayout layout) {
+ Context context = layout.getContext();
+ if (mIsOfflinePage) {
+ layout.setMessage(getOfflinePageMessageText(
+ context, context.getString(R.string.duplicate_download_infobar_text)));
+ } else {
+ layout.setMessage(getDownloadMessageText(
+ context, context.getString(R.string.duplicate_download_infobar_text)));
+ }
+ layout.setButtons(
+ context.getString(R.string.duplicate_download_infobar_download_button),
+ context.getString(R.string.duplicate_download_infobar_cancel_button));
+ }
+
+ @Override
+ public void onButtonClicked(boolean isPrimaryButton) {
gone 2016/11/07 19:45:55 Once you inherit from ConfirmInfoBar, this comes f
qinmin 2016/11/08 00:30:50 Done.
+ int action = isPrimaryButton ? ActionType.OK : ActionType.CANCEL;
+ onButtonClicked(action);
+ }
+
+}

Powered by Google App Engine
This is Rietveld 408576698