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