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 Milneimport android.view.ViewGroup;
243f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne
253f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne/**
263f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Space is a lightweight View subclass that may be used to create gaps between components
273f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * in general purpose layouts.
283f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */
293f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milnepublic final class Space extends View {
303f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    /**
313f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne     * {@inheritDoc}
323f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne     */
333f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    public Space(Context context, AttributeSet attrs, int defStyle) {
343f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne        super(context, attrs, defStyle);
354a145d72622772b920f60195e80942058984259cPhilip Milne        if (getVisibility() == VISIBLE) {
364a145d72622772b920f60195e80942058984259cPhilip Milne            setVisibility(INVISIBLE);
374a145d72622772b920f60195e80942058984259cPhilip Milne        }
383f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    }
393f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne
403f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    /**
413f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne     * {@inheritDoc}
423f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne     */
433f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    public Space(Context context, AttributeSet attrs) {
444a145d72622772b920f60195e80942058984259cPhilip Milne        this(context, attrs, 0);
453f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    }
463f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne
473f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    /**
483f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne     * {@inheritDoc}
493f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne     */
503f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    public Space(Context context) {
514a145d72622772b920f60195e80942058984259cPhilip Milne        //noinspection NullableProblems
524a145d72622772b920f60195e80942058984259cPhilip Milne        this(context, null);
533f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    }
543f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne
553f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    /**
563f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne     * Draw nothing.
573f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne     *
583f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne     * @param canvas an unused parameter.
593f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne     */
603f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    @Override
613f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    public void draw(Canvas canvas) {
623f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    }
633f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne
643f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne    /**
65a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne     * Compare to: {@link View#getDefaultSize(int, int)}
66a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne     * If mode is AT_MOST, return the child size instead of the parent size
67a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne     * (unless it is too big).
68a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne     */
69a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne    private static int getDefaultSize2(int size, int measureSpec) {
70a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne        int result = size;
71a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne        int specMode = MeasureSpec.getMode(measureSpec);
72a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne        int specSize = MeasureSpec.getSize(measureSpec);
73a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne
74a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne        switch (specMode) {
75a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne            case MeasureSpec.UNSPECIFIED:
76a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne                result = size;
77a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne                break;
78a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne            case MeasureSpec.AT_MOST:
79a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne                result = Math.min(size, specSize);
80a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne                break;
81a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne            case MeasureSpec.EXACTLY:
82a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne                result = specSize;
83a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne                break;
84a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne        }
85a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne        return result;
86a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne    }
87a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne
88a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne    @Override
89a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
90a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne        setMeasuredDimension(
91a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne                getDefaultSize2(getSuggestedMinimumWidth(), widthMeasureSpec),
92a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne                getDefaultSize2(getSuggestedMinimumHeight(), heightMeasureSpec));
93a789cafb066fd98e0e7f7ce506e3defa8104ba80Philip Milne    }
943f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne}
95