blob: a8c080a178187779cd3a90b92dde8f8be03b7429 [file] [log] [blame]
Felipe Lemecd2969c2017-10-05 08:56:04 -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;
Felipe Lemef5059c32017-11-17 13:54:55 -080022import android.annotation.Nullable;
Felipe Lemecd2969c2017-10-05 08:56:04 -070023import android.annotation.TestApi;
24import android.os.Parcel;
25import android.os.Parcelable;
26import android.util.Slog;
27import android.view.autofill.AutofillValue;
28
29import com.android.internal.util.Preconditions;
30
31import java.util.regex.Matcher;
32import java.util.regex.Pattern;
33
34/**
35 * Sanitizes a text {@link AutofillValue} using a regular expression (regex) substitution.
36 *
37 * <p>For example, to remove spaces from groups of 4-digits in a credit card:
38 *
39 * <pre class="prettyprint">
Felipe Lemef60fb122018-04-26 13:51:14 -070040 * new TextValueSanitizer(Pattern.compile("^(\\d{4})\\s?(\\d{4})\\s?(\\d{4})\\s?(\\d{4})$"),
Felipe Leme601d2202017-11-17 08:21:23 -080041 * "$1$2$3$4")
Felipe Lemecd2969c2017-10-05 08:56:04 -070042 * </pre>
43 */
44public final class TextValueSanitizer extends InternalSanitizer implements
45 Sanitizer, Parcelable {
46 private static final String TAG = "TextValueSanitizer";
47
48 private final Pattern mRegex;
49 private final String mSubst;
50
51 /**
52 * Default constructor.
53 *
54 * @param regex regular expression with groups (delimited by {@code (} and {@code (}) that
55 * are used to substitute parts of the {@link AutofillValue#getTextValue() text value}.
56 * @param subst the string that substitutes the matched regex, using {@code $} for
57 * group substitution ({@code $1} for 1st group match, {@code $2} for 2nd, etc).
58 */
59 public TextValueSanitizer(@NonNull Pattern regex, @NonNull String subst) {
60 mRegex = Preconditions.checkNotNull(regex);
61 mSubst = Preconditions.checkNotNull(subst);
62 }
63
64 /** @hide */
65 @Override
66 @TestApi
Felipe Lemef5059c32017-11-17 13:54:55 -080067 @Nullable
Felipe Lemecd2969c2017-10-05 08:56:04 -070068 public AutofillValue sanitize(@NonNull AutofillValue value) {
69 if (value == null) {
70 Slog.w(TAG, "sanitize() called with null value");
71 return null;
72 }
Felipe Lemef5059c32017-11-17 13:54:55 -080073 if (!value.isText()) {
74 if (sDebug) Slog.d(TAG, "sanitize() called with non-text value: " + value);
75 return null;
76 }
Felipe Lemecd2969c2017-10-05 08:56:04 -070077
78 final CharSequence text = value.getTextValue();
79
80 try {
81 final Matcher matcher = mRegex.matcher(text);
Felipe Lemef5059c32017-11-17 13:54:55 -080082 if (!matcher.matches()) {
83 if (sDebug) Slog.d(TAG, "sanitize(): " + mRegex + " failed for " + value);
84 return null;
85 }
Felipe Lemecd2969c2017-10-05 08:56:04 -070086
87 final CharSequence sanitized = matcher.replaceAll(mSubst);
88 return AutofillValue.forText(sanitized);
89 } catch (Exception e) {
90 Slog.w(TAG, "Exception evaluating " + mRegex + "/" + mSubst + ": " + e);
Felipe Lemef5059c32017-11-17 13:54:55 -080091 return null;
Felipe Lemecd2969c2017-10-05 08:56:04 -070092 }
93 }
94
95 /////////////////////////////////////
96 // Object "contract" methods. //
97 /////////////////////////////////////
98 @Override
99 public String toString() {
100 if (!sDebug) return super.toString();
101
102 return "TextValueSanitizer: [regex=" + mRegex + ", subst=" + mSubst + "]";
103 }
104
105 /////////////////////////////////////
106 // Parcelable "contract" methods. //
107 /////////////////////////////////////
108 @Override
109 public int describeContents() {
110 return 0;
111 }
112
113 @Override
114 public void writeToParcel(Parcel parcel, int flags) {
115 parcel.writeSerializable(mRegex);
116 parcel.writeString(mSubst);
117 }
118
119 public static final Parcelable.Creator<TextValueSanitizer> CREATOR =
120 new Parcelable.Creator<TextValueSanitizer>() {
121 @Override
122 public TextValueSanitizer createFromParcel(Parcel parcel) {
123 return new TextValueSanitizer((Pattern) parcel.readSerializable(), parcel.readString());
124 }
125
126 @Override
127 public TextValueSanitizer[] newArray(int size) {
128 return new TextValueSanitizer[size];
129 }
130 };
131}