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

Commit c0cf4f09 authored by Seigo Nonaka's avatar Seigo Nonaka
Browse files

Do not use many FDs in FontManagerService.

Currently there are over 170 font files are installed in system directory.
Opening 170+ files and keep them is not unacceptable. Pass URI instead.

At the same time, this CL hides full font path from FontConfig since
/system/fonts directory will be deprecated in future.

Bug: 36660849
Test: android.text.cts.FontManagerTest passed
Change-Id: I1d216dc9c6dec702a4ce3b946bfda6dcbe12b7fe
(cherry picked from commit 455f1bfc)
parent 76d77937
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -41078,9 +41078,9 @@ package android.text {
  public static final class FontConfig.Font implements android.os.Parcelable {
  public static final class FontConfig.Font implements android.os.Parcelable {
    method public int describeContents();
    method public int describeContents();
    method public android.text.FontConfig.Axis[] getAxes();
    method public android.text.FontConfig.Axis[] getAxes();
    method public android.os.ParcelFileDescriptor getFd();
    method public java.lang.String getFontName();
    method public java.lang.String getFontName();
    method public int getTtcIndex();
    method public int getTtcIndex();
    method public android.net.Uri getUri();
    method public int getWeight();
    method public int getWeight();
    method public boolean isItalic();
    method public boolean isItalic();
    method public void writeToParcel(android.os.Parcel, int);
    method public void writeToParcel(android.os.Parcel, int);
+1 −1
Original line number Original line Diff line number Diff line
@@ -44532,9 +44532,9 @@ package android.text {
  public static final class FontConfig.Font implements android.os.Parcelable {
  public static final class FontConfig.Font implements android.os.Parcelable {
    method public int describeContents();
    method public int describeContents();
    method public android.text.FontConfig.Axis[] getAxes();
    method public android.text.FontConfig.Axis[] getAxes();
    method public android.os.ParcelFileDescriptor getFd();
    method public java.lang.String getFontName();
    method public java.lang.String getFontName();
    method public int getTtcIndex();
    method public int getTtcIndex();
    method public android.net.Uri getUri();
    method public int getWeight();
    method public int getWeight();
    method public boolean isItalic();
    method public boolean isItalic();
    method public void writeToParcel(android.os.Parcel, int);
    method public void writeToParcel(android.os.Parcel, int);
+1 −1
Original line number Original line Diff line number Diff line
@@ -41285,9 +41285,9 @@ package android.text {
  public static final class FontConfig.Font implements android.os.Parcelable {
  public static final class FontConfig.Font implements android.os.Parcelable {
    method public int describeContents();
    method public int describeContents();
    method public android.text.FontConfig.Axis[] getAxes();
    method public android.text.FontConfig.Axis[] getAxes();
    method public android.os.ParcelFileDescriptor getFd();
    method public java.lang.String getFontName();
    method public java.lang.String getFontName();
    method public int getTtcIndex();
    method public int getTtcIndex();
    method public android.net.Uri getUri();
    method public int getWeight();
    method public int getWeight();
    method public boolean isItalic();
    method public boolean isItalic();
    method public void writeToParcel(android.os.Parcel, int);
    method public void writeToParcel(android.os.Parcel, int);
+12 −71
Original line number Original line Diff line number Diff line
@@ -22,13 +22,11 @@ import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.graphics.FontListParser;
import android.graphics.FontListParser;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcel;
import android.os.ParcelFileDescriptor;
import android.os.Parcelable;
import android.os.Parcelable;


import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.Retention;
import java.util.Arrays;




/**
/**
@@ -43,20 +41,6 @@ public final class FontConfig implements Parcelable {
        mAliases = aliases;
        mAliases = aliases;
    }
    }


    /**
     * For duplicating file descriptors.
     *
     * Note that this copy constructor can not be usable for deep copy.
     * @hide
     */
    public FontConfig(@NonNull FontConfig config) {
        mFamilies = new Family[config.mFamilies.length];
        for (int i = 0; i < config.mFamilies.length; ++i) {
            mFamilies[i] = new Family(config.mFamilies[i]);
        }
        mAliases = Arrays.copyOf(config.mAliases, config.mAliases.length);
    }

    /**
    /**
     * Returns the ordered list of families included in the system fonts.
     * Returns the ordered list of families included in the system fonts.
     */
     */
@@ -174,7 +158,7 @@ public final class FontConfig implements Parcelable {
        private final @NonNull Axis[] mAxes;
        private final @NonNull Axis[] mAxes;
        private final int mWeight;
        private final int mWeight;
        private final boolean mIsItalic;
        private final boolean mIsItalic;
        private @Nullable ParcelFileDescriptor mFd;
        private Uri mUri;


        /**
        /**
         * @hide
         * @hide
@@ -186,29 +170,6 @@ public final class FontConfig implements Parcelable {
            mAxes = axes;
            mAxes = axes;
            mWeight = weight;
            mWeight = weight;
            mIsItalic = isItalic;
            mIsItalic = isItalic;
            mFd = null;
        }

        /**
         * This is for duplicating FileDescriptors.
         *
         * Note that this copy ctor doesn't deep copy the members.
         *
         * @hide
         */
        public Font(Font origin) {
            mFontName = origin.mFontName;
            mTtcIndex = origin.mTtcIndex;
            mAxes = origin.mAxes;
            mWeight = origin.mWeight;
            mIsItalic = origin.mIsItalic;
            if (origin.mFd != null) {
                try {
                    mFd = origin.mFd.dup();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        }


        /**
        /**
@@ -247,17 +208,20 @@ public final class FontConfig implements Parcelable {
        }
        }


        /**
        /**
         * Returns a file descriptor to access the specified font. This should be closed after use.
         * Returns the content uri associated to this font.
         *
         * You can reach to the font contents by calling {@link
         * android.content.ContentResolver#openInputStream}.
         */
         */
        public @Nullable ParcelFileDescriptor getFd() {
        public @Nullable Uri getUri() {
            return mFd;
            return mUri;
        }
        }


        /**
        /**
         * @hide
         * @hide
         */
         */
        public void setFd(@NonNull ParcelFileDescriptor fd) {
        public void setUri(@NonNull Uri uri) {
            mFd = fd;
            mUri = uri;
        }
        }


        /**
        /**
@@ -269,11 +233,7 @@ public final class FontConfig implements Parcelable {
            mAxes = in.createTypedArray(Axis.CREATOR);
            mAxes = in.createTypedArray(Axis.CREATOR);
            mWeight = in.readInt();
            mWeight = in.readInt();
            mIsItalic = in.readInt() == 1;
            mIsItalic = in.readInt() == 1;
            if (in.readInt() == 1) { /* has FD */
            mUri = in.readTypedObject(Uri.CREATOR);
                mFd = ParcelFileDescriptor.CREATOR.createFromParcel(in);
            } else {
                mFd = null;
            }
        }
        }


        @Override
        @Override
@@ -283,10 +243,7 @@ public final class FontConfig implements Parcelable {
            out.writeTypedArray(mAxes, flag);
            out.writeTypedArray(mAxes, flag);
            out.writeInt(mWeight);
            out.writeInt(mWeight);
            out.writeInt(mIsItalic ? 1 : 0);
            out.writeInt(mIsItalic ? 1 : 0);
            out.writeInt(mFd == null ? 0 : 1);
            out.writeTypedObject(mUri, flag);
            if (mFd != null) {
                mFd.writeToParcel(out, flag);
            }
        }
        }


        @Override
        @Override
@@ -424,22 +381,6 @@ public final class FontConfig implements Parcelable {
            mVariant = variant;
            mVariant = variant;
        }
        }


        /**
         * For duplicating file descriptor underlying Font object.
         *
         * This copy constructor is not for deep copying.
         * @hide
         */
        public Family(Family origin) {
            mName = origin.mName;
            mLanguage = origin.mLanguage;
            mVariant = origin.mVariant;
            mFonts = new Font[origin.mFonts.length];
            for (int i = 0; i < origin.mFonts.length; ++i) {
                mFonts[i] = new Font(origin.mFonts[i]);
            }
        }

        /**
        /**
         * Returns the name given by the system to this font family.
         * Returns the name given by the system to this font family.
         */
         */
+2 −3
Original line number Original line Diff line number Diff line
@@ -189,9 +189,8 @@ public class FontListParser {
                skip(parser);
                skip(parser);
            }
            }
        }
        }
        String fullFilename = "/system/fonts/" +
        String sanitizedName = FILENAME_WHITESPACE_PATTERN.matcher(filename).replaceAll("");
                FILENAME_WHITESPACE_PATTERN.matcher(filename).replaceAll("");
        return new FontConfig.Font(sanitizedName, index,
        return new FontConfig.Font(fullFilename, index,
                axes.toArray(new FontConfig.Axis[axes.size()]), weight, isItalic);
                axes.toArray(new FontConfig.Axis[axes.size()]), weight, isItalic);
    }
    }


Loading