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.net.NetworkPolicy; 19 import android.net.NetworkStatsHistory; 20 import android.net.TrafficStats; 21 import android.support.annotation.VisibleForTesting; 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 31 import com.android.settings.R; 32 import com.android.settings.Utils; 33 import com.android.settings.graph.UsageView; 34 35 public class ChartDataUsagePreference extends Preference { 36 37 // The resolution we show on the graph so that we can squash things down to ints. 38 // Set to half a meg for now. 39 private static final long RESOLUTION = TrafficStats.MB_IN_BYTES / 2; 40 41 private final int mWarningColor; 42 private final int mLimitColor; 43 44 private NetworkPolicy mPolicy; 45 private long mStart; 46 private long mEnd; 47 private NetworkStatsHistory mNetwork; 48 private int mSecondaryColor; 49 private int mSeriesColor; 50 51 public ChartDataUsagePreference(Context context, AttributeSet attrs) { 52 super(context, attrs); 53 setSelectable(false); 54 mLimitColor = Utils.getColorAttr(context, android.R.attr.colorError); 55 mWarningColor = Utils.getColorAttr(context, android.R.attr.textColorSecondary); 56 setLayoutResource(R.layout.data_usage_graph); 57 } 58 59 @Override 60 public void onBindViewHolder(PreferenceViewHolder holder) { 61 super.onBindViewHolder(holder); 62 UsageView chart = (UsageView) holder.findViewById(R.id.data_usage); 63 if (mNetwork == null) return; 64 65 int top = getTop(); 66 chart.clearPaths(); 67 chart.configureGraph(toInt(mEnd - mStart), top); 68 calcPoints(chart); 69 chart.setBottomLabels(new CharSequence[] { 70 Utils.formatDateRange(getContext(), mStart, mStart), 71 Utils.formatDateRange(getContext(), mEnd, mEnd), 72 }); 73 74 bindNetworkPolicy(chart, mPolicy, top); 75 } 76 77 public int getTop() { 78 NetworkStatsHistory.Entry entry = null; 79 long totalData = 0; 80 final int start = mNetwork.getIndexBefore(mStart); 81 final int end = mNetwork.getIndexAfter(mEnd); 82 83 for (int i = start; i <= end; i++) { 84 entry = mNetwork.getValues(i, entry); 85 86 // increment by current bucket total 87 totalData += entry.rxBytes + entry.txBytes; 88 } 89 long policyMax = mPolicy != null ? Math.max(mPolicy.limitBytes, mPolicy.warningBytes) : 0; 90 return (int) (Math.max(totalData, policyMax) / RESOLUTION); 91 } 92 93 @VisibleForTesting 94 void calcPoints(UsageView chart) { 95 SparseIntArray points = new SparseIntArray(); 96 NetworkStatsHistory.Entry entry = null; 97 98 long totalData = 0; 99 100 final int start = mNetwork.getIndexAfter(mStart); 101 final int end = mNetwork.getIndexAfter(mEnd); 102 if (start < 0) return; 103 104 points.put(0, 0); 105 for (int i = start; i <= end; i++) { 106 entry = mNetwork.getValues(i, entry); 107 108 final long startTime = entry.bucketStart; 109 final long endTime = startTime + entry.bucketDuration; 110 111 // increment by current bucket total 112 totalData += entry.rxBytes + entry.txBytes; 113 114 if (i == 0) { 115 points.put(toInt(startTime - mStart) - 1, -1); 116 } 117 points.put(toInt(startTime - mStart + 1), (int) (totalData / RESOLUTION)); 118 points.put(toInt(endTime - mStart), (int) (totalData / RESOLUTION)); 119 } 120 if (points.size() > 1) { 121 chart.addPath(points); 122 } 123 } 124 125 private int toInt(long l) { 126 // Don't need that much resolution on these times. 127 return (int) (l / (1000 * 60)); 128 } 129 130 private void bindNetworkPolicy(UsageView chart, NetworkPolicy policy, int top) { 131 CharSequence[] labels = new CharSequence[3]; 132 int middleVisibility = 0; 133 int topVisibility = 0; 134 if (policy == null) { 135 return; 136 } 137 138 if (policy.limitBytes != NetworkPolicy.LIMIT_DISABLED) { 139 topVisibility = mLimitColor; 140 labels[2] = getLabel(policy.limitBytes, R.string.data_usage_sweep_limit, mLimitColor); 141 } 142 143 if (policy.warningBytes != NetworkPolicy.WARNING_DISABLED) { 144 chart.setDividerLoc((int) (policy.warningBytes / RESOLUTION)); 145 float weight = policy.warningBytes / RESOLUTION / (float) top; 146 float above = 1 - weight; 147 chart.setSideLabelWeights(above, weight); 148 middleVisibility = mWarningColor; 149 labels[1] = getLabel(policy.warningBytes, R.string.data_usage_sweep_warning, 150 mWarningColor); 151 } 152 153 chart.setSideLabels(labels); 154 chart.setDividerColors(middleVisibility, topVisibility); 155 } 156 157 private CharSequence getLabel(long bytes, int str, int mLimitColor) { 158 Formatter.BytesResult result = Formatter.formatBytes(getContext().getResources(), 159 bytes, Formatter.FLAG_SHORTER | Formatter.FLAG_IEC_UNITS); 160 CharSequence label = TextUtils.expandTemplate(getContext().getText(str), 161 result.value, result.units); 162 return new SpannableStringBuilder().append(label, new ForegroundColorSpan(mLimitColor), 0); 163 } 164 165 public void setNetworkPolicy(NetworkPolicy policy) { 166 mPolicy = policy; 167 notifyChanged(); 168 } 169 170 public void setVisibleRange(long start, long end) { 171 mStart = start; 172 mEnd = end; 173 notifyChanged(); 174 } 175 176 public long getInspectStart() { 177 return mStart; 178 } 179 180 public long getInspectEnd() { 181 return mEnd; 182 } 183 184 public void setNetworkStats(NetworkStatsHistory network) { 185 mNetwork = network; 186 notifyChanged(); 187 } 188 189 public void setColors(int seriesColor, int secondaryColor) { 190 mSeriesColor = seriesColor; 191 mSecondaryColor = secondaryColor; 192 notifyChanged(); 193 } 194 } 195