blob: b259ad1643f8477f514cd3070363bfe0e5086222 [file] [log] [blame]
Adrian Roosc1a80b02016-04-05 14:54:55 -07001/*
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
17package com.android.internal.widget;
18
19import com.android.internal.R;
20
21import android.annotation.Nullable;
22import android.content.Context;
23import android.content.res.TypedArray;
24import android.graphics.Canvas;
25import android.util.AttributeSet;
26import android.view.RemotableViewMethod;
27import android.view.View;
28import android.view.ViewGroup;
29import 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
37public class MessagingLinearLayout extends ViewGroup {
38
Selim Cinek64aed1a2017-02-08 14:10:03 -080039 private static final int NOT_MEASURED_BEFORE = -1;
Adrian Roosc1a80b02016-04-05 14:54:55 -070040 /**
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 Roosfeafa052016-06-01 17:09:45 -070052 /**
53 * Id of the child that's also visible in the contracted layout.
54 */
55 private int mContractedChildId;
Selim Cinek64aed1a2017-02-08 14:10:03 -080056 /**
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 Roosfeafa052016-06-01 17:09:45 -070061
Adrian Roosc1a80b02016-04-05 14:54:55 -070062 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 Roosc1a80b02016-04-05 14:54:55 -070073 case R.styleable.MessagingLinearLayout_spacing:
74 mSpacing = a.getDimensionPixelSize(i, 0);
75 break;
76 }
77 }
78
Adrian Roosc1a80b02016-04-05 14:54:55 -070079 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 Cinek64aed1a2017-02-08 14:10:03 -080087 int targetHeight = MeasureSpec.getSize(heightMeasureSpec);
Adrian Roosc1a80b02016-04-05 14:54:55 -070088 switch (MeasureSpec.getMode(heightMeasureSpec)) {
Adrian Roosc1a80b02016-04-05 14:54:55 -070089 case MeasureSpec.UNSPECIFIED:
Selim Cinek64aed1a2017-02-08 14:10:03 -080090 targetHeight = Integer.MAX_VALUE;
Adrian Roosc1a80b02016-04-05 14:54:55 -070091 break;
92 }
Selim Cinek64aed1a2017-02-08 14:10:03 -080093 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
94 boolean recalculateVisibility = mLastMeasuredWidth == NOT_MEASURED_BEFORE
95 || getMeasuredHeight() != targetHeight
96 || mLastMeasuredWidth != widthSize;
97
Adrian Roosc1a80b02016-04-05 14:54:55 -070098 final int count = getChildCount();
Selim Cinek64aed1a2017-02-08 14:10:03 -080099 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 Roosc1a80b02016-04-05 14:54:55 -0700108 }
109
Selim Cinek64aed1a2017-02-08 14:10:03 -0800110 int totalHeight = mPaddingTop + mPaddingBottom;
111 boolean first = true;
Adrian Roosc1a80b02016-04-05 14:54:55 -0700112
Selim Cinek64aed1a2017-02-08 14:10:03 -0800113 // Starting from the bottom: we measure every view as if it were the only one. If it still
Adrian Roosc1a80b02016-04-05 14:54:55 -0700114
Selim Cinek64aed1a2017-02-08 14:10:03 -0800115 // 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 Roosc1a80b02016-04-05 14:54:55 -0700144 }
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 Roos4ac8f402016-12-05 15:42:16 -0800150 // Need to redo the height because it may change due to changing indents.
Selim Cinek64aed1a2017-02-08 14:10:03 -0800151 int totalHeight = mPaddingTop + mPaddingBottom;
152 boolean first = true;
Adrian Roosc1a80b02016-04-05 14:54:55 -0700153 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 Cinek64aed1a2017-02-08 14:10:03 -0800170 if (changed || !recalculateVisibility) {
Adrian Roosc1a80b02016-04-05 14:54:55 -0700171 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 Roos4ac8f402016-12-05 15:42:16 -0800179 totalHeight = Math.max(totalHeight, totalHeight + child.getMeasuredHeight() +
180 lp.topMargin + lp.bottomMargin + (first ? 0 : mSpacing));
181 first = false;
Adrian Roosc1a80b02016-04-05 14:54:55 -0700182 }
183
184
185 setMeasuredDimension(
186 resolveSize(Math.max(getSuggestedMinimumWidth(), measuredWidth),
187 widthMeasureSpec),
188 resolveSize(Math.max(getSuggestedMinimumHeight(), totalHeight),
189 heightMeasureSpec));
Selim Cinek64aed1a2017-02-08 14:10:03 -0800190 mLastMeasuredWidth = widthSize;
Adrian Roosc1a80b02016-04-05 14:54:55 -0700191 }
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 Cinek64aed1a2017-02-08 14:10:03 -0800239 mLastMeasuredWidth = NOT_MEASURED_BEFORE;
Adrian Roosc1a80b02016-04-05 14:54:55 -0700240 }
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 Roosc1a80b02016-04-05 14:54:55 -0700271 /**
272 * Sets how many lines should be indented to avoid a floating image.
273 */
Adrian Roosfeafa052016-06-01 17:09:45 -0700274 @RemotableViewMethod
Adrian Roosc1a80b02016-04-05 14:54:55 -0700275 public void setNumIndentLines(int numberLines) {
276 mIndentLines = numberLines;
277 }
278
Adrian Roosfeafa052016-06-01 17:09:45 -0700279 /**
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 Roosc1a80b02016-04-05 14:54:55 -0700294 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}