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