1 /*
2  * Copyright (C) 2020 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.cellbroadcastreceiver;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.widget.ScrollView;
22 
23 /**
24  * Custom scroll view to display text with a max height inside a {@link CellBroadcastAlertDialog}
25  */
26 public class CustomHeightScrollView extends ScrollView {
27     private int mMaximumHeight = 0;
28 
CustomHeightScrollView(Context context)29     public CustomHeightScrollView(Context context) {
30         super(context);
31     }
32 
CustomHeightScrollView(Context context, AttributeSet attrs)33     public CustomHeightScrollView(Context context, AttributeSet attrs) {
34         super(context, attrs);
35     }
36 
CustomHeightScrollView(Context context, AttributeSet attrs, int defStyle)37     public CustomHeightScrollView(Context context, AttributeSet attrs, int defStyle) {
38         super(context, attrs, defStyle);
39     }
40 
setMaximumHeight(int height)41     public void setMaximumHeight(int height) {
42         mMaximumHeight = height;
43     }
44 
45     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)46     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
47         super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
48         if (mMaximumHeight > 0) {
49             int measuredHeight = getMeasuredHeight();
50             if (getLayoutParams().height != LayoutParams.MATCH_PARENT) {
51                 if (measuredHeight > mMaximumHeight) {
52                     setMeasuredDimension(getMeasuredWidth(), mMaximumHeight);
53                     return;
54                 }
55             }
56         }
57         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
58     }
59 }
60 
61