• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.content.integrity;
18 
19 import static com.android.internal.util.Preconditions.checkArgument;
20 
21 import android.annotation.IntDef;
22 import android.annotation.NonNull;
23 import android.annotation.Nullable;
24 import android.annotation.SystemApi;
25 import android.os.Parcel;
26 import android.os.Parcelable;
27 
28 import com.android.internal.annotations.VisibleForTesting;
29 
30 import java.lang.annotation.Retention;
31 import java.lang.annotation.RetentionPolicy;
32 import java.util.Objects;
33 
34 /**
35  * Represent rules to be used in the rule evaluation engine to match against app installs.
36  *
37  * <p>Instances of this class are immutable.
38  *
39  * @hide
40  */
41 @SystemApi
42 @VisibleForTesting
43 public final class Rule implements Parcelable {
44 
45     /** @hide */
46     @IntDef(
47             value = {
48                 DENY,
49                 FORCE_ALLOW,
50             })
51     @Retention(RetentionPolicy.SOURCE)
52     public @interface Effect {}
53 
54     /** If this rule matches the install, the install should be denied. */
55     public static final int DENY = 0;
56 
57     /**
58      * If this rule matches the install, the install will be allowed regardless of other matched
59      * rules.
60      */
61     public static final int FORCE_ALLOW = 1;
62 
63     private final @NonNull IntegrityFormula mFormula;
64     private final @Effect int mEffect;
65 
Rule(@onNull IntegrityFormula formula, @Effect int effect)66     public Rule(@NonNull IntegrityFormula formula, @Effect int effect) {
67         checkArgument(isValidEffect(effect), "Unknown effect: %d", effect);
68         this.mFormula = Objects.requireNonNull(formula);
69         this.mEffect = effect;
70     }
71 
Rule(Parcel in)72     Rule(Parcel in) {
73         mFormula = IntegrityFormula.readFromParcel(in);
74         mEffect = in.readInt();
75     }
76 
77     @NonNull
78     public static final Creator<Rule> CREATOR =
79             new Creator<Rule>() {
80                 @Override
81                 public Rule createFromParcel(Parcel in) {
82                     return new Rule(in);
83                 }
84 
85                 @Override
86                 public Rule[] newArray(int size) {
87                     return new Rule[size];
88                 }
89             };
90 
91     @Override
describeContents()92     public int describeContents() {
93         return 0;
94     }
95 
96     @Override
writeToParcel(@onNull Parcel dest, int flags)97     public void writeToParcel(@NonNull Parcel dest, int flags) {
98         IntegrityFormula.writeToParcel(mFormula, dest, flags);
99         dest.writeInt(mEffect);
100     }
101 
102     @NonNull
getFormula()103     public IntegrityFormula getFormula() {
104         return mFormula;
105     }
106 
getEffect()107     public @Effect int getEffect() {
108         return mEffect;
109     }
110 
111     @Override
toString()112     public String toString() {
113         return String.format("Rule: %s, %s", mFormula, effectToString(mEffect));
114     }
115 
116     @Override
equals(@ullable Object o)117     public boolean equals(@Nullable Object o) {
118         if (this == o) {
119             return true;
120         }
121         if (o == null || getClass() != o.getClass()) {
122             return false;
123         }
124         Rule that = (Rule) o;
125         return mEffect == that.mEffect && Objects.equals(mFormula, that.mFormula);
126     }
127 
128     @Override
hashCode()129     public int hashCode() {
130         return Objects.hash(mFormula, mEffect);
131     }
132 
effectToString(int effect)133     private static String effectToString(int effect) {
134         switch (effect) {
135             case DENY:
136                 return "DENY";
137             case FORCE_ALLOW:
138                 return "FORCE_ALLOW";
139             default:
140                 throw new IllegalArgumentException("Unknown effect " + effect);
141         }
142     }
143 
isValidEffect(int effect)144     private static boolean isValidEffect(int effect) {
145         return effect == DENY || effect == FORCE_ALLOW;
146     }
147 }
148