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

Commit 51649289 authored by Winson's avatar Winson
Browse files

Split ParsedAttributionImpl

Bug: 178218967

Change-Id: I7afc6a1188c29e6825e9e8a7618739a1f58a22d4
parent 084e3883
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import android.content.pm.SigningDetails;
import android.content.pm.parsing.component.ParsedActivity;
import android.content.pm.parsing.component.ParsedActivityImpl;
import android.content.pm.parsing.component.ParsedAttribution;
import android.content.pm.parsing.component.ParsedAttributionImpl;
import android.content.pm.parsing.component.ParsedComponent;
import android.content.pm.parsing.component.ParsedInstrumentation;
import android.content.pm.parsing.component.ParsedIntentInfo;
@@ -1290,7 +1291,8 @@ public class ParsingPackageImpl implements ParsingPackage, ParsingPackageHidden,
        this.receivers = ParsingUtils.createTypedInterfaceList(in, ParsedActivityImpl.CREATOR);
        this.services = ParsingUtils.createTypedInterfaceList(in, ParsedServiceImpl.CREATOR);
        this.providers = ParsingUtils.createTypedInterfaceList(in, ParsedProviderImpl.CREATOR);
        this.attributions = in.createTypedArrayList(ParsedAttribution.CREATOR);
        this.attributions = ParsingUtils.createTypedInterfaceList(in,
                ParsedAttributionImpl.CREATOR);
        this.permissions = in.createTypedArrayList(ParsedPermission.CREATOR);
        this.permissionGroups = in.createTypedArrayList(ParsedPermissionGroup.CREATOR);
        this.instrumentations = in.createTypedArrayList(ParsedInstrumentation.CREATOR);
