Home | History | Annotate | Download | only in tiles
      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.qs.tiles;
     18 
     19 import android.content.Context;
     20 import android.content.res.Configuration;
     21 import android.content.res.Resources;
     22 import android.util.AttributeSet;
     23 import android.view.View;
     24 import android.widget.LinearLayout;
     25 import android.widget.TextView;
     26 
     27 import com.android.systemui.FontSizeUtils;
     28 import com.android.systemui.R;
     29 import com.android.systemui.qs.DataUsageGraph;
     30 import com.android.systemui.statusbar.policy.NetworkController;
     31 
     32 import java.text.DecimalFormat;
     33 
     34 /**
     35  * Layout for the data usage detail in quick settings.
     36  */
     37 public class DataUsageDetailView extends LinearLayout {
     38 
     39     private static final double KB = 1024;
     40     private static final double MB = 1024 * KB;
     41     private static final double GB = 1024 * MB;
     42 
     43     private final DecimalFormat FORMAT = new DecimalFormat("#.##");
     44 
     45     public DataUsageDetailView(Context context, AttributeSet attrs) {
     46         super(context, attrs);
     47     }
     48 
     49     @Override
     50     protected void onConfigurationChanged(Configuration newConfig) {
     51         super.onConfigurationChanged(newConfig);
     52         FontSizeUtils.updateFontSize(this, android.R.id.title, R.dimen.qs_data_usage_text_size);
     53         FontSizeUtils.updateFontSize(this, R.id.usage_text, R.dimen.qs_data_usage_usage_text_size);
     54         FontSizeUtils.updateFontSize(this, R.id.usage_carrier_text,
     55                 R.dimen.qs_data_usage_text_size);
     56         FontSizeUtils.updateFontSize(this, R.id.usage_info_top_text,
     57                 R.dimen.qs_data_usage_text_size);
     58         FontSizeUtils.updateFontSize(this, R.id.usage_period_text, R.dimen.qs_data_usage_text_size);
     59         FontSizeUtils.updateFontSize(this, R.id.usage_info_bottom_text,
     60                 R.dimen.qs_data_usage_text_size);
     61     }
     62 
     63     public void bind(NetworkController.MobileDataController.DataUsageInfo info) {
     64         final Resources res = mContext.getResources();
     65         final int titleId;
     66         final long bytes;
     67         int usageColor = R.color.system_accent_color;
     68         final String top;
     69         String bottom = null;
     70         if (info.usageLevel < info.warningLevel || info.limitLevel <= 0) {
     71             // under warning, or no limit
     72             titleId = R.string.quick_settings_cellular_detail_data_usage;
     73             bytes = info.usageLevel;
     74             top = res.getString(R.string.quick_settings_cellular_detail_data_warning,
     75                     formatBytes(info.warningLevel));
     76         } else if (info.usageLevel <= info.limitLevel) {
     77             // over warning, under limit
     78             titleId = R.string.quick_settings_cellular_detail_remaining_data;
     79             bytes = info.limitLevel - info.usageLevel;
     80             top = res.getString(R.string.quick_settings_cellular_detail_data_used,
     81                     formatBytes(info.usageLevel));
     82             bottom = res.getString(R.string.quick_settings_cellular_detail_data_limit,
     83                     formatBytes(info.limitLevel));
     84         } else {
     85             // over limit
     86             titleId = R.string.quick_settings_cellular_detail_over_limit;
     87             bytes = info.usageLevel - info.limitLevel;
     88             top = res.getString(R.string.quick_settings_cellular_detail_data_used,
     89                     formatBytes(info.usageLevel));
     90             bottom = res.getString(R.string.quick_settings_cellular_detail_data_limit,
     91                     formatBytes(info.limitLevel));
     92             usageColor = R.color.system_warning_color;
     93         }
     94 
     95         final TextView title = (TextView) findViewById(android.R.id.title);
     96         title.setText(titleId);
     97         final TextView usage = (TextView) findViewById(R.id.usage_text);
     98         usage.setText(formatBytes(bytes));
     99         usage.setTextColor(res.getColor(usageColor));
    100         final DataUsageGraph graph = (DataUsageGraph) findViewById(R.id.usage_graph);
    101         graph.setLevels(info.limitLevel, info.warningLevel, info.usageLevel);
    102         final TextView carrier = (TextView) findViewById(R.id.usage_carrier_text);
    103         carrier.setText(info.carrier);
    104         final TextView period = (TextView) findViewById(R.id.usage_period_text);
    105         period.setText(info.period);
    106         final TextView infoTop = (TextView) findViewById(R.id.usage_info_top_text);
    107         infoTop.setVisibility(top != null ? View.VISIBLE : View.GONE);
    108         infoTop.setText(top);
    109         final TextView infoBottom = (TextView) findViewById(R.id.usage_info_bottom_text);
    110         infoBottom.setVisibility(bottom != null ? View.VISIBLE : View.GONE);
    111         infoBottom.setText(bottom);
    112     }
    113 
    114     private String formatBytes(long bytes) {
    115         final long b = Math.abs(bytes);
    116         double val;
    117         String suffix;
    118         if (b > 100 * MB) {
    119             val = b / GB;
    120             suffix = "GB";
    121         } else if (b > 100 * KB) {
    122             val = b / MB;
    123             suffix = "MB";
    124         } else {
    125             val = b / KB;
    126             suffix = "KB";
    127         }
    128         return FORMAT.format(val * (bytes < 0 ? -1 : 1)) + " " + suffix;
    129     }
    130 }
    131