1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.internal.widget;
import android.annotation.Nullable;
import android.annotation.Px;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.RemotableViewMethod;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.RemoteViews;
import com.android.internal.R;
/**
* A custom-built layout for the Notification.MessagingStyle.
*
* Evicts children until they all fit.
*/
@RemoteViews.RemoteView
public class MessagingLinearLayout extends ViewGroup {
/**
* Spacing to be applied between views.
*/
private int mSpacing;
private int mMaxDisplayedLines = Integer.MAX_VALUE;
public MessagingLinearLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
final TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.MessagingLinearLayout, 0,
0);
final int N = a.getIndexCount();
for (int i = 0; i < N; i++) {
int attr = a.getIndex(i);
switch (attr) {
case R.styleable.MessagingLinearLayout_spacing:
mSpacing = a.getDimensionPixelSize(i, 0);
break;
}
}
a.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// This is essentially a bottom-up linear layout that only adds children that fit entirely
// up to a maximum height.
int targetHeight = MeasureSpec.getSize(heightMeasureSpec);
switch (MeasureSpec.getMode(heightMeasureSpec)) {
case MeasureSpec.UNSPECIFIED:
targetHeight = Integer.MAX_VALUE;
break;
}
// Now that we know which views to take, fix up the indents and see what width we get.
int measuredWidth = mPaddingLeft + mPaddingRight;
final int count = getChildCount();
int totalHeight;
for (int i = 0; i < count; ++i) {
final View child = getChildAt(i);
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
lp.hide = true;
if (child instanceof MessagingChild) {
MessagingChild messagingChild = (MessagingChild) child;
// Whenever we encounter the message first, it's always first in the layout
messagingChild.setIsFirstInLayout(true);
}
}
totalHeight = mPaddingTop + mPaddingBottom;
boolean first = true;
int linesRemaining = mMaxDisplayedLines;
// Starting from the bottom: we measure every view as if it were the only one. If it still
// fits, we take it, otherwise we stop there.
MessagingChild previousChild = null;
View previousView = null;
int previousChildHeight = 0;
int previousTotalHeight = 0;
int previousLinesConsumed = 0;
for (int i = count - 1; i >= 0 && totalHeight < targetHeight; i--) {
if (getChildAt(i).getVisibility() == GONE) {
continue;
}
final View child = getChildAt(i);
LayoutParams lp = (LayoutParams) getChildAt(i).getLayoutParams();
MessagingChild messagingChild = null;
int spacing = mSpacing;
int previousChildIncrease = 0;
if (child instanceof MessagingChild) {
// We need to remeasure the previous child again if it's not the first anymore
if (previousChild != null && previousChild.hasDifferentHeightWhenFirst()) {
previousChild.setIsFirstInLayout(false);
measureChildWithMargins(previousView, widthMeasureSpec, 0, heightMeasureSpec,
previousTotalHeight - previousChildHeight);
previousChildIncrease = previousView.getMeasuredHeight() - previousChildHeight;
linesRemaining -= previousChild.getConsumedLines() - previousLinesConsumed;
}
messagingChild = (MessagingChild) child;
messagingChild.setMaxDisplayedLines(linesRemaining);
spacing += messagingChild.getExtraSpacing();
}
spacing = first ? 0 : spacing;
measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, totalHeight
- mPaddingTop - mPaddingBottom + spacing);
final int childHeight = child.getMeasuredHeight();
int newHeight = Math.max(totalHeight, totalHeight + childHeight + lp.topMargin +
lp.bottomMargin + spacing + previousChildIncrease);
int measureType = MessagingChild.MEASURED_NORMAL;
if (messagingChild != null) {
measureType = messagingChild.getMeasuredType();
}
// We never measure the first item as too small, we want to at least show something.
boolean isTooSmall = measureType == MessagingChild.MEASURED_TOO_SMALL && !first;
boolean isShortened = measureType == MessagingChild.MEASURED_SHORTENED
|| measureType == MessagingChild.MEASURED_TOO_SMALL && first;
boolean showView = newHeight <= targetHeight && !isTooSmall;
if (showView) {
if (messagingChild != null) {
previousLinesConsumed = messagingChild.getConsumedLines();
linesRemaining -= previousLinesConsumed;
previousChild = messagingChild;
previousView = child;
previousChildHeight = childHeight;
previousTotalHeight = totalHeight;
}
totalHeight = newHeight;
measuredWidth = Math.max(measuredWidth,
child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin
+ mPaddingLeft + mPaddingRight);
lp.hide = false;
if (isShortened || linesRemaining <= 0) {
break;
}
} else {
// We now became too short, let's make sure to reset any previous views to be first
// and remeasure it.
if (previousChild != null && previousChild.hasDifferentHeightWhenFirst()) {
previousChild.setIsFirstInLayout(true);
// We need to remeasure the previous child again since it became first
measureChildWithMargins(previousView, widthMeasureSpec, 0, heightMeasureSpec,
previousTotalHeight - previousChildHeight);
// The totalHeight is already correct here since we only set it during the
// first pass
}
break;
}
first = false;
}
setMeasuredDimension(
resolveSize(Math.max(getSuggestedMinimumWidth(), measuredWidth),
widthMeasureSpec),
Math.max(getSuggestedMinimumHeight(), totalHeight));
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
final int paddingLeft = mPaddingLeft;
int childTop;
// Where right end of child should go
final int width = right - left;
final int childRight = width - mPaddingRight;
final int layoutDirection = getLayoutDirection();
final int count = getChildCount();
childTop = mPaddingTop;
boolean first = true;
final boolean shown = isShown();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
MessagingChild messagingChild = (MessagingChild) child;
final int childWidth = child.getMeasuredWidth();
final int childHeight = child.getMeasuredHeight();
int childLeft;
if (layoutDirection == LAYOUT_DIRECTION_RTL) {
childLeft = childRight - childWidth - lp.rightMargin;
} else {
childLeft = paddingLeft + lp.leftMargin;
}
if (lp.hide) {
if (shown && lp.visibleBefore) {
// We still want to lay out the child to have great animations
child.layout(childLeft, childTop, childLeft + childWidth,
childTop + lp.lastVisibleHeight);
messagingChild.hideAnimated();
}
lp.visibleBefore = false;
continue;
} else {
lp.visibleBefore = true;
lp.lastVisibleHeight = childHeight;
}
if (!first) {
childTop += mSpacing;
}
childTop += lp.topMargin;
child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
childTop += childHeight + lp.bottomMargin;
first = false;
}
}
@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (lp.hide) {
MessagingChild messagingChild = (MessagingChild) child;
if (!messagingChild.isHidingAnimated()) {
return true;
}
}
return super.drawChild(canvas, child, drawingTime);
}
/**
* Set the spacing to be applied between views.
*/
public void setSpacing(@Px int spacing) {
if (mSpacing != spacing) {
mSpacing = spacing;
requestLayout();
}
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(mContext, attrs);
}
@Override
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
}
@Override
protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
LayoutParams copy = new LayoutParams(lp.width, lp.height);
if (lp instanceof MarginLayoutParams) {
copy.copyMarginsFrom((MarginLayoutParams) lp);
}
return copy;
}
public static boolean isGone(View view) {
if (view.getVisibility() == View.GONE) {
return true;
}
final ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp instanceof MessagingLinearLayout.LayoutParams
&& ((MessagingLinearLayout.LayoutParams) lp).hide) {
return true;
}
return false;
}
/**
* Sets how many lines should be displayed at most
*/
@RemotableViewMethod
public void setMaxDisplayedLines(int numberLines) {
mMaxDisplayedLines = numberLines;
}
public IMessagingLayout getMessagingLayout() {
View view = this;
while (true) {
ViewParent p = view.getParent();
if (p instanceof View) {
view = (View) p;
if (view instanceof IMessagingLayout) {
return (IMessagingLayout) view;
}
} else {
return null;
}
}
}
@Override
public int getBaseline() {
// When placed in a horizontal linear layout (as is the case in a single-line MessageGroup),
// align with the last visible child (which is the one that will be displayed in the single-
// line group.
int childCount = getChildCount();
for (int i = childCount - 1; i >= 0; i--) {
final View child = getChildAt(i);
if (isGone(child)) {
continue;
}
final int childBaseline = child.getBaseline();
if (childBaseline == -1) {
return -1;
}
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
return lp.topMargin + childBaseline;
}
return super.getBaseline();
}
public interface MessagingChild {
int MEASURED_NORMAL = 0;
int MEASURED_SHORTENED = 1;
int MEASURED_TOO_SMALL = 2;
int getMeasuredType();
int getConsumedLines();
void setMaxDisplayedLines(int lines);
void hideAnimated();
boolean isHidingAnimated();
/**
* Set that this view is first in layout. Relevant and only set if
* {@link #hasDifferentHeightWhenFirst()}.
* @param first is this first?
*/
default void setIsFirstInLayout(boolean first) {}
/**
* @return if this layout has different height it is first in the layout
*/
default boolean hasDifferentHeightWhenFirst() {
return false;
}
default int getExtraSpacing() {
return 0;
}
void recycle();
}
public static class LayoutParams extends MarginLayoutParams {
public boolean hide = false;
public boolean visibleBefore = false;
public int lastVisibleHeight;
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
}
public LayoutParams(int width, int height) {
super(width, height);
}
}
}
|