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

Commit 2821eeae authored by Clara Bayarri's avatar Clara Bayarri
Browse files

Preload fonts from an array

Until now we were reusing the font tags, but it is not needed,
we can declare the list of fonts to preload with a simple
resources array.

Test: manual
Bug: 36119246
Change-Id: I39819018012c322aff62e13aa69ab35467be57ac
parent c0605bb0
Loading
Loading
Loading
Loading
+7 −6
Original line number Diff line number Diff line
@@ -375,14 +375,15 @@ public class Resources {
    /**
     * @hide
     */
    public void preloadFonts(@FontRes int id) {
        final TypedValue value = obtainTempTypedValue();
    public void preloadFonts(@ArrayRes int id) {
        final TypedArray array = obtainTypedArray(id);
        try {
            final ResourcesImpl impl = mResourcesImpl;
            impl.getValue(id, value, true);
            impl.preloadFonts(this, value, id);
            final int size = array.length();
            for (int i = 0; i < size; i++) {
                array.getFont(i);
            }
        } finally {
            releaseTempTypedValue(value);
            array.recycle();
        }
    }

+0 −40
Original line number Diff line number Diff line
@@ -786,46 +786,6 @@ public class ResourcesImpl {
        return null;
    }

    /**
     * @hide
     */
    public void preloadFonts(Resources wrapper, TypedValue value, int id) {
        if (value.string == null) {
            throw new NotFoundException("Resource \"" + getResourceName(id) + "\" ("
                    + Integer.toHexString(id) + ") is not a Font: " + value);
        }

        final String file = value.string.toString();

        Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, file);
        try {
            // TODO: Stop re-ussing font-family xml tag structure and use ResourceArray instead.
            final XmlResourceParser rp = loadXmlResourceParser(
                    file, id, value.assetCookie, "font");
            final FontResourcesParser.FamilyResourceEntry familyEntry =
                    FontResourcesParser.parse(rp, wrapper);
            if (familyEntry == null) {
                Log.e(TAG, "failed to find font-family tag");
                return;
            }
            if (familyEntry instanceof FontResourcesParser.ProviderResourceEntry) {
                throw new IllegalArgumentException("Provider based fonts can not be used.");
            }
            final FontResourcesParser.FontFamilyFilesResourceEntry filesEntry =
                    (FontResourcesParser.FontFamilyFilesResourceEntry) familyEntry;
            for (FontResourcesParser.FontFileResourceEntry fileEntry : filesEntry.getEntries()) {
                int resourceId = fileEntry.getResourceId();
                wrapper.getFont(resourceId);
            }
        } catch (XmlPullParserException e) {
            Log.e(TAG, "Failed to parse xml resource " + file, e);
        } catch (IOException e) {
            Log.e(TAG, "Failed to read xml resource " + file, e);
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
        }
    }

    /**
     * Given the value and id, we can get the XML filename as in value.data, based on that, we
     * first try to load CSL from the cache. If not found, try to get from the constant state.