Add AccessibilityCheckerStatsdLogger

Bug: 326385939
Test: th
Flag: com.android.server.accessibility.enable_a11y_checker_logging
Change-Id: I28c970c62c409fbfd015f68d2a4f6c3a281f81a6
diff --git a/services/accessibility/Android.bp b/services/accessibility/Android.bp
index 311addb..efa1397 100644
--- a/services/accessibility/Android.bp
+++ b/services/accessibility/Android.bp
@@ -26,6 +26,7 @@
     },
     srcs: [
         ":services.accessibility-sources",
+        ":statslog-accessibility-java-gen",
         "//frameworks/base/packages/SettingsLib/RestrictedLockUtils:SettingsLibRestrictedLockUtilsSrc",
     ],
     libs: [
@@ -37,7 +38,6 @@
         "a11ychecker-protos-java-proto-lite",
         "com_android_server_accessibility_flags_lib",
         "//frameworks/base/packages/SystemUI/aconfig:com_android_systemui_flags_lib",
-
     ],
 }
 
@@ -81,3 +81,12 @@
         "java/**/a11ychecker/proto/*.proto",
     ],
 }
+
+genrule {
+    name: "statslog-accessibility-java-gen",
+    tools: ["stats-log-api-gen"],
+    cmd: "$(location stats-log-api-gen) --java $(out) --module accessibility" +
+        " --javaPackage com.android.server.accessibility.a11ychecker" +
+        " --javaClass AccessibilityCheckerStatsLog --minApiLevel 34",
+    out: ["java/com/android/server/accessibility/a11ychecker/AccessibilityCheckerStatsLog.java"],
+}
diff --git a/services/accessibility/java/com/android/server/accessibility/a11ychecker/AccessibilityCheckerStatsdLogger.java b/services/accessibility/java/com/android/server/accessibility/a11ychecker/AccessibilityCheckerStatsdLogger.java
new file mode 100644
index 0000000..1b3ec5a
--- /dev/null
+++ b/services/accessibility/java/com/android/server/accessibility/a11ychecker/AccessibilityCheckerStatsdLogger.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2024 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.server.accessibility.a11ychecker;
+
+import android.util.Slog;
+
+import com.android.server.accessibility.a11ychecker.A11yCheckerProto.AccessibilityCheckResultReported;
+
+import java.util.Set;
+
+
+/**
+ * Wraps the StatsdLogger for AccessibilityCheckResultReported.
+ *
+ * @hide
+ */
+public class AccessibilityCheckerStatsdLogger {
+    private static final int ATOM_ID = 910;
+    private static final String LOG_TAG = "AccessibilityCheckerStatsdLogger";
+
+    /**
+     * Writes results to statsd.
+     */
+    public static void logResults(Set<AccessibilityCheckResultReported> results) {
+        Slog.i(LOG_TAG, String.format("Writing %d AccessibilityCheckResultReported events",
+                results.size()));
+
+        for (AccessibilityCheckResultReported result : results) {
+            AccessibilityCheckerStatsLog.write(ATOM_ID,
+                    result.getPackageName(),
+                    result.getAppVersionCode(),
+                    result.getUiElementPath(),
+                    result.getActivityName(),
+                    result.getWindowTitle(),
+                    result.getSourceComponentName(),
+                    result.getSourceVersionCode(),
+                    result.getResultCheckClass().getNumber(),
+                    result.getResultType().getNumber(),
+                    result.getResultId());
+        }
+    }
+}