1 /* 2 * Copyright (C) 2015 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.volume; 18 19 import android.content.Context; 20 import android.content.res.Resources; 21 import android.util.ArrayMap; 22 import android.util.TypedValue; 23 import android.view.View; 24 import android.view.View.OnAttachStateChangeListener; 25 import android.widget.TextView; 26 27 /** 28 * Class for updating textviews on configuration change. 29 */ 30 public class ConfigurableTexts { 31 32 private final Context mContext; 33 private final ArrayMap<TextView, Integer> mTexts = new ArrayMap<>(); 34 private final ArrayMap<TextView, Integer> mTextLabels = new ArrayMap<>(); 35 ConfigurableTexts(Context context)36 public ConfigurableTexts(Context context) { 37 mContext = context; 38 } 39 add(final TextView text)40 public int add(final TextView text) { 41 return add(text, -1); 42 } 43 add(final TextView text, final int labelResId)44 public int add(final TextView text, final int labelResId) { 45 if (text == null) return 0; 46 if (mTexts.containsKey(text)) { 47 return mTexts.get(text); 48 } 49 final Resources res = mContext.getResources(); 50 final float fontScale = res.getConfiguration().fontScale; 51 final float density = res.getDisplayMetrics().density; 52 final float px = text.getTextSize(); 53 final int sp = (int)(px / fontScale / density); 54 mTexts.put(text, sp); 55 text.addOnAttachStateChangeListener(new OnAttachStateChangeListener() { 56 @Override 57 public void onViewDetachedFromWindow(View v) { 58 } 59 60 @Override 61 public void onViewAttachedToWindow(View v) { 62 setTextSizeH(text, sp); 63 } 64 }); 65 mTextLabels.put(text, labelResId); 66 return sp; 67 } 68 remove(final TextView text)69 public void remove(final TextView text) { 70 mTexts.remove(text); 71 mTextLabels.remove(text); 72 } 73 update()74 public void update() { 75 if (mTexts.isEmpty()) return; 76 mTexts.keyAt(0).post(mUpdateAll); 77 } 78 setTextSizeH(TextView text, int sp)79 private void setTextSizeH(TextView text, int sp) { 80 text.setTextSize(TypedValue.COMPLEX_UNIT_SP, sp); 81 } 82 setTextLabelH(TextView text, int labelResId)83 private void setTextLabelH(TextView text, int labelResId) { 84 try { 85 if (labelResId >= 0) { 86 Util.setText(text, mContext.getString(labelResId)); 87 } 88 } catch (Resources.NotFoundException e) { 89 // oh well. 90 } 91 } 92 93 private final Runnable mUpdateAll = new Runnable() { 94 @Override 95 public void run() { 96 for (int i = 0; i < mTexts.size(); i++) { 97 setTextSizeH(mTexts.keyAt(i), mTexts.valueAt(i)); 98 } 99 for (int i = 0; i < mTextLabels.size(); i++) { 100 setTextLabelH(mTextLabels.keyAt(i), mTextLabels.valueAt(i)); 101 } 102 } 103 }; 104 } 105