1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.chrome.browser.payments.ui;
6 
7 import android.content.Context;
8 import android.graphics.Bitmap;
9 import android.text.Spannable;
10 import android.text.SpannableStringBuilder;
11 import android.util.AttributeSet;
12 import android.widget.FrameLayout;
13 import android.widget.ImageView;
14 import android.widget.TextView;
15 
16 import androidx.annotation.ColorInt;
17 
18 import org.chromium.base.ApiCompatibilityUtils;
19 import org.chromium.chrome.R;
20 import org.chromium.chrome.browser.omnibox.ChromeAutocompleteSchemeClassifier;
21 import org.chromium.chrome.browser.profiles.Profile;
22 import org.chromium.components.browser_ui.widget.TintedDrawable;
23 import org.chromium.components.embedder_support.util.UrlConstants;
24 import org.chromium.components.omnibox.OmniboxUrlEmphasizer;
25 import org.chromium.ui.util.ColorUtils;
26 
27 /** This class represents a bar to display at the top of the payment request UI. */
28 public class PaymentRequestHeader extends FrameLayout {
29     private final @ColorInt int mBackgroundColor;
30     private Context mContext;
31 
32     /** Constructor for when the PaymentRequestHeader is inflated from XML. */
PaymentRequestHeader(Context context, AttributeSet attrs)33     public PaymentRequestHeader(Context context, AttributeSet attrs) {
34         super(context, attrs);
35         mContext = context;
36         mBackgroundColor =
37                 ApiCompatibilityUtils.getColor(getResources(), R.color.payment_request_bg);
38     }
39 
40     @Override
onFinishInflate()41     protected void onFinishInflate() {
42         super.onFinishInflate();
43         setBackgroundColor(mBackgroundColor);
44     }
45 
46     /**
47      * Sets the bitmap of the icon on the left of the header.
48      *
49      * @param bitmap The bitmap to display.
50      */
setTitleBitmap(Bitmap bitmap)51     public void setTitleBitmap(Bitmap bitmap) {
52         ((ImageView) findViewById(R.id.icon_view)).setImageBitmap(bitmap);
53     }
54 
55     /**
56      * Sets the title and origin on the header.
57      *
58      * @param title         The title to display on the header.
59      * @param origin        The origin to display on the header.
60      * @param securityLevel The security level of the page that invoked PaymentRequest.
61      * @param profile       The current profile to initialize ChromeAutocompleteSchemeClassifier.
62      */
setTitleAndOrigin(String title, String origin, int securityLevel, Profile profile)63     public void setTitleAndOrigin(String title, String origin, int securityLevel, Profile profile) {
64         ((TextView) findViewById(R.id.page_title)).setText(title);
65 
66         TextView hostName = (TextView) findViewById(R.id.hostname);
67         Spannable url = new SpannableStringBuilder(origin);
68         final boolean useDarkColors =
69                 !ColorUtils.shouldUseLightForegroundOnBackground(mBackgroundColor);
70         ChromeAutocompleteSchemeClassifier chromeAutocompleteSchemeClassifier =
71                 new ChromeAutocompleteSchemeClassifier(profile);
72         OmniboxUrlEmphasizer.emphasizeUrl(url, mContext.getResources(),
73                 chromeAutocompleteSchemeClassifier, securityLevel, false /* isInternalPage */,
74                 useDarkColors, true /* emphasizeHttpsScheme */);
75         chromeAutocompleteSchemeClassifier.destroy();
76         hostName.setText(url);
77 
78         if (origin.startsWith(UrlConstants.HTTPS_URL_PREFIX)) {
79             // Add a lock icon.
80             hostName.setCompoundDrawablesRelativeWithIntrinsicBounds(
81                     TintedDrawable.constructTintedDrawable(
82                             mContext, R.drawable.omnibox_https_valid, R.color.default_green),
83                     null, null, null);
84 
85             // Remove left padding to align left compound drawable with the title. Note that the
86             // left compound drawable has transparent boundary.
87             hostName.setPaddingRelative(0, 0, 0, 0);
88         }
89     }
90 }
91