[go: nahoru, domu]

[M123][PriceDrop] Add empty stage for product image

This CL adds the empty stage placeholder for product image when the
image is not available. The implementation is very similar to the
placeholder in TabThumbnailView. Unfortunately due to dependency issues
we are not able to directly reuse this class.

Demo: https://screenshot.googleplex.com/5zT4YvsHRtRVKy6

(cherry picked from commit cdc213e367d38dfa1dd12394ec2d913478216346)

Low-Coverage-Reason: Trivial change
Bug: 325119138, 325118066
Change-Id: If4778526bff7bf080b17a4276e57b7855318263d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5315742
Commit-Queue: Yue Zhang <yuezhanggg@chromium.org>
Reviewed-by: Xi Han <hanxi@chromium.org>
Cr-Original-Commit-Position: refs/heads/main@{#1263972}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5328607
Commit-Queue: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Auto-Submit: Yue Zhang <yuezhanggg@chromium.org>
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Reviewed-by: Yue Zhang <yuezhanggg@chromium.org>
Cr-Commit-Position: refs/branch-heads/6312@{#232}
Cr-Branched-From: 6711dcdae48edaf98cbc6964f90fac85b7d9986e-refs/heads/main@{#1262506}
diff --git a/chrome/browser/commerce/price_change/android/BUILD.gn b/chrome/browser/commerce/price_change/android/BUILD.gn
index 5349613..d327740 100644
--- a/chrome/browser/commerce/price_change/android/BUILD.gn
+++ b/chrome/browser/commerce/price_change/android/BUILD.gn
@@ -12,11 +12,13 @@
     "java/src/org/chromium/chrome/browser/price_change/PriceChangeModuleProperties.java",
     "java/src/org/chromium/chrome/browser/price_change/PriceChangeModuleView.java",
     "java/src/org/chromium/chrome/browser/price_change/PriceChangeModuleViewBinder.java",
+    "java/src/org/chromium/chrome/browser/price_change/PriceChangeModuleViewUtils.java",
   ]
   deps = [
     ":java_resources",
     "//base:base_java",
     "//base:base_shared_preferences_java",
+    "//chrome/android/features/tab_ui:java_resources",
     "//chrome/browser/commerce/android:java",
     "//chrome/browser/commerce/price_tracking/android:java",
     "//chrome/browser/magic_stack/android:java",
@@ -31,6 +33,7 @@
     "//components/embedder_support/android:util_java",
     "//components/image_fetcher:java",
     "//content/public/android:content_full_java",
+    "//third_party/android_deps:material_design_java",
     "//third_party/androidx:androidx_annotation_annotation_java",
     "//third_party/androidx:androidx_appcompat_appcompat_resources_java",
     "//third_party/androidx:androidx_core_core_java",
@@ -44,6 +47,7 @@
 android_resources("java_resources") {
   sources = [
     "java/res/drawable/price_change_module_favicon_background.xml",
+    "java/res/drawable/price_change_module_image_background.xml",
     "java/res/drawable/price_change_module_price_drop_background.xml",
     "java/res/layout/price_change_module_layout.xml",
     "java/res/values/dimens.xml",
diff --git a/chrome/browser/commerce/price_change/android/java/res/drawable/price_change_module_image_background.xml b/chrome/browser/commerce/price_change/android/java/res/drawable/price_change_module_image_background.xml
new file mode 100644
index 0000000..028d0fa
--- /dev/null
+++ b/chrome/browser/commerce/price_change/android/java/res/drawable/price_change_module_image_background.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+Copyright 2024 The Chromium Authors
+Use of this source code is governed by a BSD-style license that can be
+found in the LICENSE file.
+-->
+
+<shape xmlns:android="http://schemas.android.com/apk/res/android">
+  <solid android:color="@color/thumbnail_placeholder_on_primary_bg"/>
+  <stroke
+      android:width="1dp"
+      android:color="@macro/divider_line_bg_color" />
+  <corners android:radius="@dimen/price_change_module_product_image_corner_radius" />
+</shape>
\ No newline at end of file
diff --git a/chrome/browser/commerce/price_change/android/java/res/layout/price_change_module_layout.xml b/chrome/browser/commerce/price_change/android/java/res/layout/price_change_module_layout.xml
index 78afb24..b6d1ba9 100644
--- a/chrome/browser/commerce/price_change/android/java/res/layout/price_change_module_layout.xml
+++ b/chrome/browser/commerce/price_change/android/java/res/layout/price_change_module_layout.xml
@@ -38,8 +38,9 @@
             android:layout_marginHorizontal="20dp">
             <RelativeLayout
                 android:id="@+id/image_layout"
-                android:layout_height="@dimen/price_change_module_product_image_size"
-                android:layout_width="@dimen/price_change_module_product_image_size">
+                android:layout_height="wrap_content"
+                android:layout_width="wrap_content"
+                android:background="@drawable/price_change_module_image_background">
                 <org.chromium.components.browser_ui.widget.RoundedCornerImageView
                     android:id="@+id/product_image"
                     app:cornerRadiusBottomEnd="@dimen/price_change_module_product_image_corner_radius"
@@ -48,13 +49,14 @@
                     app:cornerRadiusTopStart="@dimen/price_change_module_product_image_corner_radius"
                     android:layout_height="@dimen/price_change_module_product_image_size"
                     android:layout_width="@dimen/price_change_module_product_image_size"
+                    android:layout_margin="@dimen/price_change_module_product_image_margin"
                     android:scaleType="centerCrop" />
                 <org.chromium.ui.widget.ChromeImageView
                     android:id="@+id/favicon_image"
                     android:background="@drawable/price_change_module_favicon_background"
                     android:elevation="4dp"
-                    android:layout_alignParentEnd="true"
-                    android:layout_alignParentBottom="true"
+                    android:layout_alignBottom="@id/product_image"
+                    android:layout_alignEnd="@id/product_image"
                     android:layout_height="@dimen/price_change_module_favicon_image_size"
                     android:layout_width="@dimen/price_change_module_favicon_image_size"
                     android:layout_marginBottom="8dp"
diff --git a/chrome/browser/commerce/price_change/android/java/res/values/dimens.xml b/chrome/browser/commerce/price_change/android/java/res/values/dimens.xml
index a7ebe75..7341da6 100644
--- a/chrome/browser/commerce/price_change/android/java/res/values/dimens.xml
+++ b/chrome/browser/commerce/price_change/android/java/res/values/dimens.xml
@@ -8,8 +8,10 @@
   <!-- Price change module dimens -->
     <dimen name="price_change_module_favicon_background_radius">4dp</dimen>
     <dimen name="price_change_module_favicon_image_size">24dp</dimen>
+    <dimen name="price_change_module_image_placeholder_padding">34dp</dimen>
     <dimen name="price_change_module_margin_top">16dp</dimen>
-    <dimen name="price_change_module_product_image_size">116dp</dimen>
+    <dimen name="price_change_module_product_image_size">114dp</dimen>
+    <dimen name="price_change_module_product_image_margin">1dp</dimen>
     <dimen name="price_change_module_product_image_corner_radius">12dp</dimen>
     <dimen name="price_change_module_price_card_corner_radius">8dp</dimen>
 </resources>
\ No newline at end of file
diff --git a/chrome/browser/commerce/price_change/android/java/src/org/chromium/chrome/browser/price_change/PriceChangeModuleView.java b/chrome/browser/commerce/price_change/android/java/src/org/chromium/chrome/browser/price_change/PriceChangeModuleView.java
index 962062b3..6cf40fe 100644
--- a/chrome/browser/commerce/price_change/android/java/src/org/chromium/chrome/browser/price_change/PriceChangeModuleView.java
+++ b/chrome/browser/commerce/price_change/android/java/src/org/chromium/chrome/browser/price_change/PriceChangeModuleView.java
@@ -7,12 +7,16 @@
 import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.Paint;
+import android.graphics.PorterDuff;
+import android.graphics.drawable.Drawable;
+import android.graphics.drawable.GradientDrawable;
 import android.util.AttributeSet;
 import android.widget.FrameLayout;
 import android.widget.ImageView;
 import android.widget.TextView;
 
 import androidx.annotation.Nullable;
+import androidx.appcompat.content.res.AppCompatResources;
 
 /** View for the price change module. */
 public class PriceChangeModuleView extends FrameLayout {
@@ -53,7 +57,11 @@
     }
 
     void setProductImage(Bitmap bitmap) {
-        mProductImageView.setImageBitmap(bitmap);
+        if (bitmap != null) {
+            mProductImageView.setImageBitmap(bitmap);
+            return;
+        }
+        initializeDefaultImage();
     }
 
     void setFaviconImage(Bitmap bitmap) {
@@ -69,4 +77,27 @@
                 mPreviousPriceView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
         mPreviousPriceView.setText(price);
     }
+
+    private void initializeDefaultImage() {
+        Context context = getContext();
+        // Initialize default background.
+        GradientDrawable background = new GradientDrawable();
+        background.setColor(PriceChangeModuleViewUtils.getBackgroundColor(context));
+        background.setCornerRadius(
+                context.getResources()
+                        .getDimensionPixelSize(
+                                R.dimen.price_change_module_product_image_corner_radius));
+        mProductImageView.setBackgroundDrawable(background);
+
+        // Initialize default icon.
+        Drawable drawable = AppCompatResources.getDrawable(context, R.drawable.ic_tab_placeholder);
+        int padding =
+                context.getResources()
+                        .getDimensionPixelSize(
+                                R.dimen.price_change_module_image_placeholder_padding);
+        mProductImageView.setImageDrawable(drawable);
+        mProductImageView.setPadding(padding, padding, padding, padding);
+        mProductImageView.setColorFilter(
+                PriceChangeModuleViewUtils.getIconColor(context), PorterDuff.Mode.SRC_IN);
+    }
 }
diff --git a/chrome/browser/commerce/price_change/android/java/src/org/chromium/chrome/browser/price_change/PriceChangeModuleViewUtils.java b/chrome/browser/commerce/price_change/android/java/src/org/chromium/chrome/browser/price_change/PriceChangeModuleViewUtils.java
new file mode 100644
index 0000000..a16c194
--- /dev/null
+++ b/chrome/browser/commerce/price_change/android/java/src/org/chromium/chrome/browser/price_change/PriceChangeModuleViewUtils.java
@@ -0,0 +1,43 @@
+// Copyright 2024 The Chromium Authors
+// 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.price_change;
+
+import android.content.Context;
+import android.content.res.TypedArray;
+import android.graphics.Color;
+
+import androidx.annotation.ColorInt;
+import androidx.annotation.StyleRes;
+
+import com.google.android.material.color.MaterialColors;
+import com.google.android.material.elevation.ElevationOverlayProvider;
+
+/** Utility class that provides theme related attributes for price change UI. */
+public class PriceChangeModuleViewUtils {
+    static @ColorInt int getBackgroundColor(Context context) {
+        int alpha =
+                context.getResources().getInteger(R.integer.tab_thumbnail_placeholder_color_alpha);
+        @StyleRes int styleRes = R.style.TabThumbnailPlaceholderStyle;
+        TypedArray ta =
+                context.obtainStyledAttributes(styleRes, R.styleable.TabThumbnailPlaceholder);
+        @ColorInt
+        int baseColor =
+                ta.getColor(R.styleable.TabThumbnailPlaceholder_colorTileBase, Color.TRANSPARENT);
+        float tileSurfaceElevation =
+                ta.getDimension(R.styleable.TabThumbnailPlaceholder_elevationTileBase, 0);
+        ta.recycle();
+        if (tileSurfaceElevation != 0) {
+            ElevationOverlayProvider eop = new ElevationOverlayProvider(context);
+            baseColor = eop.compositeOverlay(baseColor, tileSurfaceElevation);
+        }
+        return MaterialColors.compositeARGBWithAlpha(baseColor, alpha);
+    }
+
+    static @ColorInt int getIconColor(Context context) {
+        float tabElevation = context.getResources().getDimension(R.dimen.tab_bg_elevation);
+        return new ElevationOverlayProvider(context)
+                .compositeOverlayWithThemeSurfaceColorIfNeeded(tabElevation);
+    }
+}