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

Commit df3af7fd authored by Sumedh Sen's avatar Sumedh Sen
Browse files

Resize large app icons before adding them in a parcel

If an app icon is too large, scale the icon down so that it can be
parcelled and sent in an intent.

Bug: 290862169
Bug: 304567418
Test: Manual. Install the APK attached in b/290862169#comment18
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:2fc482f6fd8ba4217a4f9c956dfdb45c6494974e)
Merged-In: I5feafea491208c8fd6bbdc6b2b655d46b6964d95
Change-Id: I5feafea491208c8fd6bbdc6b2b655d46b6964d95


Original patch:
 From 2fc482f6 Mon Sep 17 00:00:00 2001
From: Sumedh Sen <sumedhsen@google.com>
Date: Wed, 11 Oct 2023 12:19:50 -0700
Subject: [PATCH] Resize large app icons before adding them in a parcel

If an app icon is too large, scale the icon down so that it can be
parcelled and sent in an intent.

Bug: 290862169
Bug: 304567418
Test: Manual. Install the APK attached in b/290862169#comment18
Change-Id: I5feafea491208c8fd6bbdc6b2b655d46b6964d95
---
parent c1647930
Loading
Loading
Loading
Loading
+2 −2
Original line number Original line Diff line number Diff line
@@ -596,7 +596,7 @@ public class PackageInstallerActivity extends AlertActivity {
                CharSequence label = mPm.getApplicationLabel(mPkgInfo.applicationInfo);
                CharSequence label = mPm.getApplicationLabel(mPkgInfo.applicationInfo);
                if (mLocalLOGV) Log.i(TAG, "creating snippet for " + label);
                if (mLocalLOGV) Log.i(TAG, "creating snippet for " + label);
                mAppSnippet = new PackageUtil.AppSnippet(label,
                mAppSnippet = new PackageUtil.AppSnippet(label,
                        mPm.getApplicationIcon(mPkgInfo.applicationInfo));
                        mPm.getApplicationIcon(mPkgInfo.applicationInfo), getBaseContext());
            } break;
            } break;


            case ContentResolver.SCHEME_FILE: {
            case ContentResolver.SCHEME_FILE: {
@@ -634,7 +634,7 @@ public class PackageInstallerActivity extends AlertActivity {
        mPkgInfo = generateStubPackageInfo(info.getAppPackageName());
        mPkgInfo = generateStubPackageInfo(info.getAppPackageName());
        mAppSnippet = new PackageUtil.AppSnippet(info.getAppLabel(),
        mAppSnippet = new PackageUtil.AppSnippet(info.getAppLabel(),
                info.getAppIcon() != null ? new BitmapDrawable(getResources(), info.getAppIcon())
                info.getAppIcon() != null ? new BitmapDrawable(getResources(), info.getAppIcon())
                        : getPackageManager().getDefaultActivityIcon());
                        : getPackageManager().getDefaultActivityIcon(), getBaseContext());
        return true;
        return true;
    }
    }


+14 −2
Original line number Original line Diff line number Diff line
@@ -18,6 +18,7 @@
package com.android.packageinstaller;
package com.android.packageinstaller;


import android.app.Activity;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.DialogFragment;
@@ -123,15 +124,20 @@ public class PackageUtil {
    static final class AppSnippet implements Parcelable {
    static final class AppSnippet implements Parcelable {
        @NonNull public CharSequence label;
        @NonNull public CharSequence label;
        @Nullable public Drawable icon;
        @Nullable public Drawable icon;
        public AppSnippet(@NonNull CharSequence label, @Nullable Drawable icon) {
        public int iconSize;

        public AppSnippet(@NonNull CharSequence label, @Nullable Drawable icon, Context context) {
            this.label = label;
            this.label = label;
            this.icon = icon;
            this.icon = icon;
            final ActivityManager am = context.getSystemService(ActivityManager.class);
            this.iconSize = am.getLauncherLargeIconSize();
        }
        }


        private AppSnippet(Parcel in) {
        private AppSnippet(Parcel in) {
            label = in.readString();
            label = in.readString();
            Bitmap bmp = in.readParcelable(getClass().getClassLoader(), Bitmap.class);
            Bitmap bmp = in.readParcelable(getClass().getClassLoader(), Bitmap.class);
            icon = new BitmapDrawable(Resources.getSystem(), bmp);
            icon = new BitmapDrawable(Resources.getSystem(), bmp);
            iconSize = in.readInt();
        }
        }


        @Override
        @Override
@@ -149,6 +155,7 @@ public class PackageUtil {
            dest.writeString(label.toString());
            dest.writeString(label.toString());
            Bitmap bmp = getBitmapFromDrawable(icon);
            Bitmap bmp = getBitmapFromDrawable(icon);
            dest.writeParcelable(bmp, 0);
            dest.writeParcelable(bmp, 0);
            dest.writeInt(iconSize);
        }
        }


        private Bitmap getBitmapFromDrawable(Drawable drawable) {
        private Bitmap getBitmapFromDrawable(Drawable drawable) {
@@ -162,6 +169,11 @@ public class PackageUtil {
            // bitmap held within
            // bitmap held within
            drawable.draw(canvas);
            drawable.draw(canvas);


            // Scale it down if the icon is too large
            if ((bmp.getWidth() > iconSize * 2) || (bmp.getHeight() > iconSize * 2)) {
                bmp = Bitmap.createScaledBitmap(bmp, iconSize, iconSize, true);
            }

            return bmp;
            return bmp;
        }
        }


@@ -218,7 +230,7 @@ public class PackageUtil {
        } catch (OutOfMemoryError e) {
        } catch (OutOfMemoryError e) {
            Log.i(LOG_TAG, "Could not load app icon", e);
            Log.i(LOG_TAG, "Could not load app icon", e);
        }
        }
        return new PackageUtil.AppSnippet(label, icon);
        return new PackageUtil.AppSnippet(label, icon, pContext);
    }
    }


    /**
    /**