1 /* 2 * Copyright (C) 2024 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 17 package com.android.systemui.communal 18 19 import android.app.StatsManager 20 import android.util.StatsEvent 21 import com.android.systemui.CoreStartable 22 import com.android.systemui.communal.domain.interactor.CommunalInteractor 23 import com.android.systemui.communal.domain.interactor.CommunalSettingsInteractor 24 import com.android.systemui.communal.shared.log.CommunalMetricsLogger 25 import com.android.systemui.dagger.SysUISingleton 26 import com.android.systemui.dagger.qualifiers.Background 27 import com.android.systemui.shared.system.SysUiStatsLog 28 import java.util.concurrent.Executor 29 import javax.inject.Inject 30 import kotlinx.coroutines.flow.first 31 import kotlinx.coroutines.runBlocking 32 33 @SysUISingleton 34 class CommunalMetricsStartable 35 @Inject 36 constructor( 37 @Background private val bgExecutor: Executor, 38 private val communalSettingsInteractor: CommunalSettingsInteractor, 39 private val communalInteractor: CommunalInteractor, 40 private val statsManager: StatsManager, 41 private val metricsLogger: CommunalMetricsLogger, 42 ) : CoreStartable, StatsManager.StatsPullAtomCallback { startnull43 override fun start() { 44 if (!communalSettingsInteractor.isCommunalFlagEnabled()) { 45 return 46 } 47 48 statsManager.setPullAtomCallback( 49 /* atomTag = */ SysUiStatsLog.COMMUNAL_HUB_SNAPSHOT, 50 /* metadata = */ null, 51 /* executor = */ bgExecutor, 52 /* callback = */ this, 53 ) 54 } 55 onPullAtomnull56 override fun onPullAtom(atomTag: Int, statsEvents: MutableList<StatsEvent>): Int { 57 if (atomTag != SysUiStatsLog.COMMUNAL_HUB_SNAPSHOT) { 58 return StatsManager.PULL_SKIP 59 } 60 61 metricsLogger.logWidgetsSnapshot( 62 statsEvents, 63 componentNames = 64 runBlocking { 65 communalInteractor.widgetContent.first().map { 66 it.componentName.flattenToString() 67 } 68 }, 69 ) 70 return StatsManager.PULL_SUCCESS 71 } 72 } 73