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 Original line Diff line number Diff line
@@ -50,6 +50,7 @@ import android.database.sqlite.SQLiteDebug;
import android.database.sqlite.SQLiteDebug.DbStats;
import android.database.sqlite.SQLiteDebug.DbStats;
import android.graphics.Bitmap;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.hardware.display.DisplayManagerGlobal;
import android.hardware.display.DisplayManagerGlobal;
import android.net.ConnectivityManager;
import android.net.ConnectivityManager;
import android.net.IConnectivityManager;
import android.net.IConnectivityManager;
@@ -5665,6 +5666,7 @@ public final class ActivityThread {
        }
        }


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


    /** @hide */
    /** @hide */
    public FontsContract() {
    public FontsContract(Context context) {
        // TODO: investigate if the system context is the best option here. ApplicationContext or
        mContext = context.getApplicationContext();
        // 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();
        mPackageManager = mContext.getPackageManager();
        mPackageManager = mContext.getPackageManager();
    }
    }


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


    /**
    /**
     * Cache for Typeface objects dynamically loaded from assets. Currently max size is 16.
     * Cache for Typeface objects dynamically loaded from assets. Currently max size is 16.
@@ -225,6 +226,20 @@ public class Typeface {
        return null;
        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,
     * 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}.
     * therefore the result is delivered to the given callback. See {@link FontRequest}.
@@ -241,18 +256,17 @@ public class Typeface {
        Typeface cachedTypeface = findFromCache(
        Typeface cachedTypeface = findFromCache(
                request.getProviderAuthority(), request.getQuery());
                request.getProviderAuthority(), request.getQuery());
        if (cachedTypeface != null) {
        if (cachedTypeface != null) {
            mHandler.post(() -> callback.onTypefaceRetrieved(cachedTypeface));
            sHandler.post(() -> callback.onTypefaceRetrieved(cachedTypeface));
            return;
            return;
        }
        }
        synchronized (sLock) {
        synchronized (sLock) {
            if (sFontsContract == null) {
            if (sFontsContract == null) {
                sFontsContract = new FontsContract();
                throw new RuntimeException("Context not initialized, can't query provider");
                mHandler = new Handler();
            }
            }
            final ResultReceiver receiver = new ResultReceiver(null) {
            final ResultReceiver receiver = new ResultReceiver(null) {
                @Override
                @Override
                public void onReceiveResult(int resultCode, Bundle resultData) {
                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);
            sFontsContract.getFont(request, receiver);