blob: c56ae84b4ae3cbe78eb36da36512f5ad77776176 [file] [log] [blame]
Felipe Leme979013d2017-06-22 10:59:23 -07001/*
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
17package android.service.autofill;
18
19import static android.view.autofill.Helper.sDebug;
20
21import android.annotation.NonNull;
Philip P. Moltmann4e5ce392017-07-11 09:14:47 -070022import android.annotation.TestApi;
Felipe Leme979013d2017-06-22 10:59:23 -070023import android.os.Parcel;
24import android.os.Parcelable;
Felipe Leme979013d2017-06-22 10:59:23 -070025import android.util.Log;
26import android.view.autofill.AutofillId;
27
28import com.android.internal.util.Preconditions;
29
Felipe Leme3a585a82017-10-18 15:34:26 -070030import java.util.Arrays;
31
Felipe Leme979013d2017-06-22 10:59:23 -070032/**
33 * Validator that returns {@code true} if the number created by concatenating all given fields
Philip P. Moltmann4e5ce392017-07-11 09:14:47 -070034 * pass a Luhn algorithm checksum. All non-digits are ignored.
Felipe Leme979013d2017-06-22 10:59:23 -070035 *
36 * <p>See {@link SaveInfo.Builder#setValidator(Validator)} for examples.
37 */
Philip P. Moltmann0e3e6f82017-07-11 17:03:49 -070038public final class LuhnChecksumValidator extends InternalValidator implements Validator,
39 Parcelable {
Felipe Leme979013d2017-06-22 10:59:23 -070040 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. Moltmann4e5ce392017-07-11 09:14:47 -070053 /**
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 Leme979013d2017-06-22 10:59:23 -070085 /** @hide */
86 @Override
Philip P. Moltmann4e5ce392017-07-11 09:14:47 -070087 @TestApi
Felipe Leme979013d2017-06-22 10:59:23 -070088 public boolean isValid(@NonNull ValueFinder finder) {
89 if (mIds == null || mIds.length == 0) return false;
90
Felipe Leme3a585a82017-10-18 15:34:26 -070091 final StringBuilder builder = new StringBuilder();
Felipe Leme979013d2017-06-22 10:59:23 -070092 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 Leme3a585a82017-10-18 15:34:26 -070098 builder.append(partialNumber);
Felipe Leme979013d2017-06-22 10:59:23 -070099 }
Philip P. Moltmann4e5ce392017-07-11 09:14:47 -0700100
Felipe Leme3a585a82017-10-18 15:34:26 -0700101 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 Leme979013d2017-06-22 10:59:23 -0700112 }
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
127 public static final Parcelable.Creator<LuhnChecksumValidator> CREATOR =
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}