[DO NOT MERGE] Fix Memory Leak: Part II of rewrite render info callback am: e22573d1d8

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/23416834

Change-Id: Ief6e710c46e8baf5bf19d5a98128ddb9e42161d0
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Haoran Zhang
2023-05-26 16:35:53 +00:00
committed by Automerger Merge Worker
2 changed files with 78 additions and 12 deletions

View File

@@ -0,0 +1,55 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.autofill;
import android.util.Slog;
import android.view.inputmethod.InlineSuggestionsRequest;
import java.lang.ref.WeakReference;
import java.util.function.Consumer;
class InlineSuggestionRequestConsumer implements Consumer<InlineSuggestionsRequest> {
static final String TAG = "InlineSuggestionRequestConsumer";
private final WeakReference<Session.AssistDataReceiverImpl> mAssistDataReceiverWeakReference;
private final WeakReference<ViewState> mViewStateWeakReference;
InlineSuggestionRequestConsumer(WeakReference<Session.AssistDataReceiverImpl>
assistDataReceiverWeakReference,
WeakReference<ViewState> viewStateWeakReference) {
mAssistDataReceiverWeakReference = assistDataReceiverWeakReference;
mViewStateWeakReference = viewStateWeakReference;
}
@Override
public void accept(InlineSuggestionsRequest inlineSuggestionsRequest) {
Session.AssistDataReceiverImpl assistDataReceiver = mAssistDataReceiverWeakReference.get();
ViewState viewState = mViewStateWeakReference.get();
if (assistDataReceiver == null) {
Slog.wtf(TAG, "assistDataReceiver is null when accepting new inline suggestion"
+ "requests");
return;
}
if (viewState == null) {
Slog.wtf(TAG, "view state is null when accepting new inline suggestion requests");
return;
}
assistDataReceiver.handleInlineSuggestionRequest(inlineSuggestionsRequest, viewState);
}
}

View File

@@ -323,7 +323,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
* Id of the View currently being displayed. * Id of the View currently being displayed.
*/ */
@GuardedBy("mLock") @GuardedBy("mLock")
@Nullable AutofillId mCurrentViewId; private @Nullable AutofillId mCurrentViewId;
@GuardedBy("mLock") @GuardedBy("mLock")
private IAutoFillManagerClient mClient; private IAutoFillManagerClient mClient;
@@ -614,7 +614,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
* TODO(b/151867668): improve how asynchronous data dependencies are handled, without using * TODO(b/151867668): improve how asynchronous data dependencies are handled, without using
* CountDownLatch. * CountDownLatch.
*/ */
private final class AssistDataReceiverImpl extends IAssistDataReceiver.Stub { final class AssistDataReceiverImpl extends IAssistDataReceiver.Stub {
@GuardedBy("mLock") @GuardedBy("mLock")
private boolean mWaitForInlineRequest; private boolean mWaitForInlineRequest;
@GuardedBy("mLock") @GuardedBy("mLock")
@@ -629,7 +629,19 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
mPendingFillRequest = null; mPendingFillRequest = null;
mWaitForInlineRequest = isInlineRequest; mWaitForInlineRequest = isInlineRequest;
mPendingInlineSuggestionsRequest = null; mPendingInlineSuggestionsRequest = null;
return isInlineRequest ? (inlineSuggestionsRequest) -> { if (isInlineRequest) {
WeakReference<AssistDataReceiverImpl> assistDataReceiverWeakReference =
new WeakReference<AssistDataReceiverImpl>(this);
WeakReference<ViewState> viewStateWeakReference =
new WeakReference<ViewState>(viewState);
return new InlineSuggestionRequestConsumer(assistDataReceiverWeakReference,
viewStateWeakReference);
}
return null;
}
void handleInlineSuggestionRequest(InlineSuggestionsRequest inlineSuggestionsRequest,
ViewState viewState) {
synchronized (mLock) { synchronized (mLock) {
if (!mWaitForInlineRequest || mPendingInlineSuggestionsRequest != null) { if (!mWaitForInlineRequest || mPendingInlineSuggestionsRequest != null) {
return; return;
@@ -639,7 +651,6 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
maybeRequestFillLocked(); maybeRequestFillLocked();
viewState.resetState(ViewState.STATE_PENDING_CREATE_INLINE_REQUEST); viewState.resetState(ViewState.STATE_PENDING_CREATE_INLINE_REQUEST);
} }
} : null;
} }
@GuardedBy("mLock") @GuardedBy("mLock")