Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: chrome/android/java_staging/src/org/chromium/chrome/browser/snackbar/TemplatePreservingTextView.java

Issue 1141283003: Upstream oodles of Chrome for Android code into Chromium. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: final patch? Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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.snackbar;
6
7 import android.content.Context;
8 import android.support.v7.widget.AppCompatTextView;
9 import android.text.TextPaint;
10 import android.text.TextUtils;
11 import android.text.TextUtils.TruncateAt;
12 import android.util.AttributeSet;
13 import android.widget.TextView;
14
15 /**
16 * A {@link AppCompatTextView} that properly clips content within a template, in stead of clipping
17 * the templated content.
18 */
19 public class TemplatePreservingTextView extends AppCompatTextView {
20 private static final String DEFAULT_TEMPLATE = "%1$s";
21 private String mTemplate;
22 private CharSequence mContent;
23
24 /**
25 * Builds an instance of an {@link TemplatePreservingTextView}.
26 * @param context A {@link Context} instance to build this {@link TextView} in.
27 * @param attrs An {@link AttributeSet} instance.
28 */
29 public TemplatePreservingTextView(Context context, AttributeSet attrs) {
30 super(context, attrs);
31 }
32
33 /**
34 * Update template for undo text
35 * @param template Template format string (eg. "Close %s")
36 */
37 public void setTemplate(String template) {
38 mTemplate = TextUtils.isEmpty(template) ? DEFAULT_TEMPLATE : template;
39 }
40
41 /**
42 * This will take {@code text} and apply it to the internal template, buildi ng a new
43 * {@link String} to set. This {code text} will be automatically truncated to fit within
44 * the template as best as possible, making sure the template does not get c lipped.
45 */
46 @Override
47 public void setText(CharSequence text, BufferType type) {
48 final int availWidth = getWidth() - getPaddingLeft() - getPaddingRight() ;
49 setClippedText(text, availWidth);
50 }
51
52 @Override
53 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
54 final int availWidth =
55 MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPa ddingRight();
56 setClippedText(mContent, availWidth);
57
58 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
59 }
60
61 private void setClippedText(CharSequence text, int availWidth) {
62 mContent = text != null ? text : "";
63
64 if (mTemplate == null) {
65 super.setText(mContent, BufferType.SPANNABLE);
66 return;
67 }
68
69 final TextPaint paint = getPaint();
70
71 // Calculate the width the template takes.
72 final String emptyTemplate = String.format(mTemplate, "");
73 final float emptyTemplateWidth = paint.measureText(emptyTemplate);
74
75 // Calculate the available width for the content.
76 final float contentWidth = Math.max(availWidth - emptyTemplateWidth, 0.f );
77
78 // Ellipsize the content to the available width.
79 CharSequence clipped = TextUtils.ellipsize(mContent, paint, contentWidth , TruncateAt.END);
80
81 // Build the full string, which should fit within availWidth.
82 String finalContent = String.format(mTemplate, clipped);
83
84 // BufferType.SPANNABLE is required so that TextView.getIterableTextForA ccessibility()
85 // doesn't call our custom setText(). See crbug.com/449311
86 super.setText(finalContent, BufferType.SPANNABLE);
87
88 // Set the content description to the non-ellipsized text
89 setContentDescription(String.format(mTemplate, mContent));
90 }
91 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698