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.TestApi; 23 import android.icu.text.DateFormat; 24 import android.os.Parcel; 25 import android.os.Parcelable; 26 import android.util.Log; 27 import android.view.autofill.AutofillId; 28 import android.view.autofill.AutofillValue; 29 import android.widget.RemoteViews; 30 import android.widget.TextView; 31 32 import com.android.internal.util.Preconditions; 33 34 import java.util.Date; 35 36 /** 37 * Replaces a {@link TextView} child of a {@link CustomDescription} with the contents of a field 38 * that is expected to have a {@link AutofillValue#forDate(long) date value}. 39 * 40 * <p>For example, a transformation to display a credit card expiration date as month/year would be: 41 * 42 * <pre class="prettyprint"> 43 * new DateTransformation(ccExpDate, new java.text.SimpleDateFormat("MM/yyyy") 44 * </pre> 45 */ 46 public final class DateTransformation extends InternalTransformation implements 47 Transformation, Parcelable { 48 private static final String TAG = "DateTransformation"; 49 50 private final AutofillId mFieldId; 51 private final DateFormat mDateFormat; 52 53 /** 54 * Creates a new transformation. 55 * 56 * @param id id of the screen field. 57 * @param dateFormat object used to transform the date value of the field to a String. 58 */ DateTransformation(@onNull AutofillId id, @NonNull DateFormat dateFormat)59 public DateTransformation(@NonNull AutofillId id, @NonNull DateFormat dateFormat) { 60 mFieldId = Preconditions.checkNotNull(id); 61 mDateFormat = Preconditions.checkNotNull(dateFormat); 62 } 63 64 /** @hide */ 65 @Override 66 @TestApi apply(@onNull ValueFinder finder, @NonNull RemoteViews parentTemplate, int childViewId)67 public void apply(@NonNull ValueFinder finder, @NonNull RemoteViews parentTemplate, 68 int childViewId) throws Exception { 69 final AutofillValue value = finder.findRawValueByAutofillId(mFieldId); 70 if (value == null) { 71 Log.w(TAG, "No value for id " + mFieldId); 72 return; 73 } 74 if (!value.isDate()) { 75 Log.w(TAG, "Value for " + mFieldId + " is not date: " + value); 76 return; 77 } 78 79 try { 80 final Date date = new Date(value.getDateValue()); 81 final String transformed = mDateFormat.format(date); 82 if (sDebug) Log.d(TAG, "Transformed " + date + " to " + transformed); 83 84 parentTemplate.setCharSequence(childViewId, "setText", transformed); 85 } catch (Exception e) { 86 Log.w(TAG, "Could not apply " + mDateFormat + " to " + value + ": " + e); 87 } 88 } 89 90 ///////////////////////////////////// 91 // Object "contract" methods. // 92 ///////////////////////////////////// 93 @Override toString()94 public String toString() { 95 if (!sDebug) return super.toString(); 96 97 return "DateTransformation: [id=" + mFieldId + ", format=" + mDateFormat + "]"; 98 } 99 100 ///////////////////////////////////// 101 // Parcelable "contract" methods. // 102 ///////////////////////////////////// 103 @Override describeContents()104 public int describeContents() { 105 return 0; 106 } 107 108 @Override writeToParcel(Parcel parcel, int flags)109 public void writeToParcel(Parcel parcel, int flags) { 110 parcel.writeParcelable(mFieldId, flags); 111 parcel.writeSerializable(mDateFormat); 112 } 113 114 public static final @android.annotation.NonNull Parcelable.Creator<DateTransformation> CREATOR = 115 new Parcelable.Creator<DateTransformation>() { 116 @Override 117 public DateTransformation createFromParcel(Parcel parcel) { 118 return new DateTransformation(parcel.readParcelable(null), 119 (DateFormat) parcel.readSerializable()); 120 } 121 122 @Override 123 public DateTransformation[] newArray(int size) { 124 return new DateTransformation[size]; 125 } 126 }; 127 } 128