1 /*
2  * Copyright (C) 2017 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.util.AttributeSet;
22 import android.view.View;
23 import android.widget.LinearLayout;
24 import android.widget.RemoteViews;
25 
26 import java.util.ArrayList;
27 
28 /**
29  * A LinearLayout that sets it's height again after the last measure pass. This is needed for
30  * MessagingLayouts where groups need to be able to snap it's height to.
31  */
32 @RemoteViews.RemoteView
33 public class RemeasuringLinearLayout extends LinearLayout {
34 
35     private ArrayList<View> mMatchParentViews = new ArrayList<>();
36 
RemeasuringLinearLayout(Context context)37     public RemeasuringLinearLayout(Context context) {
38         super(context);
39     }
40 
RemeasuringLinearLayout(Context context, @Nullable AttributeSet attrs)41     public RemeasuringLinearLayout(Context context, @Nullable AttributeSet attrs) {
42         super(context, attrs);
43     }
44 
RemeasuringLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr)45     public RemeasuringLinearLayout(Context context, @Nullable AttributeSet attrs,
46             int defStyleAttr) {
47         super(context, attrs, defStyleAttr);
48     }
49 
RemeasuringLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)50     public RemeasuringLinearLayout(Context context, AttributeSet attrs, int defStyleAttr,
51             int defStyleRes) {
52         super(context, attrs, defStyleAttr, defStyleRes);
53     }
54 
55     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)56     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
57         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
58         int count = getChildCount();
59         int height = 0;
60         boolean isVertical = getOrientation() == LinearLayout.VERTICAL;
61         boolean isWrapContent = getLayoutParams().height == LayoutParams.WRAP_CONTENT;
62         for (int i = 0; i < count; ++i) {
63             final View child = getChildAt(i);
64             if (child == null || child.getVisibility() == View.GONE) {
65                 continue;
66             }
67 
68             final LayoutParams lp = (LayoutParams) child.getLayoutParams();
69             if (!isWrapContent || lp.height != LayoutParams.MATCH_PARENT || isVertical) {
70                 int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
71                 height = Math.max(height, isVertical ? height + childHeight : childHeight);
72             } else {
73                 // We have match parent children in a wrap content view, let's measure the
74                 // view properly
75                 mMatchParentViews.add(child);
76             }
77         }
78         if (mMatchParentViews.size() > 0) {
79             int exactHeightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
80             for (View child : mMatchParentViews) {
81                 child.measure(getChildMeasureSpec(
82                         widthMeasureSpec, getPaddingStart() + getPaddingEnd(),
83                         child.getLayoutParams().width),
84                         exactHeightSpec);
85             }
86         }
87         mMatchParentViews.clear();
88         setMeasuredDimension(getMeasuredWidth(), height);
89     }
90 }
91