1 /* 2 * Copyright (C) 2018 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; 22 import android.annotation.Nullable; 23 import android.annotation.TestApi; 24 import android.icu.text.DateFormat; 25 import android.os.Parcel; 26 import android.os.Parcelable; 27 import android.util.Log; 28 import android.view.autofill.AutofillValue; 29 30 import java.util.Date; 31 import java.util.Objects; 32 33 /** 34 * Sanitizes a date {@link AutofillValue} using a {@link DateFormat}. 35 * 36 * <p>For example, to sanitize a credit card expiration date to just its month and year: 37 * 38 * <pre class="prettyprint"> 39 * new DateValueSanitizer(new java.text.SimpleDateFormat("MM/yyyy"); 40 * </pre> 41 */ 42 public final class DateValueSanitizer extends InternalSanitizer implements Sanitizer, Parcelable { 43 44 private static final String TAG = "DateValueSanitizer"; 45 46 private final DateFormat mDateFormat; 47 48 /** 49 * Default constructor. 50 * 51 * @param dateFormat date format applied to the actual date value of an input field. 52 */ DateValueSanitizer(@onNull DateFormat dateFormat)53 public DateValueSanitizer(@NonNull DateFormat dateFormat) { 54 mDateFormat = Objects.requireNonNull(dateFormat); 55 } 56 57 /** @hide */ 58 @Override 59 @TestApi 60 @Nullable sanitize(@onNull AutofillValue value)61 public AutofillValue sanitize(@NonNull AutofillValue value) { 62 if (value == null) { 63 Log.w(TAG, "sanitize() called with null value"); 64 return null; 65 } 66 if (!value.isDate()) { 67 if (sDebug) Log.d(TAG, value + " is not a date"); 68 return null; 69 } 70 71 try { 72 final Date date = new Date(value.getDateValue()); 73 74 // First convert it to string 75 final String converted = mDateFormat.format(date); 76 if (sDebug) Log.d(TAG, "Transformed " + date + " to " + converted); 77 // Then parse it back to date 78 final Date sanitized = mDateFormat.parse(converted); 79 if (sDebug) Log.d(TAG, "Sanitized to " + sanitized); 80 return AutofillValue.forDate(sanitized.getTime()); 81 } catch (Exception e) { 82 Log.w(TAG, "Could not apply " + mDateFormat + " to " + value + ": " + e); 83 return null; 84 } 85 } 86 87 ///////////////////////////////////// 88 // Object "contract" methods. // 89 ///////////////////////////////////// 90 @Override toString()91 public String toString() { 92 if (!sDebug) return super.toString(); 93 94 return "DateValueSanitizer: [dateFormat=" + mDateFormat + "]"; 95 } 96 97 ///////////////////////////////////// 98 // Parcelable "contract" methods. // 99 ///////////////////////////////////// 100 @Override describeContents()101 public int describeContents() { 102 return 0; 103 } 104 105 @Override writeToParcel(Parcel parcel, int flags)106 public void writeToParcel(Parcel parcel, int flags) { 107 parcel.writeSerializable(mDateFormat); 108 } 109 110 public static final @android.annotation.NonNull Parcelable.Creator<DateValueSanitizer> CREATOR = 111 new Parcelable.Creator<DateValueSanitizer>() { 112 @Override 113 public DateValueSanitizer createFromParcel(Parcel parcel) { 114 return new DateValueSanitizer((DateFormat) parcel.readSerializable(android.icu.text.DateFormat.class.getClassLoader(), android.icu.text.DateFormat.class)); 115 } 116 117 @Override 118 public DateValueSanitizer[] newArray(int size) { 119 return new DateValueSanitizer[size]; 120 } 121 }; 122 } 123