1 /* <lambda>null2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.systemui.unfold 17 18 import android.content.Context 19 import android.os.Trace 20 import com.android.app.tracing.TraceStateLogger 21 import com.android.systemui.CoreStartable 22 import com.android.systemui.dagger.SysUISingleton 23 import com.android.systemui.dagger.qualifiers.Application 24 import com.android.systemui.dagger.qualifiers.Background 25 import com.android.systemui.unfold.data.repository.FoldStateRepository 26 import com.android.systemui.unfold.system.DeviceStateRepository 27 import javax.inject.Inject 28 import kotlin.coroutines.CoroutineContext 29 import kotlinx.coroutines.CoroutineScope 30 import kotlinx.coroutines.launch 31 import kotlinx.coroutines.plus 32 33 /** 34 * Logs several unfold related details in a trace. Mainly used for debugging and investigate 35 * droidfooders traces. 36 */ 37 @SysUISingleton 38 class UnfoldTraceLogger 39 @Inject 40 constructor( 41 private val context: Context, 42 private val foldStateRepository: FoldStateRepository, 43 @Application applicationScope: CoroutineScope, 44 @Background private val coroutineContext: CoroutineContext, 45 private val deviceStateRepository: DeviceStateRepository 46 ) : CoreStartable { 47 private val isFoldable: Boolean 48 get() = 49 context.resources 50 .getIntArray(com.android.internal.R.array.config_foldedDeviceStates) 51 .isNotEmpty() 52 53 private val bgScope = applicationScope.plus(coroutineContext) 54 55 override fun start() { 56 if (!isFoldable) return 57 58 bgScope.launch { 59 val foldUpdateLogger = TraceStateLogger("FoldUpdate") 60 foldStateRepository.foldUpdate.collect { foldUpdateLogger.log(it.name) } 61 } 62 63 bgScope.launch { 64 foldStateRepository.hingeAngle.collect { 65 Trace.traceCounter(Trace.TRACE_TAG_APP, "hingeAngle", it.toInt()) 66 } 67 } 68 bgScope.launch { 69 val foldedStateLogger = TraceStateLogger("FoldedState") 70 deviceStateRepository.isFolded.collect { isFolded -> 71 foldedStateLogger.log( 72 if (isFolded) { 73 "folded" 74 } else { 75 "unfolded" 76 } 77 ) 78 } 79 } 80 } 81 } 82