1/*
2 * Copyright (C) 2012 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.v7.widget;
18
19import android.content.Context;
20import android.os.Build;
21import android.util.AttributeSet;
22import android.view.View;
23
24/**
25 * Custom ViewGroup providing some services to the support implementation of GridLayout.
26 *
27 */
28abstract class ViewGroup extends android.view.ViewGroup {
29
30    public ViewGroup(Context context, AttributeSet attrs, int defStyle) {
31        super(context, attrs, defStyle);
32    }
33
34    protected static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
35
36    /**
37     * Bits of {@link #getMeasuredWidthAndState()} and
38     * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
39     */
40    protected static final int MEASURED_STATE_MASK = 0xff000000;
41
42    /**
43     * Utility to reconcile a desired size and state, with constraints imposed
44     * by a MeasureSpec.  Will take the desired size, unless a different size
45     * is imposed by the constraints.  The returned value is a compound integer,
46     * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
47     * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
48     * size is smaller than the size the view wants to be.
49     *
50     * @param size How big the view wants to be
51     * @param measureSpec Constraints imposed by the parent
52     * @return Size information bit mask as defined by
53     * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
54     */
55    public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
56        if (Build.VERSION.SDK_INT >= 11) {
57            return View.resolveSizeAndState(size, measureSpec, childMeasuredState);
58        }
59        int result = size;
60        int specMode = MeasureSpec.getMode(measureSpec);
61        int specSize =  MeasureSpec.getSize(measureSpec);
62        switch (specMode) {
63        case MeasureSpec.UNSPECIFIED:
64            result = size;
65            break;
66        case MeasureSpec.AT_MOST:
67            if (specSize < size) {
68                result = specSize | MEASURED_STATE_TOO_SMALL;
69            } else {
70                result = size;
71            }
72            break;
73        case MeasureSpec.EXACTLY:
74            result = specSize;
75            break;
76        }
77        return result | (childMeasuredState&MEASURED_STATE_MASK);
78    }
79
80    protected static boolean isLayoutRtl(View view) {
81        return false;
82    }
83}
84