Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c83021b8 authored by Chaohui Wang's avatar Chaohui Wang Committed by Android (Google) Code Review
Browse files

Merge "Add TimeMeasurer for Spa"

parents 80d38949 aba3bba3
Loading
Loading
Loading
Loading
+56 −0
Original line number Diff line number Diff line
/*
 * 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()}")
        }
    }
}
+3 −0
Original line number Diff line number Diff line
@@ -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