blob: 338ba749eb794506200a48620ca41ed36d3f3609 [file] [log] [blame]
Felipe Lemec8c0a822018-01-26 15:51:56 -08001/*
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
17package android.service.autofill;
18
19import static android.view.autofill.Helper.sDebug;
20
21import android.annotation.NonNull;
22import android.annotation.TestApi;
Felipe Leme27206d72018-02-14 18:03:32 -080023import android.icu.text.DateFormat;
Felipe Lemec8c0a822018-01-26 15:51:56 -080024import android.os.Parcel;
25import android.os.Parcelable;
26import android.util.Log;
27import android.view.autofill.AutofillId;
28import android.view.autofill.AutofillValue;
29import android.widget.RemoteViews;
30import android.widget.TextView;
31
32import com.android.internal.util.Preconditions;
33
Felipe Lemec8c0a822018-01-26 15:51:56 -080034import 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 */
46public 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 */
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
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
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
104 public int describeContents() {
105 return 0;
106 }
107
108 @Override
109 public void writeToParcel(Parcel parcel, int flags) {
110 parcel.writeParcelable(mFieldId, flags);
111 parcel.writeSerializable(mDateFormat);
112 }
113
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700114 public static final @android.annotation.NonNull Parcelable.Creator<DateTransformation> CREATOR =
Felipe Lemec8c0a822018-01-26 15:51:56 -0800115 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}