Merge "[DO NOT MERGE] Fix Memory Leak: Part I of Rewrite render info remote callback." into udc-dev

This commit is contained in:
Haoran Zhang
2023-05-24 20:37:34 +00:00
committed by Android (Google) Code Review
2 changed files with 88 additions and 16 deletions

View File

@@ -0,0 +1,65 @@
/*
* 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.annotation.Nullable;
import android.os.Bundle;
import android.os.RemoteCallback;
import android.util.Slog;
import android.view.autofill.AutofillId;
import android.view.inputmethod.InlineSuggestionsRequest;
import java.lang.ref.WeakReference;
import java.util.function.Consumer;
final class InlineSuggestionRendorInfoCallbackOnResultListener implements
RemoteCallback.OnResultListener{
private static final String TAG = "InlineSuggestionRendorInfoCallbackOnResultListener";
private final int mRequestIdCopy;
private final AutofillId mFocusedId;
private final WeakReference<Session> mSessionWeakReference;
private final Consumer<InlineSuggestionsRequest> mInlineSuggestionsRequestConsumer;
InlineSuggestionRendorInfoCallbackOnResultListener(WeakReference<Session> sessionWeakReference,
int requestIdCopy,
Consumer<InlineSuggestionsRequest> inlineSuggestionsRequestConsumer,
AutofillId focusedId) {
this.mRequestIdCopy = requestIdCopy;
this.mInlineSuggestionsRequestConsumer = inlineSuggestionsRequestConsumer;
this.mSessionWeakReference = sessionWeakReference;
this.mFocusedId = focusedId;
}
public void onResult(@Nullable Bundle result) {
Session session = this.mSessionWeakReference.get();
if (session == null) {
Slog.wtf(TAG, "Session is null before trying to call onResult");
return;
}
synchronized (session.mLock) {
if (session.mDestroyed) {
Slog.wtf(TAG, "Session is destroyed before trying to call onResult");
return;
}
session.mInlineSessionController.onCreateInlineSuggestionsRequestLocked(
this.mFocusedId,
session.inlineSuggestionsRequestCacheDecorator(
this.mInlineSuggestionsRequestConsumer, this.mRequestIdCopy),
result);
}
}
}

View File

@@ -184,6 +184,7 @@ import com.android.server.wm.ActivityTaskManagerInternal;
import java.io.PrintWriter;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -321,7 +322,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
* Id of the View currently being displayed.
*/
@GuardedBy("mLock")
@Nullable private AutofillId mCurrentViewId;
@Nullable AutofillId mCurrentViewId;
@GuardedBy("mLock")
private IAutoFillManagerClient mClient;
@@ -369,7 +370,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
private Bundle mClientState;
@GuardedBy("mLock")
private boolean mDestroyed;
boolean mDestroyed;
/**
* Helper used to handle state of Save UI when it must be hiding to show a custom description
@@ -448,7 +449,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
private ArrayList<AutofillId> mAugmentedAutofillableIds;
@NonNull
private final AutofillInlineSessionController mInlineSessionController;
final AutofillInlineSessionController mInlineSessionController;
/**
* Receiver of assist data from the app's {@link Activity}.
@@ -1224,24 +1225,30 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
final RemoteInlineSuggestionRenderService remoteRenderService =
mService.getRemoteInlineSuggestionRenderServiceLocked();
if (mSessionFlags.mInlineSupportedByService
&& remoteRenderService != null
&& (isViewFocusedLocked(flags) || isRequestSupportFillDialog(flags))) {
&& remoteRenderService != null
&& (isViewFocusedLocked(flags) || isRequestSupportFillDialog(flags))) {
Consumer<InlineSuggestionsRequest> inlineSuggestionsRequestConsumer =
mAssistReceiver.newAutofillRequestLocked(viewState,
/* isInlineRequest= */ true);
if (inlineSuggestionsRequestConsumer != null) {
final AutofillId focusedId = mCurrentViewId;
final int requestIdCopy = requestId;
final AutofillId focusedId = mCurrentViewId;
WeakReference sessionWeakReference = new WeakReference<Session>(this);
InlineSuggestionRendorInfoCallbackOnResultListener
inlineSuggestionRendorInfoCallbackOnResultListener =
new InlineSuggestionRendorInfoCallbackOnResultListener(
sessionWeakReference,
requestIdCopy,
inlineSuggestionsRequestConsumer,
focusedId);
RemoteCallback inlineSuggestionRendorInfoCallback = new RemoteCallback(
inlineSuggestionRendorInfoCallbackOnResultListener, mHandler);
remoteRenderService.getInlineSuggestionsRendererInfo(
new RemoteCallback((extras) -> {
synchronized (mLock) {
mInlineSessionController.onCreateInlineSuggestionsRequestLocked(
focusedId, inlineSuggestionsRequestCacheDecorator(
inlineSuggestionsRequestConsumer, requestIdCopy),
extras);
}
}, mHandler)
);
inlineSuggestionRendorInfoCallback);
viewState.setState(ViewState.STATE_PENDING_CREATE_INLINE_REQUEST);
}
} else {
@@ -5151,7 +5158,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
}
@NonNull
private Consumer<InlineSuggestionsRequest> inlineSuggestionsRequestCacheDecorator(
Consumer<InlineSuggestionsRequest> inlineSuggestionsRequestCacheDecorator(
@NonNull Consumer<InlineSuggestionsRequest> consumer, int requestId) {
return inlineSuggestionsRequest -> {
consumer.accept(inlineSuggestionsRequest);