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

Commit 21a8baae authored by Tim Murray's avatar Tim Murray
Browse files

RemoteViews: make bitmaps immutable

Because mutability is propagated across Parcel and Binder, any mutable
Bitmaps passed to RemoteViews will end up mutable on the other side
even though those Bitmaps can't be modified from the RemoteViews
recipient.

Make all Bitmaps passed to RemoteViews immutable in order to ensure
that they can be sent without additional copies instead of the one
ashmem object.

Test: TH, RemoteViews work for widgets
Bug: 227920378

Change-Id: I477e7132f9ff78333c1eda56b9d0fc6ac520abd0
parent 41eed224
Loading
Loading
Loading
Loading
+13 −4
Original line number Original line Diff line number Diff line
@@ -1588,24 +1588,33 @@ public class RemoteViews implements Parcelable, Filter {


        @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
        @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
        ArrayList<Bitmap> mBitmaps;
        ArrayList<Bitmap> mBitmaps;
        SparseIntArray mBitmapHashes;
        int mBitmapMemory = -1;
        int mBitmapMemory = -1;


        public BitmapCache() {
        public BitmapCache() {
            mBitmaps = new ArrayList<>();
            mBitmaps = new ArrayList<>();
            mBitmapHashes = new SparseIntArray();
        }
        }


        public BitmapCache(Parcel source) {
        public BitmapCache(Parcel source) {
            mBitmaps = source.createTypedArrayList(Bitmap.CREATOR);
            mBitmaps = source.createTypedArrayList(Bitmap.CREATOR);
            mBitmapHashes = source.readSparseIntArray();
        }
        }


        public int getBitmapId(Bitmap b) {
        public int getBitmapId(Bitmap b) {
            if (b == null) {
            if (b == null) {
                return -1;
                return -1;
            } else {
            } else {
                if (mBitmaps.contains(b)) {
                int hash = b.hashCode();
                    return mBitmaps.indexOf(b);
                int hashId = mBitmapHashes.get(hash, -1);
                if (hashId != -1) {
                    return hashId;
                } else {
                } else {
                    if (b.isMutable()) {
                        b = b.asShared();
                    }
                    mBitmaps.add(b);
                    mBitmaps.add(b);
                    mBitmapHashes.put(mBitmaps.size() - 1, hash);
                    mBitmapMemory = -1;
                    mBitmapMemory = -1;
                    return (mBitmaps.size() - 1);
                    return (mBitmaps.size() - 1);
                }
                }
@@ -1616,13 +1625,13 @@ public class RemoteViews implements Parcelable, Filter {
        public Bitmap getBitmapForId(int id) {
        public Bitmap getBitmapForId(int id) {
            if (id == -1 || id >= mBitmaps.size()) {
            if (id == -1 || id >= mBitmaps.size()) {
                return null;
                return null;
            } else {
                return mBitmaps.get(id);
            }
            }
            return mBitmaps.get(id);
        }
        }


        public void writeBitmapsToParcel(Parcel dest, int flags) {
        public void writeBitmapsToParcel(Parcel dest, int flags) {
            dest.writeTypedList(mBitmaps, flags);
            dest.writeTypedList(mBitmaps, flags);
            dest.writeSparseIntArray(mBitmapHashes);
        }
        }


        public int getBitmapMemory() {
        public int getBitmapMemory() {