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 | |
| 19 | import com.android.internal.R; |
| 20 | |
| 21 | import android.annotation.Nullable; |
| 22 | import android.content.Context; |
| 23 | import android.content.res.TypedArray; |
| 24 | import android.graphics.Canvas; |
| 25 | import android.util.AttributeSet; |
| 26 | import android.view.RemotableViewMethod; |
| 27 | import android.view.View; |
| 28 | import android.view.ViewGroup; |
| 29 | import android.widget.RemoteViews; |
| 30 | |
| 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 | |
| 45 | /** |
| 46 | * The maximum height allowed. |
| 47 | */ |
| 48 | private int mMaxHeight; |
| 49 | |
| 50 | private int mIndentLines; |
| 51 | |
Adrian Roos | feafa05 | 2016-06-01 17:09:45 -0700 | [diff] [blame] | 52 | /** |
| 53 | * Id of the child that's also visible in the contracted layout. |
| 54 | */ |
| 55 | private int mContractedChildId; |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame^] | 56 | /** |
| 57 | * The last measured with in a layout pass if it was measured before or |
| 58 | * {@link #NOT_MEASURED_BEFORE} if this is the first layout pass. |
| 59 | */ |
| 60 | private int mLastMeasuredWidth = NOT_MEASURED_BEFORE; |
Adrian Roos | feafa05 | 2016-06-01 17:09:45 -0700 | [diff] [blame] | 61 | |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 62 | public MessagingLinearLayout(Context context, @Nullable AttributeSet attrs) { |
| 63 | super(context, attrs); |
| 64 | |
| 65 | final TypedArray a = context.obtainStyledAttributes(attrs, |
| 66 | R.styleable.MessagingLinearLayout, 0, |
| 67 | 0); |
| 68 | |
| 69 | final int N = a.getIndexCount(); |
| 70 | for (int i = 0; i < N; i++) { |
| 71 | int attr = a.getIndex(i); |
| 72 | switch (attr) { |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 73 | case R.styleable.MessagingLinearLayout_spacing: |
| 74 | mSpacing = a.getDimensionPixelSize(i, 0); |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 79 | a.recycle(); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | @Override |
| 84 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 85 | // This is essentially a bottom-up linear layout that only adds children that fit entirely |
| 86 | // up to a maximum height. |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame^] | 87 | int targetHeight = MeasureSpec.getSize(heightMeasureSpec); |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 88 | switch (MeasureSpec.getMode(heightMeasureSpec)) { |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 89 | case MeasureSpec.UNSPECIFIED: |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame^] | 90 | targetHeight = Integer.MAX_VALUE; |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 91 | break; |
| 92 | } |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame^] | 93 | int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
| 94 | boolean recalculateVisibility = mLastMeasuredWidth == NOT_MEASURED_BEFORE |
| 95 | || getMeasuredHeight() != targetHeight |
| 96 | || mLastMeasuredWidth != widthSize; |
| 97 | |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 98 | final int count = getChildCount(); |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame^] | 99 | if (recalculateVisibility) { |
| 100 | // We only need to recalculate the view visibilities if the view wasn't measured already |
| 101 | // in this pass, otherwise we may drop messages here already since we are measured |
| 102 | // exactly with what we returned before, which was optimized already with the |
| 103 | // line-indents. |
| 104 | for (int i = 0; i < count; ++i) { |
| 105 | final View child = getChildAt(i); |
| 106 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 107 | lp.hide = true; |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame^] | 110 | int totalHeight = mPaddingTop + mPaddingBottom; |
| 111 | boolean first = true; |
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 | // 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] | 114 | |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame^] | 115 | // fits, we take it, otherwise we stop there. |
| 116 | for (int i = count - 1; i >= 0 && totalHeight < targetHeight; i--) { |
| 117 | if (getChildAt(i).getVisibility() == GONE) { |
| 118 | continue; |
| 119 | } |
| 120 | final View child = getChildAt(i); |
| 121 | LayoutParams lp = (LayoutParams) getChildAt(i).getLayoutParams(); |
| 122 | |
| 123 | if (child instanceof ImageFloatingTextView) { |
| 124 | // Pretend we need the image padding for all views, we don't know which |
| 125 | // one will end up needing to do this (might end up not using all the space, |
| 126 | // but calculating this exactly would be more expensive). |
| 127 | ((ImageFloatingTextView) child).setNumIndentLines( |
| 128 | mIndentLines == 2 ? 3 : mIndentLines); |
| 129 | } |
| 130 | |
| 131 | measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0); |
| 132 | |
| 133 | final int childHeight = child.getMeasuredHeight(); |
| 134 | int newHeight = Math.max(totalHeight, totalHeight + childHeight + lp.topMargin + |
| 135 | lp.bottomMargin + (first ? 0 : mSpacing)); |
| 136 | first = false; |
| 137 | |
| 138 | if (newHeight <= targetHeight) { |
| 139 | totalHeight = newHeight; |
| 140 | lp.hide = false; |
| 141 | } else { |
| 142 | break; |
| 143 | } |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | // Now that we know which views to take, fix up the indents and see what width we get. |
| 148 | int measuredWidth = mPaddingLeft + mPaddingRight; |
| 149 | int imageLines = mIndentLines; |
Adrian Roos | 4ac8f40 | 2016-12-05 15:42:16 -0800 | [diff] [blame] | 150 | // Need to redo the height because it may change due to changing indents. |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame^] | 151 | int totalHeight = mPaddingTop + mPaddingBottom; |
| 152 | boolean first = true; |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 153 | for (int i = 0; i < count; i++) { |
| 154 | final View child = getChildAt(i); |
| 155 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 156 | |
| 157 | if (child.getVisibility() == GONE || lp.hide) { |
| 158 | continue; |
| 159 | } |
| 160 | |
| 161 | if (child instanceof ImageFloatingTextView) { |
| 162 | ImageFloatingTextView textChild = (ImageFloatingTextView) child; |
| 163 | if (imageLines == 2 && textChild.getLineCount() > 2) { |
| 164 | // HACK: If we need indent for two lines, and they're coming from the same |
| 165 | // view, we need extra spacing to compensate for the lack of margins, |
| 166 | // so add an extra line of indent. |
| 167 | imageLines = 3; |
| 168 | } |
| 169 | boolean changed = textChild.setNumIndentLines(Math.max(0, imageLines)); |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame^] | 170 | if (changed || !recalculateVisibility) { |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 171 | measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0); |
| 172 | } |
| 173 | imageLines -= textChild.getLineCount(); |
| 174 | } |
| 175 | |
| 176 | measuredWidth = Math.max(measuredWidth, |
| 177 | child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin |
| 178 | + mPaddingLeft + mPaddingRight); |
Adrian Roos | 4ac8f40 | 2016-12-05 15:42:16 -0800 | [diff] [blame] | 179 | totalHeight = Math.max(totalHeight, totalHeight + child.getMeasuredHeight() + |
| 180 | lp.topMargin + lp.bottomMargin + (first ? 0 : mSpacing)); |
| 181 | first = false; |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | |
| 185 | setMeasuredDimension( |
| 186 | resolveSize(Math.max(getSuggestedMinimumWidth(), measuredWidth), |
| 187 | widthMeasureSpec), |
| 188 | resolveSize(Math.max(getSuggestedMinimumHeight(), totalHeight), |
| 189 | heightMeasureSpec)); |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame^] | 190 | mLastMeasuredWidth = widthSize; |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Override |
| 194 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 195 | final int paddingLeft = mPaddingLeft; |
| 196 | |
| 197 | int childTop; |
| 198 | |
| 199 | // Where right end of child should go |
| 200 | final int width = right - left; |
| 201 | final int childRight = width - mPaddingRight; |
| 202 | |
| 203 | final int layoutDirection = getLayoutDirection(); |
| 204 | final int count = getChildCount(); |
| 205 | |
| 206 | childTop = mPaddingTop; |
| 207 | |
| 208 | boolean first = true; |
| 209 | |
| 210 | for (int i = 0; i < count; i++) { |
| 211 | final View child = getChildAt(i); |
| 212 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 213 | |
| 214 | if (child.getVisibility() == GONE || lp.hide) { |
| 215 | continue; |
| 216 | } |
| 217 | |
| 218 | final int childWidth = child.getMeasuredWidth(); |
| 219 | final int childHeight = child.getMeasuredHeight(); |
| 220 | |
| 221 | int childLeft; |
| 222 | if (layoutDirection == LAYOUT_DIRECTION_RTL) { |
| 223 | childLeft = childRight - childWidth - lp.rightMargin; |
| 224 | } else { |
| 225 | childLeft = paddingLeft + lp.leftMargin; |
| 226 | } |
| 227 | |
| 228 | if (!first) { |
| 229 | childTop += mSpacing; |
| 230 | } |
| 231 | |
| 232 | childTop += lp.topMargin; |
| 233 | child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); |
| 234 | |
| 235 | childTop += childHeight + lp.bottomMargin; |
| 236 | |
| 237 | first = false; |
| 238 | } |
Selim Cinek | 64aed1a | 2017-02-08 14:10:03 -0800 | [diff] [blame^] | 239 | mLastMeasuredWidth = NOT_MEASURED_BEFORE; |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | @Override |
| 243 | protected boolean drawChild(Canvas canvas, View child, long drawingTime) { |
| 244 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 245 | if (lp.hide) { |
| 246 | return true; |
| 247 | } |
| 248 | return super.drawChild(canvas, child, drawingTime); |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 253 | return new LayoutParams(mContext, attrs); |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | protected LayoutParams generateDefaultLayoutParams() { |
| 258 | return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); |
| 259 | |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) { |
| 264 | LayoutParams copy = new LayoutParams(lp.width, lp.height); |
| 265 | if (lp instanceof MarginLayoutParams) { |
| 266 | copy.copyMarginsFrom((MarginLayoutParams) lp); |
| 267 | } |
| 268 | return copy; |
| 269 | } |
| 270 | |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 271 | /** |
| 272 | * Sets how many lines should be indented to avoid a floating image. |
| 273 | */ |
Adrian Roos | feafa05 | 2016-06-01 17:09:45 -0700 | [diff] [blame] | 274 | @RemotableViewMethod |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 275 | public void setNumIndentLines(int numberLines) { |
| 276 | mIndentLines = numberLines; |
| 277 | } |
| 278 | |
Adrian Roos | feafa05 | 2016-06-01 17:09:45 -0700 | [diff] [blame] | 279 | /** |
| 280 | * Set id of the child that's also visible in the contracted layout. |
| 281 | */ |
| 282 | @RemotableViewMethod |
| 283 | public void setContractedChildId(int contractedChildId) { |
| 284 | mContractedChildId = contractedChildId; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Get id of the child that's also visible in the contracted layout. |
| 289 | */ |
| 290 | public int getContractedChildId() { |
| 291 | return mContractedChildId; |
| 292 | } |
| 293 | |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 294 | public static class LayoutParams extends MarginLayoutParams { |
| 295 | |
| 296 | boolean hide = false; |
| 297 | |
| 298 | public LayoutParams(Context c, AttributeSet attrs) { |
| 299 | super(c, attrs); |
| 300 | } |
| 301 | |
| 302 | public LayoutParams(int width, int height) { |
| 303 | super(width, height); |
| 304 | } |
| 305 | } |
| 306 | } |