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