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

Commit 96b62064 authored by Felix Oghina's avatar Felix Oghina
Browse files

[speech] implement recognizer proxy

This is a followup to ag/25166321: it turns out the active connection
keeps the SpeechRecognizer object from being GC'd, so implementing a
finalizer there does not work.

This CL adds a proxy object that will actually get GC'd when the client
no longer holds a reference to it. Finalizing this object destroys the
actual SpeechRecognizer.

The public API is untouched.

Test: use a sample app to purposefully leak a bunch of SpeechRecognizer
objects, forcing GC in between. Observe a single session being active at
a time.
Bug: 297249772

Change-Id: I4ff6474dbc27858ad03731916cef5729ba9e6c96
parent 1efd26d3
Loading
Loading
Loading
Loading
+44 −697

File changed.

Preview size limit exceeded, changes collapsed.

+745 −0

File added.

Preview size limit exceeded, changes collapsed.

+108 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2010 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 android.speech;

import android.content.ComponentName;
import android.content.Intent;
import android.util.CloseGuard;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.lang.ref.Reference;
import java.util.concurrent.Executor;

/**
 * @hide
 */
class SpeechRecognizerProxy extends SpeechRecognizer {

    private final CloseGuard mCloseGuard = new CloseGuard();

    private final SpeechRecognizer mDelegate;

    SpeechRecognizerProxy(final SpeechRecognizer delegate) {
        mDelegate = delegate;
        mCloseGuard.open("SpeechRecognizer#destroy()");
    }

    @Override
    public void setRecognitionListener(RecognitionListener listener) {
        mDelegate.setRecognitionListener(listener);
    }

    @Override
    public void startListening(Intent recognizerIntent) {
        mDelegate.startListening(recognizerIntent);
    }

    @Override
    public void stopListening() {
        mDelegate.stopListening();
    }

    @Override
    public void cancel() {
        mDelegate.cancel();
    }

    @Override
    public void destroy() {
        try {
            mCloseGuard.close();
            mDelegate.destroy();
        } finally {
            Reference.reachabilityFence(this);
        }
    }

    @Override
    public void checkRecognitionSupport(
            @NonNull Intent recognizerIntent,
            @NonNull Executor executor,
            @NonNull RecognitionSupportCallback supportListener) {
        mDelegate.checkRecognitionSupport(recognizerIntent, executor, supportListener);
    }

    @Override
    public void triggerModelDownload(@NonNull Intent recognizerIntent) {
        mDelegate.triggerModelDownload(recognizerIntent);
    }

    @Override
    public void triggerModelDownload(
            @NonNull Intent recognizerIntent,
            @NonNull Executor executor,
            @NonNull ModelDownloadListener listener) {
        mDelegate.triggerModelDownload(recognizerIntent, executor, listener);
    }

    @Override
    public void setTemporaryOnDeviceRecognizer(@Nullable ComponentName componentName) {
        mDelegate.setTemporaryOnDeviceRecognizer(componentName);
    }

    @Override
    protected void finalize() throws Throwable {
        try {
            mCloseGuard.warnIfOpen();
            destroy();
        } finally {
            super.finalize();
        }
    }
}
+2 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import android.speech.IRecognitionServiceManagerCallback;
import android.speech.IRecognitionSupportCallback;
import android.speech.RecognitionService;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.util.Slog;
import android.util.SparseIntArray;

@@ -253,6 +254,7 @@ final class SpeechRecognitionManagerServiceImpl extends
    @GuardedBy("mLock")
    private void incrementSessionCountForUidLocked(int uid) {
        mSessionCountByUid.put(uid, mSessionCountByUid.get(uid, 0) + 1);
        Log.i(TAG, "Client " + uid + " has opened " + mSessionCountByUid.get(uid, 0) + " sessions");
    }

    @GuardedBy("mLock")