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

Commit b39ce193 authored by Adam He's avatar Adam He Committed by Android (Google) Code Review
Browse files

Merge "Remove CountDownLatch from AssistReceiver."

parents 25a55b6e 5a9bf59f
Loading
Loading
Loading
Loading
+15 −18
Original line number Diff line number Diff line
@@ -119,7 +119,6 @@ import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Function;
@@ -374,26 +373,24 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
     * CountDownLatch.
     */
    private final class AssistDataReceiverImpl extends IAssistDataReceiver.Stub {

        @GuardedBy("mLock")
        private boolean mWaitForInlineRequest;
        @GuardedBy("mLock")
        private InlineSuggestionsRequest mPendingInlineSuggestionsRequest;
        @GuardedBy("mLock")
        private FillRequest mPendingFillRequest;
        @GuardedBy("mLock")
        private CountDownLatch mCountDownLatch = new CountDownLatch(0);

        @Nullable Consumer<InlineSuggestionsRequest> newAutofillRequestLocked(ViewState viewState,
                boolean isInlineRequest) {
            mCountDownLatch = new CountDownLatch(isInlineRequest ? 2 : 1);
            mPendingFillRequest = null;
            mWaitForInlineRequest = isInlineRequest;
            mPendingInlineSuggestionsRequest = null;
            return isInlineRequest ? (inlineSuggestionsRequest) -> {
                synchronized (mLock) {
                    if (mCountDownLatch.getCount() == 0) {
                    if (!mWaitForInlineRequest || mPendingInlineSuggestionsRequest != null) {
                        return;
                    }
                    mPendingInlineSuggestionsRequest = inlineSuggestionsRequest;
                    mCountDownLatch.countDown();
                    maybeRequestFillLocked();
                    viewState.resetState(ViewState.STATE_PENDING_CREATE_INLINE_REQUEST);
                }
@@ -401,16 +398,23 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
        }

        void maybeRequestFillLocked() {
            if (mCountDownLatch.getCount() > 0 || mPendingFillRequest == null) {
            if (mPendingFillRequest == null) {
                return;
            }

            if (mWaitForInlineRequest) {
                if (mPendingInlineSuggestionsRequest == null) {
                    return;
                }
            if (mPendingInlineSuggestionsRequest != null) {

                mPendingFillRequest = new FillRequest(mPendingFillRequest.getId(),
                        mPendingFillRequest.getFillContexts(), mPendingFillRequest.getClientState(),
                        mPendingFillRequest.getFlags(), mPendingInlineSuggestionsRequest);
            }

            mRemoteFillService.onFillRequest(mPendingFillRequest);
            mPendingInlineSuggestionsRequest = null;
            mWaitForInlineRequest = false;
            mPendingFillRequest = null;
        }

@@ -511,15 +515,8 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
                request = new FillRequest(requestId, contexts, mClientState, flags,
                        /*inlineSuggestionsRequest=*/null);

                if (mCountDownLatch.getCount() > 0) {
                mPendingFillRequest = request;
                    mCountDownLatch.countDown();
                maybeRequestFillLocked();
                } else {
                    // TODO(b/151867668): ideally this case should not happen, but it was
                    //  observed, we should figure out why and fix.
                    mRemoteFillService.onFillRequest(request);
                }
            }

            if (mActivityToken != null) {