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 5b3db011b4274..5f7d938673314 100644 --- a/services/core/java/com/android/server/graphics/fonts/FontManagerService.java +++ b/services/core/java/com/android/server/graphics/fonts/FontManagerService.java @@ -63,7 +63,7 @@ public final class FontManagerService extends IFontManager.Stub { @Override public FontConfig getFontConfig() throws RemoteException { - return getCurrentFontSettings().getSystemFontConfig(); + return getSystemFontConfig(); } /* package */ static class SystemFontException extends AndroidException { @@ -103,7 +103,7 @@ public final class FontManagerService extends IFontManager.Stub { if (!Typeface.ENABLE_LAZY_TYPEFACE_INITIALIZATION) { return null; } - return mService.getCurrentFontSettings().getSerializedSystemFontMap(); + return mService.getCurrentFontMap(); } }); publishBinderService(Context.FONT_SERVICE, mService); @@ -162,7 +162,7 @@ public final class FontManagerService extends IFontManager.Stub { @GuardedBy("FontManagerService.this") @Nullable - private SystemFontSettings mCurrentFontSettings = null; + private SharedMemory mSerializedFontMap = null; private FontManagerService(Context context) { mContext = context; @@ -188,12 +188,12 @@ public final class FontManagerService extends IFontManager.Stub { return mContext; } - @NonNull /* package */ SystemFontSettings getCurrentFontSettings() { + @NonNull /* package */ SharedMemory getCurrentFontMap() { synchronized (FontManagerService.this) { - if (mCurrentFontSettings == null) { - mCurrentFontSettings = SystemFontSettings.create(mUpdatableFontDir); + if (mSerializedFontMap == null) { + mSerializedFontMap = buildNewSerializedFontMap(); } - return mCurrentFontSettings; + return mSerializedFontMap; } } @@ -207,7 +207,7 @@ public final class FontManagerService extends IFontManager.Stub { synchronized (FontManagerService.this) { mUpdatableFontDir.installFontFile(fd, pkcs7Signature); // Create updated font map in the next getSerializedSystemFontMap() call. - mCurrentFontSettings = null; + mSerializedFontMap = null; } } @@ -245,69 +245,44 @@ public final class FontManagerService extends IFontManager.Stub { new FontManagerShellCommand(this).exec(this, in, out, err, args, callback, result); } - /* package */ static class SystemFontSettings { - private final @NonNull SharedMemory mSerializedSystemFontMap; - private final @NonNull FontConfig mSystemFontConfig; - private final @NonNull Map mSystemFallbackMap; - private final @NonNull Map mSystemTypefaceMap; + /** + * Returns an active system font configuration. + */ + public @NonNull FontConfig getSystemFontConfig() { + if (mUpdatableFontDir != null) { + return mUpdatableFontDir.getSystemFontConfig(); + } else { + return SystemFonts.getSystemPreinstalledFontConfig(); + } + } - SystemFontSettings( - @NonNull SharedMemory serializedSystemFontMap, - @NonNull FontConfig systemFontConfig, - @NonNull Map systemFallbackMap, - @NonNull Map systemTypefaceMap) { - mSerializedSystemFontMap = serializedSystemFontMap; - mSystemFontConfig = systemFontConfig; - mSystemFallbackMap = systemFallbackMap; - mSystemTypefaceMap = systemTypefaceMap; + /** + * Make new serialized font map data. + */ + public @Nullable SharedMemory buildNewSerializedFontMap() { + try { + final FontConfig fontConfig = getSystemFontConfig(); + final Map fallback = SystemFonts.buildSystemFallback(fontConfig); + final Map typefaceMap = + SystemFonts.buildSystemTypefaces(fontConfig, fallback); + + return Typeface.serializeFontMap(typefaceMap); + } catch (IOException | ErrnoException e) { + Slog.w(TAG, "Failed to serialize updatable font map. " + + "Retrying with system image fonts.", e); } - public @NonNull SharedMemory getSerializedSystemFontMap() { - return mSerializedSystemFontMap; - } - - public @NonNull FontConfig getSystemFontConfig() { - return mSystemFontConfig; - } - - public @NonNull Map getSystemFallbackMap() { - return mSystemFallbackMap; - } - - public @NonNull Map getSystemTypefaceMap() { - return mSystemTypefaceMap; - } - - public static @Nullable SystemFontSettings create( - @Nullable UpdatableFontDir updatableFontDir) { - if (updatableFontDir != null) { - final FontConfig fontConfig = updatableFontDir.getSystemFontConfig(); - final Map fallback = - SystemFonts.buildSystemFallback(fontConfig); - final Map typefaceMap = - SystemFonts.buildSystemTypefaces(fontConfig, fallback); - - try { - final SharedMemory shm = Typeface.serializeFontMap(typefaceMap); - return new SystemFontSettings(shm, fontConfig, fallback, typefaceMap); - } catch (IOException | ErrnoException e) { - Slog.w(TAG, "Failed to serialize updatable font map. " - + "Retrying with system image fonts.", e); - } - } - + try { final FontConfig fontConfig = SystemFonts.getSystemPreinstalledFontConfig(); final Map fallback = SystemFonts.buildSystemFallback(fontConfig); final Map typefaceMap = SystemFonts.buildSystemTypefaces(fontConfig, fallback); - try { - final SharedMemory shm = Typeface.serializeFontMap(typefaceMap); - return new SystemFontSettings(shm, fontConfig, fallback, typefaceMap); - } catch (IOException | ErrnoException e) { - Slog.e(TAG, "Failed to serialize SystemServer system font map", e); - } - return null; + + return Typeface.serializeFontMap(typefaceMap); + } catch (IOException | ErrnoException e) { + Slog.e(TAG, "Failed to serialize SystemServer system font map", e); } + return null; } } diff --git a/services/core/java/com/android/server/graphics/fonts/FontManagerShellCommand.java b/services/core/java/com/android/server/graphics/fonts/FontManagerShellCommand.java index fd5c020b1a15a..5a01a97dff1ad 100644 --- a/services/core/java/com/android/server/graphics/fonts/FontManagerShellCommand.java +++ b/services/core/java/com/android/server/graphics/fonts/FontManagerShellCommand.java @@ -25,6 +25,7 @@ import android.graphics.fonts.Font; import android.graphics.fonts.FontFamily; import android.graphics.fonts.FontManager; import android.graphics.fonts.FontVariationAxis; +import android.graphics.fonts.SystemFonts; import android.os.Binder; import android.os.ParcelFileDescriptor; import android.os.Process; @@ -95,8 +96,8 @@ public class FontManagerShellCommand extends ShellCommand { } /* package */ void dumpAll(@NonNull IndentingPrintWriter w) { - final FontManagerService.SystemFontSettings settings = mService.getCurrentFontSettings(); - dumpFontConfig(w, settings.getSystemFontConfig()); + FontConfig fontConfig = mService.getSystemFontConfig(); + dumpFontConfig(w, fontConfig); } private void dumpSingleFontConfig( @@ -276,19 +277,19 @@ public class FontManagerShellCommand extends ShellCommand { private int dump(ShellCommand shell) { final Context ctx = mService.getContext(); - final FontManagerService.SystemFontSettings settings = - mService.getCurrentFontSettings(); + if (!DumpUtils.checkDumpPermission(ctx, TAG, shell.getErrPrintWriter())) { return 1; } final IndentingPrintWriter writer = new IndentingPrintWriter(shell.getOutPrintWriter(), " "); String nextArg = shell.getNextArg(); + FontConfig fontConfig = mService.getSystemFontConfig(); if (nextArg == null) { - dumpFontConfig(writer, settings.getSystemFontConfig()); + dumpFontConfig(writer, fontConfig); } else { final Map fallbackMap = - settings.getSystemFallbackMap(); + SystemFonts.buildSystemFallback(fontConfig); FontFamily[] families = fallbackMap.get(nextArg); if (families == null) { writer.println("Font Family \"" + nextArg + "\" not found"); @@ -364,10 +365,9 @@ public class FontManagerShellCommand extends ShellCommand { } private int status(ShellCommand shell) throws SystemFontException { - final FontManagerService.SystemFontSettings settings = mService.getCurrentFontSettings(); final IndentingPrintWriter writer = new IndentingPrintWriter(shell.getOutPrintWriter(), " "); - FontConfig config = settings.getSystemFontConfig(); + FontConfig config = mService.getSystemFontConfig(); writer.println("Current Version: " + config.getConfigVersion()); LocalDateTime dt = LocalDateTime.ofEpochSecond(config.getLastModifiedDate(), 0,