Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 693deec1 authored by Yeabkal Wubshit's avatar Yeabkal Wubshit
Browse files

Start FontManagerService Async

The service used to be started on the critical boot path. Since fonts
will not be needed for a while after when the service got started, we
can offload the service-start work (i.e. creating/reading the font files
and setting font map) to a separate thread.

Bug: 291675714
Test: boot succeeds

Change-Id: Ie851e7ab5dc0e408d4ebc24bc0ebc57119464a43
parent d2b36a15
Loading
Loading
Loading
Loading
+29 −12
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ import com.android.internal.security.VerityUtils;
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.Collections;
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 @@ public final class FontManagerService extends IFontManager.Stub {
    /** 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 @@ public final class FontManagerService extends IFontManager.Stub {
                            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,13 +231,16 @@ public final class FontManagerService extends IFontManager.Stub {
    @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;

        SystemServerInitThreadPool.submit(() -> {
            initialize();

            // Set system font map only if there is updatable font directory.
@@ -238,6 +253,8 @@ public final class FontManagerService extends IFontManager.Stub {
                    Slog.w(TAG, "Failed to set system font map of system_server");
                }
            }
            serviceStarted.complete(null);
        }, "FontManagerService_create");
    }

    @Nullable