Improve the performance of AppDataUsagePreference

We only need aggregated summary, but NetworkCycleDataForUidLoader loads
usage for all cycles.

Create AppDataUsageSummaryRepository to reduce the system api call to
only once.

Fix: 304421722
Test: manual - on AppInfoSettings
Test: unit test
Change-Id: I115dfb51dbf77ed3fdde985aa1a968ff7462bebc
This commit is contained in:
Chaohui Wang
2023-10-10 13:18:30 +08:00
parent ceb23d4407
commit 252450b5fc
13 changed files with 391 additions and 211 deletions

View File

@@ -16,6 +16,8 @@
package com.android.settings.datausage.lib
import android.util.Range
/**
* Base data structure representing usage data in a period.
*/
@@ -23,4 +25,15 @@ data class NetworkUsageData(
val startTime: Long,
val endTime: Long,
val usage: Long,
)
) {
val timeRange = Range(startTime, endTime)
}
fun List<NetworkUsageData>.aggregate(): NetworkUsageData? = when {
isEmpty() -> null
else -> NetworkUsageData(
startTime = minOf { it.startTime },
endTime = maxOf { it.endTime },
usage = sumOf { it.usage },
)
}