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

Commit 2e723982 authored by Felipe Leme's avatar Felipe Leme Committed by Android (Google) Code Review
Browse files

Merge "Use initial value on save when value didn't change." into oc-dev

parents 49231f18 e8f68080
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -114,7 +114,7 @@ public final class FillContext implements Parcelable {
     *
     * @hide
     */
    @NonNull public ViewNode[] findViewNodesByAutofillIds(@NonNull AutofillId[] ids) {
    @NonNull public ViewNode[] findViewNodesByAutofillIds(@NonNull AutofillId... ids) {
        final LinkedList<ViewNode> nodesToProcess = new LinkedList<>();
        final ViewNode[] foundNodes = new AssistStructure.ViewNode[ids.length];

+44 −10
Original line number Diff line number Diff line
@@ -774,20 +774,29 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
                break;
            }

            final AutofillValue currentValue = viewState.getCurrentValue();
            if (currentValue == null || currentValue.isEmpty()) {
            AutofillValue value = viewState.getCurrentValue();
            if (value == null || value.isEmpty()) {
                final AutofillValue initialValue = getValueFromContexts(id);
                if (initialValue != null) {
                    if (sDebug) {
                        Slog.d(TAG, "Value of required field " + id + " didn't change; "
                                + "using initial value (" + initialValue + ") instead");
                    }
                    value = initialValue;
                } else {
                    if (sDebug) {
                        Slog.d(TAG, "showSaveLocked(): empty value for required " + id );
                    }
                    allRequiredAreNotEmpty = false;
                    break;
                }
            }
            final AutofillValue filledValue = viewState.getAutofilledValue();

            if (!currentValue.equals(filledValue)) {
            if (!value.equals(filledValue)) {
                if (sDebug) {
                    Slog.d(TAG, "showSaveLocked(): found a change on required " + id + ": "
                            + filledValue + " => " + currentValue);
                            + filledValue + " => " + value);
                }
                atLeastOneChanged = true;
            }
@@ -844,6 +853,31 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
        return mIsSaving;
    }

    /**
     * Gets the latest non-empty value for the given id in the autofill contexts.
     */
    @Nullable
    private AutofillValue getValueFromContexts(AutofillId id) {
        AutofillValue value = null;
        final int numContexts = mContexts.size();
        for (int i = 0; i < numContexts; i++) {
            final FillContext context = mContexts.get(i);
            // TODO: create a function that gets just one node so it doesn't create an array
            // unnecessarily
            final ViewNode[] nodes = context.findViewNodesByAutofillIds(id);
            if (nodes != null) {
                AutofillValue candidate = nodes[0].getAutofillValue();
                if (sDebug) {
                    Slog.d(TAG, "getValueFromContexts(" + id + ") at " + i + ": " + candidate);
                }
                if (candidate != null && !candidate.isEmpty()) {
                    value = candidate;
                }
            }
        }
        return value;
    }

    /**
     * Calls service when user requested save.
     */
@@ -1009,7 +1043,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
                    || action == ACTION_VIEW_ENTERED) {
                if (sVerbose) Slog.v(TAG, "Creating viewState for " + id + " on " + action);
                boolean isIgnored = isIgnoredLocked(id);
                viewState = new ViewState(this, id, value, this,
                viewState = new ViewState(this, id, this,
                        isIgnored ? ViewState.STATE_IGNORED : ViewState.STATE_INITIAL);
                mViewStates.put(id, viewState);
                if (isIgnored) {
@@ -1307,7 +1341,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
        if (viewState != null)  {
            viewState.setState(state);
        } else {
            viewState = new ViewState(this, id, null, this, state);
            viewState = new ViewState(this, id, this, state);
            if (sVerbose) {
                Slog.v(TAG, "Adding autofillable view with id " + id + " and state " + state);
            }
+3 −11
Original line number Diff line number Diff line
@@ -74,16 +74,14 @@ final class ViewState {
    private final Session mSession;

    private FillResponse mResponse;
    private AutofillValue mInitialValue;
    private AutofillValue mCurrentValue;
    private AutofillValue mAutofilledValue;
    private Rect mVirtualBounds;
    private int mState;

    ViewState(Session session, AutofillId id, AutofillValue value, Listener listener, int state) {
    ViewState(Session session, AutofillId id, Listener listener, int state) {
        mSession = session;
        this.id = id;
        mInitialValue = value;
        mListener = listener;
        mState = state;
    }
@@ -117,11 +115,6 @@ final class ViewState {
        mAutofilledValue = value;
    }

    @Nullable
    AutofillValue getInitialValue() {
        return mInitialValue;
    }

    @Nullable
    FillResponse getResponse() {
        return mResponse;
@@ -189,8 +182,8 @@ final class ViewState {

    @Override
    public String toString() {
        return "ViewState: [id=" + id + ", initialValue=" + mInitialValue
                + ", currentValue=" + mCurrentValue + ", autofilledValue=" + mAutofilledValue
        return "ViewState: [id=" + id + ", currentValue=" + mCurrentValue
                + ", autofilledValue=" + mAutofilledValue
                + ", bounds=" + mVirtualBounds + ", state=" + getStateAsString() + "]";
    }

@@ -207,7 +200,6 @@ final class ViewState {
                pw.println(mResponse.getRequestId());
            }
        }
        pw.print(prefix); pw.print("initialValue:" ); pw.println(mInitialValue);
        pw.print(prefix); pw.print("currentValue:" ); pw.println(mCurrentValue);
        pw.print(prefix); pw.print("autofilledValue:" ); pw.println(mAutofilledValue);
        pw.print(prefix); pw.print("virtualBounds:" ); pw.println(mVirtualBounds);