1aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette/*
2aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette * Copyright (C) 2015 The Android Open Source Project
3aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette *
4aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette * Licensed under the Apache License, Version 2.0 (the "License");
5aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette * you may not use this file except in compliance with the License.
6aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette * You may obtain a copy of the License at
7aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette *
8aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette *      http://www.apache.org/licenses/LICENSE-2.0
9aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette *
10aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette * Unless required by applicable law or agreed to in writing, software
11aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette * distributed under the License is distributed on an "AS IS" BASIS,
12aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette * See the License for the specific language governing permissions and
14aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette * limitations under the License.
15aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette */
16aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette
17aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverettepackage com.android.internal.widget;
18aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette
19aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viveretteimport android.content.Context;
20aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viveretteimport android.util.AttributeSet;
21aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viveretteimport android.view.View;
22aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette
23aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette/**
24aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette * Implementation of {@link android.widget.Space} that uses normal View drawing
25aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette * rather than a no-op. Useful for dialogs and other places where the base View
26aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette * class is too greedy when measured with AT_MOST.
27aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette */
28aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverettepublic final class DrawingSpace extends View {
29aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette    public DrawingSpace(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
30aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette        super(context, attrs, defStyleAttr, defStyleRes);
31aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette    }
32aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette
33aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette    public DrawingSpace(Context context, AttributeSet attrs, int defStyleAttr) {
34aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette        this(context, attrs, defStyleAttr, 0);
35aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette    }
36aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette
37aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette    public DrawingSpace(Context context, AttributeSet attrs) {
38aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette        this(context, attrs, 0);
39aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette    }
40aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette
41aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette    public DrawingSpace(Context context) {
42aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette        this(context, null);
43aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette    }
44aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette
45aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette    /**
46aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette     * Compare to: {@link View#getDefaultSize(int, int)}
47aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette     * <p>
48aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette     * If mode is AT_MOST, return the child size instead of the parent size
49aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette     * (unless it is too big).
50aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette     */
51aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette    private static int getDefaultSizeNonGreedy(int size, int measureSpec) {
52aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette        int result = size;
53aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette        int specMode = MeasureSpec.getMode(measureSpec);
54aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette        int specSize = MeasureSpec.getSize(measureSpec);
55aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette
56aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette        switch (specMode) {
57aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette            case MeasureSpec.UNSPECIFIED:
58aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette                result = size;
59aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette                break;
60aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette            case MeasureSpec.AT_MOST:
61aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette                result = Math.min(size, specSize);
62aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette                break;
63aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette            case MeasureSpec.EXACTLY:
64aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette                result = specSize;
65aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette                break;
66aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette        }
67aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette        return result;
68aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette    }
69aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette
70aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette    @Override
71aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
72aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette        setMeasuredDimension(
73aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette                getDefaultSizeNonGreedy(getSuggestedMinimumWidth(), widthMeasureSpec),
74aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette                getDefaultSizeNonGreedy(getSuggestedMinimumHeight(), heightMeasureSpec));
75aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette    }
76aa69922aceeea0b19639207d9e5f8bfd1f2486a7Alan Viverette}
77