Merge "[MS48.2] Remove NetworkStatsHistory from CycleAdaptor"

This commit is contained in:
Frank Li
2022-01-27 08:47:55 +00:00
committed by Gerrit Code Review
2 changed files with 57 additions and 16 deletions

View File

@@ -64,6 +64,7 @@ android_library {
"androidx.lifecycle_lifecycle-extensions", "androidx.lifecycle_lifecycle-extensions",
"guava", "guava",
"jsr305", "jsr305",
"net-utils-framework-common",
"settings-contextual-card-protos-lite", "settings-contextual-card-protos-lite",
"settings-log-bridge-protos-lite", "settings-log-bridge-protos-lite",
"contextualcards", "contextualcards",

View File

@@ -13,14 +13,17 @@
*/ */
package com.android.settings.datausage; package com.android.settings.datausage;
import android.annotation.NonNull;
import android.app.usage.NetworkStats;
import android.content.Context; import android.content.Context;
import android.net.NetworkPolicy; import android.net.NetworkPolicy;
import android.net.NetworkPolicyManager; import android.net.NetworkPolicyManager;
import android.net.NetworkStatsHistory;
import android.text.format.DateUtils; import android.text.format.DateUtils;
import android.util.Pair; import android.util.Pair;
import android.util.Range;
import android.widget.AdapterView; import android.widget.AdapterView;
import com.android.net.module.util.NetworkStatsUtils;
import com.android.settings.Utils; import com.android.settings.Utils;
import com.android.settingslib.net.ChartData; import com.android.settingslib.net.ChartData;
import com.android.settingslib.net.NetworkCycleData; import com.android.settingslib.net.NetworkCycleData;
@@ -62,9 +65,43 @@ public class CycleAdapter extends SettingsSpinnerAdapter<CycleAdapter.CycleItem>
return 0; return 0;
} }
protected static long getTotalBytesForTimeRange(List<NetworkStats.Bucket> stats,
Range<Long> range) {
long bytes = 0L;
for (NetworkStats.Bucket bucket : stats) {
final Range<Long> bucketSpan = new Range<>(
bucket.getStartTimeStamp(), bucket.getEndTimeStamp());
// Only record bytes that overlapped with the given time range. For partially
// overlapped bucket, record rational bytes assuming the traffic is uniform
// distributed within the bucket.
try {
final Range<Long> overlapped = range.intersect(bucketSpan);
final long totalOfBucket = bucket.getRxBytes() + bucket.getTxBytes();
bytes += NetworkStatsUtils.multiplySafeByRational(totalOfBucket,
overlapped.getUpper() - overlapped.getLower(),
bucketSpan.getUpper() - bucketSpan.getLower());
} catch (IllegalArgumentException e) {
// Range disjoint, ignore.
continue;
}
}
return bytes;
}
@NonNull
private Range getTimeRangeOf(@NonNull List<NetworkStats.Bucket> stats) {
long start = Long.MAX_VALUE;
long end = Long.MIN_VALUE;
for (NetworkStats.Bucket bucket : stats) {
start = Math.min(start, bucket.getStartTimeStamp());
end = Math.max(end, bucket.getEndTimeStamp());
}
return new Range(start, end);
}
/** /**
* Rebuild list based on {@link NetworkPolicy} and available * Rebuild list based on {@link NetworkPolicy} and available
* {@link NetworkStatsHistory} data. Always selects the newest item, * {@link List<NetworkStats.Bucket>} data. Always selects the newest item,
* updating the inspection range on chartData. * updating the inspection range on chartData.
*/ */
@Deprecated @Deprecated
@@ -75,19 +112,20 @@ public class CycleAdapter extends SettingsSpinnerAdapter<CycleAdapter.CycleItem>
clear(); clear();
final Context context = getContext(); final Context context = getContext();
NetworkStatsHistory.Entry entry = null;
long historyStart = Long.MAX_VALUE; long historyStart;
long historyEnd = Long.MIN_VALUE; long historyEnd;
if (chartData != null) { try {
historyStart = chartData.network.getStart(); final Range<Long> historyTimeRange = getTimeRangeOf(chartData.network);
historyEnd = chartData.network.getEnd(); historyStart = historyTimeRange.getLower();
historyEnd = historyTimeRange.getUpper();
} catch (IllegalArgumentException e) {
// Empty history.
final long now = System.currentTimeMillis();
historyStart = now;
historyEnd = now + 1;
} }
final long now = System.currentTimeMillis();
if (historyStart == Long.MAX_VALUE) historyStart = now;
if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
boolean hasCycles = false; boolean hasCycles = false;
if (policy != null) { if (policy != null) {
final Iterator<Pair<ZonedDateTime, ZonedDateTime>> it = NetworkPolicyManager final Iterator<Pair<ZonedDateTime, ZonedDateTime>> it = NetworkPolicyManager
@@ -99,8 +137,9 @@ public class CycleAdapter extends SettingsSpinnerAdapter<CycleAdapter.CycleItem>
final boolean includeCycle; final boolean includeCycle;
if (chartData != null) { if (chartData != null) {
entry = chartData.network.getValues(cycleStart, cycleEnd, entry); final long bytesInCycle = getTotalBytesForTimeRange(chartData.network,
includeCycle = (entry.rxBytes + entry.txBytes) > 0; new Range<>(cycleStart, cycleEnd));
includeCycle = bytesInCycle > 0;
} else { } else {
includeCycle = true; includeCycle = true;
} }
@@ -120,8 +159,9 @@ public class CycleAdapter extends SettingsSpinnerAdapter<CycleAdapter.CycleItem>
final boolean includeCycle; final boolean includeCycle;
if (chartData != null) { if (chartData != null) {
entry = chartData.network.getValues(cycleStart, cycleEnd, entry); final long bytesInCycle = getTotalBytesForTimeRange(chartData.network,
includeCycle = (entry.rxBytes + entry.txBytes) > 0; new Range<>(cycleStart, cycleEnd));
includeCycle = bytesInCycle > 0;
} else { } else {
includeCycle = true; includeCycle = true;
} }