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

Commit de5b606d authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Preload font files based on device locale" into sc-dev am: a4117461

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

Change-Id: Iff31903729006543943c5f911992e81f357f2e1b
parents 9d7d256a a4117461
Loading
Loading
Loading
Loading
+26 −1
Original line number Diff line number Diff line
@@ -34,9 +34,11 @@ import android.graphics.fonts.FontFamily;
import android.graphics.fonts.FontStyle;
import android.graphics.fonts.FontVariationAxis;
import android.graphics.fonts.SystemFonts;
import android.icu.util.ULocale;
import android.os.Build;
import android.os.ParcelFileDescriptor;
import android.os.SharedMemory;
import android.os.SystemProperties;
import android.os.Trace;
import android.provider.FontRequest;
import android.provider.FontsContract;
@@ -45,6 +47,7 @@ import android.system.OsConstants;
import android.text.FontConfig;
import android.util.ArrayMap;
import android.util.Base64;
import android.util.Log;
import android.util.LongSparseArray;
import android.util.LruCache;
import android.util.SparseArray;
@@ -1375,13 +1378,35 @@ public class Typeface {

    static {
        // Preload Roboto-Regular.ttf in Zygote for improving app launch performance.
        // TODO: add new attribute to fonts.xml to preload fonts in Zygote.
        preloadFontFile("/system/fonts/Roboto-Regular.ttf");

        String locale = SystemProperties.get("persist.sys.locale", "en-US");
        String script = ULocale.addLikelySubtags(ULocale.forLanguageTag(locale)).getScript();

        FontConfig config = SystemFonts.getSystemPreinstalledFontConfig();
        for (int i = 0; i < config.getFontFamilies().size(); ++i) {
            FontConfig.FontFamily family = config.getFontFamilies().get(i);
            boolean loadFamily = false;
            for (int j = 0; j < family.getLocaleList().size(); ++j) {
                String fontScript = ULocale.addLikelySubtags(
                        ULocale.forLocale(family.getLocaleList().get(j))).getScript();
                loadFamily = fontScript.equals(script);
                if (loadFamily) {
                    break;
                }
            }
            if (loadFamily) {
                for (int j = 0; j < family.getFontList().size(); ++j) {
                    preloadFontFile(family.getFontList().get(j).getFile().getAbsolutePath());
                }
            }
        }
    }

    private static void preloadFontFile(String filePath) {
        File file = new File(filePath);
        if (file.exists()) {
            Log.i(TAG, "Preloading " + file.getAbsolutePath());
            nativeWarmUpCache(filePath);
        }
    }