[Catalyst] Setup metrics logger for preference ui action
Bug: 389886085
Flag: com.android.settings.flags.catalyst
Test: atest&manual
Change-Id: I8c3ce6cbdeeefe7857132a40ea486d0f56928d7a
diff --git a/src/com/android/settings/Metrics.kt b/src/com/android/settings/Metrics.kt
new file mode 100644
index 0000000..0d5ea56
--- /dev/null
+++ b/src/com/android/settings/Metrics.kt
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2025 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.
+ */
+
+package com.android.settings
+
+import android.content.Context
+import com.android.settings.overlay.FeatureFactory
+import com.android.settingslib.core.instrumentation.MetricsFeatureProvider
+import com.android.settingslib.metadata.PreferenceUiActionMetricsLogger
+import com.android.settingslib.metadata.PreferenceMetadata
+import com.android.settingslib.metadata.PreferenceScreenMetadata
+
+/** Provides metrics for preference action. */
+interface PreferenceActionMetricsProvider {
+
+ /** Metrics action id for the preference. */
+ val preferenceActionMetrics: Int
+}
+
+/** [PreferenceUiActionMetricsLogger] of Settings app. */
+class SettingsMetricsLogger
+@JvmOverloads
+constructor(
+ private val context: Context,
+ private val metricsFeatureProvider: MetricsFeatureProvider =
+ FeatureFactory.featureFactory.metricsFeatureProvider,
+) : PreferenceUiActionMetricsLogger {
+
+ override fun logPreferenceValueChange(
+ screen: PreferenceScreenMetadata,
+ preference: PreferenceMetadata,
+ value: Any?,
+ ) {
+ if (preference !is PreferenceActionMetricsProvider) return
+ when (value) {
+ is Boolean ->
+ metricsFeatureProvider.action(context, preference.preferenceActionMetrics, value)
+ else -> {}
+ }
+ }
+}
diff --git a/src/com/android/settings/SettingsApplication.java b/src/com/android/settings/SettingsApplication.java
index 442e3c2..9c5671f 100644
--- a/src/com/android/settings/SettingsApplication.java
+++ b/src/com/android/settings/SettingsApplication.java
@@ -77,6 +77,8 @@
if (Flags.catalyst()) {
PreferenceScreenRegistry.INSTANCE.setPreferenceScreenMetadataFactories(
preferenceScreenFactories());
+ PreferenceScreenRegistry.INSTANCE.setPreferenceUiActionMetricsLogger(
+ new SettingsMetricsLogger(this));
PreferenceBindingFactory.setDefaultFactory(new SettingsPreferenceBindingFactory());
}
diff --git a/tests/robotests/Android.bp b/tests/robotests/Android.bp
index 935c687..464d970 100644
--- a/tests/robotests/Android.bp
+++ b/tests/robotests/Android.bp
@@ -58,7 +58,6 @@
"Settings-robo-testutils",
"Settings-testutils2",
"SettingsLib-robo-testutils",
- "SettingsLibPreference-testutils",
"Settings_robolectric_meta_service_file",
"aconfig_settings_flags_lib",
"android.webkit.flags-aconfig-java",
@@ -73,6 +72,7 @@
"kotlin-test",
"mockito-robolectric-prebuilt", // mockito deps order matters!
"mockito-kotlin2",
+ "SettingsLibPreference-testutils", // order matters because it depends on mockito-kotlin2
"notification_flags_lib",
"platform-test-annotations",
"testables",
@@ -115,6 +115,7 @@
libs: [
"Robolectric_all-target",
"Settings-core",
+ "androidx.test.core",
"mockito-robolectric-prebuilt",
"truth",
],
diff --git a/tests/robotests/testutils/com/android/settings/testutils/MetricsRule.kt b/tests/robotests/testutils/com/android/settings/testutils/MetricsRule.kt
new file mode 100644
index 0000000..44fa4bf
--- /dev/null
+++ b/tests/robotests/testutils/com/android/settings/testutils/MetricsRule.kt
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2025 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.
+ */
+
+package com.android.settings.testutils
+
+import android.content.Context
+import androidx.test.core.app.ApplicationProvider
+import com.android.settings.SettingsMetricsLogger
+import com.android.settingslib.core.instrumentation.MetricsFeatureProvider
+import com.android.settingslib.metadata.PreferenceScreenRegistry
+import org.junit.rules.TestWatcher
+import org.junit.runner.Description
+
+/** Test rule for metrics. */
+class MetricsRule : TestWatcher() {
+ val metricsFeatureProvider: MetricsFeatureProvider =
+ FakeFeatureFactory.setupForTest().metricsFeatureProvider
+
+ override fun starting(description: Description) {
+ val context: Context = ApplicationProvider.getApplicationContext()
+ PreferenceScreenRegistry.preferenceUiActionMetricsLogger =
+ SettingsMetricsLogger(context, metricsFeatureProvider)
+ }
+
+ override fun finished(description: Description) {
+ PreferenceScreenRegistry.preferenceUiActionMetricsLogger = null
+ }
+}