Files
app_Settings/src/com/android/settings/datausage/DataUsageListHeaderController.kt
Chaohui Wang 2205762482 Improve the latency of DataUsageList
Up to 30x faster.

Currently it load the usage detail for every day at the beginning, so
it's quite slow.

To fix,
- Not load the usage detail for every day at the beginning
- Load only the cycles first
- And only load the daily detail for the selected month

Fix: 290856342
Test: manual - on DataUsageList (cell & wifi)
Test: unit tests
Change-Id: Ie18fa68f801743389bd6b6a28e236dcf1fea00e4
2023-09-27 08:05:02 +00:00

125 lines
4.8 KiB
Kotlin

/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.datausage
import android.net.NetworkTemplate
import android.os.Bundle
import android.util.Range
import android.view.View
import android.view.accessibility.AccessibilityEvent
import android.widget.AdapterView
import android.widget.Spinner
import androidx.annotation.OpenForTesting
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.android.settings.R
import com.android.settings.core.SubSettingLauncher
import com.android.settings.datausage.CycleAdapter.SpinnerInterface
import com.android.settings.datausage.lib.INetworkCycleDataRepository
import com.android.settings.datausage.lib.NetworkCycleDataRepository
import com.android.settings.datausage.lib.NetworkUsageData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@OpenForTesting
open class DataUsageListHeaderController(
header: View,
template: NetworkTemplate,
sourceMetricsCategory: Int,
viewLifecycleOwner: LifecycleOwner,
private val onCyclesLoad: (usageDataList: List<NetworkUsageData>) -> Unit,
private val onItemSelected: (usageData: NetworkUsageData) -> Unit,
private val repository: INetworkCycleDataRepository =
NetworkCycleDataRepository(header.context, template),
) {
private val context = header.context
private val configureButton: View = header.requireViewById(R.id.filter_settings)
private val cycleSpinner: Spinner = header.requireViewById(R.id.filter_spinner)
private val cycleAdapter = CycleAdapter(context, object : SpinnerInterface {
override fun setAdapter(cycleAdapter: CycleAdapter) {
cycleSpinner.adapter = cycleAdapter
}
override fun getSelectedItem() = cycleSpinner.selectedItem
override fun setSelection(position: Int) {
cycleSpinner.setSelection(position)
}
})
private var cycles: List<NetworkUsageData> = emptyList()
private val cycleListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
if (0 <= position && position < cycleAdapter.count) {
cycles.getOrNull(position)?.let(onItemSelected)
}
}
override fun onNothingSelected(parent: AdapterView<*>?) {
// ignored
}
}
init {
configureButton.setOnClickListener {
val args = Bundle().apply {
putParcelable(DataUsageList.EXTRA_NETWORK_TEMPLATE, template)
}
SubSettingLauncher(context).apply {
setDestination(BillingCycleSettings::class.java.name)
setTitleRes(R.string.billing_cycle)
setSourceMetricsCategory(sourceMetricsCategory)
setArguments(args)
}.launch()
}
cycleSpinner.visibility = View.GONE
cycleSpinner.accessibilityDelegate = object : View.AccessibilityDelegate() {
override fun sendAccessibilityEvent(host: View, eventType: Int) {
// Ignore TYPE_VIEW_SELECTED or TalkBack will speak for it at onResume.
if (eventType == AccessibilityEvent.TYPE_VIEW_SELECTED) return
super.sendAccessibilityEvent(host, eventType)
}
}
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
cycles = withContext(Dispatchers.Default) {
repository.loadCycles()
}
updateCycleData()
}
}
}
open fun setConfigButtonVisible(visible: Boolean) {
configureButton.visibility = if (visible) View.VISIBLE else View.GONE
}
private fun updateCycleData() {
cycleSpinner.onItemSelectedListener = cycleListener
// calculate policy cycles based on available data
// generate cycle list based on policy and available history
cycleAdapter.updateCycleList(cycles.map { Range(it.startTime, it.endTime) })
cycleSpinner.visibility = View.VISIBLE
onCyclesLoad(cycles)
}
}