Merge "Start FontManagerService Async" into main
diff --git a/services/core/java/com/android/server/graphics/fonts/FontManagerService.java b/services/core/java/com/android/server/graphics/fonts/FontManagerService.java
index 080b947..c6a50ed 100644
--- a/services/core/java/com/android/server/graphics/fonts/FontManagerService.java
+++ b/services/core/java/com/android/server/graphics/fonts/FontManagerService.java
@@ -46,6 +46,7 @@
 import com.android.internal.util.DumpUtils;
 import com.android.internal.util.Preconditions;
 import com.android.server.LocalServices;
+import com.android.server.SystemServerInitThreadPool;
 import com.android.server.SystemService;
 
 import java.io.File;
@@ -61,6 +62,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.concurrent.CompletableFuture;
 
 /** A service for managing system fonts. */
 public final class FontManagerService extends IFontManager.Stub {
@@ -136,10 +138,11 @@
     /** Class to manage FontManagerService's lifecycle. */
     public static final class Lifecycle extends SystemService {
         private final FontManagerService mService;
+        private final CompletableFuture<Void> mServiceStarted = new CompletableFuture<>();
 
         public Lifecycle(@NonNull Context context, boolean safeMode) {
             super(context);
-            mService = new FontManagerService(context, safeMode);
+            mService = new FontManagerService(context, safeMode, mServiceStarted);
         }
 
         @Override
@@ -152,11 +155,20 @@
                             if (!Typeface.ENABLE_LAZY_TYPEFACE_INITIALIZATION) {
                                 return null;
                             }
+                            mServiceStarted.join();
                             return mService.getCurrentFontMap();
                         }
                     });
             publishBinderService(Context.FONT_SERVICE, mService);
         }
+
+        @Override
+        public void onBootPhase(int phase) {
+            if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
+                // Wait for FontManagerService to start since it will be needed after this point.
+                mServiceStarted.join();
+            }
+        }
     }
 
     private static class FsverityUtilImpl implements UpdatableFontDir.FsverityUtil {
@@ -219,25 +231,30 @@
     @Nullable
     private SharedMemory mSerializedFontMap = null;
 
-    private FontManagerService(Context context, boolean safeMode) {
+    private FontManagerService(
+            Context context, boolean safeMode, CompletableFuture<Void> serviceStarted) {
         if (safeMode) {
             Slog.i(TAG, "Entering safe mode. Deleting all font updates.");
             UpdatableFontDir.deleteAllFiles(new File(FONT_FILES_DIR), new File(CONFIG_XML_FILE));
         }
         mContext = context;
         mIsSafeMode = safeMode;
-        initialize();
 
-        // Set system font map only if there is updatable font directory.
-        // If there is no updatable font directory, `initialize` will have already loaded the
-        // system font map, so there's no need to set the system font map again here.
-        if  (mUpdatableFontDir != null) {
-            try {
-                Typeface.setSystemFontMap(getCurrentFontMap());
-            } catch (IOException | ErrnoException e) {
-                Slog.w(TAG, "Failed to set system font map of system_server");
+        SystemServerInitThreadPool.submit(() -> {
+            initialize();
+
+            // Set system font map only if there is updatable font directory.
+            // If there is no updatable font directory, `initialize` will have already loaded the
+            // system font map, so there's no need to set the system font map again here.
+            if  (mUpdatableFontDir != null) {
+                try {
+                    Typeface.setSystemFontMap(getCurrentFontMap());
+                } catch (IOException | ErrnoException e) {
+                    Slog.w(TAG, "Failed to set system font map of system_server");
+                }
             }
-        }
+            serviceStarted.complete(null);
+        }, "FontManagerService_create");
     }
 
     @Nullable