Selim Cinek | 4fb12d3 | 2015-11-19 18:10:48 -0800 | [diff] [blame] | 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.util.AttributeSet; |
| 26 | import android.view.RemotableViewMethod; |
| 27 | import android.widget.RemoteViews; |
| 28 | import android.widget.TextView; |
| 29 | |
Selim Cinek | 3787868 | 2016-05-02 13:40:58 -0700 | [diff] [blame] | 30 | import com.android.internal.R; |
| 31 | |
Selim Cinek | 4fb12d3 | 2015-11-19 18:10:48 -0800 | [diff] [blame] | 32 | /** |
| 33 | * A TextView that can float around an image on the end. |
| 34 | * |
| 35 | * @hide |
| 36 | */ |
| 37 | @RemoteViews.RemoteView |
| 38 | public class ImageFloatingTextView extends TextView { |
| 39 | |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 40 | /** Number of lines from the top to indent */ |
| 41 | private int mIndentLines; |
Selim Cinek | 4fb12d3 | 2015-11-19 18:10:48 -0800 | [diff] [blame] | 42 | |
dongwan0605.kim | dd8611f | 2016-09-16 16:26:57 +0900 | [diff] [blame] | 43 | /** Resolved layout direction */ |
| 44 | private int mResolvedDirection = LAYOUT_DIRECTION_UNDEFINED; |
| 45 | |
Selim Cinek | 4fb12d3 | 2015-11-19 18:10:48 -0800 | [diff] [blame] | 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()) |
Selim Cinek | 4fb12d3 | 2015-11-19 18:10:48 -0800 | [diff] [blame] | 74 | .setBreakStrategy(Layout.BREAK_STRATEGY_HIGH_QUALITY) |
Adrian Roos | 62be439 | 2016-12-05 13:59:52 -0800 | [diff] [blame] | 75 | .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_FULL) |
| 76 | .setMaxLines(getMaxLines() >= 0 ? getMaxLines() : Integer.MAX_VALUE); |
| 77 | if (shouldEllipsize) { |
| 78 | builder.setEllipsize(effectiveEllipsize) |
| 79 | .setEllipsizedWidth(ellipsisWidth); |
| 80 | } |
| 81 | |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 82 | // we set the endmargin on the requested number of lines. |
| 83 | int endMargin = getContext().getResources().getDimensionPixelSize( |
| 84 | R.dimen.notification_content_picture_margin); |
| 85 | int[] margins = null; |
| 86 | if (mIndentLines > 0) { |
| 87 | margins = new int[mIndentLines + 1]; |
| 88 | for (int i = 0; i < mIndentLines; i++) { |
| 89 | margins[i] = endMargin; |
| 90 | } |
| 91 | } |
dongwan0605.kim | dd8611f | 2016-09-16 16:26:57 +0900 | [diff] [blame] | 92 | if (mResolvedDirection == LAYOUT_DIRECTION_RTL) { |
Selim Cinek | 4fb12d3 | 2015-11-19 18:10:48 -0800 | [diff] [blame] | 93 | builder.setIndents(margins, null); |
| 94 | } else { |
| 95 | builder.setIndents(null, margins); |
| 96 | } |
| 97 | |
| 98 | return builder.build(); |
| 99 | } |
| 100 | |
dongwan0605.kim | dd8611f | 2016-09-16 16:26:57 +0900 | [diff] [blame] | 101 | @Override |
| 102 | public void onRtlPropertiesChanged(int layoutDirection) { |
| 103 | super.onRtlPropertiesChanged(layoutDirection); |
| 104 | |
| 105 | if (layoutDirection != mResolvedDirection && isLayoutDirectionResolved()) { |
| 106 | mResolvedDirection = layoutDirection; |
| 107 | if (mIndentLines > 0) { |
| 108 | // Invalidate layout. |
| 109 | setHint(getHint()); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
Selim Cinek | 4fb12d3 | 2015-11-19 18:10:48 -0800 | [diff] [blame] | 114 | @RemotableViewMethod |
| 115 | public void setHasImage(boolean hasImage) { |
Selim Cinek | 3787868 | 2016-05-02 13:40:58 -0700 | [diff] [blame] | 116 | setNumIndentLines(hasImage ? 2 : 0); |
Selim Cinek | 4fb12d3 | 2015-11-19 18:10:48 -0800 | [diff] [blame] | 117 | } |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 118 | |
| 119 | /** |
| 120 | * @param lines the number of lines at the top that should be indented by indentEnd |
| 121 | * @return whether a change was made |
| 122 | */ |
| 123 | public boolean setNumIndentLines(int lines) { |
| 124 | if (mIndentLines != lines) { |
| 125 | mIndentLines = lines; |
| 126 | // Invalidate layout. |
| 127 | setHint(getHint()); |
| 128 | return true; |
| 129 | } |
| 130 | return false; |
| 131 | } |
Selim Cinek | 4fb12d3 | 2015-11-19 18:10:48 -0800 | [diff] [blame] | 132 | } |