Home | History | Annotate | Download | only in widget
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License
     15  */
     16 
     17 package com.android.internal.widget;
     18 
     19 import android.annotation.Nullable;
     20 import android.content.Context;
     21 import android.text.BoringLayout;
     22 import android.text.Layout;
     23 import android.text.StaticLayout;
     24 import android.text.TextUtils;
     25 import android.text.method.TransformationMethod;
     26 import android.util.AttributeSet;
     27 import android.view.RemotableViewMethod;
     28 import android.widget.RemoteViews;
     29 import android.widget.TextView;
     30 
     31 import com.android.internal.R;
     32 
     33 /**
     34  * A TextView that can float around an image on the end.
     35  *
     36  * @hide
     37  */
     38 @RemoteViews.RemoteView
     39 public class ImageFloatingTextView extends TextView {
     40 
     41     /** Number of lines from the top to indent */
     42     private int mIndentLines;
     43 
     44     /** Resolved layout direction */
     45     private int mResolvedDirection = LAYOUT_DIRECTION_UNDEFINED;
     46     private int mMaxLinesForHeight = -1;
     47     private boolean mFirstMeasure = true;
     48     private int mLayoutMaxLines = -1;
     49     private boolean mBlockLayouts;
     50 
     51     public ImageFloatingTextView(Context context) {
     52         this(context, null);
     53     }
     54 
     55     public ImageFloatingTextView(Context context, @Nullable AttributeSet attrs) {
     56         this(context, attrs, 0);
     57     }
     58 
     59     public ImageFloatingTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
     60         this(context, attrs, defStyleAttr, 0);
     61     }
     62 
     63     public ImageFloatingTextView(Context context, AttributeSet attrs, int defStyleAttr,
     64             int defStyleRes) {
     65         super(context, attrs, defStyleAttr, defStyleRes);
     66     }
     67 
     68     @Override
     69     protected Layout makeSingleLayout(int wantWidth, BoringLayout.Metrics boring, int ellipsisWidth,
     70             Layout.Alignment alignment, boolean shouldEllipsize,
     71             TextUtils.TruncateAt effectiveEllipsize, boolean useSaved) {
     72         TransformationMethod transformationMethod = getTransformationMethod();
     73         CharSequence text = getText();
     74         if (transformationMethod != null) {
     75             text = transformationMethod.getTransformation(text, this);
     76         }
     77         text = text == null ? "" : text;
     78         StaticLayout.Builder builder = StaticLayout.Builder.obtain(text, 0, text.length(),
     79                 getPaint(), wantWidth)
     80                 .setAlignment(alignment)
     81                 .setTextDirection(getTextDirectionHeuristic())
     82                 .setLineSpacing(getLineSpacingExtra(), getLineSpacingMultiplier())
     83                 .setIncludePad(getIncludeFontPadding())
     84                 .setBreakStrategy(Layout.BREAK_STRATEGY_HIGH_QUALITY)
     85                 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_FULL);
     86         int maxLines;
     87         if (mMaxLinesForHeight > 0) {
     88             maxLines = mMaxLinesForHeight;
     89         } else {
     90             maxLines = getMaxLines() >= 0 ? getMaxLines() : Integer.MAX_VALUE;
     91         }
     92         builder.setMaxLines(maxLines);
     93         mLayoutMaxLines = maxLines;
     94         if (shouldEllipsize) {
     95             builder.setEllipsize(effectiveEllipsize)
     96                     .setEllipsizedWidth(ellipsisWidth);
     97         }
     98 
     99         // we set the endmargin on the requested number of lines.
    100         int endMargin = getContext().getResources().getDimensionPixelSize(
    101                 R.dimen.notification_content_picture_margin);
    102         int[] margins = null;
    103         if (mIndentLines > 0) {
    104             margins = new int[mIndentLines + 1];
    105             for (int i = 0; i < mIndentLines; i++) {
    106                 margins[i] = endMargin;
    107             }
    108         }
    109         if (mResolvedDirection == LAYOUT_DIRECTION_RTL) {
    110             builder.setIndents(margins, null);
    111         } else {
    112             builder.setIndents(null, margins);
    113         }
    114 
    115         return builder.build();
    116     }
    117 
    118     @Override
    119     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    120         int height = MeasureSpec.getSize(heightMeasureSpec);
    121         // Lets calculate how many lines the given measurement allows us.
    122         int availableHeight = height - mPaddingTop - mPaddingBottom;
    123         int maxLines = availableHeight / getLineHeight();
    124         maxLines = Math.max(1, maxLines);
    125         if (getMaxLines() > 0) {
    126             maxLines = Math.min(getMaxLines(), maxLines);
    127         }
    128         if (maxLines != mMaxLinesForHeight) {
    129             mMaxLinesForHeight = maxLines;
    130             if (getLayout() != null && mMaxLinesForHeight != mLayoutMaxLines) {
    131                 // Invalidate layout.
    132                 mBlockLayouts = true;
    133                 setHint(getHint());
    134                 mBlockLayouts = false;
    135             }
    136         }
    137         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    138     }
    139 
    140     @Override
    141     public void requestLayout() {
    142         if (!mBlockLayouts) {
    143             super.requestLayout();
    144         }
    145     }
    146 
    147     @Override
    148     public void onRtlPropertiesChanged(int layoutDirection) {
    149         super.onRtlPropertiesChanged(layoutDirection);
    150 
    151         if (layoutDirection != mResolvedDirection && isLayoutDirectionResolved()) {
    152             mResolvedDirection = layoutDirection;
    153             if (mIndentLines > 0) {
    154                 // Invalidate layout.
    155                 setHint(getHint());
    156             }
    157         }
    158     }
    159 
    160     @RemotableViewMethod
    161     public void setHasImage(boolean hasImage) {
    162         setNumIndentLines(hasImage ? 2 : 0);
    163     }
    164 
    165     /**
    166      * @param lines the number of lines at the top that should be indented by indentEnd
    167      * @return whether a change was made
    168      */
    169     public boolean setNumIndentLines(int lines) {
    170         if (mIndentLines != lines) {
    171             mIndentLines = lines;
    172             // Invalidate layout.
    173             setHint(getHint());
    174             return true;
    175         }
    176         return false;
    177     }
    178 
    179     public int getLayoutHeight() {
    180         return getLayout().getHeight();
    181     }
    182 }
    183