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

Commit 3af04708 authored by Aleksandar Kiridžić's avatar Aleksandar Kiridžić Committed by Android (Google) Code Review
Browse files

Merge "speech: Timely model download listening connection termination" into udc-qpr-dev

parents 50852a01 37f84748
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();
                                }
                            }
                        }
                    });
        }
    }