Commit fc6255e7 authored by Pavel Yatsuk's avatar Pavel Yatsuk Committed by Commit Bot

[Messages] Add support for icon resource_id in MessageWrapper

Icon resource id will be set from native feature code.
ResourceMapper::MapToJavaDrawableId() will be used for mapping from
native constant to Android resource id.

This CL adds logic to porpagate the icon resource id to
MessageBannerView.

BUG=1123947
R=twellington@chromium.org

Change-Id: I6c57d9ddd180f229bedffdae3c677fd2c8efc32f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2523472
Commit-Queue: Pavel Yatsuk <pavely@chromium.org>
Reviewed-by: default avatarTheresa  <twellington@chromium.org>
Cr-Commit-Position: refs/heads/master@{#825065}
parent bdd27e79
...@@ -33,6 +33,7 @@ android_library("java") { ...@@ -33,6 +33,7 @@ android_library("java") {
"//components/browser_ui/widget/android:java", "//components/browser_ui/widget/android:java",
"//content/public/android:content_java", "//content/public/android:content_java",
"//third_party/android_deps:androidx_annotation_annotation_java", "//third_party/android_deps:androidx_annotation_annotation_java",
"//third_party/android_deps:androidx_appcompat_appcompat_resources_java",
"//ui/android:ui_java", "//ui/android:ui_java",
] ]
} }
......
...@@ -9,6 +9,7 @@ import android.view.View.OnClickListener; ...@@ -9,6 +9,7 @@ import android.view.View.OnClickListener;
import org.chromium.ui.modelutil.PropertyKey; import org.chromium.ui.modelutil.PropertyKey;
import org.chromium.ui.modelutil.PropertyModel.WritableFloatPropertyKey; import org.chromium.ui.modelutil.PropertyModel.WritableFloatPropertyKey;
import org.chromium.ui.modelutil.PropertyModel.WritableIntPropertyKey;
import org.chromium.ui.modelutil.PropertyModel.WritableObjectPropertyKey; import org.chromium.ui.modelutil.PropertyModel.WritableObjectPropertyKey;
/** /**
...@@ -24,6 +25,7 @@ public class MessageBannerProperties { ...@@ -24,6 +25,7 @@ public class MessageBannerProperties {
new WritableObjectPropertyKey<>(); new WritableObjectPropertyKey<>();
public static final WritableObjectPropertyKey<Drawable> ICON = public static final WritableObjectPropertyKey<Drawable> ICON =
new WritableObjectPropertyKey<>(); new WritableObjectPropertyKey<>();
public static final WritableIntPropertyKey ICON_RESOURCE_ID = new WritableIntPropertyKey();
// Secondary icon is shown as a button, so content description should be always set. // Secondary icon is shown as a button, so content description should be always set.
public static final WritableObjectPropertyKey<Drawable> SECONDARY_ICON = public static final WritableObjectPropertyKey<Drawable> SECONDARY_ICON =
new WritableObjectPropertyKey<>(); new WritableObjectPropertyKey<>();
...@@ -39,11 +41,12 @@ public class MessageBannerProperties { ...@@ -39,11 +41,12 @@ public class MessageBannerProperties {
static final WritableObjectPropertyKey<Runnable> ON_TOUCH_RUNNABLE = static final WritableObjectPropertyKey<Runnable> ON_TOUCH_RUNNABLE =
new WritableObjectPropertyKey<>(); new WritableObjectPropertyKey<>();
public static final PropertyKey[] ALL_KEYS = new PropertyKey[] {PRIMARY_BUTTON_TEXT, public static final PropertyKey[] ALL_KEYS =
PRIMARY_BUTTON_CLICK_LISTENER, TITLE, DESCRIPTION, ICON, SECONDARY_ICON,
SECONDARY_ICON_CONTENT_DESCRIPTION, TRANSLATION_Y, ALPHA, ON_TOUCH_RUNNABLE};
public static final PropertyKey[] SINGLE_ACTION_MESSAGE_KEYS =
new PropertyKey[] {PRIMARY_BUTTON_TEXT, PRIMARY_BUTTON_CLICK_LISTENER, TITLE, new PropertyKey[] {PRIMARY_BUTTON_TEXT, PRIMARY_BUTTON_CLICK_LISTENER, TITLE,
DESCRIPTION, ICON, ON_DISMISSED, TRANSLATION_Y, ALPHA, ON_TOUCH_RUNNABLE}; DESCRIPTION, ICON, ICON_RESOURCE_ID, SECONDARY_ICON,
SECONDARY_ICON_CONTENT_DESCRIPTION, TRANSLATION_Y, ALPHA, ON_TOUCH_RUNNABLE};
public static final PropertyKey[] SINGLE_ACTION_MESSAGE_KEYS = new PropertyKey[] {
PRIMARY_BUTTON_TEXT, PRIMARY_BUTTON_CLICK_LISTENER, TITLE, DESCRIPTION, ICON,
ICON_RESOURCE_ID, ON_DISMISSED, TRANSLATION_Y, ALPHA, ON_TOUCH_RUNNABLE};
} }
...@@ -6,6 +6,7 @@ package org.chromium.components.messages; ...@@ -6,6 +6,7 @@ package org.chromium.components.messages;
import static org.chromium.components.messages.MessageBannerProperties.DESCRIPTION; import static org.chromium.components.messages.MessageBannerProperties.DESCRIPTION;
import static org.chromium.components.messages.MessageBannerProperties.ICON; import static org.chromium.components.messages.MessageBannerProperties.ICON;
import static org.chromium.components.messages.MessageBannerProperties.ICON_RESOURCE_ID;
import static org.chromium.components.messages.MessageBannerProperties.ON_TOUCH_RUNNABLE; import static org.chromium.components.messages.MessageBannerProperties.ON_TOUCH_RUNNABLE;
import static org.chromium.components.messages.MessageBannerProperties.PRIMARY_BUTTON_CLICK_LISTENER; import static org.chromium.components.messages.MessageBannerProperties.PRIMARY_BUTTON_CLICK_LISTENER;
import static org.chromium.components.messages.MessageBannerProperties.PRIMARY_BUTTON_TEXT; import static org.chromium.components.messages.MessageBannerProperties.PRIMARY_BUTTON_TEXT;
...@@ -15,6 +16,8 @@ import static org.chromium.components.messages.MessageBannerProperties.TITLE; ...@@ -15,6 +16,8 @@ import static org.chromium.components.messages.MessageBannerProperties.TITLE;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import androidx.appcompat.content.res.AppCompatResources;
import org.chromium.ui.modelutil.PropertyKey; import org.chromium.ui.modelutil.PropertyKey;
import org.chromium.ui.modelutil.PropertyModel; import org.chromium.ui.modelutil.PropertyModel;
...@@ -34,6 +37,9 @@ public class MessageBannerViewBinder { ...@@ -34,6 +37,9 @@ public class MessageBannerViewBinder {
view.setDescription(model.get(DESCRIPTION)); view.setDescription(model.get(DESCRIPTION));
} else if (propertyKey == ICON) { } else if (propertyKey == ICON) {
view.setIcon(model.get(ICON)); view.setIcon(model.get(ICON));
} else if (propertyKey == ICON_RESOURCE_ID) {
view.setIcon(
AppCompatResources.getDrawable(view.getContext(), model.get(ICON_RESOURCE_ID)));
} else if (propertyKey == SECONDARY_ICON) { } else if (propertyKey == SECONDARY_ICON) {
view.setSecondaryIcon(model.get(SECONDARY_ICON)); view.setSecondaryIcon(model.get(SECONDARY_ICON));
} else if (propertyKey == SECONDARY_ICON_CONTENT_DESCRIPTION) { } else if (propertyKey == SECONDARY_ICON_CONTENT_DESCRIPTION) {
......
...@@ -6,6 +6,8 @@ package org.chromium.components.messages; ...@@ -6,6 +6,8 @@ package org.chromium.components.messages;
import android.view.View; import android.view.View;
import androidx.annotation.DrawableRes;
import org.chromium.base.annotations.CalledByNative; import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace; import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods; import org.chromium.base.annotations.NativeMethods;
...@@ -58,6 +60,11 @@ public final class MessageWrapper { ...@@ -58,6 +60,11 @@ public final class MessageWrapper {
mMessageProperties.set(MessageBannerProperties.PRIMARY_BUTTON_TEXT, primaryButtonText); mMessageProperties.set(MessageBannerProperties.PRIMARY_BUTTON_TEXT, primaryButtonText);
} }
@CalledByNative
void setIconResourceId(@DrawableRes int resourceId) {
mMessageProperties.set(MessageBannerProperties.ICON_RESOURCE_ID, resourceId);
}
@CalledByNative @CalledByNative
void clearNativePtr() { void clearNativePtr() {
mNativeMessageWrapper = 0; mNativeMessageWrapper = 0;
......
...@@ -48,16 +48,23 @@ public class MessageWrapperTest { ...@@ -48,16 +48,23 @@ public class MessageWrapperTest {
@SmallTest @SmallTest
public void testMessageProperties() { public void testMessageProperties() {
MessageWrapper message = MessageWrapper.create(1); MessageWrapper message = MessageWrapper.create(1);
message.setTitle("Title");
message.setDescription("Description");
message.setPrimaryButtonText("Primary button");
PropertyModel messageProperties = message.getMessageProperties(); PropertyModel messageProperties = message.getMessageProperties();
Assert.assertEquals("Title doesn't match initial value", "Title",
message.setTitle("Title");
Assert.assertEquals("Title doesn't match provided value", "Title",
messageProperties.get(MessageBannerProperties.TITLE)); messageProperties.get(MessageBannerProperties.TITLE));
Assert.assertEquals("Description doesn't match initial value", "Description",
message.setDescription("Description");
Assert.assertEquals("Description doesn't match provided value", "Description",
messageProperties.get(MessageBannerProperties.DESCRIPTION)); messageProperties.get(MessageBannerProperties.DESCRIPTION));
Assert.assertEquals("Button text doesn't match initial value", "Primary button",
message.setPrimaryButtonText("Primary button");
Assert.assertEquals("Button text doesn't match provided value", "Primary button",
messageProperties.get(MessageBannerProperties.PRIMARY_BUTTON_TEXT)); messageProperties.get(MessageBannerProperties.PRIMARY_BUTTON_TEXT));
message.setIconResourceId(1);
Assert.assertEquals("Icon resource id doesn't match provided value", 1,
messageProperties.get(MessageBannerProperties.ICON_RESOURCE_ID));
} }
/** /**
......
...@@ -50,6 +50,12 @@ void MessageWrapper::SetPrimaryButtonText( ...@@ -50,6 +50,12 @@ void MessageWrapper::SetPrimaryButtonText(
jprimary_button_text); jprimary_button_text);
} }
void MessageWrapper::SetIconResourceId(int resource_id) {
JNIEnv* env = base::android::AttachCurrentThread();
Java_MessageWrapper_setIconResourceId(env, java_message_wrapper_,
resource_id);
}
void MessageWrapper::HandleActionClick(JNIEnv* env) { void MessageWrapper::HandleActionClick(JNIEnv* env) {
if (!action_callback_.is_null()) if (!action_callback_.is_null())
std::move(action_callback_).Run(); std::move(action_callback_).Run();
......
...@@ -33,6 +33,9 @@ class MessageWrapper { ...@@ -33,6 +33,9 @@ class MessageWrapper {
void SetTitle(const base::string16& title); void SetTitle(const base::string16& title);
void SetDescription(const base::string16& description); void SetDescription(const base::string16& description);
void SetPrimaryButtonText(const base::string16& primary_button_text); void SetPrimaryButtonText(const base::string16& primary_button_text);
// When setting a message icon use ResourceMapper::MapToJavaDrawableId to
// translate from chromium resource_id to Android drawable resource_id.
void SetIconResourceId(int resource_id);
// Following methods forward calls from java to provided callbacks. // Following methods forward calls from java to provided callbacks.
void HandleActionClick(JNIEnv* env); void HandleActionClick(JNIEnv* env);
......
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