Home | History | Annotate | Download | only in volume
      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 
     36     public ConfigurableTexts(Context context) {
     37         mContext = context;
     38     }
     39 
     40     public int add(final TextView text) {
     41         return add(text, -1);
     42     }
     43 
     44     public int add(final TextView text, final int labelResId) {
     45         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         });
     62         mTextLabels.put(text, labelResId);
     63         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 
     75     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 
     85     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             }
     91             for (int i = 0; i < mTextLabels.size(); i++) {
     92                 setTextLabelH(mTextLabels.keyAt(i), mTextLabels.valueAt(i));
     93             }
     94         }
     95     };
     96 }
     97