Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 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 | |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 17 | package android.content.integrity; |
Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 18 | |
Khaled Abdelmohsen | a4477fa | 2019-10-09 14:35:32 +0100 | [diff] [blame] | 19 | import static com.android.internal.util.Preconditions.checkArgument; |
Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 20 | |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 21 | import android.annotation.IntDef; |
| 22 | import android.annotation.NonNull; |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 25 | |
| 26 | import com.android.internal.annotations.VisibleForTesting; |
| 27 | |
| 28 | import java.lang.annotation.Retention; |
| 29 | import java.lang.annotation.RetentionPolicy; |
| 30 | import java.util.ArrayList; |
Khaled Abdelmohsen | a4477fa | 2019-10-09 14:35:32 +0100 | [diff] [blame] | 31 | import java.util.Collections; |
| 32 | import java.util.List; |
Khaled Abdelmohsen | f5f9814 | 2019-10-10 17:29:53 +0100 | [diff] [blame] | 33 | import java.util.Objects; |
Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 34 | |
| 35 | /** |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 36 | * Represents a compound formula formed by joining other simple and complex formulas with boolean |
| 37 | * connectors. |
Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 38 | * |
| 39 | * <p>Instances of this class are immutable. |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 40 | * |
| 41 | * @hide |
Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 42 | */ |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 43 | @VisibleForTesting |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 44 | public final class CompoundFormula extends IntegrityFormula implements Parcelable { |
Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 45 | |
Anton Hansson | 57b6af0 | 2019-12-10 15:38:59 +0000 | [diff] [blame] | 46 | /** @hide */ |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 47 | @IntDef(value = {AND, OR, NOT}) |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 48 | @Retention(RetentionPolicy.SOURCE) |
| 49 | public @interface Connector {} |
Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 50 | |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 51 | /** Boolean AND operator. */ |
| 52 | public static final int AND = 0; |
| 53 | |
| 54 | /** Boolean OR operator. */ |
| 55 | public static final int OR = 1; |
| 56 | |
| 57 | /** Boolean NOT operator. */ |
| 58 | public static final int NOT = 2; |
| 59 | |
| 60 | private final @Connector int mConnector; |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 61 | private final @NonNull List<IntegrityFormula> mFormulas; |
Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 62 | |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 63 | @NonNull |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 64 | public static final Creator<CompoundFormula> CREATOR = |
| 65 | new Creator<CompoundFormula>() { |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 66 | @Override |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 67 | public CompoundFormula createFromParcel(Parcel in) { |
| 68 | return new CompoundFormula(in); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 72 | public CompoundFormula[] newArray(int size) { |
| 73 | return new CompoundFormula[size]; |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 74 | } |
| 75 | }; |
| 76 | |
| 77 | /** |
| 78 | * Create a new formula from operator and operands. |
| 79 | * |
| 80 | * @throws IllegalArgumentException if the number of operands is not matching the requirements |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 81 | * for that operator (at least 2 for {@link #AND} and {@link |
| 82 | * #OR}, 1 for {@link #NOT}). |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 83 | */ |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 84 | public CompoundFormula(@Connector int connector, List<IntegrityFormula> formulas) { |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 85 | checkArgument( |
| 86 | isValidConnector(connector), String.format("Unknown connector: %d", connector)); |
Khaled Abdelmohsen | a4477fa | 2019-10-09 14:35:32 +0100 | [diff] [blame] | 87 | validateFormulas(connector, formulas); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 88 | this.mConnector = connector; |
| 89 | this.mFormulas = Collections.unmodifiableList(formulas); |
Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 92 | CompoundFormula(Parcel in) { |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 93 | mConnector = in.readInt(); |
| 94 | int length = in.readInt(); |
| 95 | checkArgument(length >= 0, "Must have non-negative length. Got " + length); |
| 96 | mFormulas = new ArrayList<>(length); |
| 97 | for (int i = 0; i < length; i++) { |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 98 | mFormulas.add(IntegrityFormula.readFromParcel(in)); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 99 | } |
Song Pan | 44e5aa5 | 2019-11-26 11:38:17 +0000 | [diff] [blame] | 100 | validateFormulas(mConnector, mFormulas); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | public @Connector int getConnector() { |
Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 104 | return mConnector; |
| 105 | } |
| 106 | |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 107 | @NonNull |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 108 | public List<IntegrityFormula> getFormulas() { |
Khaled Abdelmohsen | a4477fa | 2019-10-09 14:35:32 +0100 | [diff] [blame] | 109 | return mFormulas; |
Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Khaled Abdelmohsen | 3f878cb | 2019-10-09 19:10:38 +0100 | [diff] [blame] | 112 | @Override |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 113 | public int getTag() { |
| 114 | return IntegrityFormula.COMPOUND_FORMULA_TAG; |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 118 | public boolean matches(AppInstallMetadata appInstallMetadata) { |
| 119 | switch (getConnector()) { |
| 120 | case NOT: |
| 121 | return !getFormulas().get(0).matches(appInstallMetadata); |
| 122 | case AND: |
| 123 | return getFormulas().stream() |
| 124 | .allMatch(formula -> formula.matches(appInstallMetadata)); |
| 125 | case OR: |
| 126 | return getFormulas().stream() |
| 127 | .anyMatch(formula -> formula.matches(appInstallMetadata)); |
| 128 | default: |
| 129 | throw new IllegalArgumentException("Unknown connector " + getConnector()); |
| 130 | } |
Song Pan | 44e5aa5 | 2019-11-26 11:38:17 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
Khaled Abdelmohsen | 3f878cb | 2019-10-09 19:10:38 +0100 | [diff] [blame] | 134 | public String toString() { |
| 135 | StringBuilder sb = new StringBuilder(); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 136 | if (mFormulas.size() == 1) { |
| 137 | sb.append(String.format("%s ", connectorToString(mConnector))); |
| 138 | sb.append(mFormulas.get(0).toString()); |
| 139 | } else { |
| 140 | for (int i = 0; i < mFormulas.size(); i++) { |
| 141 | if (i > 0) { |
| 142 | sb.append(String.format(" %s ", connectorToString(mConnector))); |
| 143 | } |
| 144 | sb.append(mFormulas.get(i).toString()); |
Khaled Abdelmohsen | 3f878cb | 2019-10-09 19:10:38 +0100 | [diff] [blame] | 145 | } |
Khaled Abdelmohsen | 3f878cb | 2019-10-09 19:10:38 +0100 | [diff] [blame] | 146 | } |
| 147 | return sb.toString(); |
| 148 | } |
| 149 | |
Khaled Abdelmohsen | f5f9814 | 2019-10-10 17:29:53 +0100 | [diff] [blame] | 150 | @Override |
| 151 | public boolean equals(Object o) { |
| 152 | if (this == o) { |
| 153 | return true; |
| 154 | } |
| 155 | if (o == null || getClass() != o.getClass()) { |
| 156 | return false; |
| 157 | } |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 158 | CompoundFormula that = (CompoundFormula) o; |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 159 | return mConnector == that.mConnector && mFormulas.equals(that.mFormulas); |
Khaled Abdelmohsen | f5f9814 | 2019-10-10 17:29:53 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public int hashCode() { |
| 164 | return Objects.hash(mConnector, mFormulas); |
| 165 | } |
| 166 | |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 167 | @Override |
| 168 | public int describeContents() { |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public void writeToParcel(@NonNull Parcel dest, int flags) { |
| 174 | dest.writeInt(mConnector); |
| 175 | dest.writeInt(mFormulas.size()); |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 176 | for (IntegrityFormula formula : mFormulas) { |
| 177 | IntegrityFormula.writeToParcel(formula, dest, flags); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 181 | private static void validateFormulas( |
| 182 | @Connector int connector, List<IntegrityFormula> formulas) { |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 183 | switch (connector) { |
| 184 | case AND: |
| 185 | case OR: |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 186 | checkArgument( |
| 187 | formulas.size() >= 2, |
| 188 | String.format( |
| 189 | "Connector %s must have at least 2 formulas", |
| 190 | connectorToString(connector))); |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 191 | break; |
| 192 | case NOT: |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 193 | checkArgument( |
| 194 | formulas.size() == 1, |
| 195 | String.format( |
| 196 | "Connector %s must have 1 formula only", |
| 197 | connectorToString(connector))); |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 198 | break; |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 199 | } |
| 200 | } |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 201 | |
Song Pan | 44e5aa5 | 2019-11-26 11:38:17 +0000 | [diff] [blame] | 202 | private static String connectorToString(int connector) { |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 203 | switch (connector) { |
| 204 | case AND: |
| 205 | return "AND"; |
| 206 | case OR: |
| 207 | return "OR"; |
| 208 | case NOT: |
| 209 | return "NOT"; |
| 210 | default: |
| 211 | throw new IllegalArgumentException("Unknown connector " + connector); |
| 212 | } |
| 213 | } |
Khaled Abdelmohsen | 5809a5f | 2019-11-25 18:20:46 +0000 | [diff] [blame] | 214 | |
| 215 | private static boolean isValidConnector(int connector) { |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 216 | return connector == AND || connector == OR || connector == NOT; |
Khaled Abdelmohsen | 5809a5f | 2019-11-25 18:20:46 +0000 | [diff] [blame] | 217 | } |
Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 218 | } |