• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.hardware.devicestate.DeviceStateManager
20 import com.android.app.tracing.TraceStateLogger
21 import com.android.app.tracing.coroutines.TrackTracer
22 import com.android.app.tracing.coroutines.launchTraced as launch
23 import com.android.systemui.CoreStartable
24 import com.android.systemui.dagger.SysUISingleton
25 import com.android.systemui.dagger.qualifiers.Application
26 import com.android.systemui.dagger.qualifiers.Background
27 import com.android.systemui.unfold.data.repository.FoldStateRepository
28 import com.android.systemui.unfold.system.DeviceStateRepository
29 import com.android.systemui.util.Utils.isDeviceFoldable
30 import javax.inject.Inject
31 import kotlin.coroutines.CoroutineContext
32 import kotlinx.coroutines.CoroutineScope
33 import kotlinx.coroutines.plus
34 
35 /**
36  * Logs several unfold related details in a trace. Mainly used for debugging and investigate
37  * droidfooders traces.
38  */
39 @SysUISingleton
40 class UnfoldTraceLogger
41 @Inject
42 constructor(
43     private val context: Context,
44     private val foldStateRepository: FoldStateRepository,
45     @Application applicationScope: CoroutineScope,
46     @Background private val coroutineContext: CoroutineContext,
47     private val deviceStateRepository: DeviceStateRepository,
48     private val deviceStateManager: DeviceStateManager,
49 ) : CoreStartable {
50     private val isFoldable: Boolean = isDeviceFoldable(context.resources, deviceStateManager)
51 
52     private val bgScope = applicationScope.plus(coroutineContext)
53 
54     override fun start() {
55         if (!isFoldable) return
56 
57         bgScope.launch {
58             val foldUpdateLogger = TraceStateLogger("FoldUpdate")
59             foldStateRepository.foldUpdate.collect { foldUpdateLogger.log(it.name) }
60         }
61 
62         bgScope.launch {
63             foldStateRepository.hingeAngle.collect {
64                 TrackTracer.instantForGroup("unfold", "hingeAngle", it.toInt())
65             }
66         }
67         bgScope.launch {
68             val foldedStateLogger = TraceStateLogger("FoldedState")
69             deviceStateRepository.isFolded.collect { isFolded ->
70                 foldedStateLogger.log(
71                     if (isFolded) {
72                         "folded"
73                     } else {
74                         "unfolded"
75                     }
76                 )
77             }
78         }
79     }
80 }
81