Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.service.autofill; |
| 18 | |
| 19 | import static android.view.autofill.Helper.sDebug; |
| 20 | |
| 21 | import android.annotation.NonNull; |
| 22 | import android.os.Parcel; |
| 23 | import android.os.Parcelable; |
Felipe Leme | 3a585a8 | 2017-10-18 15:34:26 -0700 | [diff] [blame] | 24 | import android.util.Log; |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 25 | |
| 26 | import com.android.internal.util.Preconditions; |
| 27 | |
| 28 | /** |
| 29 | * Compound validator that only returns {@code true} on {@link #isValid(ValueFinder)} if all |
| 30 | * of its subvalidators return {@code true} as well. |
| 31 | * |
| 32 | * <p>Used to implement an {@code AND} logical operation. |
| 33 | * |
| 34 | * @hide |
| 35 | */ |
| 36 | final class RequiredValidators extends InternalValidator { |
| 37 | |
Felipe Leme | 3a585a8 | 2017-10-18 15:34:26 -0700 | [diff] [blame] | 38 | private static final String TAG = "RequiredValidators"; |
| 39 | |
Philip P. Moltmann | 0e3e6f8 | 2017-07-11 17:03:49 -0700 | [diff] [blame] | 40 | @NonNull private final InternalValidator[] mValidators; |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 41 | |
| 42 | RequiredValidators(@NonNull InternalValidator[] validators) { |
| 43 | mValidators = Preconditions.checkArrayElementsNotNull(validators, "validators"); |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public boolean isValid(@NonNull ValueFinder finder) { |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 48 | for (InternalValidator validator : mValidators) { |
| 49 | final boolean valid = validator.isValid(finder); |
Felipe Leme | 3a585a8 | 2017-10-18 15:34:26 -0700 | [diff] [blame] | 50 | if (sDebug) Log.d(TAG, "isValid(" + validator + "): " + valid); |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 51 | if (!valid) return false; |
| 52 | } |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | ///////////////////////////////////// |
| 57 | // Object "contract" methods. // |
| 58 | ///////////////////////////////////// |
| 59 | @Override |
| 60 | public String toString() { |
| 61 | if (!sDebug) return super.toString(); |
| 62 | |
| 63 | return new StringBuilder("RequiredValidators: [validators=").append(mValidators) |
| 64 | .append("]") |
| 65 | .toString(); |
| 66 | } |
| 67 | |
| 68 | ///////////////////////////////////// |
| 69 | // Parcelable "contract" methods. // |
| 70 | ///////////////////////////////////// |
| 71 | @Override |
| 72 | public int describeContents() { |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void writeToParcel(Parcel dest, int flags) { |
| 78 | dest.writeParcelableArray(mValidators, flags); |
| 79 | } |
| 80 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame^] | 81 | public static final @android.annotation.NonNull Parcelable.Creator<RequiredValidators> CREATOR = |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 82 | new Parcelable.Creator<RequiredValidators>() { |
| 83 | @Override |
| 84 | public RequiredValidators createFromParcel(Parcel parcel) { |
| 85 | return new RequiredValidators(parcel |
| 86 | .readParcelableArray(null, InternalValidator.class)); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public RequiredValidators[] newArray(int size) { |
| 91 | return new RequiredValidators[size]; |
| 92 | } |
| 93 | }; |
| 94 | } |