+1 −1
Original line number Diff line number Diff line
@@ -919,7 +919,7 @@ public class ParsingPackageUtils {
            }
        }

        if (!ParsedAttribution.isCombinationValid(pkg.getAttributions())) {
        if (!ParsedAttributionUtils.isCombinationValid(pkg.getAttributions())) {
            return input.error(
                    INSTALL_PARSE_FAILED_BAD_MANIFEST,
                    "Combination <attribution> tags are not valid"
+9 −238
Original line number Diff line number Diff line
@@ -34,255 +34,26 @@ import java.util.List;
 *
 * @hide
 */
@DataClass(genAidl = false, genSetters = true, genBuilder = false)
public class ParsedAttribution implements Parcelable {
    /** Maximum length of attribution tag */
    public static final int MAX_ATTRIBUTION_TAG_LEN = 50;

    /** Maximum amount of attributions per package */
    private static final int MAX_NUM_ATTRIBUTIONS = 10000;

    /** Tag of the attribution */
    private @NonNull String tag;

    /** User visible label fo the attribution */
    private @StringRes int label;

    /** Ids of previously declared attributions this attribution inherits from */
    private @NonNull List<String> inheritFrom;

    public ParsedAttribution() {}
public interface ParsedAttribution extends Parcelable {

    /**
     * @return Is this set of attributions a valid combination for a single package?
     * Maximum length of attribution tag
     * @hide
     */
    public static boolean isCombinationValid(@Nullable List<ParsedAttribution> attributions) {
        if (attributions == null) {
            return true;
        }

        ArraySet<String> attributionTags = new ArraySet<>(attributions.size());
        ArraySet<String> inheritFromAttributionTags = new ArraySet<>();

        int numAttributions = attributions.size();
        if (numAttributions > MAX_NUM_ATTRIBUTIONS) {
            return false;
        }

        for (int attributionNum = 0; attributionNum < numAttributions; attributionNum++) {
            boolean wasAdded = attributionTags.add(attributions.get(attributionNum).tag);
            if (!wasAdded) {
                // feature id is not unique
                return false;
            }
        }

        for (int attributionNum = 0; attributionNum < numAttributions; attributionNum++) {
            ParsedAttribution feature = attributions.get(attributionNum);

            int numInheritFrom = feature.inheritFrom.size();
            for (int inheritFromNum = 0; inheritFromNum < numInheritFrom; inheritFromNum++) {
                String inheritFrom = feature.inheritFrom.get(inheritFromNum);

                if (attributionTags.contains(inheritFrom)) {
                    // Cannot inherit from a attribution that is still defined
                    return false;
                }

                boolean wasAdded = inheritFromAttributionTags.add(inheritFrom);
                if (!wasAdded) {
                    // inheritFrom is not unique
                    return false;
                }
            }
        }

        return true;
    }



    // Code below generated by codegen v1.0.23.
    //
    // DO NOT MODIFY!
    // CHECKSTYLE:OFF Generated code
    //
    // To regenerate run:
    // $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/content/pm/parsing/component/ParsedAttribution.java
    //
    // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
    //   Settings > Editor > Code Style > Formatter Control
    //@formatter:off


    @android.annotation.IntDef(prefix = "MAX_", value = {
        MAX_ATTRIBUTION_TAG_LEN,
        MAX_NUM_ATTRIBUTIONS
    })
    @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.SOURCE)
    @DataClass.Generated.Member
    public @interface Max {}

    @DataClass.Generated.Member
    public static String maxToString(@Max int value) {
        switch (value) {
            case MAX_ATTRIBUTION_TAG_LEN:
                    return "MAX_ATTRIBUTION_TAG_LEN";
            case MAX_NUM_ATTRIBUTIONS:
                    return "MAX_NUM_ATTRIBUTIONS";
            default: return Integer.toHexString(value);
        }
    }
    int MAX_ATTRIBUTION_TAG_LEN = 50;

    /**
     * Creates a new ParsedAttribution.
     *
     * @param tag
     *   Tag of the attribution
     * @param label
     *   User visible label fo the attribution
     * @param inheritFrom
     * Ids of previously declared attributions this attribution inherits from
     */
    @DataClass.Generated.Member
    public ParsedAttribution(
            @NonNull String tag,
            @StringRes int label,
            @NonNull List<String> inheritFrom) {
        this.tag = tag;
        com.android.internal.util.AnnotationValidations.validate(
                NonNull.class, null, tag);
        this.label = label;
        com.android.internal.util.AnnotationValidations.validate(
                StringRes.class, null, label);
        this.inheritFrom = inheritFrom;
        com.android.internal.util.AnnotationValidations.validate(
                NonNull.class, null, inheritFrom);

        // onConstructed(); // You can define this method to get a callback
    }
    @NonNull List<String> getInheritFrom();

    /**
     * Tag of the attribution
     * User visible label for the attribution
     */
    @DataClass.Generated.Member
    public @NonNull String getTag() {
        return tag;
    }

    /**
     * User visible label fo the attribution
     */
    @DataClass.Generated.Member
    public @StringRes int getLabel() {
        return label;
    }

    /**
     * Ids of previously declared attributions this attribution inherits from
     */
    @DataClass.Generated.Member
    public @NonNull List<String> getInheritFrom() {
        return inheritFrom;
    }
    @StringRes int getLabel();

    /**
     * Tag of the attribution
     */
    @DataClass.Generated.Member
    public @NonNull ParsedAttribution setTag(@NonNull String value) {
        tag = value;
        com.android.internal.util.AnnotationValidations.validate(
                NonNull.class, null, tag);
        return this;
    }

    /**
     * User visible label fo the attribution
     */
    @DataClass.Generated.Member
    public @NonNull ParsedAttribution setLabel(@StringRes int value) {
        label = value;
        com.android.internal.util.AnnotationValidations.validate(
                StringRes.class, null, label);
        return this;
    }

    /**
     * Ids of previously declared attributions this attribution inherits from
     */
    @DataClass.Generated.Member
    public @NonNull ParsedAttribution setInheritFrom(@NonNull List<String> value) {
        inheritFrom = value;
        com.android.internal.util.AnnotationValidations.validate(
                NonNull.class, null, inheritFrom);
        return this;
    }

    @Override
    @DataClass.Generated.Member
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        // You can override field parcelling by defining methods like:
        // void parcelFieldName(Parcel dest, int flags) { ... }

        dest.writeString(tag);
        dest.writeInt(label);
        dest.writeStringList(inheritFrom);
    }

    @Override
    @DataClass.Generated.Member
    public int describeContents() { return 0; }

    /** @hide */
    @SuppressWarnings({"unchecked", "RedundantCast"})
    @DataClass.Generated.Member
    protected ParsedAttribution(@NonNull Parcel in) {
        // You can override field unparcelling by defining methods like:
        // static FieldType unparcelFieldName(Parcel in) { ... }

        String _tag = in.readString();
        int _label = in.readInt();
        List<String> _inheritFrom = new ArrayList<>();
        in.readStringList(_inheritFrom);

        this.tag = _tag;
        com.android.internal.util.AnnotationValidations.validate(
                NonNull.class, null, tag);
        this.label = _label;
        com.android.internal.util.AnnotationValidations.validate(
                StringRes.class, null, label);
        this.inheritFrom = _inheritFrom;
        com.android.internal.util.AnnotationValidations.validate(
                NonNull.class, null, inheritFrom);

        // onConstructed(); // You can define this method to get a callback
    }

    @DataClass.Generated.Member
    public static final @NonNull Parcelable.Creator<ParsedAttribution> CREATOR
            = new Parcelable.Creator<ParsedAttribution>() {
        @Override
        public ParsedAttribution[] newArray(int size) {
            return new ParsedAttribution[size];
        }

        @Override
        public ParsedAttribution createFromParcel(@NonNull Parcel in) {
            return new ParsedAttribution(in);
        }
    };

    @DataClass.Generated(
            time = 1624050667337L,
            codegenVersion = "1.0.23",
            sourceFile = "frameworks/base/core/java/android/content/pm/parsing/component/ParsedAttribution.java",
            inputSignatures = "public static final  int MAX_ATTRIBUTION_TAG_LEN\nprivate static final  int MAX_NUM_ATTRIBUTIONS\nprivate @android.annotation.NonNull java.lang.String tag\nprivate @android.annotation.StringRes int label\nprivate @android.annotation.NonNull java.util.List<java.lang.String> inheritFrom\npublic static  boolean isCombinationValid(java.util.List<android.content.pm.parsing.component.ParsedAttribution>)\nclass ParsedAttribution extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genAidl=false, genSetters=true, genBuilder=false)")
    @Deprecated
    private void __metadata() {}


    //@formatter:on
    // End of generated code

    @NonNull String getTag();
}
+222 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.content.pm.parsing.component;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.StringRes;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.ArraySet;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.DataClass;

import java.util.ArrayList;
import java.util.List;

/**
 * A {@link android.R.styleable#AndroidManifestAttribution &lt;attribution&gt;} tag parsed from the
 * manifest.
 *
 * @hide
 */
@DataClass(genAidl = false, genSetters = true, genBuilder = false, genParcelable = true)
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
public class ParsedAttributionImpl implements ParsedAttribution {

    /** Maximum amount of attributions per package */
    static final int MAX_NUM_ATTRIBUTIONS = 10000;

    /** Tag of the attribution */
    private @NonNull String tag;

    /** User visible label fo the attribution */
    private @StringRes int label;

    /** Ids of previously declared attributions this attribution inherits from */
    private @NonNull List<String> inheritFrom;

    public ParsedAttributionImpl() {}



    // Code below generated by codegen v1.0.23.
    //
    // DO NOT MODIFY!
    // CHECKSTYLE:OFF Generated code
    //
    // To regenerate run:
    // $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/content/pm/parsing/component/ParsedAttributionImpl.java
    //
    // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
    //   Settings > Editor > Code Style > Formatter Control
    //@formatter:off


    /**
     * Creates a new ParsedAttributionImpl.
     *
     * @param tag
     *   Tag of the attribution
     * @param label
     *   User visible label fo the attribution
     * @param inheritFrom
     *   Ids of previously declared attributions this attribution inherits from
     */
    @DataClass.Generated.Member
    public ParsedAttributionImpl(
            @NonNull String tag,
            @StringRes int label,
            @NonNull List<String> inheritFrom) {
        this.tag = tag;
        com.android.internal.util.AnnotationValidations.validate(
                NonNull.class, null, tag);
        this.label = label;
        com.android.internal.util.AnnotationValidations.validate(
                StringRes.class, null, label);
        this.inheritFrom = inheritFrom;
        com.android.internal.util.AnnotationValidations.validate(
                NonNull.class, null, inheritFrom);

        // onConstructed(); // You can define this method to get a callback
    }

    /**
     * Tag of the attribution
     */
    @DataClass.Generated.Member
    public @NonNull String getTag() {
        return tag;
    }

    /**
     * User visible label fo the attribution
     */
    @DataClass.Generated.Member
    public @StringRes int getLabel() {
        return label;
    }

    /**
     * Ids of previously declared attributions this attribution inherits from
     */
    @DataClass.Generated.Member
    public @NonNull List<String> getInheritFrom() {
        return inheritFrom;
    }

    /**
     * Tag of the attribution
     */
    @DataClass.Generated.Member
    public @NonNull ParsedAttributionImpl setTag(@NonNull String value) {
        tag = value;
        com.android.internal.util.AnnotationValidations.validate(
                NonNull.class, null, tag);
        return this;
    }

    /**
     * User visible label fo the attribution
     */
    @DataClass.Generated.Member
    public @NonNull ParsedAttributionImpl setLabel(@StringRes int value) {
        label = value;
        com.android.internal.util.AnnotationValidations.validate(
                StringRes.class, null, label);
        return this;
    }

    /**
     * Ids of previously declared attributions this attribution inherits from
     */
    @DataClass.Generated.Member
    public @NonNull ParsedAttributionImpl setInheritFrom(@NonNull List<String> value) {
        inheritFrom = value;
        com.android.internal.util.AnnotationValidations.validate(
                NonNull.class, null, inheritFrom);
        return this;
    }

    @Override
    @DataClass.Generated.Member
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        // You can override field parcelling by defining methods like:
        // void parcelFieldName(Parcel dest, int flags) { ... }

        dest.writeString(tag);
        dest.writeInt(label);
        dest.writeStringList(inheritFrom);
    }

    @Override
    @DataClass.Generated.Member
    public int describeContents() { return 0; }

    /** @hide */
    @SuppressWarnings({"unchecked", "RedundantCast"})
    @DataClass.Generated.Member
    protected ParsedAttributionImpl(@NonNull Parcel in) {
        // You can override field unparcelling by defining methods like:
        // static FieldType unparcelFieldName(Parcel in) { ... }

        String _tag = in.readString();
        int _label = in.readInt();
        List<String> _inheritFrom = new ArrayList<>();
        in.readStringList(_inheritFrom);

        this.tag = _tag;
        com.android.internal.util.AnnotationValidations.validate(
                NonNull.class, null, tag);
        this.label = _label;
        com.android.internal.util.AnnotationValidations.validate(
                StringRes.class, null, label);
        this.inheritFrom = _inheritFrom;
        com.android.internal.util.AnnotationValidations.validate(
                NonNull.class, null, inheritFrom);

        // onConstructed(); // You can define this method to get a callback
    }

    @DataClass.Generated.Member
    public static final @NonNull Parcelable.Creator<ParsedAttributionImpl> CREATOR
            = new Parcelable.Creator<ParsedAttributionImpl>() {
        @Override
        public ParsedAttributionImpl[] newArray(int size) {
            return new ParsedAttributionImpl[size];
        }

        @Override
        public ParsedAttributionImpl createFromParcel(@NonNull Parcel in) {
            return new ParsedAttributionImpl(in);
        }
    };

    @DataClass.Generated(
            time = 1627594502974L,
            codegenVersion = "1.0.23",
            sourceFile = "frameworks/base/core/java/android/content/pm/parsing/component/ParsedAttributionImpl.java",
            inputSignatures = "static final  int MAX_NUM_ATTRIBUTIONS\nprivate @android.annotation.NonNull java.lang.String tag\nprivate @android.annotation.StringRes int label\nprivate @android.annotation.NonNull java.util.List<java.lang.String> inheritFrom\nclass ParsedAttributionImpl extends java.lang.Object implements [android.content.pm.parsing.component.ParsedAttribution]\n@com.android.internal.util.DataClass(genAidl=false, genSetters=true, genBuilder=false, genParcelable=true)")
    @Deprecated
    private void __metadata() {}


    //@formatter:on
    // End of generated code

}
+51 −1
Original line number Diff line number Diff line
@@ -17,11 +17,13 @@
package android.content.pm.parsing.component;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.pm.parsing.result.ParseInput;
import android.content.pm.parsing.result.ParseResult;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.util.ArraySet;

import com.android.internal.R;

@@ -106,6 +108,54 @@ public class ParsedAttributionUtils {
            ((ArrayList) inheritFrom).trimToSize();
        }

        return input.success(new ParsedAttribution(attributionTag, label, inheritFrom));
        return input.success(new ParsedAttributionImpl(attributionTag, label, inheritFrom));
    }

    /**
     * @return Is this set of attributions a valid combination for a single package?
     */
    public static boolean isCombinationValid(@Nullable List<ParsedAttribution> attributions) {
        if (attributions == null) {
            return true;
        }

        ArraySet<String> attributionTags = new ArraySet<>(attributions.size());
        ArraySet<String> inheritFromAttributionTags = new ArraySet<>();

        int numAttributions = attributions.size();
        if (numAttributions > ParsedAttributionImpl.MAX_NUM_ATTRIBUTIONS) {
            return false;
        }

        for (int attributionNum = 0; attributionNum < numAttributions; attributionNum++) {
            boolean wasAdded = attributionTags.add(attributions.get(attributionNum).getTag());
            if (!wasAdded) {
                // feature id is not unique
                return false;
            }
        }

        for (int attributionNum = 0; attributionNum < numAttributions; attributionNum++) {
            ParsedAttribution feature = attributions.get(attributionNum);

            final List<String> inheritFromList = feature.getInheritFrom();
            int numInheritFrom = inheritFromList.size();
            for (int inheritFromNum = 0; inheritFromNum < numInheritFrom; inheritFromNum++) {
                String inheritFrom = inheritFromList.get(inheritFromNum);

                if (attributionTags.contains(inheritFrom)) {
                    // Cannot inherit from a attribution that is still defined
                    return false;
                }

                boolean wasAdded = inheritFromAttributionTags.add(inheritFrom);
                if (!wasAdded) {
                    // inheritFrom is not unique
                    return false;
                }
            }
        }

        return true;
    }
}
Loading