1/*
2 * Copyright (C) 2015 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 */
16package com.android.packageinstaller.permission.ui;
17
18import android.content.Context;
19import android.util.AttributeSet;
20import android.view.View;
21import android.widget.FrameLayout;
22
23/**
24 * Allows one standard layout pass, but afterwards holds getMeasuredHeight constant,
25 * however still allows drawing larger at the size needed by its children.  This allows
26 * a dialog to tell the window the height is constant (with keeps its position constant)
27 * but allows the view to grow downwards for animation.
28 */
29public class ManualLayoutFrame extends FrameLayout {
30
31    private int mDesiredHeight;
32    private int mHeight;
33    private int mWidth;
34
35    private View mOffsetView;
36
37    public ManualLayoutFrame(Context context, AttributeSet attrs) {
38        super(context, attrs);
39        setClipChildren(false);
40        setClipToPadding(false);
41    }
42
43    public int getLayoutHeight() {
44        return mDesiredHeight;
45    }
46
47    @Override
48    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
49        if (mWidth != 0) {
50            // Keep the width constant to avoid weirdness.
51            widthMeasureSpec = MeasureSpec.makeMeasureSpec(mWidth, MeasureSpec.EXACTLY);
52        }
53        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
54        mDesiredHeight = getMeasuredHeight();
55        if (mHeight == 0 && mDesiredHeight != 0) {
56            // Record the first non-zero width and height, this will be the height henceforth.
57            mHeight = mDesiredHeight;
58            mWidth = getMeasuredWidth();
59        }
60        if (mHeight != 0) {
61            // Always report the same height
62            setMeasuredDimension(getMeasuredWidth(), mHeight);
63        }
64    }
65
66    @Override
67    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
68        if (mDesiredHeight != 0) {
69            // Draw at height we expect to be.
70            setBottom(getTop() + mDesiredHeight);
71            bottom = top + mDesiredHeight;
72        }
73        super.onLayout(changed, left, top, right, bottom);
74    }
75}
76