| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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.annotation.Nullable; |
| 23 |
import android.annotation.TestApi; |
| 24 |
import android.os.Parcel; |
| 25 |
import android.os.Parcelable; |
| 26 |
import android.util.Slog; |
| 27 |
import android.view.autofill.AutofillValue; |
| 28 |
|
| 29 |
import java.util.Objects; |
| 30 |
import java.util.regex.Matcher; |
| 31 |
import java.util.regex.Pattern; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public final class TextValueSanitizer extends InternalSanitizer implements |
| 44 |
Sanitizer, Parcelable { |
| 45 |
private static final String TAG = "TextValueSanitizer"; |
| 46 |
|
| 47 |
private final Pattern mRegex; |
| 48 |
private final String mSubst; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public TextValueSanitizer(@NonNull Pattern regex, @NonNull String subst) { |
| 59 |
mRegex = Objects.requireNonNull(regex); |
| 60 |
mSubst = Objects.requireNonNull(subst); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
@Override |
| 65 |
@TestApi |
| 66 |
@Nullable |
| 67 |
public AutofillValue sanitize(@NonNull AutofillValue value) { |
| 68 |
if (value == null) { |
| 69 |
Slog.w(TAG, "sanitize() called with null value"); |
| 70 |
return null; |
| 71 |
} |
| 72 |
if (!value.isText()) { |
| 73 |
if (sDebug) Slog.d(TAG, "sanitize() called with non-text value: " + value); |
| 74 |
return null; |
| 75 |
} |
| 76 |
|
| 77 |
final CharSequence text = value.getTextValue(); |
| 78 |
|
| 79 |
try { |
| 80 |
final Matcher matcher = mRegex.matcher(text); |
| 81 |
if (!matcher.matches()) { |
| 82 |
if (sDebug) Slog.d(TAG, "sanitize(): " + mRegex + " failed for " + value); |
| 83 |
return null; |
| 84 |
} |
| 85 |
|
| 86 |
final CharSequence sanitized = matcher.replaceAll(mSubst); |
| 87 |
return AutofillValue.forText(sanitized); |
| 88 |
} catch (Exception e) { |
| 89 |
Slog.w(TAG, "Exception evaluating " + mRegex + "/" + mSubst + ": " + e); |
| 90 |
return null; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
@Override |
| 98 |
public String toString() { |
| 99 |
if (!sDebug) return super.toString(); |
| 100 |
|
| 101 |
return "TextValueSanitizer: [regex=" + mRegex + ", subst=" + mSubst + "]"; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
@Override |
| 108 |
public int describeContents() { |
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public void writeToParcel(Parcel parcel, int flags) { |
| 114 |
parcel.writeSerializable(mRegex); |
| 115 |
parcel.writeString(mSubst); |
| 116 |
} |
| 117 |
|
| 118 |
public static final @android.annotation.NonNull Parcelable.Creator<TextValueSanitizer> CREATOR = |
| 119 |
new Parcelable.Creator<TextValueSanitizer>() { |
| 120 |
@Override |
| 121 |
public TextValueSanitizer createFromParcel(Parcel parcel) { |
| 122 |
return new TextValueSanitizer((Pattern) parcel.readSerializable(java.util.regex.Pattern.class.getClassLoader(), java.util.regex.Pattern.class), parcel.readString()); |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public TextValueSanitizer[] newArray(int size) { |
| 127 |
return new TextValueSanitizer[size]; |
| 128 |
} |
| 129 |
}; |
| 130 |
} |