OLD | NEW |
| (Empty) |
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 | |
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.content.pm.PackageManager; | |
10 import android.content.pm.ResolveInfo; | |
11 import android.graphics.Typeface; | |
12 import android.net.Uri; | |
13 import android.text.Spannable; | |
14 import android.text.SpannableString; | |
15 import android.text.TextUtils; | |
16 import android.text.style.ClickableSpan; | |
17 import android.text.style.StyleSpan; | |
18 import android.view.View; | |
19 | |
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.util.List; | |
25 | |
26 /** | |
27 * An infobar to ask whether to overwrite an existing file with a new download. | |
28 */ | |
29 public class DownloadOverwriteInfoBar extends InfoBar { | |
30 private static final String TAG = "DownloadOverwriteInfoBar"; | |
31 | |
32 private final String mFileName; | |
33 private final String mDirName; | |
34 private final String mDirFullPath; | |
35 | |
36 @CalledByNative | |
37 private static InfoBar createInfoBar(String fileName, String dirName, String
dirFullPath) { | |
38 return new DownloadOverwriteInfoBar(fileName, dirName, dirFullPath); | |
39 } | |
40 | |
41 /** | |
42 * Constructs DownloadOverwriteInfoBar. | |
43 * @param fileName The file name. ex) example.jpg | |
44 * @param dirName The dir name. ex) Downloads | |
45 * @param dirFullPath The full dir path. ex) sdcards/Downloads. If empty, t
he Download Manager | |
46 * will be opened. | |
47 */ | |
48 private DownloadOverwriteInfoBar(String fileName, String dirName, String dir
FullPath) { | |
49 super(R.drawable.infobar_downloading, null, null); | |
50 mFileName = fileName; | |
51 mDirName = dirName; | |
52 mDirFullPath = dirFullPath; | |
53 } | |
54 | |
55 @Override | |
56 public void onButtonClicked(boolean isPrimaryButton) { | |
57 int action = isPrimaryButton ? ActionType.OVERWRITE | |
58 : ActionType.CREATE_NEW_FILE; | |
59 onButtonClicked(action); | |
60 } | |
61 | |
62 private boolean opensDownloadManager() { | |
63 return mDirFullPath == null || mDirFullPath.isEmpty(); | |
64 } | |
65 | |
66 private CharSequence getMessageText(Context context) { | |
67 String template = context.getString(R.string.download_overwrite_infobar_
text); | |
68 Intent intent = null; | |
69 | |
70 // If the full path is left empty, we assume that the file is in the Chr
ome Download | |
71 // Manager. So then in formatInfoBarMessage, if the intent remains null
then we are going | |
72 // to open the download manager when the directory name is clicked. | |
73 if (!opensDownloadManager()) intent = getIntentForDirectoryLaunch(mDirFu
llPath); | |
74 return formatInfoBarMessage(context, template, mFileName, mDirName, inte
nt); | |
75 } | |
76 | |
77 /** | |
78 * @param dirFullPath The full path of the directory to be launched. | |
79 * @return An Android intent that can launch the directory. | |
80 */ | |
81 private static Intent getIntentForDirectoryLaunch(String dirFullPath) { | |
82 Intent intent = new Intent(Intent.ACTION_GET_CONTENT); | |
83 Uri uri = Uri.parse(dirFullPath); | |
84 if (uri == null) { | |
85 return null; | |
86 } | |
87 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
88 intent.setDataAndType(uri, "*/*"); | |
89 return intent; | |
90 } | |
91 | |
92 @Override | |
93 public void createContent(InfoBarLayout layout) { | |
94 Context context = layout.getContext(); | |
95 layout.setMessage(getMessageText(context)); | |
96 layout.setButtons( | |
97 context.getString(R.string.download_overwrite_infobar_replace_fi
le_button), | |
98 context.getString(R.string.download_overwrite_infobar_create_new
_file_button)); | |
99 } | |
100 | |
101 /** | |
102 * Create infobar message in the form of CharSequence. | |
103 * | |
104 * @param context The context. | |
105 * @param template The template CharSequence. | |
106 * @param fileName The file name. | |
107 * @param dirName The directory name. | |
108 * @param dirNameIntent The intent to be launched when user touches the dire
ctory name link. | |
109 * @return CharSequence formatted message for InfoBar. | |
110 */ | |
111 private CharSequence formatInfoBarMessage(final Context context, String temp
late, | |
112 String fileName, String dirName, final Intent dirNameIntent) { | |
113 SpannableString formattedFileName = new SpannableString(fileName); | |
114 formattedFileName.setSpan(new StyleSpan(Typeface.BOLD), 0, fileName.leng
th(), | |
115 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); | |
116 | |
117 SpannableString formattedDirName = new SpannableString(dirName); | |
118 if (canResolveIntent(context, dirNameIntent)) { | |
119 formattedDirName.setSpan(new ClickableSpan() { | |
120 @Override | |
121 public void onClick(View view) { | |
122 context.startActivity(dirNameIntent); | |
123 } | |
124 }, 0, dirName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); | |
125 } else if (opensDownloadManager()) { | |
126 formattedDirName.setSpan(new ClickableSpan() { | |
127 @Override | |
128 public void onClick(View view) { | |
129 DownloadUtils.showDownloadManager(null, null); | |
130 } | |
131 }, 0, dirName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); | |
132 } | |
133 | |
134 return TextUtils.expandTemplate(template, formattedFileName, formattedDi
rName); | |
135 } | |
136 | |
137 private static boolean canResolveIntent(Context context, Intent intent) { | |
138 if (context == null || intent == null) { | |
139 return false; | |
140 } | |
141 final PackageManager packageManager = context.getPackageManager(); | |
142 List<ResolveInfo> resolveInfoList = | |
143 packageManager.queryIntentActivities(intent, PackageManager.MATC
H_DEFAULT_ONLY); | |
144 return resolveInfoList.size() > 0; | |
145 } | |
146 } | |
OLD | NEW |