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

Commit 6be74a57 authored by Adam He's avatar Adam He
Browse files

Refactor and simplify InlineFillUi and InlineSuggestionFactory.

* Create InlineFillUiInfo to encapsulate the numberous arguments passed
into the factory.
* Combine CreateInlineSuggestions API for regular and augmented
autofill.

Bug: 162357598
Test: atest CtsAutoFillServiceTestCases
Change-Id: I64d5ca2aec510b9e04ebc3c499ca0029dc96925c
parent f366c35b
Loading
Loading
Loading
Loading
+18 −5
Original line number Original line Diff line number Diff line
@@ -261,9 +261,13 @@ final class RemoteAugmentedAutofillService
                focusedValue != null && focusedValue.isText()
                focusedValue != null && focusedValue.isText()
                        ? focusedValue.getTextValue().toString() : null;
                        ? focusedValue.getTextValue().toString() : null;


        final InlineFillUi.InlineFillUiInfo inlineFillUiInfo =
                new InlineFillUi.InlineFillUiInfo(request, focusedId, filterText,
                        remoteRenderService, userId, sessionId);

        final InlineFillUi inlineFillUi =
        final InlineFillUi inlineFillUi =
                InlineFillUi.forAugmentedAutofill(
                InlineFillUi.forAugmentedAutofill(
                        request, inlineSuggestionsData, focusedId, filterText,
                        inlineFillUiInfo, inlineSuggestionsData,
                        new InlineFillUi.InlineSuggestionUiCallback() {
                        new InlineFillUi.InlineSuggestionUiCallback() {
                            @Override
                            @Override
                            public void autofill(Dataset dataset, int datasetIndex) {
                            public void autofill(Dataset dataset, int datasetIndex) {
@@ -305,15 +309,24 @@ final class RemoteAugmentedAutofillService
                            }
                            }


                            @Override
                            @Override
                            public void startIntentSender(IntentSender intentSender,
                            public void authenticate(int requestId, int datasetIndex) {
                                    Intent intent) {
                                Slog.e(TAG, "authenticate not implemented for augmented autofill");
                            }

                            @Override
                            public void startIntentSender(IntentSender intentSender) {
                                try {
                                try {
                                    client.startIntentSender(intentSender, intent);
                                    client.startIntentSender(intentSender, new Intent());
                                } catch (RemoteException e) {
                                } catch (RemoteException e) {
                                    Slog.w(TAG, "RemoteException starting intent sender");
                                    Slog.w(TAG, "RemoteException starting intent sender");
                                }
                                }
                            }
                            }
                        }, onErrorCallback, remoteRenderService, userId, sessionId);

                            @Override
                            public void onError() {
                                onErrorCallback.run();
                            }
                        });


        if (inlineSuggestionsCallback.apply(inlineFillUi)) {
        if (inlineSuggestionsCallback.apply(inlineFillUi)) {
            mCallbacks.logAugmentedAutofillShown(sessionId, clientState);
            mCallbacks.logAugmentedAutofillShown(sessionId, clientState);
+29 −7
Original line number Original line Diff line number Diff line
@@ -3008,14 +3008,36 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
            return false;
            return false;
        }
        }


        InlineFillUi inlineFillUi = InlineFillUi.forAutofill(
        final InlineFillUi.InlineFillUiInfo inlineFillUiInfo =
                inlineSuggestionsRequest.get(), response, focusedId, filterText,
                new InlineFillUi.InlineFillUiInfo(inlineSuggestionsRequest.get(), focusedId,
                /*uiCallback*/this, /*onErrorCallback*/ () -> {
                        filterText, remoteRenderService, userId, id);
        InlineFillUi inlineFillUi = InlineFillUi.forAutofill(inlineFillUiInfo, response,
                new InlineFillUi.InlineSuggestionUiCallback() {
                    @Override
                    public void autofill(@NonNull Dataset dataset, int datasetIndex) {
                        fill(response.getRequestId(), datasetIndex, dataset);
                    }

                    @Override
                    public void authenticate(int requestId, int datasetIndex) {
                        Session.this.authenticate(response.getRequestId(), datasetIndex,
                                response.getAuthentication(), response.getClientState(),
                                /* authenticateInline= */ true);
                    }

                    @Override
                    public void startIntentSender(@NonNull IntentSender intentSender) {
                        Session.this.startIntentSender(intentSender, new Intent());
                    }

                    @Override
                    public void onError() {
                        synchronized (mLock) {
                        synchronized (mLock) {
                            mInlineSessionController.setInlineFillUiLocked(
                            mInlineSessionController.setInlineFillUiLocked(
                                    InlineFillUi.emptyUi(focusedId));
                                    InlineFillUi.emptyUi(focusedId));
                        }
                        }
                }, remoteRenderService, userId, id);
                    }
                });
        return mInlineSessionController.setInlineFillUiLocked(inlineFillUi);
        return mInlineSessionController.setInlineFillUiLocked(inlineFillUi);
    }
    }


