Add Framework Initializer in OnDevicePersonalization

Test: TH
Bug: 218749359
Change-Id: Ia289163c2f437014438eaf204d180495e029b1c8
diff --git a/framework/Android.bp b/framework/Android.bp
index feb8c5f..c1550a9 100644
--- a/framework/Android.bp
+++ b/framework/Android.bp
@@ -66,3 +66,12 @@
     ],
     defaults_visibility: ["//visibility:public"],
 }
+
+// defaults for tests that need to build against framework-odp's @hide APIs
+java_defaults {
+    name: "framework-ondevicepersonalization-test-defaults",
+    sdk_version: "module_Tiramisu",
+    libs: [
+        "framework-ondevicepersonalization.impl",
+    ],
+}
diff --git a/framework/api/module-lib-current.txt b/framework/api/module-lib-current.txt
index d802177..f4da3d0 100644
--- a/framework/api/module-lib-current.txt
+++ b/framework/api/module-lib-current.txt
@@ -1 +1,9 @@
 // Signature format: 2.0
+package android.ondevicepersonalization {
+
+  public class OnDevicePersonalizationFrameworkInitializer {
+    method public static void registerServiceWrappers();
+  }
+
+}
+
diff --git a/framework/java/android/ondevicepersonalization/OnDevicePersonalizationFrameworkInitializer.java b/framework/java/android/ondevicepersonalization/OnDevicePersonalizationFrameworkInitializer.java
new file mode 100644
index 0000000..cfe63b4
--- /dev/null
+++ b/framework/java/android/ondevicepersonalization/OnDevicePersonalizationFrameworkInitializer.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2022 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 android.ondevicepersonalization;
+
+import static android.ondevicepersonalization.OnDevicePersonalizationManager.ON_DEVICE_PERSONALIZATION_SERVICE;
+
+import android.annotation.SystemApi;
+import android.app.SystemServiceRegistry;
+import android.content.Context;
+
+/**
+ * Class holding initialization code for the OnDevicePersonalization module.
+ *
+ * @hide
+ */
+@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
+public class OnDevicePersonalizationFrameworkInitializer {
+    private OnDevicePersonalizationFrameworkInitializer() {
+    }
+
+    /**
+     * Called by {@link SystemServiceRegistry}'s static initializer and registers all
+     * OnDevicePersonalization services to {@link Context}, so that
+     * {@link Context#getSystemService} can return them.
+     *
+     * @throws IllegalStateException if this is called from anywhere besides
+     *     {@link SystemServiceRegistry}
+     */
+    public static void registerServiceWrappers() {
+        SystemServiceRegistry.registerContextAwareService(
+                ON_DEVICE_PERSONALIZATION_SERVICE, OnDevicePersonalizationManager.class,
+                (c) -> new OnDevicePersonalizationManager(c));
+    }
+}
diff --git a/framework/java/android/ondevicepersonalization/OnDevicePersonalizationManager.java b/framework/java/android/ondevicepersonalization/OnDevicePersonalizationManager.java
index 9e13d63..95b853b 100644
--- a/framework/java/android/ondevicepersonalization/OnDevicePersonalizationManager.java
+++ b/framework/java/android/ondevicepersonalization/OnDevicePersonalizationManager.java
@@ -36,6 +36,9 @@
  * @hide
  */
 public class OnDevicePersonalizationManager {
+    public static final String ON_DEVICE_PERSONALIZATION_SERVICE =
+            "on_device_personalization_service";
+
     private boolean mBound = false;
     private static final String TAG = "OdpManager";
 
diff --git a/tests/frameworktests/Android.bp b/tests/frameworktests/Android.bp
new file mode 100644
index 0000000..5b90d92
--- /dev/null
+++ b/tests/frameworktests/Android.bp
@@ -0,0 +1,44 @@
+// Copyright (C) 2022 The Android 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.
+
+// Make test APK
+// ============================================================
+
+package {
+    default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+android_test {
+    name: "FrameworkOnDevicePersonalizationTests",
+    srcs: ["**/*.java"],
+    defaults: ["framework-ondevicepersonalization-test-defaults"],
+    min_sdk_version: "Tiramisu",
+    target_sdk_version: "Tiramisu",
+    static_libs: [
+        "androidx.test.core",
+        "androidx.test.ext.junit",
+        "androidx.test.rules",
+        "frameworks-base-testutils",
+        "guava",
+        "mockito-target-minus-junit4",
+        "truth-prebuilt",
+    ],
+    libs: [
+        "android.test.runner",
+        "android.test.base",
+    ],
+    test_suites: [
+        "general-tests",
+    ],
+}
diff --git a/tests/frameworktests/AndroidManifest.xml b/tests/frameworktests/AndroidManifest.xml
new file mode 100644
index 0000000..1ef866b
--- /dev/null
+++ b/tests/frameworktests/AndroidManifest.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright 2022 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.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="android.ondevicepersonalization">
+
+    <application android:label="FrameworkOnDevicePersonalizationTests">
+        <uses-library android:name="android.test.runner" />
+    </application>
+    <instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
+                     android:targetPackage="android.ondevicepersonalization"
+                     android:label="OnDevicePersonalization Manager Tests">
+    </instrumentation>
+
+</manifest>
diff --git a/tests/frameworktests/AndroidTest.xml b/tests/frameworktests/AndroidTest.xml
new file mode 100644
index 0000000..6d1d1ab
--- /dev/null
+++ b/tests/frameworktests/AndroidTest.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright 2022 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.
+-->
+<configuration description="Config for OnDevicePersonalization Manager unit tests">
+    <option name="config-descriptor:metadata" key="component" value="framework" />
+    <option name="config-descriptor:metadata" key="parameter" value="not_instant_app" />
+    <option name="config-descriptor:metadata" key="parameter" value="not_multi_abi" />
+    <option name="config-descriptor:metadata" key="parameter" value="secondary_user" />
+
+    <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
+        <option name="cleanup-apks" value="true"/>
+        <option name="test-file-name" value="FrameworkOnDevicePersonalizationTests.apk"/>
+    </target_preparer>
+
+    <test class="com.android.tradefed.testtype.AndroidJUnitTest">
+        <option name="package" value="android.ondevicepersonalization"/>
+    </test>
+</configuration>
\ No newline at end of file
diff --git a/tests/frameworktests/src/android/ondevicepersonalization/OnDevicePersonalizationFrameworkInitializerTest.java b/tests/frameworktests/src/android/ondevicepersonalization/OnDevicePersonalizationFrameworkInitializerTest.java
new file mode 100644
index 0000000..d36cb9b
--- /dev/null
+++ b/tests/frameworktests/src/android/ondevicepersonalization/OnDevicePersonalizationFrameworkInitializerTest.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2022 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 android.ondevicepersonalization;
+
+import static org.junit.Assert.assertThrows;
+
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+import androidx.test.filters.SmallTest;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Unit Test of
+ * {@link android.ondevicepersonalization.OnDevicePersonalizationFrameworkInitializer}.
+ */
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public class OnDevicePersonalizationFrameworkInitializerTest {
+    /**
+     * OnDevicePersonalizationFrameworkInitializer.registerServiceWrappers() should only be
+     * called by SystemServiceRegistry during boot up when Uwb is first initialized.
+     * Calling this API at any other time should throw an exception.
+     */
+    @Test
+    public void testRegisterServiceWrappers_failsWhenCalledOutsideOfSystemServiceRegistry() {
+        assertThrows(
+                IllegalStateException.class,
+                () -> OnDevicePersonalizationFrameworkInitializer.registerServiceWrappers());
+    }
+}