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

Commit caf6e0ad authored by Chris Antol's avatar Chris Antol
Browse files

Support dynamic injected preference icons from raw data

Flag: com.android.settings.flags.support_raw_dynamic_icons
Bug: 351884562
Test: Registered an injected preference in another app locally, providing icon dynamically using new metadata field
Change-Id: Ie43ee0d3148c726f631b21b50b4a774511efa924
parent 037392f3
Loading
Loading
Loading
Loading
+34 −5
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
 */
package com.android.settingslib.drawer;

import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.content.ContentResolver;
import android.content.Context;
@@ -25,7 +26,9 @@ import android.content.pm.ComponentInfo;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.RemoteException;
import android.os.UserHandle;
@@ -36,6 +39,8 @@ import android.util.ArrayMap;
import android.util.Log;
import android.util.Pair;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;

import java.util.ArrayList;
@@ -102,6 +107,9 @@ public class TileUtils {
    /** The key used to get the package name of the icon resource for the preference. */
    static final String EXTRA_PREFERENCE_ICON_PACKAGE = "com.android.settings.icon_package";

    /** The key used for the raw byte data of the icon for the preference. */
    static final String EXTRA_PREFERENCE_ICON_RAW = "com.android.settings.icon_raw";

    /**
     * Name of the meta-data item that should be set in the AndroidManifest.xml
     * to specify the key that should be used for the preference.
@@ -517,6 +525,24 @@ public class TileUtils {
        return null;
    }

    /**
     * Retrieves an icon stored in the Bundle as a Parcel with key EXTRA_PREFERENCE_ICON_RAW
     */
    @TargetApi(Build.VERSION_CODES.TIRAMISU)
    @Nullable
    public static Icon getRawIconFromUri(@NonNull Context context, @Nullable Uri uri,
            @NonNull Map<String, IContentProvider> providerMap) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
            return null;
        }
        final Bundle bundle = getBundleFromUri(context, uri, providerMap, null /* bundle */);
        if (bundle == null) {
            return null;
        }

        return bundle.getParcelable(EXTRA_PREFERENCE_ICON_RAW, Icon.class);
    }

    /**
     * Gets text associated with the input key from the content provider.
     *
@@ -564,8 +590,9 @@ public class TileUtils {
        return getBundleFromUri(context, uri, providerMap, bundle);
    }

    private static Bundle getBundleFromUri(Context context, Uri uri,
            Map<String, IContentProvider> providerMap, Bundle bundle) {
    @Nullable
    private static Bundle getBundleFromUri(@NonNull Context context, @Nullable Uri uri,
            @NonNull Map<String, IContentProvider> providerMap, @Nullable Bundle bundle) {
        final Pair<String, String> args = getMethodAndKey(uri);
        if (args == null) {
            return null;
@@ -593,8 +620,9 @@ public class TileUtils {
        }
    }

    private static IContentProvider getProviderFromUri(Context context, Uri uri,
            Map<String, IContentProvider> providerMap) {
    @Nullable
    private static IContentProvider getProviderFromUri(@NonNull Context context, @Nullable Uri uri,
            @NonNull Map<String, IContentProvider> providerMap) {
        if (uri == null) {
            return null;
        }
@@ -609,7 +637,8 @@ public class TileUtils {
    }

    /** Returns method and key of the complete uri. */
    private static Pair<String, String> getMethodAndKey(Uri uri) {
    @Nullable
    private static Pair<String, String> getMethodAndKey(@Nullable Uri uri) {
        if (uri == null) {
            return null;
        }