Merge "Add TimeMeasurer for Spa"

This commit is contained in:
Chaohui Wang
2022-10-19 03:51:42 +00:00
committed by Android (Google) Code Review
2 changed files with 59 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
/*
* Copyright (C) 2022 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.
*/
@file:OptIn(ExperimentalTime::class)
package com.android.settingslib.spa.framework.compose
import android.util.Log
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import kotlin.time.ExperimentalTime
import kotlin.time.TimeSource
const val ENABLE_MEASURE_TIME = false
interface TimeMeasurer {
fun log(msg: String) {}
fun logFirst(msg: String) {}
companion object {
private object EmptyTimeMeasurer : TimeMeasurer
@Composable
fun rememberTimeMeasurer(tag: String): TimeMeasurer = remember {
if (ENABLE_MEASURE_TIME) TimeMeasurerImpl(tag) else EmptyTimeMeasurer
}
}
}
private class TimeMeasurerImpl(private val tag: String) : TimeMeasurer {
private val mark = TimeSource.Monotonic.markNow()
private val msgLogged = mutableSetOf<String>()
override fun log(msg: String) {
Log.d(tag, "Timer $msg: ${mark.elapsedNow()}")
}
override fun logFirst(msg: String) {
if (msgLogged.add(msg)) {
Log.d(tag, "Timer $msg: ${mark.elapsedNow()}")
}
}
}

View File

@@ -28,6 +28,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.lifecycle.viewmodel.compose.viewModel
import com.android.settingslib.spa.framework.compose.LogCompositions
import com.android.settingslib.spa.framework.compose.TimeMeasurer.Companion.rememberTimeMeasurer
import com.android.settingslib.spa.framework.compose.toState
import com.android.settingslib.spa.framework.theme.SettingsDimension
import com.android.settingslib.spa.widget.ui.PlaceholderTitle
@@ -66,7 +67,9 @@ private fun <T : AppRecord> AppListWidget(
listModel: AppListModel<T>,
appItem: @Composable (itemState: AppListItemModel<T>) -> Unit,
) {
val timeMeasurer = rememberTimeMeasurer(TAG)
appListData.value?.let { (list, option) ->
timeMeasurer.logFirst("app list first loaded")
if (list.isEmpty()) {
PlaceholderTitle(stringResource(R.string.no_applications))
return