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 17package com.android.internal.widget; 18 19import android.annotation.Nullable; 20import android.content.Context; 21import android.text.BoringLayout; 22import android.text.Layout; 23import android.text.StaticLayout; 24import android.text.TextUtils; 25import android.util.AttributeSet; 26import android.view.RemotableViewMethod; 27import android.widget.RemoteViews; 28import android.widget.TextView; 29 30import com.android.internal.R; 31 32/** 33 * A TextView that can float around an image on the end. 34 * 35 * @hide 36 */ 37@RemoteViews.RemoteView 38public class ImageFloatingTextView extends TextView { 39 40 /** Number of lines from the top to indent */ 41 private int mIndentLines; 42 43 /** Resolved layout direction */ 44 private int mResolvedDirection = LAYOUT_DIRECTION_UNDEFINED; 45 46 public ImageFloatingTextView(Context context) { 47 this(context, null); 48 } 49 50 public ImageFloatingTextView(Context context, @Nullable AttributeSet attrs) { 51 this(context, attrs, 0); 52 } 53 54 public ImageFloatingTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 55 this(context, attrs, defStyleAttr, 0); 56 } 57 58 public ImageFloatingTextView(Context context, AttributeSet attrs, int defStyleAttr, 59 int defStyleRes) { 60 super(context, attrs, defStyleAttr, defStyleRes); 61 } 62 63 @Override 64 protected Layout makeSingleLayout(int wantWidth, BoringLayout.Metrics boring, int ellipsisWidth, 65 Layout.Alignment alignment, boolean shouldEllipsize, 66 TextUtils.TruncateAt effectiveEllipsize, boolean useSaved) { 67 CharSequence text = getText() == null ? "" : getText(); 68 StaticLayout.Builder builder = StaticLayout.Builder.obtain(text, 0, text.length(), 69 getPaint(), wantWidth) 70 .setAlignment(alignment) 71 .setTextDirection(getTextDirectionHeuristic()) 72 .setLineSpacing(getLineSpacingExtra(), getLineSpacingMultiplier()) 73 .setIncludePad(getIncludeFontPadding()) 74 .setEllipsize(shouldEllipsize ? effectiveEllipsize : null) 75 .setEllipsizedWidth(ellipsisWidth) 76 .setBreakStrategy(Layout.BREAK_STRATEGY_HIGH_QUALITY) 77 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_FULL); 78 // we set the endmargin on the requested number of lines. 79 int endMargin = getContext().getResources().getDimensionPixelSize( 80 R.dimen.notification_content_picture_margin); 81 int[] margins = null; 82 if (mIndentLines > 0) { 83 margins = new int[mIndentLines + 1]; 84 for (int i = 0; i < mIndentLines; i++) { 85 margins[i] = endMargin; 86 } 87 } 88 if (mResolvedDirection == LAYOUT_DIRECTION_RTL) { 89 builder.setIndents(margins, null); 90 } else { 91 builder.setIndents(null, margins); 92 } 93 94 return builder.build(); 95 } 96 97 @Override 98 public void onRtlPropertiesChanged(int layoutDirection) { 99 super.onRtlPropertiesChanged(layoutDirection); 100 101 if (layoutDirection != mResolvedDirection && isLayoutDirectionResolved()) { 102 mResolvedDirection = layoutDirection; 103 if (mIndentLines > 0) { 104 // Invalidate layout. 105 setHint(getHint()); 106 } 107 } 108 } 109 110 @RemotableViewMethod 111 public void setHasImage(boolean hasImage) { 112 setNumIndentLines(hasImage ? 2 : 0); 113 } 114 115 /** 116 * @param lines the number of lines at the top that should be indented by indentEnd 117 * @return whether a change was made 118 */ 119 public boolean setNumIndentLines(int lines) { 120 if (mIndentLines != lines) { 121 mIndentLines = lines; 122 // Invalidate layout. 123 setHint(getHint()); 124 return true; 125 } 126 return false; 127 } 128} 129