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 ee799d5021632..ae9c64b480068 100644 --- a/services/core/java/com/android/server/graphics/fonts/FontManagerService.java +++ b/services/core/java/com/android/server/graphics/fonts/FontManagerService.java @@ -62,6 +62,7 @@ public final class FontManagerService extends IFontManager.Stub { private static final String TAG = "FontManagerService"; private static final String FONT_FILES_DIR = "/data/fonts/files"; + private static final String CONFIG_XML_FILE = "/data/fonts/config/config.xml"; @RequiresPermission(Manifest.permission.UPDATE_FONTS) @Override @@ -130,9 +131,9 @@ public final class FontManagerService extends IFontManager.Stub { public static final class Lifecycle extends SystemService { private final FontManagerService mService; - public Lifecycle(@NonNull Context context) { + public Lifecycle(@NonNull Context context, boolean safeMode) { super(context); - mService = new FontManagerService(context); + mService = new FontManagerService(context, safeMode); } @Override @@ -187,18 +188,24 @@ public final class FontManagerService extends IFontManager.Stub { @Nullable private SharedMemory mSerializedFontMap = null; - private FontManagerService(Context context) { + private FontManagerService(Context context, boolean safeMode) { + 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; - mUpdatableFontDir = createUpdatableFontDir(); + mUpdatableFontDir = createUpdatableFontDir(safeMode); initialize(); } @Nullable - private static UpdatableFontDir createUpdatableFontDir() { + private static UpdatableFontDir createUpdatableFontDir(boolean safeMode) { + // Never read updatable font files in safe mode. + if (safeMode) return null; // If apk verity is supported, fs-verity should be available. if (!VerityUtils.isFsVeritySupported()) return null; - return new UpdatableFontDir(new File(FONT_FILES_DIR), - new OtfFontFileParser(), new FsverityUtilImpl()); + return new UpdatableFontDir(new File(FONT_FILES_DIR), new OtfFontFileParser(), + new FsverityUtilImpl(), new File(CONFIG_XML_FILE)); } private void initialize() { @@ -243,18 +250,23 @@ public final class FontManagerService extends IFontManager.Stub { } } - /* package */ void clearUpdates() throws SystemFontException { - if (mUpdatableFontDir == null) { - throw new SystemFontException( - FontManager.RESULT_ERROR_FONT_UPDATER_DISABLED, - "The font updater is disabled."); - } - synchronized (mUpdatableFontDirLock) { - mUpdatableFontDir.clearUpdates(); - updateSerializedFontMap(); - } + /** + * Clears all updates and restarts FontManagerService. + * + *
CAUTION: this method is not safe. Existing processes may crash due to missing font files. + * This method is only for {@link FontManagerShellCommand}. + */ + /* package */ void clearUpdates() { + UpdatableFontDir.deleteAllFiles(new File(FONT_FILES_DIR), new File(CONFIG_XML_FILE)); + initialize(); } + /** + * Restarts FontManagerService, removing not-the-latest font files. + * + *
CAUTION: this method is not safe. Existing processes may crash due to missing font files.
+ * This method is only for {@link FontManagerShellCommand}.
+ */
/* package */ void restart() {
initialize();
}
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 e4928ce65a2dc..3fecef703613b 100644
--- a/services/core/java/com/android/server/graphics/fonts/FontManagerShellCommand.java
+++ b/services/core/java/com/android/server/graphics/fonts/FontManagerShellCommand.java
@@ -448,7 +448,7 @@ public class FontManagerShellCommand extends ShellCommand {
}
}
- private int clear(ShellCommand shell) throws SystemFontException {
+ private int clear(ShellCommand shell) {
mService.clearUpdates();
shell.getOutPrintWriter().println("Success");
return 0;
diff --git a/services/core/java/com/android/server/graphics/fonts/UpdatableFontDir.java b/services/core/java/com/android/server/graphics/fonts/UpdatableFontDir.java
index 981cc8387af9e..743b4d90dd738 100644
--- a/services/core/java/com/android/server/graphics/fonts/UpdatableFontDir.java
+++ b/services/core/java/com/android/server/graphics/fonts/UpdatableFontDir.java
@@ -59,8 +59,6 @@ final class UpdatableFontDir {
private static final String TAG = "UpdatableFontDir";
private static final String RANDOM_DIR_PREFIX = "~~";
- private static final String CONFIG_XML_FILE = "/data/fonts/config/config.xml";
-
/** Interface to mock font file access in tests. */
interface FontFileParser {
String getPostScriptName(File file) throws IOException;
@@ -139,8 +137,9 @@ final class UpdatableFontDir {
*/
private final ArrayMap