Commit 7931ccb5 authored by dfalcantara's avatar dfalcantara Committed by Commit bot

Add support for passing bitmaps to Android infobars

Native infobars are already capable of choosing between
a resource and a Bitmap for the infobar icon.  Pass this
information through to the Java side so the UI widgets
can also display the Bitmap version of the icon.

BUG=453170

Review URL: https://codereview.chromium.org/889613003

Cr-Commit-Position: refs/heads/master@{#314183}
parent 7a17894f
...@@ -287,7 +287,7 @@ public class ChromeDownloadDelegate ...@@ -287,7 +287,7 @@ public class ChromeDownloadDelegate
final String cancelButtonText = mContext.getResources().getString(R.string.cancel); final String cancelButtonText = mContext.getResources().getString(R.string.cancel);
mTab.getInfoBarContainer().addInfoBar(new ConfirmInfoBar(0, mTab.getInfoBarContainer().addInfoBar(new ConfirmInfoBar(0,
this, drawableId, titleText, null, okButtonText, cancelButtonText)); this, drawableId, null, titleText, null, okButtonText, cancelButtonText));
} }
/** /**
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
package org.chromium.chrome.browser.infobar; package org.chromium.chrome.browser.infobar;
import android.graphics.Bitmap;
/** /**
* An infobar that presents the user with several buttons. * An infobar that presents the user with several buttons.
* *
...@@ -23,9 +25,9 @@ public class ConfirmInfoBar extends InfoBar { ...@@ -23,9 +25,9 @@ public class ConfirmInfoBar extends InfoBar {
private final InfoBarListeners.Confirm mConfirmListener; private final InfoBarListeners.Confirm mConfirmListener;
public ConfirmInfoBar(long nativeInfoBar, InfoBarListeners.Confirm confirmListener, public ConfirmInfoBar(long nativeInfoBar, InfoBarListeners.Confirm confirmListener,
int iconDrawableId, String message, String linkText, String primaryButtonText, int iconDrawableId, Bitmap iconBitmap, String message, String linkText,
String secondaryButtonText) { String primaryButtonText, String secondaryButtonText) {
super(confirmListener, iconDrawableId, message); super(confirmListener, iconDrawableId, iconBitmap, message);
mPrimaryButtonText = primaryButtonText; mPrimaryButtonText = primaryButtonText;
mSecondaryButtonText = secondaryButtonText; mSecondaryButtonText = secondaryButtonText;
mTertiaryButtonText = linkText; mTertiaryButtonText = linkText;
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
package org.chromium.chrome.browser.infobar; package org.chromium.chrome.browser.infobar;
import android.graphics.Bitmap;
import org.chromium.base.CalledByNative; import org.chromium.base.CalledByNative;
import org.chromium.chrome.browser.ResourceId; import org.chromium.chrome.browser.ResourceId;
...@@ -26,18 +28,19 @@ public class ConfirmInfoBarDelegate { ...@@ -26,18 +28,19 @@ public class ConfirmInfoBarDelegate {
* @param enumeratedIconId ID corresponding to the icon that will be shown for the InfoBar. * @param enumeratedIconId ID corresponding to the icon that will be shown for the InfoBar.
* The ID must have been mapped using the ResourceMapper class before * The ID must have been mapped using the ResourceMapper class before
* passing it to this function. * passing it to this function.
* @param bitmap Bitmap to use if there is no equivalent Java resource for enumeratedIconId.
* @param message Message to display to the user indicating what the InfoBar is for. * @param message Message to display to the user indicating what the InfoBar is for.
* @param linkText Link text to display in addition to the message. * @param linkText Link text to display in addition to the message.
* @param buttonOk String to display on the OK button. * @param buttonOk String to display on the OK button.
* @param buttonCancel String to display on the Cancel button. * @param buttonCancel String to display on the Cancel button.
*/ */
@CalledByNative @CalledByNative
InfoBar showConfirmInfoBar(long nativeInfoBar, int enumeratedIconId, String message, InfoBar showConfirmInfoBar(long nativeInfoBar, int enumeratedIconId, Bitmap iconBitmap,
String linkText, String buttonOk, String buttonCancel) { String message, String linkText, String buttonOk, String buttonCancel) {
int drawableId = ResourceId.mapToDrawableId(enumeratedIconId); int drawableId = ResourceId.mapToDrawableId(enumeratedIconId);
ConfirmInfoBar infoBar = new ConfirmInfoBar(nativeInfoBar, null, drawableId, message, ConfirmInfoBar infoBar = new ConfirmInfoBar(nativeInfoBar, null, drawableId, iconBitmap,
linkText, buttonOk, buttonCancel); message, linkText, buttonOk, buttonCancel);
return infoBar; return infoBar;
} }
} }
...@@ -44,8 +44,7 @@ public class DataReductionProxyInfoBar extends ConfirmInfoBar { ...@@ -44,8 +44,7 @@ public class DataReductionProxyInfoBar extends ConfirmInfoBar {
} }
DataReductionProxyInfoBar(long nativeInfoBar, int iconDrawableId) { DataReductionProxyInfoBar(long nativeInfoBar, int iconDrawableId) {
super(nativeInfoBar, null, iconDrawableId, sTitle, super(nativeInfoBar, null, iconDrawableId, null, sTitle, sLinkText, null, null);
sLinkText, null, null);
} }
@Override @Override
......
...@@ -29,7 +29,7 @@ public class GeneratedPasswordSavedInfoBar extends InfoBar { ...@@ -29,7 +29,7 @@ public class GeneratedPasswordSavedInfoBar extends InfoBar {
*/ */
public GeneratedPasswordSavedInfoBar(long nativeInfoBar, int iconDrawableId, String messageText, public GeneratedPasswordSavedInfoBar(long nativeInfoBar, int iconDrawableId, String messageText,
int inlineLinkRangeStart, int inlineLinkRangeEnd, String buttonLabel) { int inlineLinkRangeStart, int inlineLinkRangeEnd, String buttonLabel) {
super(null, iconDrawableId, null); super(null, iconDrawableId, null, null);
setNativeInfoBar(nativeInfoBar); setNativeInfoBar(nativeInfoBar);
mMessageText = messageText; mMessageText = messageText;
mInlineLinkRangeStart = inlineLinkRangeStart; mInlineLinkRangeStart = inlineLinkRangeStart;
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package org.chromium.chrome.browser.infobar; package org.chromium.chrome.browser.infobar;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap;
import android.view.View; import android.view.View;
import org.chromium.base.CalledByNative; import org.chromium.base.CalledByNative;
...@@ -36,6 +37,7 @@ public abstract class InfoBar implements InfoBarView { ...@@ -36,6 +37,7 @@ public abstract class InfoBar implements InfoBarView {
public static final int ACTION_TYPE_TRANSLATE_SHOW_ORIGINAL = 4; public static final int ACTION_TYPE_TRANSLATE_SHOW_ORIGINAL = 4;
private final int mIconDrawableId; private final int mIconDrawableId;
private final Bitmap mIconBitmap;
private final CharSequence mMessage; private final CharSequence mMessage;
private InfoBarListeners.Dismiss mListener; private InfoBarListeners.Dismiss mListener;
...@@ -64,10 +66,12 @@ public abstract class InfoBar implements InfoBarView { ...@@ -64,10 +66,12 @@ public abstract class InfoBar implements InfoBarView {
* @param iconDrawableId ID of the resource to use for the Icon. If 0, no icon will be shown. * @param iconDrawableId ID of the resource to use for the Icon. If 0, no icon will be shown.
* @param message The message to show in the infobar. * @param message The message to show in the infobar.
*/ */
public InfoBar(InfoBarListeners.Dismiss listener, int iconDrawableId, CharSequence message) { public InfoBar(InfoBarListeners.Dismiss listener, int iconDrawableId, Bitmap iconBitmap,
CharSequence message) {
mListener = listener; mListener = listener;
mId = generateId(); mId = generateId();
mIconDrawableId = iconDrawableId; mIconDrawableId = iconDrawableId;
mIconBitmap = iconBitmap;
mMessage = message; mMessage = message;
mExpireOnNavigation = true; mExpireOnNavigation = true;
} }
...@@ -145,7 +149,8 @@ public abstract class InfoBar implements InfoBarView { ...@@ -145,7 +149,8 @@ public abstract class InfoBar implements InfoBarView {
protected final View createView() { protected final View createView() {
assert mContext != null; assert mContext != null;
InfoBarLayout layout = new InfoBarLayout(mContext, this, mIconDrawableId, mMessage); InfoBarLayout layout =
new InfoBarLayout(mContext, this, mIconDrawableId, mIconBitmap, mMessage);
createContent(layout); createContent(layout);
return layout; return layout;
} }
......
...@@ -7,6 +7,7 @@ package org.chromium.chrome.browser.infobar; ...@@ -7,6 +7,7 @@ package org.chromium.chrome.browser.infobar;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.text.TextUtils; import android.text.TextUtils;
...@@ -140,10 +141,11 @@ public class InfoBarLayout extends ViewGroup implements View.OnClickListener { ...@@ -140,10 +141,11 @@ public class InfoBarLayout extends ViewGroup implements View.OnClickListener {
* @param context The context used to render. * @param context The context used to render.
* @param infoBarView InfoBarView that listens to events. * @param infoBarView InfoBarView that listens to events.
* @param iconResourceId ID of the icon to use for the InfoBar. * @param iconResourceId ID of the icon to use for the InfoBar.
* @param iconBitmap Bitmap for the icon to use, if the resource ID wasn't passed through.
* @param message The message to show in the infobar. * @param message The message to show in the infobar.
*/ */
public InfoBarLayout(Context context, InfoBarView infoBarView, int iconResourceId, public InfoBarLayout(Context context, InfoBarView infoBarView, int iconResourceId,
CharSequence message) { Bitmap iconBitmap, CharSequence message) {
super(context); super(context);
mInfoBarView = infoBarView; mInfoBarView = infoBarView;
...@@ -170,13 +172,17 @@ public class InfoBarLayout extends ViewGroup implements View.OnClickListener { ...@@ -170,13 +172,17 @@ public class InfoBarLayout extends ViewGroup implements View.OnClickListener {
addView(mCloseButton); addView(mCloseButton);
// Set up the icon. // Set up the icon.
if (iconResourceId != 0) { if (iconResourceId != 0 || iconBitmap != null) {
mIconView = new ImageView(context); mIconView = new ImageView(context);
mIconView.setImageResource(iconResourceId); if (iconResourceId != 0) {
mIconView.setFocusable(false); mIconView.setImageResource(iconResourceId);
} else if (iconBitmap != null) {
mIconView.setImageBitmap(iconBitmap);
}
mIconView.setLayoutParams(new LayoutParams(0, 0, mMargin / 2, 0)); mIconView.setLayoutParams(new LayoutParams(0, 0, mMargin / 2, 0));
mIconView.getLayoutParams().width = mIconSize; mIconView.getLayoutParams().width = mIconSize;
mIconView.getLayoutParams().height = mIconSize; mIconView.getLayoutParams().height = mIconSize;
mIconView.setFocusable(false);
} }
// Set up the message view. // Set up the message view.
...@@ -186,10 +192,10 @@ public class InfoBarLayout extends ViewGroup implements View.OnClickListener { ...@@ -186,10 +192,10 @@ public class InfoBarLayout extends ViewGroup implements View.OnClickListener {
mMessageView.setLinkTextColor(mAccentColor); mMessageView.setLinkTextColor(mAccentColor);
mMessageView.setLayoutParams(new LayoutParams(0, mMargin / 4, 0, 0)); mMessageView.setLayoutParams(new LayoutParams(0, mMargin / 4, 0, 0));
if (mIconView != null) { if (mIconView == null) {
mMainGroup = addGroup(mIconView, mMessageView);
} else {
mMainGroup = addGroup(mMessageView); mMainGroup = addGroup(mMessageView);
} else {
mMainGroup = addGroup(mIconView, mMessageView);
} }
} }
......
...@@ -30,7 +30,7 @@ public class MessageInfoBar extends InfoBar { ...@@ -30,7 +30,7 @@ public class MessageInfoBar extends InfoBar {
*/ */
public MessageInfoBar(InfoBarListeners.Dismiss listener, int iconResourceId, public MessageInfoBar(InfoBarListeners.Dismiss listener, int iconResourceId,
CharSequence title) { CharSequence title) {
super(listener, iconResourceId, title); super(listener, iconResourceId, null, title);
} }
@Override @Override
......
...@@ -45,7 +45,7 @@ public class TranslateInfoBar extends InfoBar implements SubPanelListener { ...@@ -45,7 +45,7 @@ public class TranslateInfoBar extends InfoBar implements SubPanelListener {
int infoBarType, int sourceLanguageIndex, int targetLanguageIndex, int infoBarType, int sourceLanguageIndex, int targetLanguageIndex,
boolean autoTranslatePair, boolean shouldShowNeverBar, boolean autoTranslatePair, boolean shouldShowNeverBar,
boolean triggeredFromMenu, String[] languages) { boolean triggeredFromMenu, String[] languages) {
super(null, R.drawable.infobar_translate, null); super(null, R.drawable.infobar_translate, null, null);
mTranslateDelegate = delegate; mTranslateDelegate = delegate;
mOptions = new TranslateOptions(sourceLanguageIndex, targetLanguageIndex, languages, mOptions = new TranslateOptions(sourceLanguageIndex, targetLanguageIndex, languages,
autoTranslatePair, triggeredFromMenu); autoTranslatePair, triggeredFromMenu);
...@@ -181,9 +181,9 @@ public class TranslateInfoBar extends InfoBar implements SubPanelListener { ...@@ -181,9 +181,9 @@ public class TranslateInfoBar extends InfoBar implements SubPanelListener {
layout.setMessage(getMessageText(context)); layout.setMessage(getMessageText(context));
layout.setButtons(getPrimaryButtonText(context), getSecondaryButtonText(context)); layout.setButtons(getPrimaryButtonText(context), getSecondaryButtonText(context));
if (getInfoBarType() == AFTER_TRANSLATE_INFOBAR && if (getInfoBarType() == AFTER_TRANSLATE_INFOBAR
!needsAlwaysPanel() && && !needsAlwaysPanel()
!mOptions.triggeredFromMenu()) { && !mOptions.triggeredFromMenu()) {
// Long always translate version // Long always translate version
TranslateCheckBox checkBox = new TranslateCheckBox(context, mOptions, this); TranslateCheckBox checkBox = new TranslateCheckBox(context, mOptions, this);
layout.setCustomContent(checkBox); layout.setCustomContent(checkBox);
......
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
#include "chrome/browser/infobars/infobar_service.h" #include "chrome/browser/infobars/infobar_service.h"
#include "components/infobars/core/confirm_infobar_delegate.h" #include "components/infobars/core/confirm_infobar_delegate.h"
#include "jni/ConfirmInfoBarDelegate_jni.h" #include "jni/ConfirmInfoBarDelegate_jni.h"
#include "ui/gfx/android/java_bitmap.h"
#include "ui/gfx/image/image.h"
// InfoBarService ------------------------------------------------------------- // InfoBarService -------------------------------------------------------------
...@@ -46,10 +48,15 @@ base::android::ScopedJavaLocalRef<jobject> ConfirmInfoBar::CreateRenderInfoBar( ...@@ -46,10 +48,15 @@ base::android::ScopedJavaLocalRef<jobject> ConfirmInfoBar::CreateRenderInfoBar(
base::android::ConvertUTF16ToJavaString( base::android::ConvertUTF16ToJavaString(
env, delegate->GetLinkText()); env, delegate->GetLinkText());
ScopedJavaLocalRef<jobject> java_bitmap;
if (!delegate->GetIcon().IsEmpty()) {
java_bitmap = gfx::ConvertToJavaBitmap(delegate->GetIcon().ToSkBitmap());
}
return Java_ConfirmInfoBarDelegate_showConfirmInfoBar( return Java_ConfirmInfoBarDelegate_showConfirmInfoBar(
env, java_confirm_delegate_.obj(), reinterpret_cast<intptr_t>(this), env, java_confirm_delegate_.obj(), reinterpret_cast<intptr_t>(this),
GetEnumeratedIconId(), message_text.obj(), link_text.obj(), GetEnumeratedIconId(), java_bitmap.obj(), message_text.obj(),
ok_button_text.obj(), cancel_button_text.obj()); link_text.obj(), ok_button_text.obj(), cancel_button_text.obj());
} }
void ConfirmInfoBar::OnLinkClicked(JNIEnv* env, jobject obj) { void ConfirmInfoBar::OnLinkClicked(JNIEnv* env, jobject obj) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment