[TeX] Removed JNI dependency

-used code gen to precompute metric ids hash & type

Bug: 276463350
Test: atest android.cts.statsdatom.express.ExpresslogAtomsTests
Test: atest ExpressLogApisTests
Change-Id: I4126d111f0003cec94d1c232e276cbc1cab8755d
Merged-In: I4126d111f0003cec94d1c232e276cbc1cab8755d
diff --git a/java/com/android/modules/expresslog/Android.bp b/java/com/android/modules/expresslog/Android.bp
index cacc7f8..59504bd 100644
--- a/java/com/android/modules/expresslog/Android.bp
+++ b/java/com/android/modules/expresslog/Android.bp
@@ -28,12 +28,16 @@
     libs: [
         "framework-statsd",
     ],
+    static_libs: [
+        "expresslog-catalog",
+    ],
 }
 
 genrule {
     name: "statslog-expresslog-java-gen",
     tools: ["stats-log-api-gen"],
     cmd: "$(location stats-log-api-gen) --java $(out) --module expresslog" +
-        " --javaPackage com.android.modules.expresslog --javaClass StatsExpressLog",
+        " --javaPackage com.android.modules.expresslog" +
+        " --javaClass StatsExpressLog",
     out: ["com/android/modules/expresslog/StatsExpressLog.java"],
 }
diff --git a/java/com/android/modules/expresslog/Counter.java b/java/com/android/modules/expresslog/Counter.java
index b788c3f..bcacb8b 100644
--- a/java/com/android/modules/expresslog/Counter.java
+++ b/java/com/android/modules/expresslog/Counter.java
@@ -18,8 +18,6 @@
 
 import android.annotation.NonNull;
 
-import com.android.modules.expresslog.StatsExpressLog;
-
 /** Counter encapsulates StatsD write API calls */
 public final class Counter {
 
@@ -49,7 +47,8 @@
      * @param amount to increment counter
      */
     public static void logIncrement(@NonNull String metricId, long amount) {
-        final long metricIdHash = Utils.hashString(metricId);
+        final long metricIdHash =
+                MetricIds.getMetricIdHash(metricId, MetricIds.METRIC_TYPE_COUNTER);
         StatsExpressLog.write(StatsExpressLog.EXPRESS_EVENT_REPORTED, metricIdHash, amount);
     }
 
@@ -60,7 +59,8 @@
      * @param amount to increment counter
      */
     public static void logIncrementWithUid(@NonNull String metricId, int uid, long amount) {
-        final long metricIdHash = Utils.hashString(metricId);
+        final long metricIdHash =
+                MetricIds.getMetricIdHash(metricId, MetricIds.METRIC_TYPE_COUNTER_WITH_UID);
         StatsExpressLog.write(
             StatsExpressLog.EXPRESS_UID_EVENT_REPORTED, metricIdHash, amount, uid);
     }
diff --git a/java/com/android/modules/expresslog/Histogram.java b/java/com/android/modules/expresslog/Histogram.java
index be300bf..4f61c85 100644
--- a/java/com/android/modules/expresslog/Histogram.java
+++ b/java/com/android/modules/expresslog/Histogram.java
@@ -20,14 +20,12 @@
 import android.annotation.IntRange;
 import android.annotation.NonNull;
 
-import com.android.modules.expresslog.StatsExpressLog;
-
 import java.util.Arrays;
 
 /** Histogram encapsulates StatsD write API calls */
 public final class Histogram {
 
-    private final long mMetricIdHash;
+    private final String mMetricId;
     private final BinOptions mBinOptions;
 
     /**
@@ -37,7 +35,7 @@
      * @param binOptions to calculate bin index for samples
      */
     public Histogram(@NonNull String metricId, @NonNull BinOptions binOptions) {
-        mMetricIdHash = Utils.hashString(metricId);
+        mMetricId = metricId;
         mBinOptions = binOptions;
     }
 
@@ -47,9 +45,10 @@
      * @param sample value
      */
     public void logSample(float sample) {
+        final long hash = MetricIds.getMetricIdHash(mMetricId, MetricIds.METRIC_TYPE_HISTOGRAM);
         final int binIndex = mBinOptions.getBinForSample(sample);
-        StatsExpressLog.write(StatsExpressLog.EXPRESS_HISTOGRAM_SAMPLE_REPORTED, mMetricIdHash,
-                /*count*/ 1, binIndex);
+        StatsExpressLog.write(
+                StatsExpressLog.EXPRESS_HISTOGRAM_SAMPLE_REPORTED, hash, /*count*/ 1, binIndex);
     }
 
     /**
@@ -59,9 +58,15 @@
      * @param sample value
      */
     public void logSampleWithUid(int uid, float sample) {
+        final long hash =
+                MetricIds.getMetricIdHash(mMetricId, MetricIds.METRIC_TYPE_HISTOGRAM_WITH_UID);
         final int binIndex = mBinOptions.getBinForSample(sample);
-        StatsExpressLog.write(StatsExpressLog.EXPRESS_UID_HISTOGRAM_SAMPLE_REPORTED,
-                mMetricIdHash, /*count*/ 1, binIndex, uid);
+        StatsExpressLog.write(
+                StatsExpressLog.EXPRESS_UID_HISTOGRAM_SAMPLE_REPORTED,
+                hash, /*count*/
+                1,
+                binIndex,
+                uid);
     }
 
     /** Used by Histogram to map data sample to corresponding bin */
diff --git a/java/com/android/modules/expresslog/Utils.java b/java/com/android/modules/expresslog/Utils.java
deleted file mode 100644
index fde90fc..0000000
--- a/java/com/android/modules/expresslog/Utils.java
+++ /dev/null
@@ -1,21 +0,0 @@
-/*
- * Copyright (C) 2023 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.modules.expresslog;
-
-final class Utils {
-    static native long hashString(String stringToHash);
-}
diff --git a/javatests/com/android/modules/expresslog/Android.bp b/javatests/com/android/modules/expresslog/Android.bp
index dd52750..9396e17 100644
--- a/javatests/com/android/modules/expresslog/Android.bp
+++ b/javatests/com/android/modules/expresslog/Android.bp
@@ -37,40 +37,7 @@
         "android.test.runner",
     ],
 
-    jni_libs: [
-        "libexpresslog_test_jni",
-    ],
-
     test_suites: [
         "general-tests",
     ],
 }
-
-cc_library_shared {
-    name: "libexpresslog_test_jni",
-
-    sdk_version: "current",
-    min_sdk_version: "30",
-
-    cflags: [
-        "-Wall",
-        "-Werror",
-        "-Wextra",
-
-        "-DNAMESPACE_FOR_HASH_FUNCTIONS=farmhash",
-    ],
-    srcs: [
-        "jni/onload.cpp",
-    ],
-    header_libs: [
-        "liblog_headers",
-        "libnativehelper_header_only",
-    ],
-    shared_libs: [
-        "liblog",
-    ],
-    static_libs: [
-        "libexpresslog_jni",
-        "libtextclassifier_hash_static",
-    ],
-}
diff --git a/javatests/com/android/modules/expresslog/ScaledRangeOptionsTest.java b/javatests/com/android/modules/expresslog/ScaledRangeOptionsTest.java
index 8defce7..1c42788 100644
--- a/javatests/com/android/modules/expresslog/ScaledRangeOptionsTest.java
+++ b/javatests/com/android/modules/expresslog/ScaledRangeOptionsTest.java
@@ -27,10 +27,6 @@
 @RunWith(JUnit4.class)
 @SmallTest
 public class ScaledRangeOptionsTest {
-    static {
-        System.loadLibrary("expresslog_test_jni");
-    }
-
     private static final String TAG = ScaledRangeOptionsTest.class.getSimpleName();
 
     @Test
diff --git a/javatests/com/android/modules/expresslog/UniformOptionsTest.java b/javatests/com/android/modules/expresslog/UniformOptionsTest.java
index 3cc03ec..cad4c3f 100644
--- a/javatests/com/android/modules/expresslog/UniformOptionsTest.java
+++ b/javatests/com/android/modules/expresslog/UniformOptionsTest.java
@@ -26,10 +26,6 @@
 @RunWith(JUnit4.class)
 @SmallTest
 public class UniformOptionsTest {
-    static {
-        System.loadLibrary("expresslog_test_jni");
-    }
-
     private static final String TAG = UniformOptionsTest.class.getSimpleName();
 
     @Test
diff --git a/javatests/com/android/modules/expresslog/jni/.clang-format b/javatests/com/android/modules/expresslog/jni/.clang-format
deleted file mode 100644
index cead3a0..0000000
--- a/javatests/com/android/modules/expresslog/jni/.clang-format
+++ /dev/null
@@ -1,17 +0,0 @@
-BasedOnStyle: Google
-AllowShortIfStatementsOnASingleLine: true
-AllowShortFunctionsOnASingleLine: false
-AllowShortLoopsOnASingleLine: true
-BinPackArguments: true
-BinPackParameters: true
-ColumnLimit: 100
-CommentPragmas: NOLINT:.*
-ContinuationIndentWidth: 8
-DerivePointerAlignment: false
-IndentWidth: 4
-PointerAlignment: Left
-TabWidth: 4
-AccessModifierOffset: -4
-IncludeCategories:
-  - Regex:    '^"Log\.h"'
-    Priority:    -1
diff --git a/javatests/com/android/modules/expresslog/jni/onload.cpp b/javatests/com/android/modules/expresslog/jni/onload.cpp
deleted file mode 100644
index a112467..0000000
--- a/javatests/com/android/modules/expresslog/jni/onload.cpp
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright (C) 2023 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.
- */
-
-#define LOG_TAG "TeX"
-
-#include <jni.h>
-#include <log/log.h>
-
-namespace android {
-extern int register_com_android_modules_expresslog_Utils(JNIEnv* env);
-}  // namespace android
-
-using namespace android;
-
-extern "C" jint JNI_OnLoad(JavaVM* vm, void* /* reserved */) {
-    JNIEnv* env = NULL;
-    jint result = -1;
-
-    if (vm->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK) {
-        ALOGE("GetEnv failed!");
-        return result;
-    }
-    ALOG_ASSERT(env, "Could not retrieve the env!");
-
-    register_com_android_modules_expresslog_Utils(env);
-    return JNI_VERSION_1_4;
-}
diff --git a/jni/expresslog/.clang-format b/jni/expresslog/.clang-format
deleted file mode 100644
index cead3a0..0000000
--- a/jni/expresslog/.clang-format
+++ /dev/null
@@ -1,17 +0,0 @@
-BasedOnStyle: Google
-AllowShortIfStatementsOnASingleLine: true
-AllowShortFunctionsOnASingleLine: false
-AllowShortLoopsOnASingleLine: true
-BinPackArguments: true
-BinPackParameters: true
-ColumnLimit: 100
-CommentPragmas: NOLINT:.*
-ContinuationIndentWidth: 8
-DerivePointerAlignment: false
-IndentWidth: 4
-PointerAlignment: Left
-TabWidth: 4
-AccessModifierOffset: -4
-IncludeCategories:
-  - Regex:    '^"Log\.h"'
-    Priority:    -1
diff --git a/jni/expresslog/Android.bp b/jni/expresslog/Android.bp
deleted file mode 100644
index 7bef576..0000000
--- a/jni/expresslog/Android.bp
+++ /dev/null
@@ -1,53 +0,0 @@
-//
-// Copyright (C) 2023 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.
-
-// JNI library for Utils.hashString
-package {
-    default_applicable_licenses: ["Android-Apache-2.0"],
-}
-
-cc_library_static {
-    name: "libexpresslog_jni",
-
-    sdk_version: "current",
-    min_sdk_version: "30",
-
-    cflags: [
-        "-Wall",
-        "-Werror",
-        "-Wextra",
-
-        "-DNAMESPACE_FOR_HASH_FUNCTIONS=farmhash",
-    ],
-    srcs: [
-        "com_android_modules_expresslog_Utils.cpp",
-    ],
-    header_libs: [
-        "liblog_headers",
-        "libnativehelper_header_only",
-        "libtextclassifier_hash_headers",
-    ],
-    shared_libs: [
-        "liblog",
-    ],
-    static_libs: [
-        "libtextclassifier_hash_static",
-    ],
-    visibility: ["//visibility:public"],
-    apex_available: [
-        "//apex_available:anyapex",
-        "//apex_available:platform",
-    ],
-}
diff --git a/jni/expresslog/com_android_modules_expresslog_Utils.cpp b/jni/expresslog/com_android_modules_expresslog_Utils.cpp
deleted file mode 100644
index ed4d0ed..0000000
--- a/jni/expresslog/com_android_modules_expresslog_Utils.cpp
+++ /dev/null
@@ -1,85 +0,0 @@
-/*
- * Copyright (C) 2023 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.
- */
-
-#define LOG_NAMESPACE "TeX.tag."
-#define LOG_TAG "TeX"
-
-#include <log/log.h>
-#include <nativehelper/scoped_local_ref.h>
-#include <nativehelper/scoped_utf_chars.h>
-#include <utils/hash/farmhash.h>
-
-// ----------------------------------------------------------------------------
-// JNI Glue
-// ----------------------------------------------------------------------------
-
-static jclass gStringClass = nullptr;
-
-/**
- * Class:     com_android_modules_expresslog_Utils
- * Method:    hashString
- * Signature: (Ljava/lang/String;)J
- */
-static jlong hashString(JNIEnv* env, jclass /*class*/, jstring metricNameObj) {
-    ScopedUtfChars name(env, metricNameObj);
-    if (name.c_str() == nullptr) {
-        return 0;
-    }
-
-    return static_cast<jlong>(farmhash::Fingerprint64(name.c_str(), name.size()));
-}
-
-static const JNINativeMethod gMethods[] = {
-        {"hashString", "(Ljava/lang/String;)J", (void*)hashString},
-};
-
-namespace android {
-
-int register_com_android_modules_expresslog_Utils(JNIEnv* env, const char* const utilsClassName) {
-    static const char* const kStringClassName = "java/lang/String";
-
-    ScopedLocalRef<jclass> utilsCls(env, env->FindClass(utilsClassName));
-    if (utilsCls.get() == nullptr) {
-        ALOGE("jni expresslog registration failure, class not found '%s'", utilsClassName);
-        return JNI_ERR;
-    }
-
-    jclass stringClass = env->FindClass(kStringClassName);
-    if (stringClass == nullptr) {
-        ALOGE("jni expresslog registration failure, class not found '%s'", kStringClassName);
-        return JNI_ERR;
-    }
-    gStringClass = static_cast<jclass>(env->NewGlobalRef(stringClass));
-    if (gStringClass == nullptr) {
-        ALOGE("jni expresslog Unable to create global reference '%s'", kStringClassName);
-        return JNI_ERR;
-    }
-
-    const jint count = sizeof(gMethods) / sizeof(gMethods[0]);
-    int status = env->RegisterNatives(utilsCls.get(), gMethods, count);
-    if (status < 0) {
-        ALOGE("jni expresslog registration failure, status: %d", status);
-        return JNI_ERR;
-    }
-    return JNI_VERSION_1_4;
-}
-
-int register_com_android_modules_expresslog_Utils(JNIEnv* env) {
-    static const char* const kUtilsClassName = "com/android/modules/expresslog/Utils";
-    return register_com_android_modules_expresslog_Utils(env, kUtilsClassName);
-}
-
-}  // namespace android