Index: chrome/android/java/src/org/chromium/chrome/browser/payments/ui/SectionUiUtils.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/payments/ui/SectionUiUtils.java b/chrome/android/java/src/org/chromium/chrome/browser/payments/ui/SectionUiUtils.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2980cc7095ab37fa98a42c5111e8a0b64da5d376 |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/payments/ui/SectionUiUtils.java |
@@ -0,0 +1,104 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// 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.payments.ui; |
+ |
+import android.content.Context; |
+import android.text.Layout; |
+import android.text.TextPaint; |
+import android.view.View; |
+import android.widget.TextView; |
+ |
+import org.chromium.chrome.R; |
+ |
+import javax.annotation.Nullable; |
+ |
+/** |
+ * Utility functions for PaymentRequestSection. |
+ * This class is not supposed to be instantiated. |
+ */ |
+public class SectionUiUtils { |
+ /** Avoid instantiation by accident. */ |
+ private SectionUiUtils() {} |
+ |
+ /** |
+ * Show the section summary in the view in a single line. This is called when there is no |
+ * selected item in the section. |
+ * |
+ * @param context The context. |
+ * @param section The section to summarize. |
+ * @param view The view to display the summary. |
+ */ |
+ public static void showSectionSummaryInTextViewInSingeLine( |
+ final Context context, final SectionInformation section, final TextView view) { |
+ int optionCount = section.getSize(); |
+ if (optionCount == 0) { |
+ view.setText(null); |
+ return; |
+ } |
+ |
+ // Listen for layout event to check whether the string can be fit in a single line and |
+ // manually elide the string in the middle if necessary. |
+ if (view.getLayout() == null && optionCount > 1) { |
+ view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { |
+ @Override |
+ public void onLayoutChange(View v, int left, int top, int right, int bottom, |
+ int oldLeft, int oldTop, int oldRight, int oldBottom) { |
+ if (section.getSelectedItem() != null) { |
+ view.removeOnLayoutChangeListener(this); |
+ return; |
+ } |
+ |
+ Layout layout = view.getLayout(); |
+ if (layout.getEllipsisCount(0) > 0) { |
+ String summary = getSectionSummaryForPreviewInASingleLine( |
+ context, section, layout, view.getPaint()); |
+ view.setText(summary); |
+ } |
+ } |
+ }); |
+ } |
+ |
+ String summary = SectionUiUtils.getSectionSummaryForPreviewInASingleLine( |
+ context, section, view.getLayout(), view.getPaint()); |
+ view.setText(summary); |
+ } |
+ |
+ private static String getSectionSummaryForPreviewInASingleLine(Context context, |
+ SectionInformation section, @Nullable Layout layout, @Nullable TextPaint paint) { |
+ int optionCount = section.getSize(); |
+ assert optionCount != 0; |
+ |
+ // If there is only one option in the section, return the summary of that option in a single |
+ // line, let the TextView automatically elide it at the end. |
+ // If there are more than one options in the section, then the returned string pattern is |
gone
2017/04/10 23:19:03
Move lines 75-77 to line 85. Closer to what it ac
gogerald1
2017/04/11 00:20:28
Done.
|
+ // "<summary of the first option>... and N more". N+1 is the total number of options in the |
+ // section. If layout or paint is null, return the summary string without shrink. Otherwise, |
gone
2017/04/10 23:19:03
nit:
without shrinking.
or
return the full summary
gogerald1
2017/04/11 00:20:28
Done.
|
+ // check and shrink "<summary of the first option>" so as to make the entire summary string |
+ // fits in a single line. |
+ PaymentOption option = section.getItem(0); |
+ String labelSeparator = context.getString(R.string.autofill_address_summary_separator); |
+ String optionSummary = option.getPreviewString(labelSeparator, -1); |
+ int moreOptionCount = optionCount - 1; |
+ if (moreOptionCount == 0) return optionSummary; |
+ |
+ // Append string to indicate how many more options in the given section. |
+ int resId = section.getPreviewStringResourceId(); |
+ assert resId > 0; |
+ String summary = context.getResources().getQuantityString( |
+ resId, moreOptionCount, optionSummary, moreOptionCount); |
+ if (paint == null || layout == null) { |
+ return summary; |
+ } |
+ |
+ int ellipsizedWidth = layout.getEllipsizedWidth(); |
+ while (Layout.getDesiredWidth(summary, paint) > ellipsizedWidth) { |
gone
2017/04/10 23:19:03
Explain why this while loop terminates. There's n
gogerald1
2017/04/11 00:20:28
Done.
|
+ optionSummary = option.getPreviewString(labelSeparator, optionSummary.length()); |
+ summary = context.getResources().getQuantityString( |
+ resId, moreOptionCount, optionSummary, moreOptionCount); |
+ } |
+ |
+ return summary; |
+ } |
+} |