Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 19 | import android.annotation.Nullable; |
| 20 | import android.content.Context; |
| 21 | import android.content.res.TypedArray; |
| 22 | import android.graphics.Canvas; |
| 23 | import android.util.AttributeSet; |
| 24 | import android.view.RemotableViewMethod; |
| 25 | import android.view.View; |
| 26 | import android.view.ViewGroup; |
| 27 | import android.widget.RemoteViews; |
| 28 | |
Selim Cinek | 9281e5e | 2017-03-08 17:29:55 -0800 | [diff] [blame] | 29 | import com.android.internal.R; |
| 30 | |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 31 | /** |
| 32 | * A custom-built layout for the Notification.MessagingStyle. |
| 33 | * |
| 34 | * Evicts children until they all fit. |
| 35 | */ |
| 36 | @RemoteViews.RemoteView |
| 37 | public class MessagingLinearLayout extends ViewGroup { |
| 38 | |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame] | 39 | private static final int NOT_MEASURED_BEFORE = -1; |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Spacing to be applied between views. |
| 42 | */ |
| 43 | private int mSpacing; |
| 44 | |
Selim Cinek | e62255c | 2017-09-28 18:23:23 -0700 | [diff] [blame^] | 45 | private int mMaxDisplayedLines = Integer.MAX_VALUE; |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 46 | |
Adrian Roos | feafa05 | 2016-06-01 17:09:45 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Id of the child that's also visible in the contracted layout. |
| 49 | */ |
| 50 | private int mContractedChildId; |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame] | 51 | /** |
| 52 | * The last measured with in a layout pass if it was measured before or |
| 53 | * {@link #NOT_MEASURED_BEFORE} if this is the first layout pass. |
| 54 | */ |
| 55 | private int mLastMeasuredWidth = NOT_MEASURED_BEFORE; |
Adrian Roos | feafa05 | 2016-06-01 17:09:45 -0700 | [diff] [blame] | 56 | |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 57 | public MessagingLinearLayout(Context context, @Nullable AttributeSet attrs) { |
| 58 | super(context, attrs); |
| 59 | |
| 60 | final TypedArray a = context.obtainStyledAttributes(attrs, |
| 61 | R.styleable.MessagingLinearLayout, 0, |
| 62 | 0); |
| 63 | |
| 64 | final int N = a.getIndexCount(); |
| 65 | for (int i = 0; i < N; i++) { |
| 66 | int attr = a.getIndex(i); |
| 67 | switch (attr) { |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 68 | case R.styleable.MessagingLinearLayout_spacing: |
| 69 | mSpacing = a.getDimensionPixelSize(i, 0); |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 74 | a.recycle(); |
| 75 | } |
| 76 | |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 77 | @Override |
| 78 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 79 | // This is essentially a bottom-up linear layout that only adds children that fit entirely |
| 80 | // up to a maximum height. |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame] | 81 | int targetHeight = MeasureSpec.getSize(heightMeasureSpec); |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 82 | switch (MeasureSpec.getMode(heightMeasureSpec)) { |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 83 | case MeasureSpec.UNSPECIFIED: |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame] | 84 | targetHeight = Integer.MAX_VALUE; |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 85 | break; |
| 86 | } |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame] | 87 | int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
| 88 | boolean recalculateVisibility = mLastMeasuredWidth == NOT_MEASURED_BEFORE |
| 89 | || getMeasuredHeight() != targetHeight |
| 90 | || mLastMeasuredWidth != widthSize; |
| 91 | |
Selim Cinek | 88188f2 | 2017-09-19 16:46:56 -0700 | [diff] [blame] | 92 | // Now that we know which views to take, fix up the indents and see what width we get. |
| 93 | int measuredWidth = mPaddingLeft + mPaddingRight; |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 94 | final int count = getChildCount(); |
Selim Cinek | 88188f2 | 2017-09-19 16:46:56 -0700 | [diff] [blame] | 95 | int totalHeight = getMeasuredHeight(); |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame] | 96 | if (recalculateVisibility) { |
| 97 | // We only need to recalculate the view visibilities if the view wasn't measured already |
| 98 | // in this pass, otherwise we may drop messages here already since we are measured |
| 99 | // exactly with what we returned before, which was optimized already with the |
| 100 | // line-indents. |
| 101 | for (int i = 0; i < count; ++i) { |
| 102 | final View child = getChildAt(i); |
| 103 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 104 | lp.hide = true; |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Selim Cinek | 88188f2 | 2017-09-19 16:46:56 -0700 | [diff] [blame] | 107 | totalHeight = mPaddingTop + mPaddingBottom; |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame] | 108 | boolean first = true; |
Selim Cinek | e62255c | 2017-09-28 18:23:23 -0700 | [diff] [blame^] | 109 | int linesRemaining = mMaxDisplayedLines; |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 110 | |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame] | 111 | // Starting from the bottom: we measure every view as if it were the only one. If it still |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 112 | |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame] | 113 | // fits, we take it, otherwise we stop there. |
| 114 | for (int i = count - 1; i >= 0 && totalHeight < targetHeight; i--) { |
| 115 | if (getChildAt(i).getVisibility() == GONE) { |
| 116 | continue; |
| 117 | } |
| 118 | final View child = getChildAt(i); |
| 119 | LayoutParams lp = (LayoutParams) getChildAt(i).getLayoutParams(); |
Selim Cinek | e62255c | 2017-09-28 18:23:23 -0700 | [diff] [blame^] | 120 | MessagingChild messagingChild = null; |
| 121 | if (child instanceof MessagingChild) { |
| 122 | messagingChild = (MessagingChild) child; |
| 123 | messagingChild.setMaxDisplayedLines(linesRemaining); |
| 124 | } |
Selim Cinek | 9281e5e | 2017-03-08 17:29:55 -0800 | [diff] [blame] | 125 | int spacing = first ? 0 : mSpacing; |
| 126 | measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, totalHeight |
| 127 | - mPaddingTop - mPaddingBottom + spacing); |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame] | 128 | |
| 129 | final int childHeight = child.getMeasuredHeight(); |
| 130 | int newHeight = Math.max(totalHeight, totalHeight + childHeight + lp.topMargin + |
Selim Cinek | 9281e5e | 2017-03-08 17:29:55 -0800 | [diff] [blame] | 131 | lp.bottomMargin + spacing); |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame] | 132 | first = false; |
Selim Cinek | 88188f2 | 2017-09-19 16:46:56 -0700 | [diff] [blame] | 133 | int measureType = MessagingChild.MEASURED_NORMAL; |
Selim Cinek | e62255c | 2017-09-28 18:23:23 -0700 | [diff] [blame^] | 134 | if (messagingChild != null) { |
| 135 | measureType = messagingChild.getMeasuredType(); |
Selim Cinek | 88188f2 | 2017-09-19 16:46:56 -0700 | [diff] [blame] | 136 | linesRemaining -= messagingChild.getConsumedLines(); |
Selim Cinek | 9281e5e | 2017-03-08 17:29:55 -0800 | [diff] [blame] | 137 | } |
Selim Cinek | 88188f2 | 2017-09-19 16:46:56 -0700 | [diff] [blame] | 138 | boolean isShortened = measureType == MessagingChild.MEASURED_SHORTENED; |
| 139 | boolean isTooSmall = measureType == MessagingChild.MEASURED_TOO_SMALL; |
| 140 | if (newHeight <= targetHeight && !isTooSmall) { |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame] | 141 | totalHeight = newHeight; |
Selim Cinek | 88188f2 | 2017-09-19 16:46:56 -0700 | [diff] [blame] | 142 | measuredWidth = Math.max(measuredWidth, |
| 143 | child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin |
| 144 | + mPaddingLeft + mPaddingRight); |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame] | 145 | lp.hide = false; |
Selim Cinek | e62255c | 2017-09-28 18:23:23 -0700 | [diff] [blame^] | 146 | if (isShortened || linesRemaining <= 0) { |
Selim Cinek | 88188f2 | 2017-09-19 16:46:56 -0700 | [diff] [blame] | 147 | break; |
| 148 | } |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame] | 149 | } else { |
| 150 | break; |
| 151 | } |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 155 | setMeasuredDimension( |
| 156 | resolveSize(Math.max(getSuggestedMinimumWidth(), measuredWidth), |
| 157 | widthMeasureSpec), |
| 158 | resolveSize(Math.max(getSuggestedMinimumHeight(), totalHeight), |
| 159 | heightMeasureSpec)); |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame] | 160 | mLastMeasuredWidth = widthSize; |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Override |
| 164 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 165 | final int paddingLeft = mPaddingLeft; |
| 166 | |
| 167 | int childTop; |
| 168 | |
| 169 | // Where right end of child should go |
| 170 | final int width = right - left; |
| 171 | final int childRight = width - mPaddingRight; |
| 172 | |
| 173 | final int layoutDirection = getLayoutDirection(); |
| 174 | final int count = getChildCount(); |
| 175 | |
| 176 | childTop = mPaddingTop; |
| 177 | |
| 178 | boolean first = true; |
| 179 | |
| 180 | for (int i = 0; i < count; i++) { |
| 181 | final View child = getChildAt(i); |
| 182 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 183 | |
| 184 | if (child.getVisibility() == GONE || lp.hide) { |
| 185 | continue; |
| 186 | } |
| 187 | |
| 188 | final int childWidth = child.getMeasuredWidth(); |
| 189 | final int childHeight = child.getMeasuredHeight(); |
| 190 | |
| 191 | int childLeft; |
| 192 | if (layoutDirection == LAYOUT_DIRECTION_RTL) { |
| 193 | childLeft = childRight - childWidth - lp.rightMargin; |
| 194 | } else { |
| 195 | childLeft = paddingLeft + lp.leftMargin; |
| 196 | } |
| 197 | |
| 198 | if (!first) { |
| 199 | childTop += mSpacing; |
| 200 | } |
| 201 | |
| 202 | childTop += lp.topMargin; |
| 203 | child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); |
| 204 | |
| 205 | childTop += childHeight + lp.bottomMargin; |
| 206 | |
| 207 | first = false; |
| 208 | } |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame] | 209 | mLastMeasuredWidth = NOT_MEASURED_BEFORE; |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | @Override |
| 213 | protected boolean drawChild(Canvas canvas, View child, long drawingTime) { |
| 214 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 215 | if (lp.hide) { |
| 216 | return true; |
| 217 | } |
| 218 | return super.drawChild(canvas, child, drawingTime); |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 223 | return new LayoutParams(mContext, attrs); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | protected LayoutParams generateDefaultLayoutParams() { |
| 228 | return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); |
| 229 | |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) { |
| 234 | LayoutParams copy = new LayoutParams(lp.width, lp.height); |
| 235 | if (lp instanceof MarginLayoutParams) { |
| 236 | copy.copyMarginsFrom((MarginLayoutParams) lp); |
| 237 | } |
| 238 | return copy; |
| 239 | } |
| 240 | |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 241 | /** |
Selim Cinek | e62255c | 2017-09-28 18:23:23 -0700 | [diff] [blame^] | 242 | * Sets how many lines should be displayed at most |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 243 | */ |
Adrian Roos | feafa05 | 2016-06-01 17:09:45 -0700 | [diff] [blame] | 244 | @RemotableViewMethod |
Selim Cinek | e62255c | 2017-09-28 18:23:23 -0700 | [diff] [blame^] | 245 | public void setMaxDisplayedLines(int numberLines) { |
| 246 | mMaxDisplayedLines = numberLines; |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Selim Cinek | 88188f2 | 2017-09-19 16:46:56 -0700 | [diff] [blame] | 249 | public interface MessagingChild { |
| 250 | int MEASURED_NORMAL = 0; |
| 251 | int MEASURED_SHORTENED = 1; |
| 252 | int MEASURED_TOO_SMALL = 2; |
Selim Cinek | e62255c | 2017-09-28 18:23:23 -0700 | [diff] [blame^] | 253 | |
Selim Cinek | 88188f2 | 2017-09-19 16:46:56 -0700 | [diff] [blame] | 254 | int getMeasuredType(); |
| 255 | int getConsumedLines(); |
Selim Cinek | e62255c | 2017-09-28 18:23:23 -0700 | [diff] [blame^] | 256 | void setMaxDisplayedLines(int lines); |
Adrian Roos | feafa05 | 2016-06-01 17:09:45 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 259 | public static class LayoutParams extends MarginLayoutParams { |
| 260 | |
| 261 | boolean hide = false; |
| 262 | |
| 263 | public LayoutParams(Context c, AttributeSet attrs) { |
| 264 | super(c, attrs); |
| 265 | } |
| 266 | |
| 267 | public LayoutParams(int width, int height) { |
| 268 | super(width, height); |
| 269 | } |
| 270 | } |
| 271 | } |