1/*
2 * Copyright (C) 2011 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.widget;
18
19import android.content.Context;
20import android.graphics.Canvas;
21import android.util.AttributeSet;
22import android.view.View;
23import android.view.ViewGroup;
24
25/**
26 * Space is a lightweight View subclass that may be used to create gaps between components
27 * in general purpose layouts.
28 */
29public final class Space extends View {
30    /**
31     * {@inheritDoc}
32     */
33    public Space(Context context, AttributeSet attrs, int defStyle) {
34        super(context, attrs, defStyle);
35        if (getVisibility() == VISIBLE) {
36            setVisibility(INVISIBLE);
37        }
38    }
39
40    /**
41     * {@inheritDoc}
42     */
43    public Space(Context context, AttributeSet attrs) {
44        this(context, attrs, 0);
45    }
46
47    /**
48     * {@inheritDoc}
49     */
50    public Space(Context context) {
51        //noinspection NullableProblems
52        this(context, null);
53    }
54
55    /**
56     * Draw nothing.
57     *
58     * @param canvas an unused parameter.
59     */
60    @Override
61    public void draw(Canvas canvas) {
62    }
63
64    /**
65     * Compare to: {@link View#getDefaultSize(int, int)}
66     * If mode is AT_MOST, return the child size instead of the parent size
67     * (unless it is too big).
68     */
69    private static int getDefaultSize2(int size, int measureSpec) {
70        int result = size;
71        int specMode = MeasureSpec.getMode(measureSpec);
72        int specSize = MeasureSpec.getSize(measureSpec);
73
74        switch (specMode) {
75            case MeasureSpec.UNSPECIFIED:
76                result = size;
77                break;
78            case MeasureSpec.AT_MOST:
79                result = Math.min(size, specSize);
80                break;
81            case MeasureSpec.EXACTLY:
82                result = specSize;
83                break;
84        }
85        return result;
86    }
87
88    @Override
89    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
90        setMeasuredDimension(
91                getDefaultSize2(getSuggestedMinimumWidth(), widthMeasureSpec),
92                getDefaultSize2(getSuggestedMinimumHeight(), heightMeasureSpec));
93    }
94}
95