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

Commit 5b430a61 authored by Evan Chen's avatar Evan Chen
Browse files

Scales the device icon instead of enforce the size

The icon should be 24dp x 24dp. If a different size is provided,

it will be automatically scaled.

Bug: 389722352
Test: cts
Flag: EXEMPT test improvement

Change-Id: Iab26fd8b0ac43da4381c70858bb907f429eba48c
parent f2db7d41
Loading
Loading
Loading
Loading
+14 −6
Original line number Diff line number Diff line
@@ -287,8 +287,8 @@ public final class AssociationInfo implements Parcelable {
    /**
     * Get the device icon of the associated device. The device icon represents the device type.
     *
     * @return the device icon, or {@code null} if no device icon has been set for the
     * associated device.
     * @return the device icon with size 24dp x 24dp.
     * If the associated device has no icon set, it returns {@code null}.
     *
     * @see AssociationRequest.Builder#setDeviceIcon(Icon)
     */
@@ -377,6 +377,7 @@ public final class AssociationInfo implements Parcelable {
        if (this == o) return true;
        if (!(o instanceof AssociationInfo)) return false;
        final AssociationInfo that = (AssociationInfo) o;

        return mId == that.mId
                && mUserId == that.mUserId
                && mSelfManaged == that.mSelfManaged
@@ -391,11 +392,17 @@ public final class AssociationInfo implements Parcelable {
                && Objects.equals(mDeviceProfile, that.mDeviceProfile)
                && Objects.equals(mAssociatedDevice, that.mAssociatedDevice)
                && mSystemDataSyncFlags == that.mSystemDataSyncFlags
                && (mDeviceIcon == null ? that.mDeviceIcon == null
                : mDeviceIcon.sameAs(that.mDeviceIcon))
                && isSameIcon(mDeviceIcon, that.mDeviceIcon)
                && Objects.equals(mDeviceId, that.mDeviceId);
    }

    private boolean isSameIcon(Icon iconA, Icon iconB) {
        // Because we've already rescaled and converted both icons to bitmaps,
        // we can now directly compare them by bitmap.
        return (iconA == null && iconB == null)
                || (iconA != null && iconB != null && iconA.getBitmap().sameAs(iconB.getBitmap()));
    }

    @Override
    public int hashCode() {
        return Objects.hash(mId, mUserId, mPackageName, mDeviceMacAddress, mDisplayName,
@@ -425,7 +432,7 @@ public final class AssociationInfo implements Parcelable {
        dest.writeLong(mTimeApprovedMs);
        dest.writeLong(mLastTimeConnectedMs);
        dest.writeInt(mSystemDataSyncFlags);
        if (mDeviceIcon != null) {
        if (Flags.associationDeviceIcon() && mDeviceIcon != null) {
            dest.writeInt(1);
            mDeviceIcon.writeToParcel(dest, flags);
        } else {
@@ -455,7 +462,8 @@ public final class AssociationInfo implements Parcelable {
        mTimeApprovedMs = in.readLong();
        mLastTimeConnectedMs = in.readLong();
        mSystemDataSyncFlags = in.readInt();
        if (in.readInt() == 1) {
        int deviceIcon = in.readInt();
        if (Flags.associationDeviceIcon() && deviceIcon == 1) {
            mDeviceIcon = Icon.CREATOR.createFromParcel(in);
        } else {
            mDeviceIcon = null;
+7 −2
Original line number Diff line number Diff line
@@ -385,6 +385,10 @@ public final class AssociationRequest implements Parcelable {
    public void setAssociatedDevice(AssociatedDevice associatedDevice) {
        mAssociatedDevice = associatedDevice;
    }
    /** @hide */
    public void setDeviceIcon(Icon deviceIcon) {
        mDeviceIcon = deviceIcon;
    }

    /** @hide */
    @NonNull
@@ -492,9 +496,10 @@ public final class AssociationRequest implements Parcelable {
        /**
         * Set the device icon for the self-managed device and to display the icon in the
         * self-managed association dialog.
         * <p>The given device icon will be resized to 24dp x 24dp.
         *
         * @throws IllegalArgumentException if the icon is not exactly 24dp by 24dp
         * or if it is {@link Icon#TYPE_URI} or {@link Icon#TYPE_URI_ADAPTIVE_BITMAP}.
         * @throws IllegalArgumentException if the icon is
         * {@link Icon#TYPE_URI} or {@link Icon#TYPE_URI_ADAPTIVE_BITMAP}.
         * @see #setSelfManaged(boolean)
         */
        @NonNull
+20 −31
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ import static android.Manifest.permission.REQUEST_COMPANION_PROFILE_WATCH;
import static android.graphics.drawable.Icon.TYPE_URI;
import static android.graphics.drawable.Icon.TYPE_URI_ADAPTIVE_BITMAP;


import android.annotation.CallbackExecutor;
import android.annotation.FlaggedApi;
import android.annotation.IntDef;
@@ -52,10 +51,10 @@ import android.content.Intent;
import android.content.IntentSender;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.graphics.drawable.VectorDrawable;
import android.net.MacAddress;
import android.os.Binder;
import android.os.Handler;
@@ -110,6 +109,7 @@ import java.util.function.Consumer;
@RequiresFeature(PackageManager.FEATURE_COMPANION_DEVICE_SETUP)
public final class CompanionDeviceManager {
    private static final String TAG = "CDM_CompanionDeviceManager";
    private static final int ICON_TARGET_SIZE = 24;

    /** @hide */
    @IntDef(prefix = {"RESULT_"}, value = {
@@ -474,10 +474,8 @@ public final class CompanionDeviceManager {

        if (Flags.associationDeviceIcon()) {
            final Icon deviceIcon = request.getDeviceIcon();

            if (deviceIcon != null && !isValidIcon(deviceIcon, mContext)) {
                throw new IllegalArgumentException("The size of the device icon must be "
                        + "24dp x 24dp to ensure proper display");
            if (deviceIcon != null) {
                request.setDeviceIcon(scaleIcon(deviceIcon, mContext));
            }
        }

@@ -547,10 +545,8 @@ public final class CompanionDeviceManager {

        if (Flags.associationDeviceIcon()) {
            final Icon deviceIcon = request.getDeviceIcon();

            if (deviceIcon != null && !isValidIcon(deviceIcon, mContext)) {
                throw new IllegalArgumentException("The size of the device icon must be "
                        + "24dp x 24dp to ensure proper display");
            if (deviceIcon != null) {
                request.setDeviceIcon(scaleIcon(deviceIcon, mContext));
            }
        }

@@ -2024,33 +2020,26 @@ public final class CompanionDeviceManager {
        }
    }

    private boolean isValidIcon(Icon icon, Context context) {
    private Icon scaleIcon(Icon icon, Context context) {
        if (icon == null) return null;
        if (icon.getType() == TYPE_URI_ADAPTIVE_BITMAP || icon.getType() == TYPE_URI) {
            throw new IllegalArgumentException("The URI based Icon is not supported.");
        }
        Drawable drawable = icon.loadDrawable(context);
        float density = context.getResources().getDisplayMetrics().density;

        Bitmap bitmap;
        Drawable drawable = icon.loadDrawable(context);
        if (drawable instanceof BitmapDrawable) {
            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();

            float widthDp = bitmap.getWidth() / density;
            float heightDp = bitmap.getHeight() / density;

            if (widthDp != 24 || heightDp != 24) {
                return false;
            }
        } else if (drawable instanceof VectorDrawable) {
            VectorDrawable vectorDrawable = (VectorDrawable) drawable;
            float widthDp = vectorDrawable.getIntrinsicWidth() / density;
            float heightDp = vectorDrawable.getIntrinsicHeight() / density;

            if (widthDp != 24 || heightDp != 24) {
                return false;
            }
            bitmap = Bitmap.createScaledBitmap(
                    ((BitmapDrawable) drawable).getBitmap(), ICON_TARGET_SIZE, ICON_TARGET_SIZE,
                    false);
        } else {
            throw new IllegalArgumentException("The format of the device icon is unsupported.");
            bitmap = Bitmap.createBitmap(context.getResources().getDisplayMetrics(),
                    ICON_TARGET_SIZE, ICON_TARGET_SIZE, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            drawable.draw(canvas);
        }
        return true;

        return Icon.createWithBitmap(bitmap);
    }
}