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

Commit 341c54a4 authored by Wen Zhang's avatar Wen Zhang
Browse files

Fix equals and hash code methods for the Property class

The Property class cannot directly compare objects, so its equals method needs to be overwritten, and the hashCode method needs to be overwritten to reduce hash conflicts.

Bug:377941785
Test:atest PackageParserTest

Change-Id: Id6afd0f7d3e28e7d67e890831ad2538beff6d2e8
parent 6cc7c9df
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
@@ -292,6 +292,10 @@ public abstract class PackageManager {
     * <p>
     * The value of a property will only have a single type, as defined by
     * the property itself.
     *
     * <p class="note"><strong>Note:</strong>
     * In android version {@link Build.VERSION_CODES#VANILLA_ICE_CREAM} and earlier,
     * the {@code equals} and {@code hashCode} methods for this class may not function as expected.
     */
    public static final class Property implements Parcelable {
        private static final int TYPE_BOOLEAN = 1;
@@ -523,6 +527,40 @@ public abstract class PackageManager {
                return new Property[size];
            }
        };

        @Override
        public boolean equals(Object obj) {
            if (!(obj instanceof Property)) {
                return false;
            }
            final Property property = (Property) obj;
            return mType == property.mType &&
                    Objects.equals(mName, property.mName) &&
                    Objects.equals(mClassName, property.mClassName) &&
                    Objects.equals(mPackageName, property.mPackageName) &&
                    (mType == TYPE_BOOLEAN ? mBooleanValue == property.mBooleanValue :
                     mType == TYPE_FLOAT ? Float.compare(mFloatValue, property.mFloatValue) == 0 :
                     mType == TYPE_INTEGER ? mIntegerValue == property.mIntegerValue :
                     mType == TYPE_RESOURCE ? mIntegerValue == property.mIntegerValue :
                     mStringValue.equals(property.mStringValue));
        }

        @Override
        public int hashCode() {
            int result = Objects.hash(mName, mType, mClassName, mPackageName);
            if (mType == TYPE_BOOLEAN) {
                result = 31 * result + (mBooleanValue ? 1 : 0);
            } else if (mType == TYPE_FLOAT) {
                result = 31 * result + Float.floatToIntBits(mFloatValue);
            } else if (mType == TYPE_INTEGER) {
                result = 31 * result + mIntegerValue;
            } else if (mType == TYPE_RESOURCE) {
                result = 31 * result + mIntegerValue;
            } else if (mType == TYPE_STRING) {
                result = 31 * result + mStringValue.hashCode();
            }
            return result;
        }
    }

    /**