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
17package android.support.design.internal;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.view.View;
22import android.view.ViewGroup;
23
24/**
25 * A simple ViewGroup that aligns all the views inside on a baseline. Note: bottom padding for this
26 * view will be measured starting from the baseline.
27 *
28 * @hide
29 */
30public class BaselineLayout extends ViewGroup {
31    private int mBaseline = -1;
32
33    public BaselineLayout(Context context) {
34        super(context, null, 0);
35    }
36
37    public BaselineLayout(Context context, AttributeSet attrs) {
38        super(context, attrs, 0);
39    }
40
41    public BaselineLayout(Context context, AttributeSet attrs, int defStyleAttr) {
42        super(context, attrs, defStyleAttr);
43    }
44
45    @Override
46    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
47        final int count = getChildCount();
48        int maxWidth = 0;
49        int maxHeight = 0;
50        int maxChildBaseline = -1;
51        int maxChildDescent = -1;
52        int childState = 0;
53
54        for (int i = 0; i < count; i++) {
55            final View child = getChildAt(i);
56            if (child.getVisibility() == GONE) {
57                continue;
58            }
59
60            measureChild(child, widthMeasureSpec, heightMeasureSpec);
61            final int baseline = child.getBaseline();
62            if (baseline != -1) {
63                maxChildBaseline = Math.max(maxChildBaseline, baseline);
64                maxChildDescent = Math.max(maxChildDescent, child.getMeasuredHeight() - baseline);
65            }
66            maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
67            maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
68            childState = View.combineMeasuredStates(childState, child.getMeasuredState());
69        }
70        if (maxChildBaseline != -1) {
71            maxChildDescent = Math.max(maxChildDescent, getPaddingBottom());
72            maxHeight = Math.max(maxHeight, maxChildBaseline + maxChildDescent);
73            mBaseline = maxChildBaseline;
74        }
75        maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
76        maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
77        setMeasuredDimension(
78                View.resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
79                View.resolveSizeAndState(maxHeight, heightMeasureSpec,
80                        childState << MEASURED_HEIGHT_STATE_SHIFT));
81    }
82
83    @Override
84    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
85        final int count = getChildCount();
86        final int parentLeft = getPaddingLeft();
87        final int parentRight = right - left - getPaddingRight();
88        final int parentContentWidth = parentRight - parentLeft;
89        final int parentTop = getPaddingTop();
90
91        for (int i = 0; i < count; i++) {
92            final View child = getChildAt(i);
93            if (child.getVisibility() == GONE) {
94                continue;
95            }
96
97            final int width = child.getMeasuredWidth();
98            final int height = child.getMeasuredHeight();
99
100            final int childLeft = parentLeft + (parentContentWidth - width) / 2;
101            final int childTop;
102            if (mBaseline != -1 && child.getBaseline() != -1) {
103                childTop = parentTop + mBaseline - child.getBaseline();
104            } else {
105                childTop = parentTop;
106            }
107
108            child.layout(childLeft, childTop, childLeft + width, childTop + height);
109        }
110    }
111
112    @Override
113    public int getBaseline() {
114        return mBaseline;
115    }
116}
117