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..871ef6d81808eeec84d8043a75187f5fef226f5d |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/infobar/DownloadOverwriteInfoBar.java |
@@ -0,0 +1,106 @@ |
+// Copyright 2014 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.util.Log; |
+import android.view.View; |
+ |
+import org.chromium.chrome.R; |
+ |
+/** |
+ * 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"; |
+ |
+ private final String mFileName; |
+ private final String mDirName; |
+ private final String mDirFullPath; |
+ |
+ public DownloadOverwriteInfoBar( |
+ long nativeInfoBarPtr, String fileName, String dirName, String dirFullPath) { |
+ super(null, R.drawable.infobar_multiple_downloads, null); |
+ mFileName = fileName; |
+ mDirName = dirName; |
+ mDirFullPath = dirFullPath; |
+ setNativeInfoBar(nativeInfoBarPtr); |
+ } |
+ |
+ @Override |
+ public void onCloseButtonClicked() { |
+ nativeOnCloseButtonClicked(mNativeInfoBarPtr); |
+ } |
+ |
+ @Override |
+ public void onButtonClicked(boolean isPrimaryButton) { |
+ int action = actionFor(isPrimaryButton); |
+ nativeOnButtonClicked(mNativeInfoBarPtr, action, ""); |
+ } |
+ |
+ private int actionFor(boolean isPrimaryButton) { |
+ return isPrimaryButton ? InfoBar.ACTION_TYPE_OK : InfoBar.ACTION_TYPE_CANCEL; |
Ted C
2014/10/15 00:19:44
I would just inline this in the onButtonClicked me
Changwan Ryu
2014/10/15 22:23:24
Done.
|
+ } |
+ |
+ 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) { |
+ Intent intent = new Intent(Intent.ACTION_GET_CONTENT); |
+ Uri uri = Uri.parse(dirFullPath); |
+ if (uri == null) { |
+ return null; |
+ } |
+ intent.setDataAndType(uri, "*/*"); |
+ return intent; |
+ } |
+ |
+ private String getPrimaryButtonText(Context context) { |
+ return context.getString(R.string.download_overwrite_infobar_yes_replace_button); |
Ted C
2014/10/15 00:19:44
I would inline these two get*ButtonText methods as
Changwan Ryu
2014/10/15 22:23:24
Done.
|
+ } |
+ |
+ private String getSecondaryButtonText(Context context) { |
+ return context.getString(R.string.download_overwrite_infobar_no_thanks_button); |
+ } |
+ |
+ @Override |
+ public void createContent(InfoBarLayout layout) { |
+ Context context = layout.getContext(); |
+ layout.setMessage(getMessageText(context)); |
+ layout.setButtons(getPrimaryButtonText(context), getSecondaryButtonText(context)); |
+ } |
+ |
+ 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); |
+ formattedDirName.setSpan(new ClickableSpan() { |
Ted C
2014/10/15 00:19:44
If the intent is null, then I don't think you shou
Changwan Ryu
2014/10/15 22:23:24
Done.
|
+ @Override |
+ public void onClick(View view) { |
+ if (dirNameIntent == null) { |
+ Log.w(TAG, "Touching the dir name created a null intent."); |
+ return; |
+ } |
+ context.startActivity(dirNameIntent); |
Ted C
2014/10/15 00:19:44
Before calling this, you should see if the intent
Changwan Ryu
2014/10/15 22:23:24
Done.
|
+ } |
+ }, 0, dirName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); |
+ |
+ return TextUtils.expandTemplate(template, formattedFileName, formattedDirName); |
+ } |
+} |