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
This commit is contained in:
@@ -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,25 +231,30 @@ 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;
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user