OLD | NEW |
(Empty) | |
| 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.util.AttributeSet; |
| 9 import android.view.View; |
| 10 import android.view.View.MeasureSpec; |
| 11 import android.view.ViewGroup.MarginLayoutParams; |
| 12 import android.widget.LinearLayout; |
| 13 |
| 14 import org.chromium.chrome.R; |
| 15 |
| 16 /** This class represents a bar to display at the bottom of the payment request
UI. */ |
| 17 public class PaymentRequestBottomBar extends LinearLayout { |
| 18 private View mLogoWithName; |
| 19 private View mLogo; |
| 20 private View mPrimaryButton; |
| 21 private View mSecondaryButton; |
| 22 private View mSpace; |
| 23 |
| 24 /** Constructor for when the PaymentRequestBottomBar is inflated from XML. *
/ |
| 25 public PaymentRequestBottomBar(Context context, AttributeSet attrs) { |
| 26 super(context, attrs); |
| 27 } |
| 28 |
| 29 @Override |
| 30 protected void onFinishInflate() { |
| 31 super.onFinishInflate(); |
| 32 |
| 33 mLogoWithName = findViewById(R.id.logo_name); |
| 34 mLogo = findViewById(R.id.logo); |
| 35 mPrimaryButton = findViewById(R.id.button_primary); |
| 36 mSecondaryButton = findViewById(R.id.button_secondary); |
| 37 mSpace = findViewById(R.id.space); |
| 38 } |
| 39 |
| 40 @Override |
| 41 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 42 // Views layout_width must be set to match_parent. |
| 43 assert MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY; |
| 44 |
| 45 measureChild(mLogoWithName, widthMeasureSpec, heightMeasureSpec); |
| 46 int logoWithNameWidth = mLogoWithName.getMeasuredWidth(); |
| 47 |
| 48 measureChild(mLogo, widthMeasureSpec, heightMeasureSpec); |
| 49 int logoWidth = mLogo.getMeasuredWidth(); |
| 50 |
| 51 measureChild(mPrimaryButton, widthMeasureSpec, heightMeasureSpec); |
| 52 int primaryButtonWidth = mPrimaryButton.getMeasuredWidth(); |
| 53 |
| 54 measureChild(mSecondaryButton, widthMeasureSpec, heightMeasureSpec); |
| 55 int secondaryButtonWidth = mSecondaryButton.getMeasuredWidth(); |
| 56 |
| 57 MarginLayoutParams layoutParams = (MarginLayoutParams) getLayoutParams()
; |
| 58 int totalWidthWithoutLogo = primaryButtonWidth + secondaryButtonWidth +
getPaddingLeft() |
| 59 + getPaddingRight() + layoutParams.leftMargin + layoutParams.rig
htMargin; |
| 60 |
| 61 int blankSpaceWidth; |
| 62 int maxTotalWidth = MeasureSpec.getSize(widthMeasureSpec); |
| 63 if (totalWidthWithoutLogo + logoWithNameWidth <= maxTotalWidth) { |
| 64 mLogo.setVisibility(View.GONE); |
| 65 mLogoWithName.setVisibility(View.VISIBLE); |
| 66 blankSpaceWidth = maxTotalWidth - totalWidthWithoutLogo - logoWithNa
meWidth; |
| 67 } else if (totalWidthWithoutLogo + logoWidth <= maxTotalWidth) { |
| 68 mLogo.setVisibility(View.VISIBLE); |
| 69 mLogoWithName.setVisibility(View.GONE); |
| 70 blankSpaceWidth = maxTotalWidth - totalWidthWithoutLogo - logoWidth; |
| 71 } else { |
| 72 mLogo.setVisibility(View.GONE); |
| 73 mLogoWithName.setVisibility(View.GONE); |
| 74 blankSpaceWidth = maxTotalWidth < totalWidthWithoutLogo |
| 75 ? 0 |
| 76 : maxTotalWidth - totalWidthWithoutLogo; |
| 77 assert maxTotalWidth >= totalWidthWithoutLogo |
| 78 : "Screen width is expected to fit the two buttons at least.
"; |
| 79 } |
| 80 |
| 81 // Sets the blank space width. |
| 82 mSpace.getLayoutParams().width = blankSpaceWidth; |
| 83 measureChild(mSpace, MeasureSpec.makeMeasureSpec(blankSpaceWidth, Measur
eSpec.EXACTLY), |
| 84 heightMeasureSpec); |
| 85 |
| 86 // Note that mLogoWithName and mLogo must have the same height. |
| 87 int maxMeasuredHeight = Math.max(mLogoWithName.getMeasuredHeight(), |
| 88 Math.max(mPrimaryButton.getMeasuredHeight(), mSecondaryButton.ge
tMeasuredHeight())); |
| 89 int totalHeightWithPadding = maxMeasuredHeight + getPaddingTop() + getPa
ddingBottom() |
| 90 + layoutParams.topMargin + layoutParams.bottomMargin; |
| 91 int measuredHeightSpec = |
| 92 MeasureSpec.makeMeasureSpec(totalHeightWithPadding, MeasureSpec.
EXACTLY); |
| 93 setMeasuredDimension(widthMeasureSpec, measuredHeightSpec); |
| 94 } |
| 95 } |
OLD | NEW |