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.setupwizardlib.view;
18 
19 import android.annotation.TargetApi;
20 import android.content.Context;
21 import android.content.res.TypedArray;
22 import android.os.Build.VERSION_CODES;
23 import android.util.AttributeSet;
24 import android.widget.FrameLayout;
25 
26 import com.android.setupwizardlib.R;
27 
28 /**
29  * A FrameLayout subclass that has an "intrinsic size", which is the size it wants to be if that is
30  * within the constraints given by the parent. The intrinsic size can be set with the
31  * {@code android:width} and {@code android:height} attributes in XML.
32  *
33  * Note that for the intrinsic size to be meaningful, {@code android:layout_width} and/or
34  * {@code android:layout_height} will need to be {@code wrap_content}.
35  */
36 public class IntrinsicSizeFrameLayout extends FrameLayout {
37 
38     private int mIntrinsicHeight = 0;
39     private int mIntrinsicWidth = 0;
40 
IntrinsicSizeFrameLayout(Context context)41     public IntrinsicSizeFrameLayout(Context context) {
42         super(context);
43         init(context, null, 0);
44     }
45 
IntrinsicSizeFrameLayout(Context context, AttributeSet attrs)46     public IntrinsicSizeFrameLayout(Context context, AttributeSet attrs) {
47         super(context, attrs);
48         init(context, attrs, 0);
49     }
50 
51     @TargetApi(VERSION_CODES.HONEYCOMB)
IntrinsicSizeFrameLayout(Context context, AttributeSet attrs, int defStyleAttr)52     public IntrinsicSizeFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
53         super(context, attrs, defStyleAttr);
54         init(context, attrs, defStyleAttr);
55     }
56 
init(Context context, AttributeSet attrs, int defStyleAttr)57     private void init(Context context, AttributeSet attrs, int defStyleAttr) {
58         final TypedArray a = context.obtainStyledAttributes(attrs,
59                 R.styleable.SuwIntrinsicSizeFrameLayout, defStyleAttr, 0);
60         mIntrinsicHeight =
61                 a.getDimensionPixelSize(R.styleable.SuwIntrinsicSizeFrameLayout_android_height, 0);
62         mIntrinsicWidth =
63                 a.getDimensionPixelSize(R.styleable.SuwIntrinsicSizeFrameLayout_android_width, 0);
64         a.recycle();
65     }
66 
67     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)68     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
69         super.onMeasure(getIntrinsicMeasureSpec(widthMeasureSpec, mIntrinsicWidth),
70                 getIntrinsicMeasureSpec(heightMeasureSpec, mIntrinsicHeight));
71     }
72 
getIntrinsicMeasureSpec(int measureSpec, int intrinsicSize)73     private int getIntrinsicMeasureSpec(int measureSpec, int intrinsicSize) {
74         if (intrinsicSize <= 0) {
75             // Intrinsic size is not set, just return the original spec
76             return measureSpec;
77         }
78         final int mode = MeasureSpec.getMode(measureSpec);
79         final int size = MeasureSpec.getSize(measureSpec);
80         if (mode == MeasureSpec.UNSPECIFIED) {
81             // Parent did not give any constraint, so we'll be the intrinsic size
82             return MeasureSpec.makeMeasureSpec(mIntrinsicHeight, MeasureSpec.EXACTLY);
83         } else if (mode == MeasureSpec.AT_MOST) {
84             // If intrinsic size is within parents constraint, take the intrinsic size.
85             // Otherwise take the parents size because that's closest to the intrinsic size.
86             return MeasureSpec.makeMeasureSpec(Math.min(size, mIntrinsicHeight),
87                     MeasureSpec.EXACTLY);
88         }
89         // Parent specified EXACTLY, or in all other cases, just return the original spec
90         return measureSpec;
91     }
92 }
93