From 08abd46f618357e987502cf805296ddfcb09a3de Mon Sep 17 00:00:00 2001 From: Feng Cao Date: Mon, 20 Apr 2020 17:47:27 -0700 Subject: [PATCH] Make autofill requested by augmented autofill service non-manual * The use case of this API was for augmented autofill service to send updated suggestions * Before this change, the dynamic autofill request by the augmented autofill service only triggers a manual request, but this has caused some regular autofill providers to always some suggestion due to their special handling for the manual request. Thus the augmented autofill service will not receive the request. * With this cahnge, the request cancels the previous session to start a new session, and also it triggers a regular request (non-manual) so the autofill provider will not special handle the request. Test: atest CtsAutoFillServiceTestCases Bug: 154543563 Change-Id: I233125a6070394a102ad40b9a50b98a43d952b9f --- .../augmented/AugmentedAutofillService.java | 12 ++++++---- .../view/autofill/AutofillManager.java | 23 +++++++++++++++++-- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/core/java/android/service/autofill/augmented/AugmentedAutofillService.java b/core/java/android/service/autofill/augmented/AugmentedAutofillService.java index cca45f5724893..c2234bad3803b 100644 --- a/core/java/android/service/autofill/augmented/AugmentedAutofillService.java +++ b/core/java/android/service/autofill/augmented/AugmentedAutofillService.java @@ -163,14 +163,18 @@ public abstract class AugmentedAutofillService extends Service { } /** - * The child class of the service can call this method to initiate an Autofill flow. + * The child class of the service can call this method to initiate a new Autofill flow. If all + * conditions are met, it will make a request to the client app process to explicitly cancel + * the current autofill session and create a new session. For example, an augmented autofill + * service may notice some events which make it think a good time to provide updated + * augmented autofill suggestions. * *

The request would be respected only if the previous augmented autofill request was * made for the same {@code activityComponent} and {@code autofillId}, and the field is * currently on focus. * - *

The request would start a new autofill flow. It doesn't guarantee that the - * {@link AutofillManager} will proceed with the request. + *

The request would cancel the current session and start a new autofill flow. + * It doesn't guarantee that the {@link AutofillManager} will proceed with the request. * * @param activityComponent the client component for which the autofill is requested for * @param autofillId the client field id for which the autofill is requested for @@ -179,8 +183,6 @@ public abstract class AugmentedAutofillService extends Service { */ public final boolean requestAutofill(@NonNull ComponentName activityComponent, @NonNull AutofillId autofillId) { - // TODO(b/149531989): revisit this. The request should start a new autofill session - // rather than reusing the existing session. final AutofillProxy proxy = mAutofillProxyForLastRequest; if (proxy == null || !proxy.mComponentName.equals(activityComponent) || !proxy.mFocusedId.equals(autofillId)) { diff --git a/core/java/android/view/autofill/AutofillManager.java b/core/java/android/view/autofill/AutofillManager.java index 6d3dbfe16b780..1773ec2b17eec 100644 --- a/core/java/android/view/autofill/AutofillManager.java +++ b/core/java/android/view/autofill/AutofillManager.java @@ -882,6 +882,25 @@ public final class AutofillManager { notifyViewEntered(view, FLAG_MANUAL_REQUEST); } + /** + * Explicitly cancels the current session and requests a new autofill context. + * + *

Normally, the autofill context is automatically started if necessary when + * {@link #notifyViewEntered(View)} is called, but this method should be used in + * cases where it must be explicitly started or restarted. Currently, this method should only + * be called by + * {@link android.service.autofill.augmented.AugmentedAutofillService#requestAutofill( + * ComponentName, AutofillId)} to cancel the current session and trigger the autofill flow in + * a new session, giving the autofill service or the augmented autofill service a chance to + * send updated suggestions. + * + * @param view view requesting the new autofill context. + */ + void requestAutofillFromNewSession(@NonNull View view) { + cancel(); + notifyViewEntered(view); + } + /** * Explicitly requests a new autofill context for virtual views. * @@ -1403,7 +1422,7 @@ public final class AutofillManager { * methods such as {@link android.app.Activity#finish()}. */ public void cancel() { - if (sVerbose) Log.v(TAG, "cancel() called by app"); + if (sVerbose) Log.v(TAG, "cancel() called by app or augmented autofill service"); if (!hasAutofillFeature()) { return; } @@ -3484,7 +3503,7 @@ public final class AutofillManager { if (sVerbose) { Log.v(TAG, "requestAutofill() by AugmentedAutofillService."); } - afm.post(() -> afm.requestAutofill(view)); + afm.post(() -> afm.requestAutofillFromNewSession(view)); return true; }