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

Commit c0b203cc authored by Todd Kennedy's avatar Todd Kennedy
Browse files

Add package name and class name to Property

Instead of returning the package/property name from the
query methods, make it part of the Property object itself.

Bug: 169258655
Test: atest FrameworksServicesTests:PackageParserTest
Change-Id: I1c620cde317dc05fb49367a226a612bc01ae7f55
parent b0036c0a
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -12348,9 +12348,11 @@ package android.content.pm {
  public static final class PackageManager.Property implements android.os.Parcelable {
    method public int describeContents();
    method public boolean getBoolean();
    method @Nullable public String getClassName();
    method public float getFloat();
    method public int getInteger();
    method @NonNull public String getName();
    method @NonNull public String getPackageName();
    method public int getResourceId();
    method @Nullable public String getString();
    method public boolean isBoolean();
+45 −16
Original line number Diff line number Diff line
@@ -131,38 +131,48 @@ public abstract class PackageManager {
        private static final int TYPE_RESOURCE = 4;
        private static final int TYPE_STRING = 5;
        private final String mName;
        private final int mType;
        private final String mClassName;
        private final String mPackageName;
        private boolean mBooleanValue;
        private float mFloatValue;
        private int mIntegerValue;
        private String mStringValue;
        private final int mType;

        /** @hide */
        @VisibleForTesting
        public Property(@NonNull String name, int type) {
        public Property(@NonNull String name, int type,
                @NonNull String packageName, @Nullable String className) {
            assert name != null;
            assert type >= TYPE_BOOLEAN && type <= TYPE_STRING;
            assert packageName != null;
            this.mName = name;
            this.mType = type;
            this.mPackageName = packageName;
            this.mClassName = className;
        }
        /** @hide */
        public Property(@NonNull String name, boolean value) {
            this(name, TYPE_BOOLEAN);
        public Property(@NonNull String name, boolean value,
                String packageName, String className) {
            this(name, TYPE_BOOLEAN, packageName, className);
            mBooleanValue = value;
        }
        /** @hide */
        public Property(@NonNull String name, float value) {
            this(name, TYPE_FLOAT);
        public Property(@NonNull String name, float value,
                String packageName, String className) {
            this(name, TYPE_FLOAT, packageName, className);
            mFloatValue = value;
        }
        /** @hide */
        public Property(@NonNull String name, int value, boolean isResource) {
            this(name, isResource ? TYPE_RESOURCE : TYPE_INTEGER);
        public Property(@NonNull String name, int value, boolean isResource,
                String packageName, String className) {
            this(name, isResource ? TYPE_RESOURCE : TYPE_INTEGER, packageName, className);
            mIntegerValue = value;
        }
        /** @hide */
        public Property(@NonNull String name, String value) {
            this(name, TYPE_STRING);
        public Property(@NonNull String name, String value,
                String packageName, String className) {
            this(name, TYPE_STRING, packageName, className);
            mStringValue = value;
        }

@@ -179,6 +189,22 @@ public abstract class PackageManager {
            return mName;
        }

        /**
         * Returns the name of the package where this this property was defined.
         */
        @NonNull public String getPackageName() {
            return mPackageName;
        }

        /**
         * Returns the classname of the component where this property was defined.
         * <p>If the property was defined within and &lt;application&gt; tag, retutrns
         * {@code null}
         */
        @Nullable public String getClassName() {
            return mClassName;
        }

        /**
         * Returns the boolean value set for the property.
         * <p>If the property is not of a boolean type, returns {@code false}.
@@ -296,6 +322,8 @@ public abstract class PackageManager {
            } else if (mType == TYPE_STRING) {
                dest.writeString(mStringValue);
            }
            dest.writeString(mPackageName);
            dest.writeString(mClassName);
        }

        @NonNull
@@ -304,16 +332,18 @@ public abstract class PackageManager {
            public Property createFromParcel(@NonNull Parcel source) {
                final String name = source.readString();
                final int type = source.readInt();
                final String packageName = source.readString();
                final String className = source.readString();
                if (type == TYPE_BOOLEAN) {
                    return new Property(name, source.readBoolean());
                    return new Property(name, source.readBoolean(), packageName, className);
                } else if (type == TYPE_FLOAT) {
                    return new Property(name, source.readFloat());
                    return new Property(name, source.readFloat(), packageName, className);
                } else if (type == TYPE_INTEGER) {
                    return new Property(name, source.readInt(), false);
                    return new Property(name, source.readInt(), false, packageName, className);
                } else if (type == TYPE_RESOURCE) {
                    return new Property(name, source.readInt(), true);
                    return new Property(name, source.readInt(), true, packageName, className);
                } else if (type == TYPE_STRING) {
                    return new Property(name, source.readString());
                    return new Property(name, source.readString(), packageName, className);
                }
                return null;
            }
@@ -8571,7 +8601,6 @@ public abstract class PackageManager {
        throw new UnsupportedOperationException(
                "getMimeGroup not implemented in subclass");
    }

    /**
     * Grants implicit visibility of the package that provides an authority to a querying UID.
     *
+18 −15
Original line number Diff line number Diff line
@@ -52,6 +52,7 @@ import android.content.pm.parsing.component.ParsedActivity;
import android.content.pm.parsing.component.ParsedActivityUtils;
import android.content.pm.parsing.component.ParsedAttribution;
import android.content.pm.parsing.component.ParsedAttributionUtils;
import android.content.pm.parsing.component.ParsedComponent;
import android.content.pm.parsing.component.ParsedInstrumentation;
import android.content.pm.parsing.component.ParsedInstrumentationUtils;
import android.content.pm.parsing.component.ParsedIntentInfo;
@@ -700,15 +701,15 @@ public class ParsingPackageUtils {
                // note: application meta-data is stored off to the side, so it can
                // remain null in the primary copy (we like to avoid extra copies because
                // it can be large)
                ParseResult<Property> metaDataResult = parseMetaData(pkg, res, parser,
                        "<meta-data>", input);
                ParseResult<Property> metaDataResult = parseMetaData(pkg, null, res,
                        parser, "<meta-data>", input);
                if (metaDataResult.isSuccess() && metaDataResult.getResult() != null) {
                    pkg.setMetaData(metaDataResult.getResult().toBundle(pkg.getMetaData()));
                }
                return metaDataResult;
            case "property":
                ParseResult<Property> propertyResult = parseMetaData(pkg, res, parser,
                        "<property>", input);
                ParseResult<Property> propertyResult = parseMetaData(pkg, null, res,
                        parser, "<property>", input);
                if (propertyResult.isSuccess()) {
                    pkg.addProperty(propertyResult.getResult());
                }
@@ -2093,15 +2094,15 @@ public class ParsingPackageUtils {
                // note: application meta-data is stored off to the side, so it can
                // remain null in the primary copy (we like to avoid extra copies because
                // it can be large)
                final ParseResult<Property> metaDataResult = parseMetaData(pkg, res, parser,
                        "<meta-data>", input);
                final ParseResult<Property> metaDataResult = parseMetaData(pkg, null, res,
                        parser, "<meta-data>", input);
                if (metaDataResult.isSuccess() && metaDataResult.getResult() != null) {
                    pkg.setMetaData(metaDataResult.getResult().toBundle(pkg.getMetaData()));
                }
                return metaDataResult;
            case "property":
                final ParseResult<Property> propertyResult = parseMetaData(pkg, res, parser,
                        "<property>", input);
                final ParseResult<Property> propertyResult = parseMetaData(pkg, null, res,
                        parser, "<property>", input);
                if (propertyResult.isSuccess()) {
                    pkg.addProperty(propertyResult.getResult());
                }
@@ -2754,8 +2755,8 @@ public class ParsingPackageUtils {
     * Parse a meta data defined on the enclosing tag.
     * <p>Meta data can be defined by either &lt;meta-data&gt; or &lt;property&gt; elements.
     */
    public static ParseResult<Property> parseMetaData(ParsingPackage pkg, Resources res,
            XmlResourceParser parser, String tagName, ParseInput input) {
    public static ParseResult<Property> parseMetaData(ParsingPackage pkg, ParsedComponent component,
            Resources res, XmlResourceParser parser, String tagName, ParseInput input) {
        TypedArray sa = res.obtainAttributes(parser, R.styleable.AndroidManifestMetaData);
        try {
            final Property property;
@@ -2765,23 +2766,25 @@ public class ParsingPackageUtils {
                return input.error(tagName + " requires an android:name attribute");
            }

            final String packageName = pkg.getPackageName();
            final String className = component != null ? component.getName() : null;
            TypedValue v = sa.peekValue(R.styleable.AndroidManifestMetaData_resource);
            if (v != null && v.resourceId != 0) {
                property = new Property(name, v.resourceId, true);
                property = new Property(name, v.resourceId, true, packageName, className);
            } else {
                v = sa.peekValue(R.styleable.AndroidManifestMetaData_value);
                if (v != null) {
                    if (v.type == TypedValue.TYPE_STRING) {
                        final CharSequence cs = v.coerceToString();
                        final String stringValue = cs != null ? cs.toString() : null;
                        property = new Property(name, stringValue);
                        property = new Property(name, stringValue, packageName, className);
                    } else if (v.type == TypedValue.TYPE_INT_BOOLEAN) {
                        property = new Property(name, v.data != 0);
                        property = new Property(name, v.data != 0, packageName, className);
                    } else if (v.type >= TypedValue.TYPE_FIRST_INT
                            && v.type <= TypedValue.TYPE_LAST_INT) {
                        property = new Property(name, v.data, false);
                        property = new Property(name, v.data, false, packageName, className);
                    } else if (v.type == TypedValue.TYPE_FLOAT) {
                        property = new Property(name, v.getFloat());
                        property = new Property(name, v.getFloat(), packageName, className);
                    } else {
                        if (!PackageParser.RIGID_PARSER) {
                            Slog.w(TAG,
+4 −4
Original line number Diff line number Diff line
@@ -98,8 +98,8 @@ class ParsedComponentUtils {

    static ParseResult<Bundle> addMetaData(ParsedComponent component, ParsingPackage pkg,
            Resources resources, XmlResourceParser parser, ParseInput input) {
        ParseResult<Property> result = ParsingPackageUtils.parseMetaData(pkg, resources,
                parser, "<meta-data>", input);
        ParseResult<Property> result = ParsingPackageUtils.parseMetaData(pkg, component,
                resources, parser, "<meta-data>", input);
        if (result.isError()) {
            return input.error(result);
        }
@@ -112,8 +112,8 @@ class ParsedComponentUtils {

    static ParseResult<Property> addProperty(ParsedComponent component, ParsingPackage pkg,
            Resources resources, XmlResourceParser parser, ParseInput input) {
        ParseResult<Property> result = ParsingPackageUtils.parseMetaData(pkg, resources,
                parser, "<property>", input);
        ParseResult<Property> result = ParsingPackageUtils.parseMetaData(pkg, component,
                resources, parser, "<property>", input);
        if (result.isError()) {
            return input.error(result);
        }
+44 −14
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.content.pm;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

@@ -34,7 +35,8 @@ public class PackageManagerPropertyTests {

    @Test
    public void testBooleanProperty() throws Exception {
        final Property p = new Property("booleanProperty", true);
        final Property p =
                new Property("booleanProperty", true, "android", null);
        assertTrue(p.isBoolean());
        assertFalse(p.isFloat());
        assertFalse(p.isInteger());
@@ -45,17 +47,21 @@ public class PackageManagerPropertyTests {
        assertEquals(0, p.getInteger());
        assertEquals(0, p.getResourceId());
        assertEquals(null, p.getString());
        assertEquals("android", p.getPackageName());
        assertNull(p.getClassName());
    }

    @Test
    public void testBooleanPropertyToBundle() throws Exception {
        final Bundle b = new Property("booleanProperty", true).toBundle(null);
        final Bundle b =
                new Property("booleanProperty", true, "android", null).toBundle(null);
        assertTrue(b.getBoolean("booleanProperty"));
    }

    @Test
    public void testFloatProperty() throws Exception {
        final Property p = new Property("floatProperty", 3.14f);
        final Property p =
                new Property("floatProperty", 3.14f, "android", null);
        assertFalse(p.isBoolean());
        assertTrue(p.isFloat());
        assertFalse(p.isInteger());
@@ -66,17 +72,21 @@ public class PackageManagerPropertyTests {
        assertEquals(0, p.getInteger());
        assertEquals(0, p.getResourceId());
        assertEquals(null, p.getString());
        assertEquals("android", p.getPackageName());
        assertNull(p.getClassName());
    }

    @Test
    public void testFloatPropertyToBundle() throws Exception {
        final Bundle b = new Property("floatProperty", 3.14f).toBundle(null);
        final Bundle b =
                new Property("floatProperty", 3.14f, "android", null).toBundle(null);
        assertEquals(3.14f, b.getFloat("floatProperty"), 0.0f);
    }

    @Test
    public void testIntegerProperty() throws Exception {
        final Property p = new Property("integerProperty", 42, false);
        final Property p =
                new Property("integerProperty", 42, false, "android", null);
        assertFalse(p.isBoolean());
        assertFalse(p.isFloat());
        assertTrue(p.isInteger());
@@ -87,17 +97,21 @@ public class PackageManagerPropertyTests {
        assertEquals(42, p.getInteger());
        assertEquals(0, p.getResourceId());
        assertEquals(null, p.getString());
        assertEquals("android", p.getPackageName());
        assertNull(p.getClassName());
    }

    @Test
    public void testIntegerPropertyToBundle() throws Exception {
        final Bundle b = new Property("integerProperty", 42, false).toBundle(null);
        final Bundle b =
                new Property("integerProperty", 42, false, "android", null).toBundle(null);
        assertEquals(42, b.getInt("integerProperty"));
    }

    @Test
    public void testResourceProperty() throws Exception {
        final Property p = new Property("resourceProperty", 0x7f010001, true);
        final Property p =
                new Property("resourceProperty", 0x7f010001, true, "android", null);
        assertFalse(p.isBoolean());
        assertFalse(p.isFloat());
        assertFalse(p.isInteger());
@@ -108,17 +122,21 @@ public class PackageManagerPropertyTests {
        assertEquals(0, p.getInteger());
        assertEquals(0x7f010001, p.getResourceId());
        assertEquals(null, p.getString());
        assertEquals("android", p.getPackageName());
        assertNull(p.getClassName());
    }

    @Test
    public void testResourcePropertyToBundle() throws Exception {
        final Bundle b = new Property("resourceProperty", 0x7f010001, true).toBundle(null);
        final Bundle b =
                new Property("resourceProperty", 0x7f010001, true, "android", null).toBundle(null);
        assertEquals(0x7f010001, b.getInt("resourceProperty"));
    }

    @Test
    public void testStringProperty() throws Exception {
        final Property p = new Property("stringProperty", "koala");
        final Property p =
                new Property("stringProperty", "koala", "android", null);
        assertFalse(p.isBoolean());
        assertFalse(p.isFloat());
        assertFalse(p.isInteger());
@@ -129,18 +147,21 @@ public class PackageManagerPropertyTests {
        assertEquals(0, p.getInteger());
        assertEquals(0, p.getResourceId());
        assertEquals("koala", p.getString());
        assertEquals("android", p.getPackageName());
        assertNull(p.getClassName());
    }

    @Test
    public void testStringPropertyToBundle() throws Exception {
        final Bundle b = new Property("stringProperty", "koala").toBundle(null);
        final Bundle b =
                new Property("stringProperty", "koala", "android", null).toBundle(null);
        assertEquals("koala", b.getString("stringProperty"));
    }

    @Test
    public void testProperty_invalidName() throws Exception {
        try {
            final Property p = new Property(null, 1);
            final Property p = new Property(null, 1, "android", null);
            fail("expected assertion error");
        } catch (AssertionError expected) {
        }
@@ -149,19 +170,28 @@ public class PackageManagerPropertyTests {
    @Test
    public void testProperty_invalidType() throws Exception {
        try {
            final Property p = new Property(null, 0);
            final Property p = new Property("invalidTypeProperty", 0, "android", null);
            fail("expected assertion error");
        } catch (AssertionError expected) {
        }

        try {
            final Property p = new Property(null, 6);
            final Property p = new Property("invalidTypeProperty", 6, "android", null);
            fail("expected assertion error");
        } catch (AssertionError expected) {
        }

        try {
            final Property p = new Property(null, -1);
            final Property p = new Property("invalidTypeProperty", -1, "android", null);
            fail("expected assertion error");
        } catch (AssertionError expected) {
        }
    }

    @Test
    public void testProperty_noPackageName() throws Exception {
        try {
            final Property p = new Property(null, 1, null, null);
            fail("expected assertion error");
        } catch (AssertionError expected) {
        }