blob: 20de5bb334625e0a25472f9ef715edc45d425e6f [file] [log] [blame]
John Spurlockf88d8082015-03-25 18:09:51 -04001/*
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
17package com.android.systemui.volume;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.util.ArrayMap;
22import android.util.TypedValue;
23import android.view.View;
24import android.view.View.OnAttachStateChangeListener;
25import android.widget.TextView;
26
27/**
Julia Reynolds03c548f2016-12-14 15:02:38 -050028 * Class for updating textviews on configuration change.
John Spurlockf88d8082015-03-25 18:09:51 -040029 */
Julia Reynolds03c548f2016-12-14 15:02:38 -050030public class ConfigurableTexts {
John Spurlockf88d8082015-03-25 18:09:51 -040031
32 private final Context mContext;
33 private final ArrayMap<TextView, Integer> mTexts = new ArrayMap<>();
Julia Reynolds03c548f2016-12-14 15:02:38 -050034 private final ArrayMap<TextView, Integer> mTextLabels = new ArrayMap<>();
John Spurlockf88d8082015-03-25 18:09:51 -040035
Julia Reynolds03c548f2016-12-14 15:02:38 -050036 public ConfigurableTexts(Context context) {
John Spurlockf88d8082015-03-25 18:09:51 -040037 mContext = context;
38 }
39
40 public int add(final TextView text) {
Julia Reynolds03c548f2016-12-14 15:02:38 -050041 return add(text, -1);
42 }
43
44 public int add(final TextView text, final int labelResId) {
John Spurlockf88d8082015-03-25 18:09:51 -040045 if (text == null) return 0;
46 final Resources res = mContext.getResources();
47 final float fontScale = res.getConfiguration().fontScale;
48 final float density = res.getDisplayMetrics().density;
49 final float px = text.getTextSize();
50 final int sp = (int)(px / fontScale / density);
51 mTexts.put(text, sp);
52 text.addOnAttachStateChangeListener(new OnAttachStateChangeListener() {
53 @Override
54 public void onViewDetachedFromWindow(View v) {
55 }
56
57 @Override
58 public void onViewAttachedToWindow(View v) {
59 setTextSizeH(text, sp);
60 }
61 });
Julia Reynolds03c548f2016-12-14 15:02:38 -050062 mTextLabels.put(text, labelResId);
John Spurlockf88d8082015-03-25 18:09:51 -040063 return sp;
64 }
65
66 public void update() {
67 if (mTexts.isEmpty()) return;
68 mTexts.keyAt(0).post(mUpdateAll);
69 }
70
71 private void setTextSizeH(TextView text, int sp) {
72 text.setTextSize(TypedValue.COMPLEX_UNIT_SP, sp);
73 }
74
Julia Reynolds03c548f2016-12-14 15:02:38 -050075 private void setTextLabelH(TextView text, int labelResId) {
76 try {
77 if (labelResId >= 0) {
78 Util.setText(text, mContext.getString(labelResId));
79 }
80 } catch (Resources.NotFoundException e) {
81 // oh well.
82 }
83 }
84
John Spurlockf88d8082015-03-25 18:09:51 -040085 private final Runnable mUpdateAll = new Runnable() {
86 @Override
87 public void run() {
88 for (int i = 0; i < mTexts.size(); i++) {
89 setTextSizeH(mTexts.keyAt(i), mTexts.valueAt(i));
90 }
Julia Reynolds03c548f2016-12-14 15:02:38 -050091 for (int i = 0; i < mTextLabels.size(); i++) {
92 setTextLabelH(mTextLabels.keyAt(i), mTextLabels.valueAt(i));
93 }
John Spurlockf88d8082015-03-25 18:09:51 -040094 }
95 };
96}