From 27d0446d088df09493e279c61966cc93474ddbdb Mon Sep 17 00:00:00 2001 From: Felipe Leme Date: Wed, 14 Feb 2018 15:08:58 -0800 Subject: [PATCH] Optimization: don't call AFM.notifyValueChanged() when value didn't change. Test: manual verification Test: atest CtsAutoFillServiceTestCases Bug: 73078981 Change-Id: Ib0a40d4ab0e6f4a1300622fa4d7a2f6026aa98a1 --- core/java/android/widget/RadioGroup.java | 10 +++++++--- core/java/android/widget/TextView.java | 24 ++++++++++++++++++++---- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/core/java/android/widget/RadioGroup.java b/core/java/android/widget/RadioGroup.java index 5c4d4d2a7fa00..c987147997429 100644 --- a/core/java/android/widget/RadioGroup.java +++ b/core/java/android/widget/RadioGroup.java @@ -183,13 +183,17 @@ public class RadioGroup extends LinearLayout { } private void setCheckedId(@IdRes int id) { + boolean changed = id != mCheckedId; mCheckedId = id; + if (mOnCheckedChangeListener != null) { mOnCheckedChangeListener.onCheckedChanged(this, mCheckedId); } - final AutofillManager afm = mContext.getSystemService(AutofillManager.class); - if (afm != null) { - afm.notifyValueChanged(this); + if (changed) { + final AutofillManager afm = mContext.getSystemService(AutofillManager.class); + if (afm != null) { + afm.notifyValueChanged(this); + } } } diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 5710db3ce8e0e..1e02c3062d97c 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -791,11 +791,18 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener // mAutoSizeStepGranularityInPx. private boolean mHasPresetAutoSizeValues = false; + // Autofill-related attributes + // // Indicates whether the text was set statically or dynamically, so it can be used to // sanitize autofill requests. private boolean mTextSetFromXmlOrResourceId = false; - // Resource id used to set the text - used for autofill purposes. + // Resource id used to set the text. private @StringRes int mTextId = ResourceId.ID_NULL; + // Last value used on AFM.notifyValueChanged(), used to optimize autofill workflow by avoiding + // calls when the value did not change + private CharSequence mLastValueSentToAutofillManager; + // + // End of autofill-related attributes /** * Kick-start the font cache for the zygote process (to pay the cost of @@ -5665,7 +5672,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener if (needEditableForNotification) { sendAfterTextChanged((Editable) text); } else { - // Always notify AutoFillManager - it will return right away if autofill is disabled. notifyAutoFillManagerAfterTextChangedIfNeeded(); } @@ -9697,11 +9703,21 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener return; } final AutofillManager afm = mContext.getSystemService(AutofillManager.class); - if (afm != null) { + if (afm == null) { + return; + } + + if (mLastValueSentToAutofillManager == null + || !mLastValueSentToAutofillManager.equals(mText)) { if (android.view.autofill.Helper.sVerbose) { - Log.v(LOG_TAG, "sendAfterTextChanged(): notify AFM for text=" + mText); + Log.v(LOG_TAG, "notifying AFM after text changed"); } afm.notifyValueChanged(TextView.this); + mLastValueSentToAutofillManager = mText; + } else { + if (android.view.autofill.Helper.sVerbose) { + Log.v(LOG_TAG, "not notifying AFM on unchanged text"); + } } }