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