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

Commit c4a6d29a authored by Clara Bayarri's avatar Clara Bayarri Committed by Android (Google) Code Review
Browse files

Merge "Provide ApplicationContext to Typeface for font requests"

parents 73ba9da6 ff221613
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ import android.database.sqlite.SQLiteDebug;
import android.database.sqlite.SQLiteDebug.DbStats;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.hardware.display.DisplayManagerGlobal;
import android.net.ConnectivityManager;
import android.net.IConnectivityManager;
@@ -5665,6 +5666,7 @@ public final class ActivityThread {
        }

        // Preload fonts resources
        Typeface.setApplicationContext(appContext);
        try {
            final ApplicationInfo info =
                    getPackageManager().getApplicationInfo(
+2 −6
Original line number Diff line number Diff line
@@ -138,12 +138,8 @@ public class FontsContract {
    private HandlerThread mThread;

    /** @hide */
    public FontsContract() {
        // TODO: investigate if the system context is the best option here. ApplicationContext or
        // the one passed by developer?
        // TODO: Looks like ActivityThread.currentActivityThread() can return null. Check when it
        // returns null and check if we need to handle null case.
        mContext = ActivityThread.currentActivityThread().getSystemContext();
    public FontsContract(Context context) {
        mContext = context.getApplicationContext();
        mPackageManager = mContext.getPackageManager();
    }

+19 −5
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import static android.content.res.FontResourcesParser.FamilyResourceEntry;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.fonts.FontRequest;
import android.graphics.fonts.FontResult;
@@ -88,7 +89,7 @@ public class Typeface {
    @GuardedBy("sLock")
    private static FontsContract sFontsContract;
    @GuardedBy("sLock")
    private static Handler mHandler;
    private static Handler sHandler;

    /**
     * Cache for Typeface objects dynamically loaded from assets. Currently max size is 16.
@@ -225,6 +226,20 @@ public class Typeface {
        return null;
    }

    /**
     * Set the application context so we can generate font requests from the provider. This should
     * be called from ActivityThread when the application binds, as we preload fonts.
     * @hide
     */
    public static void setApplicationContext(Context context) {
        synchronized (sLock) {
            if (sFontsContract == null) {
                sFontsContract = new FontsContract(context);
                sHandler = new Handler();
            }
        }
    }

    /**
     * Create a typeface object given a font request. The font will be asynchronously fetched,
     * therefore the result is delivered to the given callback. See {@link FontRequest}.
@@ -241,18 +256,17 @@ public class Typeface {
        Typeface cachedTypeface = findFromCache(
                request.getProviderAuthority(), request.getQuery());
        if (cachedTypeface != null) {
            mHandler.post(() -> callback.onTypefaceRetrieved(cachedTypeface));
            sHandler.post(() -> callback.onTypefaceRetrieved(cachedTypeface));
            return;
        }
        synchronized (sLock) {
            if (sFontsContract == null) {
                sFontsContract = new FontsContract();
                mHandler = new Handler();
                throw new RuntimeException("Context not initialized, can't query provider");
            }
            final ResultReceiver receiver = new ResultReceiver(null) {
                @Override
                public void onReceiveResult(int resultCode, Bundle resultData) {
                    mHandler.post(() -> receiveResult(request, callback, resultCode, resultData));
                    sHandler.post(() -> receiveResult(request, callback, resultCode, resultData));
                }
            };
            sFontsContract.getFont(request, receiver);