Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8839835d authored by TYM Tsai's avatar TYM Tsai
Browse files

Starts a new request on switching back to the existing app

The response may be old and incorrect when leave the app then go back. To
avoid using the wrong response, going back to the app will trigger a new
request.

Bug: 146903532
Test: atest CtsAutoFillServiceTestCases
Change-Id: Ibab426183d39e3c51fb0f89ec6ba6ea48012b6a8
parent b8bf014c
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -2539,7 +2539,8 @@ public class Activity extends ContextThemeWrapper
        mCalled = true;

        if (mAutoFillResetNeeded) {
            getAutofillManager().onInvisibleForAutofill();
            // If stopped without changing the configurations, the response should expire.
            getAutofillManager().onInvisibleForAutofill(!mChangingConfigurations);
        } else if (mIntent != null
                && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)
                && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_CROSS_ACTIVITY)) {
+10 −1
Original line number Diff line number Diff line
@@ -230,6 +230,7 @@ public final class AutofillManager {
    /** @hide */ public static final int ACTION_VIEW_ENTERED =  2;
    /** @hide */ public static final int ACTION_VIEW_EXITED = 3;
    /** @hide */ public static final int ACTION_VALUE_CHANGED = 4;
    /** @hide */ public static final int ACTION_RESPONSE_EXPIRED = 5;

    /** @hide */ public static final int NO_LOGGING = 0;
    /** @hide */ public static final int FLAG_ADD_CLIENT_ENABLED = 0x1;
@@ -776,11 +777,19 @@ public final class AutofillManager {
     *
     * @see AutofillClient#autofillClientIsVisibleForAutofill()
     *
     * @param isExpiredResponse The response has expired or not
     *
     * {@hide}
     */
    public void onInvisibleForAutofill() {
    public void onInvisibleForAutofill(boolean isExpiredResponse) {
        synchronized (mLock) {
            mOnInvisibleCalled = true;

            if (isExpiredResponse) {
                // Notify service the response has expired.
                updateSessionLocked(/* id= */ null, /* bounds= */ null, /* value= */ null,
                        ACTION_RESPONSE_EXPIRED, /* flags= */ 0);
            }
        }
    }

+35 −4
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.server.autofill;
import static android.service.autofill.AutofillFieldClassificationService.EXTRA_SCORES;
import static android.service.autofill.FillRequest.FLAG_MANUAL_REQUEST;
import static android.service.autofill.FillRequest.INVALID_REQUEST_ID;
import static android.view.autofill.AutofillManager.ACTION_RESPONSE_EXPIRED;
import static android.view.autofill.AutofillManager.ACTION_START_SESSION;
import static android.view.autofill.AutofillManager.ACTION_VALUE_CHANGED;
import static android.view.autofill.AutofillManager.ACTION_VIEW_ENTERED;
@@ -267,6 +268,9 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
    @GuardedBy("mLock")
    private final LocalLog mWtfHistory;

    @GuardedBy("mLock")
    private boolean mExpiredResponse;

    /**
     * Map of {@link MetricsEvent#AUTOFILL_REQUEST} metrics, keyed by fill request id.
     */
@@ -679,6 +683,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
    @GuardedBy("mLock")
    private void requestNewFillResponseLocked(@NonNull ViewState viewState, int newState,
            int flags) {
        mExpiredResponse = false;
        if (mForAugmentedAutofillOnly || mRemoteFillService == null) {
            if (sVerbose) {
                Slog.v(TAG, "requestNewFillResponse(): triggering augmented autofill instead "
@@ -1295,6 +1300,9 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
            }
        }

        // The client becomes invisible for the authentication, the response is effective.
        mExpiredResponse = false;

        final Parcelable result = data.getParcelable(AutofillManager.EXTRA_AUTHENTICATION_RESULT);
        final Bundle newClientState = data.getBundle(AutofillManager.EXTRA_CLIENT_STATE);
        if (sDebug) {
@@ -2310,16 +2318,18 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
     * @param id The id of the view that is entered.
     * @param viewState The view that is entered.
     * @param flags The flag that was passed by the AutofillManager.
     *
     * @return {@code true} if a new fill response is requested.
     */
    @GuardedBy("mLock")
    private void requestNewFillResponseOnViewEnteredIfNecessaryLocked(@NonNull AutofillId id,
    private boolean requestNewFillResponseOnViewEnteredIfNecessaryLocked(@NonNull AutofillId id,
            @NonNull ViewState viewState, int flags) {
        if ((flags & FLAG_MANUAL_REQUEST) != 0) {
            mForAugmentedAutofillOnly = false;
            if (sDebug) Slog.d(TAG, "Re-starting session on view " + id + " and flags " + flags);
            maybeRequestInlineSuggestionsRequestThenFillLocked(viewState,
                    ViewState.STATE_RESTARTED_SESSION, flags);
            return;
            return true;
        }

        // If it's not, then check if it it should start a partition.
@@ -2330,12 +2340,14 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
            }
            maybeRequestInlineSuggestionsRequestThenFillLocked(viewState,
                    ViewState.STATE_STARTED_PARTITION, flags);
            return true;
        } else {
            if (sVerbose) {
                Slog.v(TAG, "Not starting new partition for view " + id + ": "
                        + viewState.getStateAsString());
            }
        }
        return false;
    }

    /**
@@ -2343,7 +2355,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
     *
     * @param id The id of the view that is entered
     *
     * @return {@code true} iff a new partition should be started
     * @return {@code true} if a new partition should be started
     */
    @GuardedBy("mLock")
    private boolean shouldStartNewPartitionLocked(@NonNull AutofillId id) {
@@ -2351,6 +2363,13 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
            return true;
        }

        if (mExpiredResponse) {
            if (sDebug) {
                Slog.d(TAG, "Starting a new partition because the response has expired.");
            }
            return true;
        }

        final int numResponses = mResponses.size();
        if (numResponses >= AutofillManagerService.getPartitionMaxCount()) {
            Slog.e(TAG, "Not starting a new partition on " + id + " because session " + this.id
@@ -2402,6 +2421,14 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
                    + id + " destroyed");
            return;
        }
        if (action == ACTION_RESPONSE_EXPIRED) {
            mExpiredResponse = true;
            if (sDebug) {
                Slog.d(TAG, "Set the response has expired.");
            }
            return;
        }

        id.setSessionId(this.id);
        if (sVerbose) {
            Slog.v(TAG, "updateLocked(" + this.id + "): id=" + id + ", action="
@@ -2565,7 +2592,9 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
                    return;
                }

                requestNewFillResponseOnViewEnteredIfNecessaryLocked(id, viewState, flags);
                if (requestNewFillResponseOnViewEnteredIfNecessaryLocked(id, viewState, flags)) {
                    return;
                }

                if (isSameViewEntered) {
                    return;
@@ -3711,6 +3740,8 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
                return "VIEW_EXITED";
            case ACTION_VALUE_CHANGED:
                return "VALUE_CHANGED";
            case ACTION_RESPONSE_EXPIRED:
                return "RESPONSE_EXPIRED";
            default:
                return "UNKNOWN_" + action;
        }