[MS48.2] Remove NetworkStatsHistory from CycleAdaptor

While the ChartData changes the types of stored data.
Modify CycleAdaptor accordlingly for the compatibility.

Test: atest clockwork-settings-robotests
      make RunSettingsRoboTests -j40
      make RunSettingsLibRoboTests
           ROBOTEST_FILTER=DataUsageControllerTest
      make RunSettingsLibRoboTests
           ROBOTEST_FILTER=NetworkCycleChartDataLoaderTest
      make RunSettingsLibRoboTests
           ROBOTEST_FILTER=NetworkCycleDataForUidLoaderTest
      make RunSettingsLibRoboTests
           ROBOTEST_FILTER=NetworkCycleDataLoaderTest
      make RunSettingsLibRoboTests
           ROBOTEST_FILTER=DataUsageUtilsTest
Bug: 204830222
Ignore-AOSP-First: Related API conflict, need master first.

  (cherry-picked from ag/16686514)

Change-Id: Ia778d680d5354fb67476db0763dfab017284dc4a
Merged-In: Ia778d680d5354fb67476db0763dfab017284dc4a
This commit is contained in:
Junyu Lai
2022-01-10 10:39:00 +00:00
committed by Frank Li
parent fa785d2c86
commit d7451d16ff
2 changed files with 57 additions and 16 deletions

View File

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

View File

@@ -13,14 +13,17 @@
*/
package com.android.settings.datausage;
import android.annotation.NonNull;
import android.app.usage.NetworkStats;
import android.content.Context;
import android.net.NetworkPolicy;
import android.net.NetworkPolicyManager;
import android.net.NetworkStatsHistory;
import android.text.format.DateUtils;
import android.util.Pair;
import android.util.Range;
import android.widget.AdapterView;
import com.android.net.module.util.NetworkStatsUtils;
import com.android.settings.Utils;
import com.android.settingslib.net.ChartData;
import com.android.settingslib.net.NetworkCycleData;
@@ -62,9 +65,43 @@ public class CycleAdapter extends SettingsSpinnerAdapter<CycleAdapter.CycleItem>
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
* {@link NetworkStatsHistory} data. Always selects the newest item,
* {@link List<NetworkStats.Bucket>} data. Always selects the newest item,
* updating the inspection range on chartData.
*/
@Deprecated
@@ -75,19 +112,20 @@ public class CycleAdapter extends SettingsSpinnerAdapter<CycleAdapter.CycleItem>
clear();
final Context context = getContext();
NetworkStatsHistory.Entry entry = null;
long historyStart = Long.MAX_VALUE;
long historyEnd = Long.MIN_VALUE;
if (chartData != null) {
historyStart = chartData.network.getStart();
historyEnd = chartData.network.getEnd();
long historyStart;
long historyEnd;
try {
final Range<Long> historyTimeRange = getTimeRangeOf(chartData.network);
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;
if (policy != null) {
final Iterator<Pair<ZonedDateTime, ZonedDateTime>> it = NetworkPolicyManager
@@ -99,8 +137,9 @@ public class CycleAdapter extends SettingsSpinnerAdapter<CycleAdapter.CycleItem>
final boolean includeCycle;
if (chartData != null) {
entry = chartData.network.getValues(cycleStart, cycleEnd, entry);
includeCycle = (entry.rxBytes + entry.txBytes) > 0;
final long bytesInCycle = getTotalBytesForTimeRange(chartData.network,
new Range<>(cycleStart, cycleEnd));
includeCycle = bytesInCycle > 0;
} else {
includeCycle = true;
}
@@ -120,8 +159,9 @@ public class CycleAdapter extends SettingsSpinnerAdapter<CycleAdapter.CycleItem>
final boolean includeCycle;
if (chartData != null) {
entry = chartData.network.getValues(cycleStart, cycleEnd, entry);
includeCycle = (entry.rxBytes + entry.txBytes) > 0;
final long bytesInCycle = getTotalBytesForTimeRange(chartData.network,
new Range<>(cycleStart, cycleEnd));
includeCycle = bytesInCycle > 0;
} else {
includeCycle = true;
}