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

Commit ff221613 authored by Clara Bayarri's avatar Clara Bayarri
Browse files

Provide ApplicationContext to Typeface for font requests

We were using a generic context that caused crashes for
the content providers.

Bug: 34657204
Test: manual, calling getCallingPackage() from the provider
doesn't crash anymore.

Change-Id: I2b61e6510d8c6647007987373d03ee5dc97c0889
parent 3c002390
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
@@ -19,6 +19,7 @@ package android.graphics;
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;
@@ -83,7 +84,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.
@@ -231,6 +232,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}.
@@ -247,18 +262,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);