+75 −39
Original line number Original line Diff line number Diff line
@@ -20,7 +20,7 @@ import static com.android.server.autofill.Helper.sVerbose;


import android.annotation.NonNull;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.content.Intent;
import android.annotation.UserIdInt;
import android.content.IntentSender;
import android.content.IntentSender;
import android.service.autofill.Dataset;
import android.service.autofill.Dataset;
import android.service.autofill.FillResponse;
import android.service.autofill.FillResponse;
@@ -32,6 +32,7 @@ import android.util.SparseArray;
import android.view.autofill.AutofillId;
import android.view.autofill.AutofillId;
import android.view.autofill.AutofillValue;
import android.view.autofill.AutofillValue;
import android.view.inputmethod.InlineSuggestion;
import android.view.inputmethod.InlineSuggestion;
import android.view.inputmethod.InlineSuggestionInfo;
import android.view.inputmethod.InlineSuggestionsRequest;
import android.view.inputmethod.InlineSuggestionsRequest;
import android.view.inputmethod.InlineSuggestionsResponse;
import android.view.inputmethod.InlineSuggestionsResponse;


@@ -93,59 +94,72 @@ public final class InlineFillUi {
     */
     */
    @NonNull
    @NonNull
    public static InlineFillUi emptyUi(@NonNull AutofillId autofillId) {
    public static InlineFillUi emptyUi(@NonNull AutofillId autofillId) {
        return new InlineFillUi(autofillId, new SparseArray<>(), null);
        return new InlineFillUi(autofillId);
    }

    /**
     * Encapsulates various arguments used by {@link #forAutofill} and {@link #forAugmentedAutofill}
     */
    public static class InlineFillUiInfo {

        public int mUserId;
        public int mSessionId;
        public InlineSuggestionsRequest mInlineRequest;
        public AutofillId mFocusedId;
        public String mFilterText;
        public RemoteInlineSuggestionRenderService mRemoteRenderService;

        public InlineFillUiInfo(@NonNull InlineSuggestionsRequest inlineRequest,
                @NonNull AutofillId focusedId, @NonNull String filterText,
                @NonNull RemoteInlineSuggestionRenderService remoteRenderService,
                @UserIdInt int userId, int sessionId) {
            mUserId = userId;
            mSessionId = sessionId;
            mInlineRequest = inlineRequest;
            mFocusedId = focusedId;
            mFilterText = filterText;
            mRemoteRenderService = remoteRenderService;
        }
    }
    }


    /**
    /**
     * Returns an inline autofill UI for a field based on an Autofilll response.
     * Returns an inline autofill UI for a field based on an Autofilll response.
     */
     */
    @NonNull
    @NonNull
    public static InlineFillUi forAutofill(@NonNull InlineSuggestionsRequest request,
    public static InlineFillUi forAutofill(@NonNull InlineFillUiInfo inlineFillUiInfo,
            @NonNull FillResponse response,
            @NonNull FillResponse response,
            @NonNull AutofillId focusedViewId, @Nullable String filterText,
            @NonNull InlineSuggestionUiCallback uiCallback) {
            @NonNull AutoFillUI.AutoFillUiCallback uiCallback,
        if (response.getAuthentication() != null && response.getInlinePresentation() != null) {
            @NonNull Runnable onErrorCallback,
            @Nullable RemoteInlineSuggestionRenderService remoteRenderService,
            int userId, int sessionId) {

        if (InlineSuggestionFactory.responseNeedAuthentication(response)) {
            InlineSuggestion inlineAuthentication =
            InlineSuggestion inlineAuthentication =
                    InlineSuggestionFactory.createInlineAuthentication(request, response,
                    InlineSuggestionFactory.createInlineAuthentication(inlineFillUiInfo, response,
                            uiCallback, onErrorCallback, remoteRenderService, userId, sessionId);
                            uiCallback);
            return new InlineFillUi(focusedViewId, inlineAuthentication, filterText);
            return new InlineFillUi(inlineFillUiInfo, inlineAuthentication);
        } else if (response.getDatasets() != null) {
        } else if (response.getDatasets() != null) {
            SparseArray<Pair<Dataset, InlineSuggestion>> inlineSuggestions =
            SparseArray<Pair<Dataset, InlineSuggestion>> inlineSuggestions =
                    InlineSuggestionFactory.createAutofillInlineSuggestions(request,
                    InlineSuggestionFactory.createInlineSuggestions(inlineFillUiInfo,
                            response.getRequestId(),
                            InlineSuggestionInfo.SOURCE_AUTOFILL, response.getDatasets(),
                            response.getDatasets(), focusedViewId, uiCallback, onErrorCallback,
                            uiCallback);
                            remoteRenderService, userId, sessionId);
            return new InlineFillUi(inlineFillUiInfo, inlineSuggestions);
            return new InlineFillUi(focusedViewId, inlineSuggestions, filterText);
        }
        }
        return new InlineFillUi(focusedViewId, new SparseArray<>(), filterText);
        return new InlineFillUi(inlineFillUiInfo, new SparseArray<>());
    }
    }


    /**
    /**
     * Returns an inline autofill UI for a field based on an Autofilll response.
     * Returns an inline autofill UI for a field based on an Autofilll response.
     */
     */
    @NonNull
    @NonNull
    public static InlineFillUi forAugmentedAutofill(@NonNull InlineSuggestionsRequest request,
    public static InlineFillUi forAugmentedAutofill(@NonNull InlineFillUiInfo inlineFillUiInfo,
            @NonNull List<Dataset> datasets,
            @NonNull List<Dataset> datasets,
            @NonNull AutofillId focusedViewId, @Nullable String filterText,
            @NonNull InlineSuggestionUiCallback uiCallback) {
            @NonNull InlineSuggestionUiCallback uiCallback,
            @NonNull Runnable onErrorCallback,
            @Nullable RemoteInlineSuggestionRenderService remoteRenderService,
            int userId, int sessionId) {
        SparseArray<Pair<Dataset, InlineSuggestion>> inlineSuggestions =
        SparseArray<Pair<Dataset, InlineSuggestion>> inlineSuggestions =
                InlineSuggestionFactory.createAugmentedAutofillInlineSuggestions(request, datasets,
                InlineSuggestionFactory.createInlineSuggestions(inlineFillUiInfo,
                        focusedViewId,
                        InlineSuggestionInfo.SOURCE_PLATFORM, datasets, uiCallback);
                        uiCallback, onErrorCallback, remoteRenderService, userId, sessionId);
        return new InlineFillUi(inlineFillUiInfo, inlineSuggestions);
        return new InlineFillUi(focusedViewId, inlineSuggestions, filterText);
    }
    }


    InlineFillUi(@NonNull AutofillId autofillId,
    private InlineFillUi(@Nullable InlineFillUiInfo inlineFillUiInfo,
            @NonNull SparseArray<Pair<Dataset, InlineSuggestion>> inlineSuggestions,
            @NonNull SparseArray<Pair<Dataset, InlineSuggestion>> inlineSuggestions) {
            @Nullable String filterText) {
        mAutofillId = inlineFillUiInfo.mFocusedId;
        mAutofillId = autofillId;
        int size = inlineSuggestions.size();
        int size = inlineSuggestions.size();
        mDatasets = new ArrayList<>(size);
        mDatasets = new ArrayList<>(size);
        mInlineSuggestions = new ArrayList<>(size);
        mInlineSuggestions = new ArrayList<>(size);
@@ -154,16 +168,26 @@ public final class InlineFillUi {
            mDatasets.add(value.first);
            mDatasets.add(value.first);
            mInlineSuggestions.add(value.second);
            mInlineSuggestions.add(value.second);
        }
        }
        mFilterText = filterText;
        mFilterText = inlineFillUiInfo.mFilterText;
    }
    }


    InlineFillUi(@NonNull AutofillId autofillId, InlineSuggestion inlineSuggestion,
    private InlineFillUi(@NonNull InlineFillUiInfo inlineFillUiInfo,
            @Nullable String filterText) {
            @NonNull InlineSuggestion inlineSuggestion) {
        mAutofillId = autofillId;
        mAutofillId = inlineFillUiInfo.mFocusedId;
        mDatasets = null;
        mDatasets = null;
        mInlineSuggestions = new ArrayList<>();
        mInlineSuggestions = new ArrayList<>();
        mInlineSuggestions.add(inlineSuggestion);
        mInlineSuggestions.add(inlineSuggestion);
        mFilterText = filterText;
        mFilterText = inlineFillUiInfo.mFilterText;
    }

    /**
     * Only used for constructing an empty InlineFillUi with {@link #emptyUi}
     */
    private InlineFillUi(@NonNull AutofillId focusedId) {
        mAutofillId = focusedId;
        mDatasets = new ArrayList<>(0);
        mInlineSuggestions = new ArrayList<>(0);
        mFilterText = null;
    }
    }


    @NonNull
    @NonNull
