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.systemui;
18 
19 import android.annotation.StyleRes;
20 import android.content.res.TypedArray;
21 import android.util.TypedValue;
22 import android.view.View;
23 import android.widget.TextView;
24 
25 /**
26  * Utility class to update the font size when the configuration has changed.
27  */
28 public final class FontSizeUtils {
29 
FontSizeUtils()30     private FontSizeUtils() {}
31 
updateFontSize(View parent, int viewId, int dimensId)32     public static void updateFontSize(View parent, int viewId, int dimensId) {
33         updateFontSize((TextView) parent.findViewById(viewId), dimensId);
34     }
35 
updateFontSize(TextView v, int dimensId)36     public static void updateFontSize(TextView v, int dimensId) {
37         if (v != null) {
38             v.setTextSize(TypedValue.COMPLEX_UNIT_PX,
39                     v.getResources().getDimensionPixelSize(dimensId));
40         }
41     }
42 
43     /**
44      * Updates the font size according to the style given.
45      *
46      * @param v     Text to update.
47      * @param resId Style applying to the text.
48      */
updateFontSizeFromStyle(TextView v, @StyleRes int resId)49     public static void updateFontSizeFromStyle(TextView v, @StyleRes int resId) {
50         int[] attrs = {android.R.attr.textSize};
51         int indexOfAttrTextSize = 0;
52         TypedArray ta = v.getContext().obtainStyledAttributes(resId, attrs);
53         int updatedTextPixelSize = ta.getDimensionPixelSize(indexOfAttrTextSize,
54                 (int) v.getTextSize());
55         v.setTextSize(TypedValue.COMPLEX_UNIT_PX, updatedTextPixelSize);
56         ta.recycle();
57     }
58 }
59