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; |
Philip P. Moltmann | 4e5ce39 | 2017-07-11 09:14:47 -0700 | [diff] [blame] | 22 | import android.annotation.TestApi; |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 25 | import android.util.Log; |
| 26 | import android.view.autofill.AutofillId; |
| 27 | |
| 28 | import com.android.internal.util.Preconditions; |
| 29 | |
Felipe Leme | 3a585a8 | 2017-10-18 15:34:26 -0700 | [diff] [blame] | 30 | import java.util.Arrays; |
| 31 | |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Validator that returns {@code true} if the number created by concatenating all given fields |
Philip P. Moltmann | 4e5ce39 | 2017-07-11 09:14:47 -0700 | [diff] [blame] | 34 | * pass a Luhn algorithm checksum. All non-digits are ignored. |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 35 | * |
| 36 | * <p>See {@link SaveInfo.Builder#setValidator(Validator)} for examples. |
| 37 | */ |
Philip P. Moltmann | 0e3e6f8 | 2017-07-11 17:03:49 -0700 | [diff] [blame] | 38 | public final class LuhnChecksumValidator extends InternalValidator implements Validator, |
| 39 | Parcelable { |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 40 | private static final String TAG = "LuhnChecksumValidator"; |
| 41 | |
| 42 | private final AutofillId[] mIds; |
| 43 | |
| 44 | /** |
| 45 | * Default constructor. |
| 46 | * |
| 47 | * @param ids id of fields that comprises the number to be checked. |
| 48 | */ |
| 49 | public LuhnChecksumValidator(@NonNull AutofillId... ids) { |
| 50 | mIds = Preconditions.checkArrayElementsNotNull(ids, "ids"); |
| 51 | } |
| 52 | |
Philip P. Moltmann | 4e5ce39 | 2017-07-11 09:14:47 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Checks if the Luhn checksum is valid. |
| 55 | * |
| 56 | * @param number The number including the checksum |
| 57 | */ |
| 58 | private static boolean isLuhnChecksumValid(@NonNull String number) { |
| 59 | int sum = 0; |
| 60 | boolean isDoubled = false; |
| 61 | |
| 62 | for (int i = number.length() - 1; i >= 0; i--) { |
| 63 | final int digit = number.charAt(i) - '0'; |
| 64 | if (digit < 0 || digit > 9) { |
| 65 | // Ignore non-digits |
| 66 | continue; |
| 67 | } |
| 68 | |
| 69 | int addend; |
| 70 | if (isDoubled) { |
| 71 | addend = digit * 2; |
| 72 | if (addend > 9) { |
| 73 | addend -= 9; |
| 74 | } |
| 75 | } else { |
| 76 | addend = digit; |
| 77 | } |
| 78 | sum += addend; |
| 79 | isDoubled = !isDoubled; |
| 80 | } |
| 81 | |
| 82 | return sum % 10 == 0; |
| 83 | } |
| 84 | |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 85 | /** @hide */ |
| 86 | @Override |
Philip P. Moltmann | 4e5ce39 | 2017-07-11 09:14:47 -0700 | [diff] [blame] | 87 | @TestApi |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 88 | public boolean isValid(@NonNull ValueFinder finder) { |
| 89 | if (mIds == null || mIds.length == 0) return false; |
| 90 | |
Felipe Leme | 3a585a8 | 2017-10-18 15:34:26 -0700 | [diff] [blame] | 91 | final StringBuilder builder = new StringBuilder(); |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 92 | for (AutofillId id : mIds) { |
| 93 | final String partialNumber = finder.findByAutofillId(id); |
| 94 | if (partialNumber == null) { |
| 95 | if (sDebug) Log.d(TAG, "No partial number for id " + id); |
| 96 | return false; |
| 97 | } |
Felipe Leme | 3a585a8 | 2017-10-18 15:34:26 -0700 | [diff] [blame] | 98 | builder.append(partialNumber); |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 99 | } |
Philip P. Moltmann | 4e5ce39 | 2017-07-11 09:14:47 -0700 | [diff] [blame] | 100 | |
Felipe Leme | 3a585a8 | 2017-10-18 15:34:26 -0700 | [diff] [blame] | 101 | final String number = builder.toString(); |
| 102 | boolean valid = isLuhnChecksumValid(number); |
| 103 | if (sDebug) Log.d(TAG, "isValid(" + number.length() + " chars): " + valid); |
| 104 | return valid; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public String toString() { |
| 109 | if (!sDebug) return super.toString(); |
| 110 | |
| 111 | return "LuhnChecksumValidator: [ids=" + Arrays.toString(mIds) + "]"; |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | ///////////////////////////////////// |
| 115 | // Parcelable "contract" methods. // |
| 116 | ///////////////////////////////////// |
| 117 | @Override |
| 118 | public int describeContents() { |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void writeToParcel(Parcel parcel, int flags) { |
| 124 | parcel.writeParcelableArray(mIds, flags); |
| 125 | } |
| 126 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame^] | 127 | public static final @android.annotation.NonNull Parcelable.Creator<LuhnChecksumValidator> CREATOR = |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 128 | new Parcelable.Creator<LuhnChecksumValidator>() { |
| 129 | @Override |
| 130 | public LuhnChecksumValidator createFromParcel(Parcel parcel) { |
| 131 | return new LuhnChecksumValidator(parcel.readParcelableArray(null, AutofillId.class)); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public LuhnChecksumValidator[] newArray(int size) { |
| 136 | return new LuhnChecksumValidator[size]; |
| 137 | } |
| 138 | }; |
| 139 | } |