1 /* <lambda>null2 * 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.shared.log 18 19 import android.util.StatsEvent 20 import com.android.systemui.communal.dagger.CommunalModule.Companion.LOGGABLE_PREFIXES 21 import com.android.systemui.dagger.SysUISingleton 22 import com.android.systemui.shared.system.SysUiStatsLog 23 import javax.inject.Inject 24 import javax.inject.Named 25 26 @SysUISingleton 27 class CommunalMetricsLogger 28 @Inject 29 constructor( 30 @Named(LOGGABLE_PREFIXES) private val loggablePrefixes: List<String>, 31 private val statsLogProxy: StatsLogProxy, 32 ) { 33 34 /** Logs an add widget event for metrics. No-op if widget is not loggable. */ 35 fun logAddWidget(componentName: String, rank: Int?) { 36 if (!componentName.isLoggable()) { 37 return 38 } 39 40 statsLogProxy.writeCommunalHubWidgetEventReported( 41 SysUiStatsLog.COMMUNAL_HUB_WIDGET_EVENT_REPORTED__ACTION__ADD, 42 componentName, 43 rank ?: -1, 44 ) 45 } 46 47 /** Logs a remove widget event for metrics. No-op if widget is not loggable. */ 48 fun logRemoveWidget(componentName: String, rank: Int) { 49 if (!componentName.isLoggable()) { 50 return 51 } 52 53 statsLogProxy.writeCommunalHubWidgetEventReported( 54 SysUiStatsLog.COMMUNAL_HUB_WIDGET_EVENT_REPORTED__ACTION__REMOVE, 55 componentName, 56 rank, 57 ) 58 } 59 60 /** Logs a tap widget event for metrics. No-op if widget is not loggable. */ 61 fun logTapWidget(componentName: String, rank: Int) { 62 if (!componentName.isLoggable()) { 63 return 64 } 65 66 statsLogProxy.writeCommunalHubWidgetEventReported( 67 SysUiStatsLog.COMMUNAL_HUB_WIDGET_EVENT_REPORTED__ACTION__TAP, 68 componentName, 69 rank, 70 ) 71 } 72 73 fun logResizeWidget(componentName: String, rank: Int, spanY: Int = 0) { 74 if (!componentName.isLoggable()) { 75 return 76 } 77 78 statsLogProxy.writeCommunalHubWidgetEventReported( 79 SysUiStatsLog.COMMUNAL_HUB_WIDGET_EVENT_REPORTED__ACTION__RESIZE, 80 componentName, 81 rank = rank, 82 spanY = spanY, 83 ) 84 } 85 86 /** Logs loggable widgets and the total widget count as a [StatsEvent]. */ 87 fun logWidgetsSnapshot(statsEvents: MutableList<StatsEvent>, componentNames: List<String>) { 88 val loggableComponentNames = componentNames.filter { it.isLoggable() }.toTypedArray() 89 statsEvents.add( 90 statsLogProxy.buildCommunalHubSnapshotStatsEvent( 91 componentNames = loggableComponentNames, 92 widgetCount = componentNames.size, 93 ) 94 ) 95 } 96 97 /** Whether the component name matches any of the loggable prefixes. */ 98 private fun String.isLoggable(): Boolean { 99 return loggablePrefixes.any { loggablePrefix -> startsWith(loggablePrefix) } 100 } 101 102 /** Proxy of [SysUiStatsLog] for testing purpose. */ 103 interface StatsLogProxy { 104 /** Logs a [SysUiStatsLog.COMMUNAL_HUB_WIDGET_EVENT_REPORTED] stats event. */ 105 fun writeCommunalHubWidgetEventReported( 106 action: Int, 107 componentName: String, 108 rank: Int, 109 spanY: Int = 0, 110 ) 111 112 /** Builds a [SysUiStatsLog.COMMUNAL_HUB_SNAPSHOT] stats event. */ 113 fun buildCommunalHubSnapshotStatsEvent( 114 componentNames: Array<String>, 115 widgetCount: Int, 116 ): StatsEvent 117 } 118 } 119 120 /** Redirects calls to [SysUiStatsLog]. */ 121 @SysUISingleton 122 class CommunalStatsLogProxyImpl @Inject constructor() : CommunalMetricsLogger.StatsLogProxy { writeCommunalHubWidgetEventReportednull123 override fun writeCommunalHubWidgetEventReported( 124 action: Int, 125 componentName: String, 126 rank: Int, 127 spanY: Int, 128 ) { 129 SysUiStatsLog.write( 130 SysUiStatsLog.COMMUNAL_HUB_WIDGET_EVENT_REPORTED, 131 action, 132 componentName, 133 rank, 134 spanY, 135 ) 136 } 137 buildCommunalHubSnapshotStatsEventnull138 override fun buildCommunalHubSnapshotStatsEvent( 139 componentNames: Array<String>, 140 widgetCount: Int, 141 ): StatsEvent { 142 return SysUiStatsLog.buildStatsEvent( 143 SysUiStatsLog.COMMUNAL_HUB_SNAPSHOT, 144 componentNames, 145 widgetCount, 146 ) 147 } 148 } 149