1ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette/*
2ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette * Copyright (C) 2014 The Android Open Source Project
3ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette *
4ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette * Licensed under the Apache License, Version 2.0 (the "License");
5ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette * you may not use this file except in compliance with the License.
6ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette * You may obtain a copy of the License at
7ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette *
8ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette *      http://www.apache.org/licenses/LICENSE-2.0
9ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette *
10ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette * Unless required by applicable law or agreed to in writing, software
11ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette * distributed under the License is distributed on an "AS IS" BASIS,
12ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette * See the License for the specific language governing permissions and
14ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette * limitations under the License.
15ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette */
16ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette
17ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverettepackage com.android.internal.widget;
18ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette
19ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viveretteimport android.content.Context;
20ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viveretteimport android.util.AttributeSet;
21ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viveretteimport android.widget.ImageView;
22ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette
23ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette/**
24ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette * Extension of ImageView that correctly applies maxWidth and maxHeight.
25ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette */
26ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverettepublic class PreferenceImageView extends ImageView {
27ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette
28ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette    public PreferenceImageView(Context context) {
29ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette        this(context, null);
30ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette    }
31ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette
32ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette    public PreferenceImageView(Context context, AttributeSet attrs) {
33ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette        this(context, attrs, 0);
34ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette    }
35ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette
36ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette    public PreferenceImageView(Context context, AttributeSet attrs, int defStyleAttr) {
37ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette        this(context, attrs, defStyleAttr, 0);
38ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette    }
39ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette
40ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette    public PreferenceImageView(Context context, AttributeSet attrs, int defStyleAttr,
41ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette            int defStyleRes) {
42ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette        super(context, attrs, defStyleAttr, defStyleRes);
43ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette    }
44ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette
45ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette    @Override
46ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
47ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
48ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette        if (widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.UNSPECIFIED) {
49ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette            final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
50ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette            final int maxWidth = getMaxWidth();
51ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette            if (maxWidth != Integer.MAX_VALUE
52ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette                    && (maxWidth < widthSize || widthMode == MeasureSpec.UNSPECIFIED)) {
53ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette                widthMeasureSpec = MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST);
54ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette            }
55ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette        }
56ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette
57ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
58ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette        if (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED) {
59ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette            final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
60ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette            final int maxHeight = getMaxHeight();
61ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette            if (maxHeight != Integer.MAX_VALUE
62ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette                    && (maxHeight < heightSize || heightMode == MeasureSpec.UNSPECIFIED)) {
63ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette                heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
64ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette            }
65ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette        }
66ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette
67ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
68ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette    }
69ecd7c96aa4144224c1e6c47d079d043abb86839cAlan Viverette}
70