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 package android.ext.services.autofill; 17 18 import android.os.Bundle; 19 import android.view.autofill.AutofillValue; 20 21 import androidx.annotation.Nullable; 22 import androidx.annotation.VisibleForTesting; 23 24 final class CreditCardMatcher { 25 26 /** 27 * Required arg for {@link #calculateScore} that provides the min and max lengths for 28 * credit card number. 29 * 30 * <p>must be non-negative and less than or equal to 31 * {@link #REQUIRED_ARG_MAX_CREDIT_CARD_LENGTH}.</p> 32 * 33 * <p>Must supply an int.</p> 34 */ 35 public static final String REQUIRED_ARG_MIN_CREDIT_CARD_LENGTH = "REQUIRED_ARG_MIN_CC_LENGTH"; 36 37 /** 38 * Required arg for {@link #calculateScore} that provides the max length for 39 * credit card number. 40 * 41 * <p>must be non-negative and greater or equal to 42 * {@link #REQUIRED_ARG_MIN_CREDIT_CARD_LENGTH}.</p> 43 * 44 * <p>Must supply an int.</p> 45 */ 46 public static final String REQUIRED_ARG_MAX_CREDIT_CARD_LENGTH = "REQUIRED_ARG_MAX_CC_LENGTH"; 47 48 /** 49 * Optional arg for {@link #calculateScore} that provides the suffix length e.g. last N digits 50 * of the credit card number. 51 * 52 * <p>If argument is not provided, the suffix length defaults to 4.</p> 53 * 54 * <p>Must supply an int.</p> 55 */ 56 public static final String OPTIONAL_ARG_SUFFIX_LENGTH = "OPTIONAL_ARG_SUFFIX_LENGTH"; 57 58 /** 59 * Gets the field classification score of a credit card number string and a string representing 60 * the last four digits of the credit card. 61 * 62 * @return {@code 1.0} if the last four digits of the credit card matches with the given four 63 * digits, and the length of the credit card number is within the given requirements, 64 * {@code 0.0} otherwise. 65 * 66 * @param actualValue credit card number. 67 * @param userDataValue four digit string. 68 */ 69 @VisibleForTesting calculateScore(@ullable AutofillValue actualValue, @Nullable String userDataValue, @Nullable Bundle args)70 static float calculateScore(@Nullable AutofillValue actualValue, 71 @Nullable String userDataValue, @Nullable Bundle args) { 72 if (actualValue == null || !actualValue.isText() || userDataValue == null || args == null) { 73 return 0; 74 } 75 final String actualValueText = actualValue.getTextValue().toString(); 76 77 final int minCreditCardLength = args.getInt(REQUIRED_ARG_MIN_CREDIT_CARD_LENGTH, -1); 78 final int maxCreditCardLength = args.getInt(REQUIRED_ARG_MAX_CREDIT_CARD_LENGTH, -1); 79 if (minCreditCardLength < 0 || maxCreditCardLength < minCreditCardLength) { 80 throw new IllegalArgumentException("bad length arguments"); 81 } 82 83 final int actualValueLength = actualValueText.length(); 84 final int userDataLength = userDataValue.length(); 85 final int suffixLength = args.getInt(OPTIONAL_ARG_SUFFIX_LENGTH, 4); 86 if (suffixLength <= 0) { 87 throw new IllegalArgumentException("bad suffix length argument"); 88 } 89 90 // Satisfies length checks. 91 if (actualValueLength != suffixLength || userDataLength < minCreditCardLength 92 || userDataLength > maxCreditCardLength || userDataLength < actualValueLength) { 93 return 0; 94 } 95 final String userDataLastN = userDataValue.substring( 96 userDataLength - suffixLength); 97 98 // Last 4 digits are equal. 99 return actualValueText.equalsIgnoreCase(userDataLastN) ? 1 : 0; 100 } 101 } 102