1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.settings.datausage; 16 17 import android.content.Context; 18 import android.content.res.TypedArray; 19 import android.net.NetworkPolicy; 20 import android.net.NetworkStatsHistory; 21 import android.net.TrafficStats; 22 import android.support.v7.preference.Preference; 23 import android.support.v7.preference.PreferenceViewHolder; 24 import android.text.SpannableStringBuilder; 25 import android.text.TextUtils; 26 import android.text.format.Formatter; 27 import android.text.style.ForegroundColorSpan; 28 import android.util.AttributeSet; 29 import android.util.SparseIntArray; 30 import com.android.settings.R; 31 import com.android.settings.Utils; 32 import com.android.settingslib.graph.UsageView; 33 34 public class ChartDataUsagePreference extends Preference { 35 36 // The resolution we show on the graph so that we can squash things down to ints. 37 // Set to half a meg for now. 38 private static final long RESOLUTION = TrafficStats.MB_IN_BYTES / 2; 39 40 private final int mWarningColor; 41 private final int mLimitColor; 42 43 private NetworkPolicy mPolicy; 44 private long mStart; 45 private long mEnd; 46 private NetworkStatsHistory mNetwork; 47 private int mSecondaryColor; 48 private int mSeriesColor; 49 ChartDataUsagePreference(Context context, AttributeSet attrs)50 public ChartDataUsagePreference(Context context, AttributeSet attrs) { 51 super(context, attrs); 52 setSelectable(false); 53 mLimitColor = Utils.getColorAttr(context, android.R.attr.colorError); 54 mWarningColor = Utils.getColorAttr(context, android.R.attr.textColorSecondary); 55 setLayoutResource(R.layout.data_usage_graph); 56 } 57 58 @Override onBindViewHolder(PreferenceViewHolder holder)59 public void onBindViewHolder(PreferenceViewHolder holder) { 60 super.onBindViewHolder(holder); 61 UsageView chart = (UsageView) holder.findViewById(R.id.data_usage); 62 if (mNetwork == null) return; 63 64 int top = getTop(); 65 chart.clearPaths(); 66 chart.configureGraph(toInt(mEnd - mStart), top, false, false); 67 calcPoints(chart); 68 chart.setBottomLabels(new CharSequence[] { 69 Utils.formatDateRange(getContext(), mStart, mStart), 70 Utils.formatDateRange(getContext(), mEnd, mEnd), 71 }); 72 73 bindNetworkPolicy(chart, mPolicy, top); 74 } 75 getTop()76 public int getTop() { 77 NetworkStatsHistory.Entry entry = null; 78 long totalData = 0; 79 final int start = mNetwork.getIndexBefore(mStart); 80 final int end = mNetwork.getIndexAfter(mEnd); 81 82 for (int i = start; i <= end; i++) { 83 entry = mNetwork.getValues(i, entry); 84 85 // increment by current bucket total 86 totalData += entry.rxBytes + entry.txBytes; 87 } 88 long policyMax = mPolicy != null ? Math.max(mPolicy.limitBytes, mPolicy.warningBytes) : 0; 89 return (int) (Math.max(totalData, policyMax) / RESOLUTION); 90 } 91 calcPoints(UsageView chart)92 private void calcPoints(UsageView chart) { 93 SparseIntArray points = new SparseIntArray(); 94 NetworkStatsHistory.Entry entry = null; 95 96 long totalData = 0; 97 98 final int start = mNetwork.getIndexAfter(mStart); 99 final int end = mNetwork.getIndexAfter(mEnd); 100 if (start < 0) return; 101 102 points.put(0, 0); 103 for (int i = start; i <= end; i++) { 104 entry = mNetwork.getValues(i, entry); 105 106 final long startTime = entry.bucketStart; 107 final long endTime = startTime + entry.bucketDuration; 108 109 // increment by current bucket total 110 totalData += entry.rxBytes + entry.txBytes; 111 112 points.put(toInt(startTime - mStart + 1), (int) (totalData / RESOLUTION)); 113 points.put(toInt(endTime - mStart), (int) (totalData / RESOLUTION)); 114 } 115 if (points.size() > 1) { 116 chart.addPath(points); 117 } 118 } 119 toInt(long l)120 private int toInt(long l) { 121 // Don't need that much resolution on these times. 122 return (int) (l / (1000 * 60)); 123 } 124 bindNetworkPolicy(UsageView chart, NetworkPolicy policy, int top)125 private void bindNetworkPolicy(UsageView chart, NetworkPolicy policy, int top) { 126 CharSequence[] labels = new CharSequence[3]; 127 int middleVisibility = 0; 128 int topVisibility = 0; 129 if (policy == null) { 130 return; 131 } 132 133 if (policy.limitBytes != NetworkPolicy.LIMIT_DISABLED) { 134 topVisibility = mLimitColor; 135 labels[2] = getLabel(policy.limitBytes, R.string.data_usage_sweep_limit, mLimitColor); 136 } 137 138 if (policy.warningBytes != NetworkPolicy.WARNING_DISABLED) { 139 chart.setDividerLoc((int) (policy.warningBytes / RESOLUTION)); 140 float weight = policy.warningBytes / RESOLUTION / (float) top; 141 float above = 1 - weight; 142 chart.setSideLabelWeights(above, weight); 143 middleVisibility = mWarningColor; 144 labels[1] = getLabel(policy.warningBytes, R.string.data_usage_sweep_warning, 145 mWarningColor); 146 } 147 148 chart.setSideLabels(labels); 149 chart.setDividerColors(middleVisibility, topVisibility); 150 } 151 getLabel(long bytes, int str, int mLimitColor)152 private CharSequence getLabel(long bytes, int str, int mLimitColor) { 153 Formatter.BytesResult result = Formatter.formatBytes(getContext().getResources(), 154 bytes, Formatter.FLAG_SHORTER); 155 CharSequence label = TextUtils.expandTemplate(getContext().getText(str), 156 result.value, result.units); 157 return new SpannableStringBuilder().append(label, new ForegroundColorSpan(mLimitColor), 0); 158 } 159 setNetworkPolicy(NetworkPolicy policy)160 public void setNetworkPolicy(NetworkPolicy policy) { 161 mPolicy = policy; 162 notifyChanged(); 163 } 164 setVisibleRange(long start, long end)165 public void setVisibleRange(long start, long end) { 166 mStart = start; 167 mEnd = end; 168 notifyChanged(); 169 } 170 getInspectStart()171 public long getInspectStart() { 172 return mStart; 173 } 174 getInspectEnd()175 public long getInspectEnd() { 176 return mEnd; 177 } 178 setNetworkStats(NetworkStatsHistory network)179 public void setNetworkStats(NetworkStatsHistory network) { 180 mNetwork = network; 181 notifyChanged(); 182 } 183 setColors(int seriesColor, int secondaryColor)184 public void setColors(int seriesColor, int secondaryColor) { 185 mSeriesColor = seriesColor; 186 mSecondaryColor = secondaryColor; 187 notifyChanged(); 188 } 189 } 190