Home | History | Annotate | Download | only in autofill
      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;
     24 import android.util.Log;
     25 
     26 import com.android.internal.util.Preconditions;
     27 
     28 /**
     29  * Compound validator that returns {@code true} on {@link #isValid(ValueFinder)} if any
     30  * of its subvalidators returns {@code true} as well.
     31  *
     32  * <p>Used to implement an {@code OR} logical operation.
     33  *
     34  * @hide
     35  */
     36 final class OptionalValidators extends InternalValidator {
     37 
     38     private static final String TAG = "OptionalValidators";
     39 
     40     @NonNull private final InternalValidator[] mValidators;
     41 
     42     OptionalValidators(@NonNull InternalValidator[] validators) {
     43         mValidators = Preconditions.checkArrayElementsNotNull(validators, "validators");
     44     }
     45 
     46     @Override
     47     public boolean isValid(@NonNull ValueFinder finder) {
     48         for (InternalValidator validator : mValidators) {
     49             final boolean valid = validator.isValid(finder);
     50             if (sDebug) Log.d(TAG, "isValid(" + validator + "): " + valid);
     51             if (valid) return true;
     52         }
     53 
     54         return false;
     55     }
     56 
     57     /////////////////////////////////////
     58     // Object "contract" methods. //
     59     /////////////////////////////////////
     60     @Override
     61     public String toString() {
     62         if (!sDebug) return super.toString();
     63 
     64         return new StringBuilder("OptionalValidators: [validators=").append(mValidators)
     65                 .append("]")
     66                 .toString();
     67     }
     68 
     69     /////////////////////////////////////
     70     // Parcelable "contract" methods. //
     71     /////////////////////////////////////
     72     @Override
     73     public int describeContents() {
     74         return 0;
     75     }
     76 
     77     @Override
     78     public void writeToParcel(Parcel dest, int flags) {
     79         dest.writeParcelableArray(mValidators, flags);
     80     }
     81 
     82     public static final Parcelable.Creator<OptionalValidators> CREATOR =
     83             new Parcelable.Creator<OptionalValidators>() {
     84         @Override
     85         public OptionalValidators createFromParcel(Parcel parcel) {
     86             return new OptionalValidators(parcel
     87                 .readParcelableArray(null, InternalValidator.class));
     88         }
     89 
     90         @Override
     91         public OptionalValidators[] newArray(int size) {
     92             return new OptionalValidators[size];
     93         }
     94     };
     95 }
     96