[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:
@@ -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",
|
||||||
|
@@ -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,18 +112,19 @@ 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 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();
|
final long now = System.currentTimeMillis();
|
||||||
if (historyStart == Long.MAX_VALUE) historyStart = now;
|
historyStart = now;
|
||||||
if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
|
historyEnd = now + 1;
|
||||||
|
}
|
||||||
|
|
||||||
boolean hasCycles = false;
|
boolean hasCycles = false;
|
||||||
if (policy != null) {
|
if (policy != null) {
|
||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user