1 /*
2  * Copyright 2018 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 
17 package androidx.preference.internal;
18 
19 import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
20 
21 import android.content.Context;
22 import android.content.res.TypedArray;
23 import android.util.AttributeSet;
24 import android.widget.ImageView;
25 
26 import androidx.annotation.RestrictTo;
27 import androidx.preference.R;
28 
29 /**
30  * Extension of ImageView that correctly applies maxWidth and maxHeight.
31  * @hide
32  */
33 @RestrictTo(LIBRARY_GROUP)
34 public class PreferenceImageView extends ImageView {
35 
36     private int mMaxWidth = Integer.MAX_VALUE;
37     private int mMaxHeight = Integer.MAX_VALUE;
38 
PreferenceImageView(Context context)39     public PreferenceImageView(Context context) {
40         this(context, null);
41     }
42 
PreferenceImageView(Context context, AttributeSet attrs)43     public PreferenceImageView(Context context, AttributeSet attrs) {
44         this(context, attrs, 0);
45     }
46 
PreferenceImageView(Context context, AttributeSet attrs, int defStyleAttr)47     public PreferenceImageView(Context context, AttributeSet attrs, int defStyleAttr) {
48         super(context, attrs, defStyleAttr);
49 
50         final TypedArray a = context.obtainStyledAttributes(
51                 attrs, R.styleable.PreferenceImageView, defStyleAttr, 0);
52 
53         setMaxWidth(a.getDimensionPixelSize(
54                 R.styleable.PreferenceImageView_maxWidth, Integer.MAX_VALUE));
55 
56         setMaxHeight(a.getDimensionPixelSize(
57                 R.styleable.PreferenceImageView_maxHeight, Integer.MAX_VALUE));
58 
59         a.recycle();
60     }
61 
62 //    public PreferenceImageView(Context context, AttributeSet attrs, int defStyleAttr,
63 //            int defStyleRes) {
64 //        super(context, attrs, defStyleAttr, defStyleRes);
65 //    }
66 
67     @Override
setMaxWidth(int maxWidth)68     public void setMaxWidth(int maxWidth) {
69         mMaxWidth = maxWidth;
70         super.setMaxWidth(maxWidth);
71     }
72 
73     @Override
getMaxWidth()74     public int getMaxWidth() {
75         return mMaxWidth;
76     }
77 
78     @Override
setMaxHeight(int maxHeight)79     public void setMaxHeight(int maxHeight) {
80         mMaxHeight = maxHeight;
81         super.setMaxHeight(maxHeight);
82     }
83 
84     @Override
getMaxHeight()85     public int getMaxHeight() {
86         return mMaxHeight;
87     }
88 
89     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)90     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
91         final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
92         if (widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.UNSPECIFIED) {
93             final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
94             final int maxWidth = getMaxWidth();
95             if (maxWidth != Integer.MAX_VALUE
96                     && (maxWidth < widthSize || widthMode == MeasureSpec.UNSPECIFIED)) {
97                 widthMeasureSpec = MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST);
98             }
99         }
100 
101         final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
102         if (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED) {
103             final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
104             final int maxHeight = getMaxHeight();
105             if (maxHeight != Integer.MAX_VALUE
106                     && (maxHeight < heightSize || heightMode == MeasureSpec.UNSPECIFIED)) {
107                 heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
108             }
109         }
110 
111         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
112     }
113 }
114