@@ -294,10 +318,22 @@ public final class InlineFillUi {
         */
         */
        void autofill(@NonNull Dataset dataset, int datasetIndex);
        void autofill(@NonNull Dataset dataset, int datasetIndex);


        /**
         * Callback to authenticate a dataset.
         *
         * <p>Only implemented by regular autofill for now.</p>
         */
        void authenticate(int requestId, int datasetIndex);

        /**
        /**
         * Callback to start Intent in client app.
         * Callback to start Intent in client app.
         */
         */
        void startIntentSender(@NonNull IntentSender intentSender, @NonNull Intent intent);
        void startIntentSender(@NonNull IntentSender intentSender);

        /**
         * Callback on errors.
         */
        void onError();
    }
    }


    /**
    /**
+49 −140

File changed.

Preview size limit exceeded, changes collapsed.

+9 −13
Original line number Original line Diff line number Diff line
@@ -57,24 +57,20 @@ final class RemoteInlineSuggestionViewConnector {
    private final Consumer<IntentSender> mStartIntentSenderFromClientApp;
    private final Consumer<IntentSender> mStartIntentSenderFromClientApp;


    RemoteInlineSuggestionViewConnector(
    RemoteInlineSuggestionViewConnector(
            @Nullable RemoteInlineSuggestionRenderService remoteRenderService,
            @NonNull InlineFillUi.InlineFillUiInfo inlineFillUiInfo,
            int userId, int sessionId,
            @NonNull InlinePresentation inlinePresentation,
            @NonNull InlinePresentation inlinePresentation,
            @Nullable IBinder hostInputToken,
            int displayId,
            @NonNull Runnable onAutofillCallback,
            @NonNull Runnable onAutofillCallback,
            @NonNull Runnable onErrorCallback,
            @NonNull InlineFillUi.InlineSuggestionUiCallback uiCallback) {
            @NonNull Consumer<IntentSender> startIntentSenderFromClientApp) {
        mRemoteRenderService = inlineFillUiInfo.mRemoteRenderService;
        mRemoteRenderService = remoteRenderService;
        mInlinePresentation = inlinePresentation;
        mInlinePresentation = inlinePresentation;
        mHostInputToken = hostInputToken;
        mHostInputToken = inlineFillUiInfo.mInlineRequest.getHostInputToken();
        mDisplayId = displayId;
        mDisplayId = inlineFillUiInfo.mInlineRequest.getHostDisplayId();
        mUserId = userId;
        mUserId = inlineFillUiInfo.mUserId;
        mSessionId = sessionId;
        mSessionId = inlineFillUiInfo.mSessionId;


        mOnAutofillCallback = onAutofillCallback;
        mOnAutofillCallback = onAutofillCallback;
        mOnErrorCallback = onErrorCallback;
        mOnErrorCallback = uiCallback::onError;
        mStartIntentSenderFromClientApp = startIntentSenderFromClientApp;
        mStartIntentSenderFromClientApp = uiCallback::startIntentSender;
    }
    }


    /**
    /**