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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.chrome.browser.infobar;
6
7 import android.content.Context;
8 import android.content.Intent;
9 import android.graphics.Typeface;
10 import android.net.Uri;
11 import android.text.Spannable;
12 import android.text.SpannableString;
13 import android.text.TextUtils;
14 import android.text.style.ClickableSpan;
15 import android.text.style.StyleSpan;
16 import android.view.View;
17 import android.webkit.MimeTypeMap;
18
19 import org.chromium.base.annotations.CalledByNative;
20 import org.chromium.chrome.R;
21 import org.chromium.chrome.browser.IntentHandler;
22 import org.chromium.chrome.browser.download.DownloadManagerService;
23 import org.chromium.chrome.browser.download.DownloadUtils;
24
25 import java.io.File;
26
27 /**
28 * An infobar to ask whether to proceed downloading a file that already exists l ocally.
29 */
30 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.
31 private static final String TAG = "DuplicateDownloadInfoBar";
32 private final String mFilePath;
33 private final boolean mIsOfflinePage;
34 private final String mPageUrl;
35
36 @CalledByNative
37 private static InfoBar createInfoBar(String filePath, boolean isOfflinePage, String pageUrl) {
38 return new DuplicateDownloadInfoBar(filePath, isOfflinePage, pageUrl);
39 }
40
41 /**
42 * Constructs DuplicateDownloadInfoBar.
43 * @param filePath The file path.
44 */
45 private DuplicateDownloadInfoBar(String filePath, boolean isOfflinePage, Str ing pageUrl) {
46 super(R.drawable.infobar_downloading, null, null);
47 mFilePath = filePath;
48 mIsOfflinePage = isOfflinePage;
49 mPageUrl = pageUrl;
50 }
51
52 /**
53 * Gets the infobar text for regular downloads.
54 * @param context Context to be used.
55 * @param template Template of the text to be displayed.
56 */
57 private CharSequence getDownloadMessageText(final Context context, final Str ing template) {
58 final File file = new File(mFilePath);
59 final Uri fileUri = Uri.fromFile(file);
60 final String mimeType = getMimeTypeFromFile(fileUri);
61 final String filename = file.getName();
62 return getMessageText(template, filename, new ClickableSpan() {
63 @Override
64 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
65 if (DownloadManagerService.isSupportedMimeType(mimeType)) {
66 final Uri shareUri = DownloadUtils.getUriForItem(file);
67 Intent intent = DownloadUtils.getMediaViewerIntentForDownloa dItem(
68 fileUri, shareUri, mimeType);
69 IntentHandler.startActivityForTrustedIntent(intent, context) ;
70 } else {
71 Intent intent = DownloadUtils.createViewIntentForDownloadIte m(
72 fileUri, mimeType);
73 context.startActivity(intent);
74 }
75 }
76 });
77 }
78
79 /**
80 * Gets the infobar text for offline page downloads.
81 * @param context Context to be used.
82 * @param template Template of the text to be displayed.
83 */
84 private CharSequence getOfflinePageMessageText(final Context context, final String template) {
85 return getMessageText(template, mFilePath, new ClickableSpan() {
86 @Override
87 public void onClick(View view) {
88 // TODO(qinmin): open the offline page on local storage instead of opening the url.
89 // However, there could be multiple stored offline pages for the same url, need to
90 // figure out which one to use.
91 Intent intent = new Intent(Intent.ACTION_VIEW);
92 intent.setData(Uri.parse(mPageUrl));
93 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
94 intent.setPackage(context.getPackageName());
95 context.startActivity(intent);
96 }
97 });
98 }
99
100 /**
101 * Helper method to get the text to be displayed on the infobar.
102 * @param template Message template.
103 * @param fileName Name of the file.
104 * @param clickableSpan Action to perform when clicking on the file name.
105 * @return message to be displayed on the infobar.
106 */
107 private CharSequence getMessageText(final String template, final String file Name,
108 final ClickableSpan clickableSpan) {
109 final SpannableString formattedFilePath = new SpannableString(fileName);
110 formattedFilePath.setSpan(new StyleSpan(Typeface.BOLD), 0, fileName.leng th(),
111 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
112 formattedFilePath.setSpan(clickableSpan, 0, fileName.length(),
113 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
114 return TextUtils.expandTemplate(template, formattedFilePath);
115 }
116
117 /**
118 * Retrieve the mime type based on the given file.
119 * @param fileUri Uri of the file
120 * @return Possible mime type of the file.
121 */
122 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.
123 String extension = MimeTypeMap.getSingleton().getFileExtensionFromUrl(fi leUri.toString());
124 return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
125 }
126
127 @Override
128 public void createContent(InfoBarLayout layout) {
129 Context context = layout.getContext();
130 if (mIsOfflinePage) {
131 layout.setMessage(getOfflinePageMessageText(
132 context, context.getString(R.string.duplicate_download_infob ar_text)));
133 } else {
134 layout.setMessage(getDownloadMessageText(
135 context, context.getString(R.string.duplicate_download_infob ar_text)));
136 }
137 layout.setButtons(
138 context.getString(R.string.duplicate_download_infobar_download_b utton),
139 context.getString(R.string.duplicate_download_infobar_cancel_but ton));
140 }
141
142 @Override
143 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.
144 int action = isPrimaryButton ? ActionType.OK : ActionType.CANCEL;
145 onButtonClicked(action);
146 }
147
148 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698