/
AutofillUiUtils.java
439 lines (402 loc) · 19 KB
/
AutofillUiUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.autofill;
import android.app.Activity;
import android.content.ComponentCallbacks;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.ColorFilter;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;
import androidx.annotation.IntDef;
import androidx.annotation.VisibleForTesting;
import androidx.core.content.res.ResourcesCompat;
import androidx.core.widget.TextViewCompat;
import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.base.Callback;
import org.chromium.base.ContextUtils;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.feedback.HelpAndFeedbackLauncherImpl;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.ui.text.NoUnderlineClickableSpan;
import org.chromium.ui.text.SpanApplier;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Calendar;
import java.util.LinkedList;
/**
* Helper methods that can be used across multiple Autofill UIs.
*/
public class AutofillUiUtils {
/**
* Interface to provide the horizontal and vertical offset for the tooltip.
*/
public interface OffsetProvider {
/** Returns the X offset for the tooltip. */
int getXOffset(TextView textView);
/** Returns the Y offset for the tooltip. */
int getYOffset(TextView textView);
}
// 200ms is chosen small enough not to be detectable to human eye, but big
// enough for to avoid any race conditions on modern machines.
private static final int TOOLTIP_DEFERRED_PERIOD_MS = 200;
public static final int EXPIRATION_FIELDS_LENGTH = 2;
@IntDef({ErrorType.EXPIRATION_MONTH, ErrorType.EXPIRATION_YEAR, ErrorType.EXPIRATION_DATE,
ErrorType.CVC, ErrorType.CVC_AND_EXPIRATION, ErrorType.NOT_ENOUGH_INFO, ErrorType.NONE})
@Retention(RetentionPolicy.SOURCE)
public @interface ErrorType {
int EXPIRATION_MONTH = 1;
int EXPIRATION_YEAR = 2;
int EXPIRATION_DATE = 3;
int CVC = 4;
int CVC_AND_EXPIRATION = 5;
int NOT_ENOUGH_INFO = 6;
int NONE = 7;
}
// Constants used to log UMA "enum" histograms about the type of links clicked in mobile virtual
// card dialogs. Entries should not be renumbered and numeric values should never be reused.
@IntDef({VirtualCardDialogLink.EDUCATION_TEXT, VirtualCardDialogLink.GOOGLE_LEGAL_MESSAGE,
VirtualCardDialogLink.ISSUER_LEGAL_MESSAGE, VirtualCardDialogLink.NUM_ENTRIES})
@Retention(RetentionPolicy.SOURCE)
public @interface VirtualCardDialogLink {
int EDUCATION_TEXT = 0;
int GOOGLE_LEGAL_MESSAGE = 1;
int ISSUER_LEGAL_MESSAGE = 2;
int NUM_ENTRIES = 3;
}
/**
* Launches the Autofill help page on top of the current @{link android.app.Activity} and
* current @{link Profile}.
*/
public static void launchAutofillHelpPage(Activity activity, Profile profile) {
HelpAndFeedbackLauncherImpl.getInstance().show(
activity, activity.getString(R.string.help_context_autofill), profile, null);
}
/**
* Show Tooltip UI.
*
* @param context Context required to get resources.
* @param popup {@PopupWindow} that shows tooltip UI.
* @param text Text to be shown in tool tip UI.
* @param offsetProvider Interface to provide the X and Y offsets.
* @param anchorView Anchor view under which tooltip popup has to be shown
* @param dismissAction Tooltip dismissive action.
*/
public static void showTooltip(Context context, PopupWindow popup, int text,
OffsetProvider offsetProvider, View anchorView, final Runnable dismissAction) {
TextView textView = new TextView(context);
textView.setText(text);
TextViewCompat.setTextAppearance(
textView, R.style.TextAppearance_TextMedium_Primary_Baseline_Light);
Resources resources = context.getResources();
int hPadding = resources.getDimensionPixelSize(R.dimen.autofill_tooltip_horizontal_padding);
int vPadding = resources.getDimensionPixelSize(R.dimen.autofill_tooltip_vertical_padding);
textView.setPadding(hPadding, vPadding, hPadding, vPadding);
textView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
popup.setContentView(textView);
popup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popup.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
popup.setOutsideTouchable(true);
popup.setBackgroundDrawable(ApiCompatibilityUtils.getDrawable(
resources, R.drawable.store_locally_tooltip_background));
// An alternate solution is to extend TextView and override onConfigurationChanged. However,
// due to lemon compression, onConfigurationChanged never gets called.
final ComponentCallbacks componentCallbacks = new ComponentCallbacks() {
@Override
public void onConfigurationChanged(Configuration configuration) {
// If the popup was already showing dismiss it. This may happen during an
// orientation change.
if (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
&& popup != null) {
popup.dismiss();
}
}
@Override
public void onLowMemory() {}
};
ContextUtils.getApplicationContext().registerComponentCallbacks(componentCallbacks);
popup.setOnDismissListener(() -> {
Handler h = new Handler();
h.postDelayed(dismissAction, TOOLTIP_DEFERRED_PERIOD_MS);
ContextUtils.getApplicationContext().unregisterComponentCallbacks(componentCallbacks);
});
popup.showAsDropDown(anchorView, offsetProvider.getXOffset(textView),
offsetProvider.getYOffset(textView));
textView.announceForAccessibility(textView.getText());
}
/**
* Determines what type of error, if any, is present in the expiration date fields of the
* prompt.
*
* @param monthInput EditText for the month field.
* @param yearInput EditText for the year field.
* @param didFocusOnMonth True if the month field was ever in focus.
* @param didFocusOnYear True if the year field was ever in focus.
* @return The ErrorType value representing the type of error found for the expiration date
* unmask fields.
*/
@ErrorType
public static int getExpirationDateErrorType(EditText monthInput, EditText yearInput,
boolean didFocusOnMonth, boolean didFocusOnYear) {
Calendar calendar = Calendar.getInstance();
int thisYear = calendar.get(Calendar.YEAR);
int thisMonth = calendar.get(Calendar.MONTH) + 1; // calendar month is 0-based
int month = getMonth(monthInput);
if (month == -1) {
if (monthInput.getText().length() == EXPIRATION_FIELDS_LENGTH
|| (!monthInput.isFocused() && didFocusOnMonth)) {
return ErrorType.EXPIRATION_MONTH;
}
// If year was focused before, proceed to check if year is valid.
if (!didFocusOnYear) {
return ErrorType.NOT_ENOUGH_INFO;
}
}
int year = getFourDigitYear(yearInput);
if (year == -1) {
if (yearInput.getText().length() == EXPIRATION_FIELDS_LENGTH
|| (!yearInput.isFocused() && didFocusOnYear)) {
return ErrorType.EXPIRATION_YEAR;
}
return ErrorType.NOT_ENOUGH_INFO;
}
// Year is valid but month is still being edited.
if (month == -1) {
return ErrorType.NOT_ENOUGH_INFO;
}
if (year == thisYear && month < thisMonth) {
return ErrorType.EXPIRATION_DATE;
}
return ErrorType.NONE;
}
/**
* @param yearInput EditText for the year field.
* @return The expiration year the user entered.
* Two digit values (such as 17) will be converted to 4 digit years (such as 2017).
* Returns -1 if the input is empty or otherwise not a valid year (previous year or
* more than 10 years in the future).
*/
public static int getFourDigitYear(EditText yearInput) {
Calendar calendar = Calendar.getInstance();
int thisYear = calendar.get(Calendar.YEAR);
try {
int year = Integer.parseInt(yearInput.getText().toString());
if (year < 0) return -1;
if (year < 100) year += thisYear - thisYear % 100;
if (year < thisYear || year > thisYear + 10) return -1;
return year;
} catch (NumberFormatException e) {
return -1;
}
}
/**
* @param monthInput EditText for the month field.
* @return The expiration month the user entered.
* Returns -1 if not a valid month.
*/
@VisibleForTesting
static int getMonth(EditText monthInput) {
try {
int month = Integer.parseInt(monthInput.getText().toString());
if (month < 1 || month > 12) {
return -1;
}
return month;
} catch (NumberFormatException e) {
return -1;
}
}
/**
* @param context Context required to get resources.
* @param errorType Type of the error used to get the resource string.
* @return Error string retrieved from the string resources.
*/
public static String getErrorMessage(Context context, @ErrorType int errorType) {
Resources resources = context.getResources();
switch (errorType) {
case ErrorType.EXPIRATION_MONTH:
return resources.getString(
R.string.autofill_card_unmask_prompt_error_try_again_expiration_month);
case ErrorType.EXPIRATION_YEAR:
return resources.getString(
R.string.autofill_card_unmask_prompt_error_try_again_expiration_year);
case ErrorType.EXPIRATION_DATE:
return resources.getString(
R.string.autofill_card_unmask_prompt_error_try_again_expiration_date);
case ErrorType.CVC:
return resources.getString(
R.string.autofill_card_unmask_prompt_error_try_again_cvc);
case ErrorType.CVC_AND_EXPIRATION:
return resources.getString(
R.string.autofill_card_unmask_prompt_error_try_again_cvc_and_expiration);
case ErrorType.NONE:
case ErrorType.NOT_ENOUGH_INFO:
default:
return "";
}
}
/**
* Shows (or removes) the appropriate error message and apply the error filter to the
* appropriate fields depending on the error type.
*
* @param errorType The type of error detected.
* @param context Context required to get resources,
* @param errorMessageTextView TextView to display the error message.
*/
public static void showDetailedErrorMessage(
@ErrorType int errorType, Context context, TextView errorMessageTextView) {
switch (errorType) {
case ErrorType.NONE:
case ErrorType.NOT_ENOUGH_INFO:
clearInputError(errorMessageTextView);
break;
default:
String errorMessage = getErrorMessage(context, errorType);
showErrorMessage(errorMessage, errorMessageTextView);
}
}
/**
* Sets the error message on the inputs.
* @param message The error message to show.
* @param errorMessageTextView TextView used to display the error message.
*/
public static void showErrorMessage(String message, TextView errorMessageTextView) {
assert message != null;
// Set the message to display;
errorMessageTextView.setText(message);
errorMessageTextView.setVisibility(View.VISIBLE);
// A null message is passed in during card verification, which also makes an announcement.
// Announcing twice in a row may cancel the first announcement.
errorMessageTextView.announceForAccessibility(message);
}
/**
* Removes the error message on the inputs.
* @param errorMessageTextView TextView used to display the error message.
*/
public static void clearInputError(TextView errorMessageTextView) {
errorMessageTextView.setText(null);
errorMessageTextView.setVisibility(View.GONE);
}
/**
* Applies the error filter to the invalid fields based on the errorType.
*
* @param errorType The ErrorType value representing the type of error found for the unmask
* fields.
* @param context Context required to get resources,
* @param monthInput EditText for the month field.
* @param yearInput EditText for the year field.
* @param cvcInput EditText for the cvc field.
*/
public static void updateColorForInputs(@ErrorType int errorType, Context context,
EditText monthInput, EditText yearInput, EditText cvcInput) {
ColorFilter filter =
new PorterDuffColorFilter(ApiCompatibilityUtils.getColor(context.getResources(),
R.color.input_underline_error_color),
PorterDuff.Mode.SRC_IN);
// Decide on what field(s) to apply the filter.
boolean filterMonth = errorType == ErrorType.EXPIRATION_MONTH
|| errorType == ErrorType.EXPIRATION_DATE
|| errorType == ErrorType.CVC_AND_EXPIRATION;
boolean filterYear = errorType == ErrorType.EXPIRATION_YEAR
|| errorType == ErrorType.EXPIRATION_DATE
|| errorType == ErrorType.CVC_AND_EXPIRATION;
updateColorForInput(monthInput, filterMonth ? filter : null);
updateColorForInput(yearInput, filterYear ? filter : null);
if (cvcInput != null) {
boolean filterCvc =
errorType == ErrorType.CVC || errorType == ErrorType.CVC_AND_EXPIRATION;
updateColorForInput(cvcInput, filterCvc ? filter : null);
}
}
/**
* Sets the stroke color for the given input.
* @param input The input to modify.
* @param filter The color filter to apply to the background.
*/
public static void updateColorForInput(EditText input, ColorFilter filter) {
input.getBackground().mutate().setColorFilter(filter);
}
/**
* Appends the title string with a logo and sets it as the text on the TextView.
*
* @param context The context used for fetching the required resources.
* @param titleTextView The TextView containing the title that the title and the logo should be
* set on.
* @param title The title string for the TextView.
* @param logoResourceId The resource id for the icon to inlined within the title string.
*/
public static void inlineTitleStringWithLogo(
Context context, TextView titleTextView, String title, int logoResourceId) {
Drawable mInlineTitleIcon = ResourcesCompat.getDrawable(
context.getResources(), logoResourceId, context.getTheme());
// The first character will be replaced by the logo, and the consecutive spaces after
// are used as padding.
SpannableString titleWithLogo = new SpannableString(" " + title);
// How much the original logo should scale up in size to match height of text.
float scaleFactor = titleTextView.getTextSize() / mInlineTitleIcon.getIntrinsicHeight();
mInlineTitleIcon.setBounds(
/* left */ 0, /* top */
0,
/* right */ (int) (scaleFactor * mInlineTitleIcon.getIntrinsicWidth()),
/* bottom */ (int) (scaleFactor * mInlineTitleIcon.getIntrinsicHeight()));
titleWithLogo.setSpan(new ImageSpan(mInlineTitleIcon, ImageSpan.ALIGN_CENTER),
/* start */ 0,
/* end */ 1,
/* flags */ Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
titleTextView.setText(titleWithLogo, TextView.BufferType.SPANNABLE);
}
/**
* Generates a SpannableString from a list of {@link LegalMessageLine}.
*
* @param context The context used for fetching the required resources.
* @param legalMessageLines The list of LegalMessageLines to be represented as a string.
* @param onClickCallback The callback for the link clicks.
* @return A {@link SpannableStringBuilder} that can directly be set on a TextView.
*/
public static SpannableStringBuilder getSpannableStringForLegalMessageLines(Context context,
LinkedList<LegalMessageLine> legalMessageLines, Callback<String> onClickCallback) {
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
for (LegalMessageLine line : legalMessageLines) {
SpannableString text = new SpannableString(line.text);
for (final LegalMessageLine.Link link : line.links) {
text.setSpan(new NoUnderlineClickableSpan(context.getResources(),
view -> onClickCallback.onResult(link.url)),
link.start, link.end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
spannableStringBuilder.append(text);
}
return spannableStringBuilder;
}
/**
* Returns a {@link SpannableString} containing a {@link NoUnderlineClickableSpan} for the text
* contained within the tags <link1></link1>.
* @param context The context required to fetch the resources.
* @param stringResourceId The resource id of the string on which the clickable span should be
* applied.
* @param url The url that should be opened when the clickable span is clicked.
* @param onClickCallback The callback for the link clicks.
* @return {@link SpannableString} that can be directly set on the TextView.
*/
public static SpannableString getSpannableStringWithClickableSpansToOpenLinksInCustomTabs(
Context context, int stringResourceId, String url, Callback<String> onClickCallback) {
return SpanApplier.applySpans(context.getString(stringResourceId),
new SpanApplier.SpanInfo("<link1>", "</link1>",
new NoUnderlineClickableSpan(
context.getResources(), view -> onClickCallback.onResult(url))));
}
}