• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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 com.android.internal.widget;
18 
19 import android.compat.annotation.UnsupportedAppUsage;
20 import android.content.Context;
21 import android.os.Build;
22 import android.util.AttributeSet;
23 import android.widget.ImageView;
24 
25 /**
26  * Extension of ImageView that correctly applies maxWidth and maxHeight.
27  */
28 public class PreferenceImageView extends ImageView {
29 
PreferenceImageView(Context context)30     public PreferenceImageView(Context context) {
31         this(context, null);
32     }
33 
34     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
PreferenceImageView(Context context, AttributeSet attrs)35     public PreferenceImageView(Context context, AttributeSet attrs) {
36         this(context, attrs, 0);
37     }
38 
PreferenceImageView(Context context, AttributeSet attrs, int defStyleAttr)39     public PreferenceImageView(Context context, AttributeSet attrs, int defStyleAttr) {
40         this(context, attrs, defStyleAttr, 0);
41     }
42 
PreferenceImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)43     public PreferenceImageView(Context context, AttributeSet attrs, int defStyleAttr,
44             int defStyleRes) {
45         super(context, attrs, defStyleAttr, defStyleRes);
46     }
47 
48     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)49     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
50         final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
51         if (widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.UNSPECIFIED) {
52             final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
53             final int maxWidth = getMaxWidth();
54             if (maxWidth != Integer.MAX_VALUE
55                     && (maxWidth < widthSize || widthMode == MeasureSpec.UNSPECIFIED)) {
56                 widthMeasureSpec = MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST);
57             }
58         }
59 
60         final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
61         if (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED) {
62             final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
63             final int maxHeight = getMaxHeight();
64             if (maxHeight != Integer.MAX_VALUE
65                     && (maxHeight < heightSize || heightMode == MeasureSpec.UNSPECIFIED)) {
66                 heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
67             }
68         }
69 
70         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
71     }
72 }
73