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

Commit 37f84748 authored by Aleksandar Kiridzic's avatar Aleksandar Kiridzic Committed by Aleksandar Kiridžić
Browse files

speech: Timely model download listening connection termination

Callbacks onSuccess, onSchedule and onError in ModelDownloadListener
should be terminal ones, i.e., no callbacks should be called
after one of these. Callback forwarding will be stopped
inside the recognition service.

Bug: 283102476
Test: test app, atest CtsVoiceRecognitionTestCases
Change-Id: If1fdb09bd10ae36fe3eb02c562e6d2069745bb8e
parent e75a45ec
Loading
Loading
Loading
Loading
+46 −16
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import android.os.Message;
import android.os.RemoteException;
import android.util.Log;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.util.function.pooled.PooledLambda;

import java.lang.ref.WeakReference;
@@ -232,41 +233,70 @@ public abstract class RecognitionService extends Service {
                    intent,
                    attributionSource,
                    new ModelDownloadListener() {

                        private final Object mLock = new Object();

                        @GuardedBy("mLock")
                        private boolean mIsTerminated = false;

                        @Override
                        public void onProgress(int completedPercent) {
                            synchronized (mLock) {
                                if (mIsTerminated) {
                                    return;
                                }
                                try {
                                    listener.onProgress(completedPercent);
                                } catch (RemoteException e) {
                                    throw e.rethrowFromSystemServer();
                                }
                            }
                        }

                        @Override
                        public void onSuccess() {
                            synchronized (mLock) {
                                if (mIsTerminated) {
                                    return;
                                }
                                mIsTerminated = true;
                                try {
                                    listener.onSuccess();
                                } catch (RemoteException e) {
                                    throw e.rethrowFromSystemServer();
                                }
                            }
                        }

                        @Override
                        public void onScheduled() {
                            synchronized (mLock) {
                                if (mIsTerminated) {
                                    return;
                                }
                                mIsTerminated = true;
                                try {
                                    listener.onScheduled();
                                } catch (RemoteException e) {
                                    throw e.rethrowFromSystemServer();
                                }
                            }
                        }

                        @Override
                        public void onError(int error) {
                            synchronized (mLock) {
                                if (mIsTerminated) {
                                    return;
                                }
                                mIsTerminated = true;
                                try {
                                    listener.onError(error);
                                } catch (RemoteException e) {
                                    throw e.rethrowFromSystemServer();
                                }
                            }
                        }
                    });
        }
    }