From e579033d768234f9951527ff5be6e7adc29ed1bb Mon Sep 17 00:00:00 2001 From: Ben Lin Date: Mon, 1 Oct 2018 16:50:10 -0700 Subject: [PATCH] Notify AutofillManager in onResume() IFF activity was not recreated. In Activity#onResume(), AutofillManager is notified when there is a current focus, in cases where user backgrounds and then foregrounds the app, etc. However, onResume() is also called when screen rotation occurs, but because both the window and the layout is torn down, Activity#getCurrentFocus() returns null, thus AutofillManager is not notified because there is no current focus. This is true on phones. However, in some devices, such as Chromebooks, window is preserved on rotation. The API is supported in Window Management's task resize capabilities. Because the window is not torn down, Activity#getCurrentFocus() now returns true, even if an upcoming layout pass is bound to happen. Thus, AutofillManager gets notified twice: once on screen rotation (onResume()), and another during the subsequent layout pass. This changes the logic to explicitly check for the case where activity is recreated from a previous saved instance. If it is, then skip notifying AutofillManager, as the subsequent layout pass will do the job. Bug: 111619119 Test: atest android.autofillservice.cts.SessionLifecycleTest#testDatasetVisibleWhileAutofilledAppIsLifecycled atest android.autofillservice.cts.DuplicateIdActivityTest#testDoNotRestoreDuplicateAutofillIds Change-Id: I1816abfa23d39d6f3370c9f32c6de7fc5ae0398e (cherry picked from commit af5a24eea08975990c7487be8b64657bf61641b7) --- core/java/android/app/Activity.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/core/java/android/app/Activity.java b/core/java/android/app/Activity.java index 482ef2d1a6fd8..74a9819c925fa 100644 --- a/core/java/android/app/Activity.java +++ b/core/java/android/app/Activity.java @@ -1387,7 +1387,15 @@ public class Activity extends ContextThemeWrapper if (mAutoFillResetNeeded) { if (!mAutoFillIgnoreFirstResumePause) { View focus = getCurrentFocus(); - if (focus != null && focus.canNotifyAutofillEnterExitEvent()) { + // On Activity rotation situation (mRestoredFromBundle is true), + // we should not call on AutofillManager in onResume() + // since the next Layout pass will do that. + // However, there are both cases where Activity#getCurrentFocus() + // will return null (window not preserved) and not null (window IS + // preserved), so we need to explicitly check for mRestoredFromBundle + // here. + if (!mRestoredFromBundle && focus != null + && focus.canNotifyAutofillEnterExitEvent()) { // TODO: in Activity killed/recreated case, i.e. SessionLifecycleTest# // testDatasetVisibleWhileAutofilledAppIsLifecycled: the View's initial // window visibility after recreation is INVISIBLE in onResume() and next frame