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
17package com.android.internal.widget;
18
19import android.annotation.Nullable;
20import android.content.Context;
21import android.util.AttributeSet;
22import android.view.View;
23import android.widget.LinearLayout;
24import android.widget.RemoteViews;
25
26/**
27 * A LinearLayout that sets it's height again after the last measure pass. This is needed for
28 * MessagingLayouts where groups need to be able to snap it's height to.
29 */
30@RemoteViews.RemoteView
31public class RemeasuringLinearLayout extends LinearLayout {
32
33    public RemeasuringLinearLayout(Context context) {
34        super(context);
35    }
36
37    public RemeasuringLinearLayout(Context context, @Nullable AttributeSet attrs) {
38        super(context, attrs);
39    }
40
41    public RemeasuringLinearLayout(Context context, @Nullable AttributeSet attrs,
42            int defStyleAttr) {
43        super(context, attrs, defStyleAttr);
44    }
45
46    public RemeasuringLinearLayout(Context context, AttributeSet attrs, int defStyleAttr,
47            int defStyleRes) {
48        super(context, attrs, defStyleAttr, defStyleRes);
49    }
50
51    @Override
52    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
53        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
54        int count = getChildCount();
55        int height = 0;
56        for (int i = 0; i < count; ++i) {
57            final View child = getChildAt(i);
58            if (child == null || child.getVisibility() == View.GONE) {
59                continue;
60            }
61
62            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
63            height = Math.max(height, height + child.getMeasuredHeight() + lp.topMargin +
64                    lp.bottomMargin);
65        }
66        setMeasuredDimension(getMeasuredWidth(), height);
67    }
68}
69