| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
@SystemApi |
| 42 |
@VisibleForTesting |
| 43 |
public final class Rule implements Parcelable { |
| 44 |
|
| 45 |
|
| 46 |
@IntDef( |
| 47 |
value = { |
| 48 |
DENY, |
| 49 |
FORCE_ALLOW, |
| 50 |
}) |
| 51 |
@Retention(RetentionPolicy.SOURCE) |
| 52 |
public @interface Effect {} |
| 53 |
|
| 54 |
|
| 55 |
public static final int DENY = 0; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public static final int FORCE_ALLOW = 1; |
| 62 |
|
| 63 |
private final @NonNull IntegrityFormula mFormula; |
| 64 |
private final @Effect int mEffect; |
| 65 |
|
| 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 |
|
| 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 |
| 92 |
public int describeContents() { |
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public void writeToParcel(@NonNull Parcel dest, int flags) { |
| 98 |
IntegrityFormula.writeToParcel(mFormula, dest, flags); |
| 99 |
dest.writeInt(mEffect); |
| 100 |
} |
| 101 |
|
| 102 |
@NonNull |
| 103 |
public IntegrityFormula getFormula() { |
| 104 |
return mFormula; |
| 105 |
} |
| 106 |
|
| 107 |
public @Effect int getEffect() { |
| 108 |
return mEffect; |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public String toString() { |
| 113 |
return String.format("Rule: %s, %s", mFormula, effectToString(mEffect)); |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 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 |
| 129 |
public int hashCode() { |
| 130 |
return Objects.hash(mFormula, mEffect); |
| 131 |
} |
| 132 |
|
| 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 |
|
| 144 |
private static boolean isValidEffect(int effect) { |
| 145 |
return effect == DENY || effect == FORCE_ALLOW; |
| 146 |
} |
| 147 |
} |