13f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne/*
23f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Copyright (C) 2011 The Android Open Source Project
33f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne *
43f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Licensed under the Apache License, Version 2.0 (the "License");
53f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * you may not use this file except in compliance with the License.
63f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * You may obtain a copy of the License at
73f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne *
83f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne *      http://www.apache.org/licenses/LICENSE-2.0
93f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne *
103f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Unless required by applicable law or agreed to in writing, software
113f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * distributed under the License is distributed on an "AS IS" BASIS,
123f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * See the License for the specific language governing permissions and
143f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * limitations under the License.
153f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */
163f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne
173f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milnepackage android.widget;
183f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne
193f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milneimport android.content.Context;
203f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milneimport android.graphics.Canvas;
213f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milneimport android.util.AttributeSet;
223f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milneimport android.view.View;
233f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne
243f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne/**
253f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Space is a lightweight View subclass that may be used to create gaps between components
263f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * in general purpose layouts.
273f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */
283f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milnepublic final class Space extends View {
293f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    /**
303f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne     * {@inheritDoc}
313f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne     */
32617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette    public Space(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
33617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette        super(context, attrs, defStyleAttr, defStyleRes);
344a145d72622772b920f60195e80942058984259cPhilip Milne        if (getVisibility() == VISIBLE) {
354a145d72622772b920f60195e80942058984259cPhilip Milne            setVisibility(INVISIBLE);
364a145d72622772b920f60195e80942058984259cPhilip Milne        }
373f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    }
383f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne
393f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    /**
403f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne     * {@inheritDoc}
413f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne     */
42617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette    public Space(Context context, AttributeSet attrs, int defStyleAttr) {
43617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette        this(context, attrs, defStyleAttr, 0);
44617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette    }
45617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette
46617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette    /**
47617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette     * {@inheritDoc}
48617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette     */
493f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    public Space(Context context, AttributeSet attrs) {
504a145d72622772b920f60195e80942058984259cPhilip Milne        this(context, attrs, 0);
513f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    }
523f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne
533f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    /**
543f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne     * {@inheritDoc}
553f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne     */
563f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    public Space(Context context) {
574a145d72622772b920f60195e80942058984259cPhilip Milne        //noinspection NullableProblems
584a145d72622772b920f60195e80942058984259cPhilip Milne        this(context, null);
593f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    }
603f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne
613f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    /**
623f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne     * Draw nothing.
633f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne     *
643f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne     * @param canvas an unused parameter.
653f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne     */
663f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    @Override
673f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    public void draw(Canvas canvas) {
683f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    }
693f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne
703f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    /**
71a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne     * Compare to: {@link View#getDefaultSize(int, int)}
72a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne     * If mode is AT_MOST, return the child size instead of the parent size
73a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne     * (unless it is too big).
74a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne     */
75a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne    private static int getDefaultSize2(int size, int measureSpec) {
76a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne        int result = size;
77a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne        int specMode = MeasureSpec.getMode(measureSpec);
78a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne        int specSize = MeasureSpec.getSize(measureSpec);
79a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne
80a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne        switch (specMode) {
81a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne            case MeasureSpec.UNSPECIFIED:
82a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne                result = size;
83a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne                break;
84a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne            case MeasureSpec.AT_MOST:
85a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne                result = Math.min(size, specSize);
86a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne                break;
87a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne            case MeasureSpec.EXACTLY:
88a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne                result = specSize;
89a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne                break;
90a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne        }
91a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne        return result;
92a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne    }
93a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne
94a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne    @Override
95a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
96a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne        setMeasuredDimension(
97a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne                getDefaultSize2(getSuggestedMinimumWidth(), widthMeasureSpec),
98a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne                getDefaultSize2(getSuggestedMinimumHeight(), heightMeasureSpec));
99a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne    }
1003f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne}
101