• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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 
29 import com.android.internal.R;
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 
39     /**
40      * Spacing to be applied between views.
41      */
42     private int mSpacing;
43 
44     private int mMaxDisplayedLines = Integer.MAX_VALUE;
45 
46     private MessagingLayout mMessagingLayout;
47 
MessagingLinearLayout(Context context, @Nullable AttributeSet attrs)48     public MessagingLinearLayout(Context context, @Nullable AttributeSet attrs) {
49         super(context, attrs);
50 
51         final TypedArray a = context.obtainStyledAttributes(attrs,
52                 R.styleable.MessagingLinearLayout, 0,
53                 0);
54 
55         final int N = a.getIndexCount();
56         for (int i = 0; i < N; i++) {
57             int attr = a.getIndex(i);
58             switch (attr) {
59                 case R.styleable.MessagingLinearLayout_spacing:
60                     mSpacing = a.getDimensionPixelSize(i, 0);
61                     break;
62             }
63         }
64 
65         a.recycle();
66     }
67 
68     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)69     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
70         // This is essentially a bottom-up linear layout that only adds children that fit entirely
71         // up to a maximum height.
72         int targetHeight = MeasureSpec.getSize(heightMeasureSpec);
73         switch (MeasureSpec.getMode(heightMeasureSpec)) {
74             case MeasureSpec.UNSPECIFIED:
75                 targetHeight = Integer.MAX_VALUE;
76                 break;
77         }
78 
79         // Now that we know which views to take, fix up the indents and see what width we get.
80         int measuredWidth = mPaddingLeft + mPaddingRight;
81         final int count = getChildCount();
82         int totalHeight;
83         for (int i = 0; i < count; ++i) {
84             final View child = getChildAt(i);
85             final LayoutParams lp = (LayoutParams) child.getLayoutParams();
86             lp.hide = true;
87         }
88 
89         totalHeight = mPaddingTop + mPaddingBottom;
90         boolean first = true;
91         int linesRemaining = mMaxDisplayedLines;
92         // Starting from the bottom: we measure every view as if it were the only one. If it still
93         // fits, we take it, otherwise we stop there.
94         for (int i = count - 1; i >= 0 && totalHeight < targetHeight; i--) {
95             if (getChildAt(i).getVisibility() == GONE) {
96                 continue;
97             }
98             final View child = getChildAt(i);
99             LayoutParams lp = (LayoutParams) getChildAt(i).getLayoutParams();
100             MessagingChild messagingChild = null;
101             int spacing = mSpacing;
102             if (child instanceof MessagingChild) {
103                 messagingChild = (MessagingChild) child;
104                 messagingChild.setMaxDisplayedLines(linesRemaining);
105                 spacing += messagingChild.getExtraSpacing();
106             }
107             spacing = first ? 0 : spacing;
108             measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, totalHeight
109                     - mPaddingTop - mPaddingBottom + spacing);
110 
111             final int childHeight = child.getMeasuredHeight();
112             int newHeight = Math.max(totalHeight, totalHeight + childHeight + lp.topMargin +
113                     lp.bottomMargin + spacing);
114             int measureType = MessagingChild.MEASURED_NORMAL;
115             if (messagingChild != null) {
116                 measureType = messagingChild.getMeasuredType();
117                 linesRemaining -= messagingChild.getConsumedLines();
118             }
119 
120             // We never measure the first item as too small, we want to at least show something.
121             boolean isTooSmall = measureType == MessagingChild.MEASURED_TOO_SMALL && !first;
122             boolean isShortened = measureType == MessagingChild.MEASURED_SHORTENED
123                     || measureType == MessagingChild.MEASURED_TOO_SMALL && first;
124             if (newHeight <= targetHeight && !isTooSmall) {
125                 totalHeight = newHeight;
126                 measuredWidth = Math.max(measuredWidth,
127                         child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin
128                                 + mPaddingLeft + mPaddingRight);
129                 lp.hide = false;
130                 if (isShortened || linesRemaining <= 0) {
131                     break;
132                 }
133             } else {
134                 break;
135             }
136             first = false;
137         }
138 
139         setMeasuredDimension(
140                 resolveSize(Math.max(getSuggestedMinimumWidth(), measuredWidth),
141                         widthMeasureSpec),
142                 Math.max(getSuggestedMinimumHeight(), totalHeight));
143     }
144 
145     @Override
onLayout(boolean changed, int left, int top, int right, int bottom)146     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
147         final int paddingLeft = mPaddingLeft;
148 
149         int childTop;
150 
151         // Where right end of child should go
152         final int width = right - left;
153         final int childRight = width - mPaddingRight;
154 
155         final int layoutDirection = getLayoutDirection();
156         final int count = getChildCount();
157 
158         childTop = mPaddingTop;
159 
160         boolean first = true;
161         final boolean shown = isShown();
162         for (int i = 0; i < count; i++) {
163             final View child = getChildAt(i);
164             if (child.getVisibility() == GONE) {
165                 continue;
166             }
167             final LayoutParams lp = (LayoutParams) child.getLayoutParams();
168             MessagingChild messagingChild = (MessagingChild) child;
169 
170             final int childWidth = child.getMeasuredWidth();
171             final int childHeight = child.getMeasuredHeight();
172 
173             int childLeft;
174             if (layoutDirection == LAYOUT_DIRECTION_RTL) {
175                 childLeft = childRight - childWidth - lp.rightMargin;
176             } else {
177                 childLeft = paddingLeft + lp.leftMargin;
178             }
179             if (lp.hide) {
180                 if (shown && lp.visibleBefore) {
181                     // We still want to lay out the child to have great animations
182                     child.layout(childLeft, childTop, childLeft + childWidth,
183                             childTop + lp.lastVisibleHeight);
184                     messagingChild.hideAnimated();
185                 }
186                 lp.visibleBefore = false;
187                 continue;
188             } else {
189                 lp.visibleBefore = true;
190                 lp.lastVisibleHeight = childHeight;
191             }
192 
193             if (!first) {
194                 childTop += mSpacing;
195             }
196 
197             childTop += lp.topMargin;
198             child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
199 
200             childTop += childHeight + lp.bottomMargin;
201 
202             first = false;
203         }
204     }
205 
206     @Override
drawChild(Canvas canvas, View child, long drawingTime)207     protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
208         final LayoutParams lp = (LayoutParams) child.getLayoutParams();
209         if (lp.hide) {
210             MessagingChild messagingChild = (MessagingChild) child;
211             if (!messagingChild.isHidingAnimated()) {
212                 return true;
213             }
214         }
215         return super.drawChild(canvas, child, drawingTime);
216     }
217 
218     @Override
generateLayoutParams(AttributeSet attrs)219     public LayoutParams generateLayoutParams(AttributeSet attrs) {
220         return new LayoutParams(mContext, attrs);
221     }
222 
223     @Override
generateDefaultLayoutParams()224     protected LayoutParams generateDefaultLayoutParams() {
225         return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
226 
227     }
228 
229     @Override
generateLayoutParams(ViewGroup.LayoutParams lp)230     protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
231         LayoutParams copy = new LayoutParams(lp.width, lp.height);
232         if (lp instanceof MarginLayoutParams) {
233             copy.copyMarginsFrom((MarginLayoutParams) lp);
234         }
235         return copy;
236     }
237 
isGone(View view)238     public static boolean isGone(View view) {
239         if (view.getVisibility() == View.GONE) {
240             return true;
241         }
242         final ViewGroup.LayoutParams lp = view.getLayoutParams();
243         if (lp instanceof MessagingLinearLayout.LayoutParams
244                 && ((MessagingLinearLayout.LayoutParams) lp).hide) {
245             return true;
246         }
247         return false;
248     }
249 
250     /**
251      * Sets how many lines should be displayed at most
252      */
253     @RemotableViewMethod
setMaxDisplayedLines(int numberLines)254     public void setMaxDisplayedLines(int numberLines) {
255         mMaxDisplayedLines = numberLines;
256     }
257 
setMessagingLayout(MessagingLayout layout)258     public void setMessagingLayout(MessagingLayout layout) {
259         mMessagingLayout = layout;
260     }
261 
getMessagingLayout()262     public MessagingLayout getMessagingLayout() {
263         return mMessagingLayout;
264     }
265 
266     public interface MessagingChild {
267         int MEASURED_NORMAL = 0;
268         int MEASURED_SHORTENED = 1;
269         int MEASURED_TOO_SMALL = 2;
270 
getMeasuredType()271         int getMeasuredType();
getConsumedLines()272         int getConsumedLines();
setMaxDisplayedLines(int lines)273         void setMaxDisplayedLines(int lines);
hideAnimated()274         void hideAnimated();
isHidingAnimated()275         boolean isHidingAnimated();
getExtraSpacing()276         default int getExtraSpacing() {
277             return 0;
278         }
279     }
280 
281     public static class LayoutParams extends MarginLayoutParams {
282 
283         public boolean hide = false;
284         public boolean visibleBefore = false;
285         public int lastVisibleHeight;
286 
LayoutParams(Context c, AttributeSet attrs)287         public LayoutParams(Context c, AttributeSet attrs) {
288             super(c, attrs);
289         }
290 
LayoutParams(int width, int height)291         public LayoutParams(int width, int height) {
292             super(width, height);
293         }
294     }
295 }
296