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