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

Commit cd50731b authored by Ahaan Ugale's avatar Ahaan Ugale
Browse files

Some cleanup for autofill.Session.updateLocked()

Cleanup for the VIEW_CHANGED case:
* method decomposition
* clarify comment wording and placement
* remove now unnecessary comments

Test: atest CtsAutoFillServiceTestCases CtsInputMethodTestCases

Change-Id: If04eb5ab9a0a83c5c80d3378923dee54493bb629
parent 753216eb
Loading
Loading
Loading
Loading
+60 −58
Original line number Diff line number Diff line
@@ -2436,65 +2436,9 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
                    forceRemoveSelfLocked(AutofillManager.STATE_UNKNOWN_COMPAT_MODE);
                    return;
                }

                if (!Objects.equals(value, viewState.getCurrentValue())) {
                    if ((value == null || value.isEmpty())
                            && viewState.getCurrentValue() != null
                            && viewState.getCurrentValue().isText()
                            && viewState.getCurrentValue().getTextValue() != null
                            && getSaveInfoLocked() != null) {
                        final int length = viewState.getCurrentValue().getTextValue().length();
                        if (sDebug) {
                            Slog.d(TAG, "updateLocked(" + id + "): resetting value that was "
                                    + length + " chars long");
                        }
                        final LogMaker log = newLogMaker(MetricsEvent.AUTOFILL_VALUE_RESET)
                                .addTaggedData(MetricsEvent.FIELD_AUTOFILL_PREVIOUS_LENGTH, length);
                        mMetricsLogger.write(log);
                    }

                    // Always update the internal state.
                    viewState.setCurrentValue(value);

                    // Must check if this update was caused by autofilling the view, in which
                    // case we just update the value, but not the UI.
                    final AutofillValue filledValue = viewState.getAutofilledValue();
                    if (filledValue != null) {
                        if (filledValue.equals(value)) {
                            if (sVerbose) {
                                Slog.v(TAG, "ignoring autofilled change on id " + id);
                            }
                            viewState.resetState(ViewState.STATE_CHANGED);
                            return;
                        }
                        else {
                            if ((viewState.id.equals(this.mCurrentViewId)) &&
                                    (viewState.getState() & ViewState.STATE_AUTOFILLED) != 0) {
                                // Remove autofilled state once field is changed after autofilling.
                                if (sVerbose) {
                                    Slog.v(TAG, "field changed after autofill on id " + id);
                                }
                                viewState.resetState(ViewState.STATE_AUTOFILLED);
                                final ViewState currentView = mViewStates.get(mCurrentViewId);
                                currentView.maybeCallOnFillReady(flags);
                            }
                        }
                    }

                    // Update the internal state...
                    viewState.setState(ViewState.STATE_CHANGED);

                    //..and the UI
                    final String filterText;
                    if (value == null || !value.isText()) {
                        filterText = null;
                    } else {
                        final CharSequence text = value.getTextValue();
                        // Text should never be null, but it doesn't hurt to check to avoid a
                        // system crash...
                        filterText = (text == null) ? null : text.toString();
                    }
                    getUiForShowing().filterFillUi(filterText, this);
                    logIfViewClearedLocked(id, value, viewState);
                    updateViewStateAndUiOnValueChangedLocked(id, value, viewState, flags);
                }
                break;
            case ACTION_VIEW_ENTERED:
@@ -2573,6 +2517,64 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
        return ArrayUtils.contains(response.getIgnoredIds(), id);
    }

    @GuardedBy("mLock")
    private void logIfViewClearedLocked(AutofillId id, AutofillValue value, ViewState viewState) {
        if ((value == null || value.isEmpty())
                && viewState.getCurrentValue() != null
                && viewState.getCurrentValue().isText()
                && viewState.getCurrentValue().getTextValue() != null
                && getSaveInfoLocked() != null) {
            final int length = viewState.getCurrentValue().getTextValue().length();
            if (sDebug) {
                Slog.d(TAG, "updateLocked(" + id + "): resetting value that was "
                        + length + " chars long");
            }
            final LogMaker log = newLogMaker(MetricsEvent.AUTOFILL_VALUE_RESET)
                    .addTaggedData(MetricsEvent.FIELD_AUTOFILL_PREVIOUS_LENGTH, length);
            mMetricsLogger.write(log);
        }
    }

    @GuardedBy("mLock")
    private void updateViewStateAndUiOnValueChangedLocked(AutofillId id, AutofillValue value,
            ViewState viewState, int flags) {
        viewState.setCurrentValue(value);

        final AutofillValue filledValue = viewState.getAutofilledValue();
        if (filledValue != null) {
            if (filledValue.equals(value)) {
                // When the update is caused by autofilling the view, just update the
                // value, not the UI.
                if (sVerbose) {
                    Slog.v(TAG, "ignoring autofilled change on id " + id);
                }
                viewState.resetState(ViewState.STATE_CHANGED);
                return;
            } else if ((viewState.id.equals(this.mCurrentViewId))
                    && (viewState.getState() & ViewState.STATE_AUTOFILLED) != 0) {
                // Remove autofilled state once field is changed after autofilling.
                if (sVerbose) {
                    Slog.v(TAG, "field changed after autofill on id " + id);
                }
                viewState.resetState(ViewState.STATE_AUTOFILLED);
                final ViewState currentView = mViewStates.get(mCurrentViewId);
                currentView.maybeCallOnFillReady(flags);
            }
        }

        viewState.setState(ViewState.STATE_CHANGED);
        final String filterText;
        if (value == null || !value.isText()) {
            filterText = null;
        } else {
            final CharSequence text = value.getTextValue();
            // Text should never be null, but it doesn't hurt to check to avoid a
            // system crash...
            filterText = (text == null) ? null : text.toString();
        }
        getUiForShowing().filterFillUi(filterText, this);
    }

    @Override
    public void onFillReady(@NonNull FillResponse response, @NonNull AutofillId filledId,
            @Nullable AutofillValue value) {