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

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: do null check on webcontents 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.ContextUtils;
20 import org.chromium.base.annotations.CalledByNative;
21 import org.chromium.chrome.R;
22 import org.chromium.chrome.browser.download.DownloadUtils;
23
24 import java.io.File;
25
26 /**
27 * An infobar to ask whether to proceed downloading a file that already exists l ocally.
28 */
29 public class DuplicateDownloadInfoBar extends ConfirmInfoBar {
30 private static final String TAG = "DuplicateDownloadInfoBar";
31 private final String mFilePath;
32 private final boolean mIsOfflinePage;
33 private final String mPageUrl;
34 private final boolean mIsIncognito;
35
36 @CalledByNative
37 private static InfoBar createInfoBar(
38 String filePath, boolean isOfflinePage, String pageUrl, boolean isIn cognito) {
39 return new DuplicateDownloadInfoBar(
40 ContextUtils.getApplicationContext(), filePath, isOfflinePage, p ageUrl,
41 isIncognito);
42 }
43
44 /**
45 * Constructs DuplicateDownloadInfoBar.
46 * @param context Application context.
47 * @param filePath The file path.
48 * @param isOfflinePage Whether the download is for offline page.
49 * @param pageUrl Url of the page, ignored if this is a regular download.
50 * @param isIncognito Whether download is Incognito.
51 */
52 private DuplicateDownloadInfoBar(
53 Context context, String filePath, boolean isOfflinePage, String page Url,
54 boolean isIncognito) {
55 super(R.drawable.infobar_downloading, null, null, null,
56 context.getString(R.string.duplicate_download_infobar_download_b utton),
57 context.getString(R.string.cancel));
58 mFilePath = filePath;
59 mIsOfflinePage = isOfflinePage;
60 mPageUrl = pageUrl;
61 mIsIncognito = isIncognito;
62 }
63
64 /**
65 * Gets the infobar text for regular downloads.
66 * @param context Context to be used.
67 * @param template Template of the text to be displayed.
68 */
69 private CharSequence getDownloadMessageText(final Context context, final Str ing template) {
70 final File file = new File(mFilePath);
71 final Uri fileUri = Uri.fromFile(file);
72 final String mimeType = getMimeTypeFromUri(fileUri);
73 final String filename = file.getName();
74 return getMessageText(template, filename, new ClickableSpan() {
75 @Override
76 public void onClick(View view) {
77 DownloadUtils.openFile(file, mimeType, mIsIncognito);
78 }
79 });
80 }
81
82 /**
83 * Gets the infobar text for offline page downloads.
84 * @param context Context to be used.
85 * @param template Template of the text to be displayed.
86 */
87 private CharSequence getOfflinePageMessageText(final Context context, final String template) {
88 return getMessageText(template, mFilePath, new ClickableSpan() {
89 @Override
90 public void onClick(View view) {
91 // TODO(qinmin): open the offline page on local storage instead of opening the url.
92 // However, there could be multiple stored offline pages for the same url, need to
93 // figure out which one to use.
94 Intent intent = new Intent(Intent.ACTION_VIEW);
95 intent.setData(Uri.parse(mPageUrl));
96 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
97 intent.setPackage(context.getPackageName());
98 context.startActivity(intent);
99 }
100 });
101 }
102
103 /**
104 * Helper method to get the text to be displayed on the infobar.
105 * @param template Message template.
106 * @param fileName Name of the file.
107 * @param clickableSpan Action to perform when clicking on the file name.
108 * @return message to be displayed on the infobar.
109 */
110 private CharSequence getMessageText(final String template, final String file Name,
111 final ClickableSpan clickableSpan) {
112 final SpannableString formattedFilePath = new SpannableString(fileName);
113 formattedFilePath.setSpan(new StyleSpan(Typeface.BOLD), 0, fileName.leng th(),
114 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
115 formattedFilePath.setSpan(clickableSpan, 0, fileName.length(),
116 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
117 return TextUtils.expandTemplate(template, formattedFilePath);
118 }
119
120 /**
121 * Retrieve the mime type based on the given file URI.
122 * @param fileUri URI of the file
123 * @return Possible mime type of the file.
124 */
125 private static String getMimeTypeFromUri(Uri fileUri) {
126 String extension = MimeTypeMap.getSingleton().getFileExtensionFromUrl(fi leUri.toString());
127 return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
128 }
129
130 @Override
131 public void createContent(InfoBarLayout layout) {
132 super.createContent(layout);
133 Context context = layout.getContext();
134 if (mIsOfflinePage) {
135 layout.setMessage(getOfflinePageMessageText(
136 context, context.getString(R.string.duplicate_download_infob ar_text)));
137 } else {
138 layout.setMessage(getDownloadMessageText(
139 context, context.getString(R.string.duplicate_download_infob ar_text)));
140 }
141 }
142 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698