Move service registration code into a System API (1/n)

This is an effort to modularize RollbackManager.

AIDL interfaces (IRollbackManager in this case) can’t be used across
module boundaries. We need to add a wrapper around the service
registraction code to expose to SystemServiceRegistry.

The approach is the same as ag/9746851.

Bug: 150347230
Test: m
Change-Id: I83e1cfedf4b4f6bf796e8a4a0ea81d813390f622
diff --git a/api/module-lib-current.txt b/api/module-lib-current.txt
index 69b52693..63159ed 100644
--- a/api/module-lib-current.txt
+++ b/api/module-lib-current.txt
@@ -9,6 +9,14 @@
 
 }
 
+package android.content.rollback {
+
+  public class RollbackManagerFrameworkInitializer {
+    method public static void initialize();
+  }
+
+}
+
 package android.net {
 
   public final class TetheredClient implements android.os.Parcelable {
diff --git a/core/java/android/app/SystemServiceRegistry.java b/core/java/android/app/SystemServiceRegistry.java
index be07b37..4a7f5ee 100644
--- a/core/java/android/app/SystemServiceRegistry.java
+++ b/core/java/android/app/SystemServiceRegistry.java
@@ -67,8 +67,7 @@
 import android.content.pm.PackageManager;
 import android.content.pm.ShortcutManager;
 import android.content.res.Resources;
-import android.content.rollback.IRollbackManager;
-import android.content.rollback.RollbackManager;
+import android.content.rollback.RollbackManagerFrameworkInitializer;
 import android.debug.AdbManager;
 import android.debug.IAdbManager;
 import android.hardware.ConsumerIrManager;
@@ -1250,16 +1249,6 @@
                         return new RoleControllerManager(ctx.getOuterContext());
                     }});
 
-        registerService(Context.ROLLBACK_SERVICE, RollbackManager.class,
-                new CachedServiceFetcher<RollbackManager>() {
-                    @Override
-                    public RollbackManager createService(ContextImpl ctx)
-                            throws ServiceNotFoundException {
-                        IBinder b = ServiceManager.getServiceOrThrow(Context.ROLLBACK_SERVICE);
-                        return new RollbackManager(ctx.getOuterContext(),
-                                IRollbackManager.Stub.asInterface(b));
-                    }});
-
         registerService(Context.DYNAMIC_SYSTEM_SERVICE, DynamicSystemManager.class,
                 new CachedServiceFetcher<DynamicSystemManager>() {
                     @Override
@@ -1346,6 +1335,7 @@
             AppSearchManagerFrameworkInitializer.initialize();
             WifiFrameworkInitializer.registerServiceWrappers();
             StatsFrameworkInitializer.registerServiceWrappers();
+            RollbackManagerFrameworkInitializer.initialize();
         } finally {
             // If any of the above code throws, we're in a pretty bad shape and the process
             // will likely crash, but we'll reset it just in case there's an exception handler...
diff --git a/core/java/android/content/rollback/RollbackManagerFrameworkInitializer.java b/core/java/android/content/rollback/RollbackManagerFrameworkInitializer.java
new file mode 100644
index 0000000..c5e4f99
--- /dev/null
+++ b/core/java/android/content/rollback/RollbackManagerFrameworkInitializer.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2020 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.content.rollback;
+
+import android.annotation.SystemApi;
+import android.app.SystemServiceRegistry;
+import android.content.Context;
+
+/**
+ * Class holding initialization code for the RollbackManager module.
+ *
+ * @hide
+ */
+@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
+public class RollbackManagerFrameworkInitializer {
+    private RollbackManagerFrameworkInitializer() {}
+
+    /**
+     * Called by {@link SystemServiceRegistry}'s static initializer and registers RollbackManager
+     * to {@link Context}, so that {@link Context#getSystemService} can return it.
+     *
+     * @throws IllegalStateException if this is called from anywhere besides
+     *     {@link SystemServiceRegistry}
+     */
+    public static void initialize() {
+        SystemServiceRegistry.registerContextAwareService(
+                Context.ROLLBACK_SERVICE, RollbackManager.class,
+                (context, b) -> new RollbackManager(context, IRollbackManager.Stub.asInterface(b)));
+    }
+}