Index: chrome/android/java/src/org/chromium/chrome/browser/infobar/DownloadOverwriteInfoBar.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/infobar/DownloadOverwriteInfoBar.java b/chrome/android/java/src/org/chromium/chrome/browser/infobar/DownloadOverwriteInfoBar.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..86910d830fbd3ca49b2e71254b707fa5a7b7b60d |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/infobar/DownloadOverwriteInfoBar.java |
@@ -0,0 +1,136 @@ |
+// Copyright 2015 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.content.pm.PackageManager; |
+import android.content.pm.ResolveInfo; |
+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 org.chromium.chrome.R; |
+ |
+import java.util.List; |
+ |
+/** |
+ * An infobar to ask whether to overwrite an existing file with a new download. |
+ */ |
+public class DownloadOverwriteInfoBar extends InfoBar { |
+ private static final String TAG = "DownloadOverwriteInfoBar"; |
+ |
+ /** |
+ * Java-side callback that can be used when this infobar was created by Java component. |
+ */ |
+ public interface Callback { |
Ted C
2015/02/20 01:48:08
FWIW, I think you should always more verbosely qua
Changwan Ryu
2015/02/23 06:50:37
Done.
|
+ /** |
+ * Called when close button is clicked. |
+ */ |
+ void onCloseButtonClicked(DownloadOverwriteInfoBar infoBar); |
+ /** |
+ * Called when a button is clicked. |
+ * @param action Action type defined in {@link InfoBar}. |
+ */ |
+ void onAction(DownloadOverwriteInfoBar infoBar, int action); |
+ } |
+ |
+ private final Callback mCallback; |
+ private final String mFileName; |
+ private final String mDirName; |
+ private final String mDirFullPath; |
+ |
+ /** |
+ * Constructs DownloadOverwriteInfoBar. |
+ * @param nativeInfoBarPtr Pointer value of the native infobar. 0 if there is no native |
+ * counterpart. |
+ * @param callback Java-side callback. Null if it was created from native side. |
+ * @param fileName The file name. ex) example.jpg |
+ * @param dirName The dir name. ex) Downloads |
+ * @param dirFullPath The full dir path. ex) sdcards/Downloads |
+ */ |
+ public DownloadOverwriteInfoBar(long nativeInfoBarPtr, Callback callback, String fileName, |
+ String dirName, String dirFullPath) { |
+ super(null, R.drawable.infobar_downloading, null, null); |
+ mCallback = callback; |
+ mFileName = fileName; |
+ mDirName = dirName; |
+ mDirFullPath = dirFullPath; |
+ setNativeInfoBar(nativeInfoBarPtr); |
+ } |
+ |
+ @Override |
+ public void onCloseButtonClicked() { |
+ if (mNativeInfoBarPtr != 0) nativeOnCloseButtonClicked(mNativeInfoBarPtr); |
+ if (mCallback != null) mCallback.onCloseButtonClicked(this); |
+ } |
+ |
+ @Override |
+ public void onButtonClicked(boolean isPrimaryButton) { |
+ int action = isPrimaryButton ? InfoBar.ACTION_TYPE_OVERWRITE |
+ : InfoBar.ACTION_TYPE_CREATE_NEW_FILE; |
Ted C
2015/02/20 01:48:08
this is C++ style indenting and it should be 8 fro
Changwan Ryu
2015/02/23 06:50:37
Done.
|
+ if (mNativeInfoBarPtr != 0) nativeOnButtonClicked(mNativeInfoBarPtr, action, ""); |
+ if (mCallback != null) mCallback.onAction(this, action); |
+ } |
+ |
+ private CharSequence getMessageText(Context context) { |
+ String template = context.getString(R.string.download_overwrite_infobar_text); |
+ Intent intent = getIntentForDirectoryLaunch(mDirFullPath); |
+ return formatInfoBarMessage(context, template, mFileName, mDirName, intent); |
+ } |
+ |
+ private static Intent getIntentForDirectoryLaunch(String dirFullPath) { |
qinmin
2015/02/20 01:00:40
javadoc here and below
Changwan Ryu
2015/02/23 06:50:37
Done.
|
+ Intent intent = new Intent(Intent.ACTION_GET_CONTENT); |
+ Uri uri = Uri.parse(dirFullPath); |
+ if (uri == null) { |
+ return null; |
+ } |
+ intent.setDataAndType(uri, "*/*"); |
+ return intent; |
+ } |
+ |
+ @Override |
+ public void createContent(InfoBarLayout layout) { |
+ Context context = layout.getContext(); |
+ layout.setMessage(getMessageText(context)); |
+ layout.setButtons( |
+ context.getString(R.string.download_overwrite_infobar_replace_file_button), |
+ context.getString(R.string.download_overwrite_infobar_create_new_file_button)); |
+ } |
+ |
+ private static CharSequence formatInfoBarMessage(final Context context, String template, |
+ String fileName, String dirName, final Intent dirNameIntent) { |
+ SpannableString formattedFileName = new SpannableString(fileName); |
+ formattedFileName.setSpan(new StyleSpan(Typeface.BOLD), 0, fileName.length(), |
+ Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); |
+ |
+ SpannableString formattedDirName = new SpannableString(dirName); |
+ if (canResolveIntent(context, dirNameIntent)) { |
+ formattedDirName.setSpan(new ClickableSpan() { |
+ @Override |
+ public void onClick(View view) { |
+ context.startActivity(dirNameIntent); |
+ } |
+ }, 0, dirName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); |
+ } |
+ |
+ return TextUtils.expandTemplate(template, formattedFileName, formattedDirName); |
+ } |
+ |
+ private static boolean canResolveIntent(Context context, Intent intent) { |
+ if (context == null || intent == null) { |
+ return false; |
+ } |
+ final PackageManager packageManager = context.getPackageManager(); |
+ List<ResolveInfo> resolveInfoList = |
+ packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); |
+ return resolveInfoList.size() > 0; |
+ } |
+} |