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

Commit 1da1ca17 authored by Kohsuke Yatoh's avatar Kohsuke Yatoh Committed by Automerger Merge Worker
Browse files

Merge "Add FontManager#updateFont API" into sc-dev am: ad685084 am: ca3b164d

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/13422366

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: Ide21f938f21b37119bba546f6198fbd4ab865404
parents c1c1111d ca3b164d
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -2610,6 +2610,17 @@ package android.graphics.fonts {
  public class FontManager {
    method @Nullable public android.text.FontConfig getFontConfig();
    method @RequiresPermission(android.Manifest.permission.UPDATE_FONTS) public int updateFontFile(@NonNull android.os.ParcelFileDescriptor, @NonNull byte[], @IntRange(from=0) int);
    field public static final int RESULT_ERROR_DOWNGRADING = -5; // 0xfffffffb
    field public static final int RESULT_ERROR_FAILED_TO_WRITE_FONT_FILE = -1; // 0xffffffff
    field public static final int RESULT_ERROR_FAILED_UPDATE_CONFIG = -6; // 0xfffffffa
    field public static final int RESULT_ERROR_FONT_UPDATER_DISABLED = -7; // 0xfffffff9
    field public static final int RESULT_ERROR_INVALID_FONT_FILE = -3; // 0xfffffffd
    field public static final int RESULT_ERROR_INVALID_FONT_NAME = -4; // 0xfffffffc
    field public static final int RESULT_ERROR_REMOTE_EXCEPTION = -9; // 0xfffffff7
    field public static final int RESULT_ERROR_VERIFICATION_FAILURE = -2; // 0xfffffffe
    field public static final int RESULT_ERROR_VERSION_MISMATCH = -8; // 0xfffffff8
    field public static final int RESULT_SUCCESS = 0; // 0x0
  }
}
@@ -13664,7 +13675,9 @@ package android.text {
  public final class FontConfig implements android.os.Parcelable {
    method public int describeContents();
    method @NonNull public java.util.List<android.text.FontConfig.Alias> getAliases();
    method @IntRange(from=0) public int getConfigVersion();
    method @NonNull public java.util.List<android.text.FontConfig.FontFamily> getFontFamilies();
    method public long getLastModifiedTimeMillis();
    method public void writeToParcel(@NonNull android.os.Parcel, int);
    field @NonNull public static final android.os.Parcelable.Creator<android.text.FontConfig> CREATOR;
  }
+13 −0
Original line number Diff line number Diff line
@@ -826,6 +826,17 @@ package android.graphics.fonts {

  public class FontManager {
    method @Nullable public android.text.FontConfig getFontConfig();
    method @RequiresPermission(android.Manifest.permission.UPDATE_FONTS) public int updateFontFile(@NonNull android.os.ParcelFileDescriptor, @NonNull byte[], @IntRange(from=0) int);
    field public static final int RESULT_ERROR_DOWNGRADING = -5; // 0xfffffffb
    field public static final int RESULT_ERROR_FAILED_TO_WRITE_FONT_FILE = -1; // 0xffffffff
    field public static final int RESULT_ERROR_FAILED_UPDATE_CONFIG = -6; // 0xfffffffa
    field public static final int RESULT_ERROR_FONT_UPDATER_DISABLED = -7; // 0xfffffff9
    field public static final int RESULT_ERROR_INVALID_FONT_FILE = -3; // 0xfffffffd
    field public static final int RESULT_ERROR_INVALID_FONT_NAME = -4; // 0xfffffffc
    field public static final int RESULT_ERROR_REMOTE_EXCEPTION = -9; // 0xfffffff7
    field public static final int RESULT_ERROR_VERIFICATION_FAILURE = -2; // 0xfffffffe
    field public static final int RESULT_ERROR_VERSION_MISMATCH = -8; // 0xfffffff8
    field public static final int RESULT_SUCCESS = 0; // 0x0
  }

}
@@ -2032,7 +2043,9 @@ package android.text {
  public final class FontConfig implements android.os.Parcelable {
    method public int describeContents();
    method @NonNull public java.util.List<android.text.FontConfig.Alias> getAliases();
    method @IntRange(from=0) public int getConfigVersion();
    method @NonNull public java.util.List<android.text.FontConfig.FontFamily> getFontFamilies();
    method public long getLastModifiedTimeMillis();
    method public void writeToParcel(@NonNull android.os.Parcel, int);
    field @NonNull public static final android.os.Parcelable.Creator<android.text.FontConfig> CREATOR;
  }
+130 −39
Original line number Diff line number Diff line
@@ -16,13 +16,17 @@

package android.graphics.fonts;

import android.Manifest;
import android.annotation.IntDef;
import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.annotation.SystemApi;
import android.annotation.SystemService;
import android.annotation.TestApi;
import android.content.Context;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.text.FontConfig;
import android.util.Log;
@@ -35,6 +39,10 @@ import java.util.Objects;

/**
 * This class gives you control of system installed font files.
 *
 * <p>
 * This class gives you the information of system font configuration and ability of changing them.
 *
 * @hide
 */
@SystemApi
@@ -45,68 +53,87 @@ public class FontManager {
    private final @NonNull IFontManager mIFontManager;

    /** @hide */
    @IntDef(prefix = "ERROR_CODE_",
            value = { ERROR_CODE_OK, ERROR_CODE_FAILED_TO_WRITE_FONT_FILE,
                    ERROR_CODE_VERIFICATION_FAILURE, ERROR_CODE_FONT_NAME_MISMATCH,
                    ERROR_CODE_INVALID_FONT_FILE, ERROR_CODE_MISSING_POST_SCRIPT_NAME,
                    ERROR_CODE_DOWNGRADING, ERROR_CODE_FAILED_TO_CREATE_CONFIG_FILE,
                    ERROR_CODE_FONT_UPDATER_DISABLED })
    @IntDef(prefix = "RESULT_",
            value = { RESULT_SUCCESS, RESULT_ERROR_FAILED_TO_WRITE_FONT_FILE,
                    RESULT_ERROR_VERIFICATION_FAILURE, RESULT_ERROR_VERSION_MISMATCH,
                    RESULT_ERROR_INVALID_FONT_FILE, RESULT_ERROR_INVALID_FONT_NAME,
                    RESULT_ERROR_DOWNGRADING, RESULT_ERROR_FAILED_UPDATE_CONFIG,
                    RESULT_ERROR_FONT_UPDATER_DISABLED, RESULT_ERROR_REMOTE_EXCEPTION })
    @Retention(RetentionPolicy.SOURCE)
    public @interface ErrorCode {}
    public @interface ResultCode {}

    /**
     * Indicates an operation has processed successfully.
     * @hide
     * Indicates that the request has been processed successfully.
     */
    public static final int ERROR_CODE_OK = 0;
    public static final int RESULT_SUCCESS = 0;

    /**
     * Indicates a failure of writing font files.
     * @hide
     * Indicates that a failure occurred while writing the font file to disk.
     *
     * This is an internal error that the system cannot place the font file for being used by
     * application.
     */
    public static final int ERROR_CODE_FAILED_TO_WRITE_FONT_FILE = -1;
    public static final int RESULT_ERROR_FAILED_TO_WRITE_FONT_FILE = -1;

    /**
     * Indicates a failure of fs-verity setup.
     * @hide
     * Indicates that a failure occurred during the verification of the font file.
     *
     * The system failed to verify given font file contents and signature with system installed
     * certificate.
     */
    public static final int ERROR_CODE_VERIFICATION_FAILURE = -2;
    public static final int RESULT_ERROR_VERIFICATION_FAILURE = -2;

    /**
     * Indicates a failure of verifying the font name with PostScript name.
     * @hide
     * Indicates that a failure occurred as a result of invalid font format or content.
     *
     * Android only accepts OpenType compliant font files.
     */
    public static final int ERROR_CODE_FONT_NAME_MISMATCH = -3;
    public static final int RESULT_ERROR_INVALID_FONT_FILE = -3;

    /**
     * Indicates a failure of placing fonts due to unexpected font contents.
     * @hide
     * Indicates a failure due to missing PostScript name in font's name table.
     *
     * Indicates that a failure occurred since PostScript name in the name table(ID=6) was missing.
     * The font is expected to have a PostScript name.
     */
    public static final int ERROR_CODE_INVALID_FONT_FILE = -4;
    public static final int RESULT_ERROR_INVALID_FONT_NAME = -4;

    /**
     * Indicates a failure due to missing PostScript name in name table.
     * @hide
     * Indicates that a failure occurred due to downgrading the font version.
     *
     * The font must have equal or newer revision in its head table.
     */
    public static final int ERROR_CODE_MISSING_POST_SCRIPT_NAME = -5;
    public static final int RESULT_ERROR_DOWNGRADING = -5;

    /**
     * Indicates a failure of placing fonts due to downgrading.
     * @hide
     * Indicates that a failure occurred while updating system font configuration.
     *
     * This is an internal error that the system couldn't update the {@link FontConfig}.
     */
    public static final int ERROR_CODE_DOWNGRADING = -6;
    public static final int RESULT_ERROR_FAILED_UPDATE_CONFIG = -6;

    /**
     * Indicates a failure of writing system font configuration XML file.
     * @hide
     * Indicates a failure due to disabled font updater.
     *
     * This is typically returned due to missing Linux kernel feature.
     * The font updater only works with the Linux kernel that has fs-verity feature. The fs-verity
     * is required after the device shipped with Android 11. Thus the updated device may not have
     * fs-verity feature and font updater is disabled.
     */
    public static final int ERROR_CODE_FAILED_TO_CREATE_CONFIG_FILE = -7;
    public static final int RESULT_ERROR_FONT_UPDATER_DISABLED = -7;

    /**
     * Indicates a failure due to disabled font updater.
     * @hide
     * Indicates that a failure occurred because provided {@code baseVersion} did not match.
     *
     * The {@code baseVersion} provided does not match to the current {@link FontConfig} version.
     * Please get the latest configuration and update {@code baseVersion} accordingly.
     */
    public static final int ERROR_CODE_FONT_UPDATER_DISABLED = -8;
    public static final int RESULT_ERROR_VERSION_MISMATCH = -8;

    /**
     * Indicates a failure due to IPC communication.
     */
    public static final int RESULT_ERROR_REMOTE_EXCEPTION = -9;

    /**
     * Indicates a failure of opening font file.
@@ -115,7 +142,7 @@ public class FontManager {
     *
     * @hide
     */
    public static final int ERROR_CODE_FAILED_TO_OPEN_FONT_FILE = -10001;
    public static final int RESULT_ERROR_FAILED_TO_OPEN_FONT_FILE = -10001;

    /**
     * Indicates a failure of opening signature file.
@@ -124,7 +151,7 @@ public class FontManager {
     *
     * @hide
     */
    public static final int ERROR_CODE_FAILED_TO_OPEN_SIGNATURE_FILE = -10002;
    public static final int RESULT_ERROR_FAILED_TO_OPEN_SIGNATURE_FILE = -10002;

    /**
     * Indicates a failure of invalid shell command arguments.
@@ -133,7 +160,7 @@ public class FontManager {
     *
     * @hide
     */
    public static final int ERROR_CODE_INVALID_SHELL_ARGUMENT = -10003;
    public static final int RESULT_ERROR_INVALID_SHELL_ARGUMENT = -10003;

    /**
     * Indicates a failure of reading signature file.
@@ -142,7 +169,7 @@ public class FontManager {
     *
     * @hide
     */
    public static final int ERROR_CODE_INVALID_SIGNATURE_FILE = -10004;
    public static final int RESULT_ERROR_INVALID_SIGNATURE_FILE = -10004;

    /**
     * Indicates a failure due to exceeding allowed signature file size (8kb).
@@ -151,7 +178,7 @@ public class FontManager {
     *
     * @hide
     */
    public static final int ERROR_CODE_SIGNATURE_TOO_LARGE = -10005;
    public static final int RESULT_ERROR_SIGNATURE_TOO_LARGE = -10005;


    private FontManager(@NonNull IFontManager iFontManager) {
@@ -177,6 +204,70 @@ public class FontManager {
        }
    }

    /**
     * Update system installed font file.
     *
     * <p>
     * To protect devices, system font updater relies on the Linux Kernel feature called fs-verity.
     * If the device is not ready for fs-verity, {@link #RESULT_ERROR_FONT_UPDATER_DISABLED} will be
     * returned.
     *
     * Android only accepts OpenType compliant font files. If other font files are provided,
     * {@link #RESULT_ERROR_INVALID_FONT_FILE} will be returned.
     *
     * The font file to be updated is identified by PostScript name stored in name table. If the
     * font file doesn't have PostScript name entry, {@link #RESULT_ERROR_INVALID_FONT_NAME} will be
     * returned.
     *
     * The entire font file is verified with the given signature for the system installed
     * certificate. If the system cannot verify the font contents,
     * {@link #RESULT_ERROR_VERIFICATION_FAILURE} will be returned.
     *
     * The font file must have newer or equal revision number in the head table. In other words, the
     * downgrading font file is not allowed. If the older font file is provided,
     * {@link #RESULT_ERROR_DOWNGRADING} will be returned.
     *
     * The caller must specify the base config version for keeping consist system configuration. If
     * the system configuration is updated for some reason between you get config with
     * {@link #getFontConfig()} and calling this method, {@link #RESULT_ERROR_VERSION_MISMATCH} will
     * be returned. Get the latest font configuration by calling {@link #getFontConfig()} again and
     * try with the latest config version again.
     *
     * @param pfd A file descriptor of the font file.
     * @param signature A PKCS#7 detached signature for verifying entire font files.
     * @param baseVersion A base config version to be updated. You can get latest config version by
     *                    {@link FontConfig#getConfigVersion()} via {@link #getFontConfig()}. If the
     *                    system has newer config version, the update will fail with
     *                    {@link #RESULT_ERROR_VERSION_MISMATCH}. Try to get the latest config and
     *                    try update again.
     * @return result code.
     *
     * @see FontConfig#getConfigVersion()
     * @see #getFontConfig()
     * @see #RESULT_SUCCESS
     * @see #RESULT_ERROR_FAILED_TO_WRITE_FONT_FILE
     * @see #RESULT_ERROR_VERIFICATION_FAILURE
     * @see #RESULT_ERROR_VERSION_MISMATCH
     * @see #RESULT_ERROR_INVALID_FONT_FILE
     * @see #RESULT_ERROR_INVALID_FONT_NAME
     * @see #RESULT_ERROR_DOWNGRADING
     * @see #RESULT_ERROR_FAILED_UPDATE_CONFIG
     * @see #RESULT_ERROR_FONT_UPDATER_DISABLED
     * @see #RESULT_ERROR_REMOTE_EXCEPTION
     */
    @RequiresPermission(Manifest.permission.UPDATE_FONTS) public @ResultCode int updateFontFile(
            @NonNull ParcelFileDescriptor pfd,
            @NonNull byte[] signature,
            @IntRange(from = 0) int baseVersion
    ) {
        try {
            return mIFontManager.updateFont(pfd, signature, baseVersion);
        } catch (RemoteException e) {
            Log.e(TAG, "Failed to call updateFont API", e);
            return RESULT_ERROR_REMOTE_EXCEPTION;
        }
    }

    /**
     * Factory method of the FontManager.
     *
+10 −9
Original line number Diff line number Diff line
@@ -53,7 +53,7 @@ import java.util.List;
public final class FontConfig implements Parcelable {
    private final @NonNull List<FontFamily> mFamilies;
    private final @NonNull List<Alias> mAliases;
    private final long mLastModifiedDate;
    private final long mLastModifiedTimeMillis;
    private final int mConfigVersion;

    /**
@@ -65,10 +65,10 @@ public final class FontConfig implements Parcelable {
     * @hide Only system server can create this instance and passed via IPC.
     */
    public FontConfig(@NonNull List<FontFamily> families, @NonNull List<Alias> aliases,
            long lastModifiedDate, @IntRange(from = 0) int configVersion) {
            long lastModifiedTimeMillis, @IntRange(from = 0) int configVersion) {
        mFamilies = families;
        mAliases = aliases;
        mLastModifiedDate = lastModifiedDate;
        mLastModifiedTimeMillis = lastModifiedTimeMillis;
        mConfigVersion = configVersion;
    }

@@ -93,20 +93,21 @@ public final class FontConfig implements Parcelable {
    }

    /**
     * Returns the last modified date as Java epoch seconds.
     * Returns the last modified time in milliseconds.
     *
     * This is a value of {@link System#currentTimeMillis()} when the system font configuration was
     * modified last time.
     *
     * If there is no update, this return 0.
     * @hide
     */
    public long getLastModifiedDate() {
        return mLastModifiedDate;
    public long getLastModifiedTimeMillis() {
        return mLastModifiedTimeMillis;
    }

    /**
     * Returns the monotonically increasing config version value.
     *
     * The config version is reset to 0 when the system is restarted.
     * @hide
     */
    public @IntRange(from = 0) int getConfigVersion() {
        return mConfigVersion;
@@ -132,7 +133,7 @@ public final class FontConfig implements Parcelable {
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeParcelableList(mFamilies, flags);
        dest.writeParcelableList(mAliases, flags);
        dest.writeLong(mLastModifiedDate);
        dest.writeLong(mLastModifiedTimeMillis);
        dest.writeInt(mConfigVersion);
    }

+3 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.internal.graphics.fonts;

import android.os.ParcelFileDescriptor;
import android.text.FontConfig;
import android.graphics.fonts.SystemFontState;

@@ -26,4 +27,6 @@ import android.graphics.fonts.SystemFontState;
 */
interface IFontManager {
    FontConfig getFontConfig();

    int updateFont(in ParcelFileDescriptor fd, in byte[] signature, int baseVersion);
}
Loading