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

Commit 5e0aed49 authored by Seigo Nonaka's avatar Seigo Nonaka
Browse files

Fix NPE happens if no source was specified to font element.

This also fixes unexpected fallback to the old implemntation by calling
allowUnsupportedFont() which is only for backward compatibility.

This CL also remove getResourceId() method. Nobody uses this method.

Bug: 37865521
Bug: 37844248
Test: am instrument -w -e class android.content.res.cts.ResourcesTest\
      android.content.cts/android.support.test.runner.AndroidJUnitRunner
Change-Id: I987448add728c53c916f24a2ea8f337be417248f
parent 35dcb5f5
Loading
Loading
Loading
Loading
+12 −10
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ import com.android.internal.R;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Xml;

import org.xmlpull.v1.XmlPullParser;
@@ -34,6 +35,7 @@ import java.util.List;
 * @hide
 */
public class FontResourcesParser {
    private static final String TAG = "FontResourcesParser";
    private static final int NORMAL_WEIGHT = 400;
    private static final int ITALIC = 1;

@@ -79,12 +81,10 @@ public class FontResourcesParser {
        private boolean mItalic;
        private int mResourceId;

        public FontFileResourceEntry(@NonNull String fileName, int weight, boolean italic,
                int resourceId) {
        public FontFileResourceEntry(@NonNull String fileName, int weight, boolean italic) {
            mFileName = fileName;
            mWeight = weight;
            mItalic = italic;
            mResourceId = resourceId;
        }

        public @NonNull String getFileName() {
@@ -98,10 +98,6 @@ public class FontResourcesParser {
        public boolean isItalic() {
            return mItalic;
        }

        public int getResourceId() {
            return mResourceId;
        }
    }

    // A class represents file based font-family element in xml file.
@@ -140,6 +136,7 @@ public class FontResourcesParser {
            return readFamily(parser, resources);
        } else {
            skip(parser);
            Log.e(TAG, "Failed to find font-family tag");
            return null;
        }
    }
@@ -184,7 +181,10 @@ public class FontResourcesParser {
            if (parser.getEventType() != XmlPullParser.START_TAG) continue;
            String tag = parser.getName();
            if (tag.equals("font")) {
                fonts.add(readFont(parser, resources));
                final FontFileResourceEntry entry = readFont(parser, resources);
                if (entry != null) {
                    fonts.add(entry);
                }
            } else {
                skip(parser);
            }
@@ -203,12 +203,14 @@ public class FontResourcesParser {
        int weight = array.getInt(R.styleable.FontFamilyFont_fontWeight, NORMAL_WEIGHT);
        boolean isItalic = ITALIC == array.getInt(R.styleable.FontFamilyFont_fontStyle, 0);
        String filename = array.getString(R.styleable.FontFamilyFont_font);
        int resourceId = array.getResourceId(R.styleable.FontFamilyFont_font, 0);
        array.recycle();
        while (parser.next() != XmlPullParser.END_TAG) {
            skip(parser);
        }
        return new FontFileResourceEntry(filename, weight, isItalic, resourceId);
        if (filename == null) {
            return null;
        }
        return new FontFileResourceEntry(filename, weight, isItalic);
    }

    private static void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
+0 −1
Original line number Diff line number Diff line
@@ -789,7 +789,6 @@ public class ResourcesImpl {
                final FontResourcesParser.FamilyResourceEntry familyEntry =
                        FontResourcesParser.parse(rp, wrapper);
                if (familyEntry == null) {
                    Log.e(TAG, "Failed to find font-family tag");
                    return null;
                }
                return Typeface.createFromResources(familyEntry, mAssets, file);
+4 −5
Original line number Diff line number Diff line
@@ -230,6 +230,7 @@ public class Typeface {

            FontFamily fontFamily = new FontFamily();
            for (final FontFileResourceEntry fontFile : filesEntry.getEntries()) {
                // TODO: Add ttc and variation font support. (b/37853920)
                if (!fontFamily.addFontFromAssetManager(mgr, fontFile.getFileName(),
                        0 /* resourceCookie */, false /* isAsset */, 0 /* ttcIndex */,
                        fontFile.getWeight(), fontFile.isItalic() ? STYLE_ITALIC : STYLE_NORMAL,
@@ -237,11 +238,9 @@ public class Typeface {
                    return null;
                }
            }
            // Due to backward compatibility, even if the font is not supported by our font stack,
            // we need to place the empty font at the first place. The typeface with empty font
            // behaves different from default typeface especially in fallback font selection.
            fontFamily.allowUnsupportedFont();
            fontFamily.freeze();
            if (!fontFamily.freeze()) {
                return null;
            }
            FontFamily[] familyChain = { fontFamily };
            typeface = createFromFamiliesWithDefault(familyChain,
                    RESOLVE_BY_FONT_TABLE, RESOLVE_BY_FONT_TABLE);