| 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.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 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 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 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public DateValueSanitizer(@NonNull DateFormat dateFormat) { |
| 54 |
mDateFormat = Objects.requireNonNull(dateFormat); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
@Override |
| 59 |
@TestApi |
| 60 |
@Nullable |
| 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 |
|
| 75 |
final String converted = mDateFormat.format(date); |
| 76 |
if (sDebug) Log.d(TAG, "Transformed " + date + " to " + converted); |
| 77 |
|
| 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 |
|
| 89 |
|
| 90 |
@Override |
| 91 |
public String toString() { |
| 92 |
if (!sDebug) return super.toString(); |
| 93 |
|
| 94 |
return "DateValueSanitizer: [dateFormat=" + mDateFormat + "]"; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
@Override |
| 101 |
public int describeContents() { |
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 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 |
} |