Add a system TextToSpeech implementation that initiates the connection through the system server.

    This change includes the new System Service that allows the supervised binding to the TextToSpeech service provider.
    It proxies the binding process from the client instead of the direct client -> texttospeech connection.

Bug: 178112052
Test: atest CtsSpeechTestCases
Test: forest apct/device_boot_health_check
Change-Id: I0709e71460fa01ab025c92753a20bce38f562845
diff --git a/services/Android.bp b/services/Android.bp
index 61591c2..3154628 100644
--- a/services/Android.bp
+++ b/services/Android.bp
@@ -33,6 +33,7 @@
         ":services.startop.iorap-sources",
         ":services.systemcaptions-sources",
         ":services.translation-sources",
+        ":services.texttospeech-sources",
         ":services.usage-sources",
         ":services.usb-sources",
         ":services.voiceinteraction-sources",
@@ -83,6 +84,7 @@
         "services.startop",
         "services.systemcaptions",
         "services.translation",
+        "services.texttospeech",
         "services.usage",
         "services.usb",
         "services.voiceinteraction",
diff --git a/services/core/java/com/android/server/infra/AbstractMasterSystemService.java b/services/core/java/com/android/server/infra/AbstractMasterSystemService.java
index c4c0f68..bd577f3 100644
--- a/services/core/java/com/android/server/infra/AbstractMasterSystemService.java
+++ b/services/core/java/com/android/server/infra/AbstractMasterSystemService.java
@@ -371,6 +371,9 @@
             int durationMs) {
         Slog.i(mTag, "setTemporaryService(" + userId + ") to " + componentName + " for "
                 + durationMs + "ms");
+        if (mServiceNameResolver == null) {
+            return;
+        }
         enforceCallingPermissionForManagement();
 
         Objects.requireNonNull(componentName);
@@ -404,6 +407,9 @@
         enforceCallingPermissionForManagement();
 
         synchronized (mLock) {
+            if (mServiceNameResolver == null) {
+                return false;
+            }
             final boolean changed = mServiceNameResolver.setDefaultServiceEnabled(userId, enabled);
             if (!changed) {
                 if (verbose) {
@@ -434,6 +440,10 @@
     public final boolean isDefaultServiceEnabled(@UserIdInt int userId) {
         enforceCallingPermissionForManagement();
 
+        if (mServiceNameResolver == null) {
+            return false;
+        }
+
         synchronized (mLock) {
             return mServiceNameResolver.isDefaultServiceEnabled(userId);
         }
@@ -958,6 +968,10 @@
             public void onPackageModified(String packageName) {
                 if (verbose) Slog.v(mTag, "onPackageModified(): " + packageName);
 
+                if (mServiceNameResolver == null) {
+                    return;
+                }
+
                 final int userId = getChangingUserId();
                 final String serviceName = mServiceNameResolver.getDefaultServiceName(userId);
                 if (serviceName == null) {
diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java
index 2b09d12..dd2dd81 100644
--- a/services/java/com/android/server/SystemServer.java
+++ b/services/java/com/android/server/SystemServer.java
@@ -326,6 +326,8 @@
             "com.android.server.musicrecognition.MusicRecognitionManagerService";
     private static final String SYSTEM_CAPTIONS_MANAGER_SERVICE_CLASS =
             "com.android.server.systemcaptions.SystemCaptionsManagerService";
+    private static final String TEXT_TO_SPEECH_MANAGER_SERVICE_CLASS =
+            "com.android.server.texttospeech.TextToSpeechManagerService";
     private static final String TIME_ZONE_RULES_MANAGER_SERVICE_CLASS =
             "com.android.server.timezone.RulesManagerService$Lifecycle";
     private static final String IOT_SERVICE_CLASS =
@@ -1713,6 +1715,7 @@
             startAttentionService(context, t);
             startRotationResolverService(context, t);
             startSystemCaptionsManagerService(context, t);
+            startTextToSpeechManagerService(context, t);
 
             // System Speech Recognition Service
             if (deviceHasConfigString(context,
@@ -2918,6 +2921,13 @@
         t.traceEnd();
     }
 
+    private void startTextToSpeechManagerService(@NonNull Context context,
+            @NonNull TimingsTraceAndSlog t) {
+        t.traceBegin("StartTextToSpeechManagerService");
+        mSystemServiceManager.startService(TEXT_TO_SPEECH_MANAGER_SERVICE_CLASS);
+        t.traceEnd();
+    }
+
     private void startContentCaptureService(@NonNull Context context,
             @NonNull TimingsTraceAndSlog t) {
         // First check if it was explicitly enabled by DeviceConfig
diff --git a/services/texttospeech/Android.bp b/services/texttospeech/Android.bp
new file mode 100644
index 0000000..bacc932
--- /dev/null
+++ b/services/texttospeech/Android.bp
@@ -0,0 +1,13 @@
+filegroup {
+    name: "services.texttospeech-sources",
+    srcs: ["java/**/*.java"],
+    path: "java",
+    visibility: ["//frameworks/base/services"],
+}
+
+java_library_static {
+    name: "services.texttospeech",
+    defaults: ["platform_service_defaults"],
+    srcs: [":services.texttospeech-sources"],
+    libs: ["services.core"],
+}
\ No newline at end of file
diff --git a/services/texttospeech/java/com/android/server/texttospeech/TextToSpeechManagerPerUserService.java b/services/texttospeech/java/com/android/server/texttospeech/TextToSpeechManagerPerUserService.java
new file mode 100644
index 0000000..f805904
--- /dev/null
+++ b/services/texttospeech/java/com/android/server/texttospeech/TextToSpeechManagerPerUserService.java
@@ -0,0 +1,184 @@
+/*
+ * Copyright (C) 2020 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.texttospeech;
+
+import static com.android.internal.infra.AbstractRemoteService.PERMANENT_BOUND_TIMEOUT_MS;
+
+import android.annotation.NonNull;
+import android.annotation.UserIdInt;
+import android.app.AppGlobals;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ServiceInfo;
+import android.os.IBinder.DeathRecipient;
+import android.os.RemoteException;
+import android.speech.tts.ITextToSpeechService;
+import android.speech.tts.ITextToSpeechSession;
+import android.speech.tts.ITextToSpeechSessionCallback;
+import android.speech.tts.TextToSpeech;
+import android.util.Slog;
+
+import com.android.internal.annotations.GuardedBy;
+import com.android.internal.infra.ServiceConnector;
+import com.android.server.infra.AbstractPerUserSystemService;
+
+import java.util.NoSuchElementException;
+
+/**
+ * Manages per-user text to speech session activated by {@link TextToSpeechManagerService}.
+ * Creates {@link TtsClient} interface object with direct connection to
+ * {@link android.speech.tts.TextToSpeechService} provider.
+ *
+ * @see ITextToSpeechSession
+ * @see TextToSpeech
+ */
+final class TextToSpeechManagerPerUserService extends
+        AbstractPerUserSystemService<TextToSpeechManagerPerUserService,
+                TextToSpeechManagerService> {
+
+    private static final String TAG = TextToSpeechManagerPerUserService.class.getSimpleName();
+
+    TextToSpeechManagerPerUserService(
+            @NonNull TextToSpeechManagerService master,
+            @NonNull Object lock, @UserIdInt int userId) {
+        super(master, lock, userId);
+    }
+
+    void createSessionLocked(String engine, ITextToSpeechSessionCallback sessionCallback) {
+        TextToSpeechSessionConnection.start(getContext(), mUserId, engine, sessionCallback);
+    }
+
+    @GuardedBy("mLock")
+    @Override // from PerUserSystemService
+    @NonNull
+    protected ServiceInfo newServiceInfoLocked(
+            @SuppressWarnings("unused") @NonNull ComponentName serviceComponent)
+            throws PackageManager.NameNotFoundException {
+        try {
+            return AppGlobals.getPackageManager().getServiceInfo(serviceComponent,
+                    PackageManager.GET_META_DATA, mUserId);
+        } catch (RemoteException e) {
+            throw new PackageManager.NameNotFoundException(
+                    "Could not get service for " + serviceComponent);
+        }
+    }
+
+    private static class TextToSpeechSessionConnection extends
+            ServiceConnector.Impl<ITextToSpeechService> {
+
+        private final String mEngine;
+        private final ITextToSpeechSessionCallback mCallback;
+        private final DeathRecipient mUnbindOnDeathHandler;
+
+        static void start(Context context, @UserIdInt int userId, String engine,
+                ITextToSpeechSessionCallback callback) {
+            new TextToSpeechSessionConnection(context, userId, engine, callback).start();
+        }
+
+        private TextToSpeechSessionConnection(Context context, @UserIdInt int userId, String engine,
+                ITextToSpeechSessionCallback callback) {
+            super(context,
+                    new Intent(TextToSpeech.Engine.INTENT_ACTION_TTS_SERVICE).setPackage(engine),
+                    Context.BIND_AUTO_CREATE,
+                    userId,
+                    ITextToSpeechService.Stub::asInterface);
+            mEngine = engine;
+            mCallback = callback;
+            mUnbindOnDeathHandler = () -> unbindEngine("client process death is reported");
+        }
+
+        private void start() {
+            Slog.d(TAG, "Trying to start connection to TTS engine: " + mEngine);
+
+            connect()
+                    .thenAccept(
+                            serviceBinder -> {
+                                if (serviceBinder != null) {
+                                    Slog.d(TAG,
+                                            "Connected successfully to TTS engine: " + mEngine);
+                                    try {
+                                        mCallback.onConnected(new ITextToSpeechSession.Stub() {
+                                            @Override
+                                            public void disconnect() {
+                                                unbindEngine("client disconnection request");
+                                            }
+                                        }, serviceBinder.asBinder());
+
+                                        mCallback.asBinder().linkToDeath(mUnbindOnDeathHandler, 0);
+                                    } catch (RemoteException ex) {
+                                        Slog.w(TAG, "Error notifying the client on connection", ex);
+
+                                        unbindEngine(
+                                                "failed communicating with the client - process "
+                                                        + "is dead");
+                                    }
+                                } else {
+                                    Slog.w(TAG, "Failed to obtain TTS engine binder");
+                                    runSessionCallbackMethod(
+                                            () -> mCallback.onError("Failed creating TTS session"));
+                                }
+                            })
+                    .exceptionally(ex -> {
+                        Slog.w(TAG, "TTS engine binding error", ex);
+                        runSessionCallbackMethod(
+                                () -> mCallback.onError(
+                                        "Failed creating TTS session: " + ex.getCause()));
+
+                        return null;
+                    });
+        }
+
+        @Override // from ServiceConnector.Impl
+        protected void onServiceConnectionStatusChanged(
+                ITextToSpeechService service, boolean connected) {
+            if (!connected) {
+                Slog.w(TAG, "Disconnected from TTS engine");
+                runSessionCallbackMethod(mCallback::onDisconnected);
+
+                try {
+                    mCallback.asBinder().unlinkToDeath(mUnbindOnDeathHandler, 0);
+                } catch (NoSuchElementException ex) {
+                    Slog.d(TAG, "The death recipient was not linked.");
+                }
+            }
+        }
+
+        @Override // from ServiceConnector.Impl
+        protected long getAutoDisconnectTimeoutMs() {
+            return PERMANENT_BOUND_TIMEOUT_MS;
+        }
+
+        private void unbindEngine(String reason) {
+            Slog.d(TAG, "Unbinding TTS engine: " + mEngine + ". Reason: " + reason);
+            unbind();
+        }
+    }
+
+    static void runSessionCallbackMethod(ThrowingRunnable callbackRunnable) {
+        try {
+            callbackRunnable.runOrThrow();
+        } catch (RemoteException ex) {
+            Slog.w(TAG, "Failed running callback method", ex);
+        }
+    }
+
+    interface ThrowingRunnable {
+        void runOrThrow() throws RemoteException;
+    }
+}
diff --git a/services/texttospeech/java/com/android/server/texttospeech/TextToSpeechManagerService.java b/services/texttospeech/java/com/android/server/texttospeech/TextToSpeechManagerService.java
new file mode 100644
index 0000000..9015563
--- /dev/null
+++ b/services/texttospeech/java/com/android/server/texttospeech/TextToSpeechManagerService.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2020 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.texttospeech;
+
+import static com.android.server.texttospeech.TextToSpeechManagerPerUserService.runSessionCallbackMethod;
+
+import android.annotation.NonNull;
+import android.annotation.UserIdInt;
+import android.content.Context;
+import android.os.UserHandle;
+import android.speech.tts.ITextToSpeechManager;
+import android.speech.tts.ITextToSpeechSessionCallback;
+
+import com.android.server.infra.AbstractMasterSystemService;
+
+
+/**
+ * A service that  allows secured synthesizing of text to speech audio. Upon request creates a
+ * session
+ * that is managed by {@link TextToSpeechManagerPerUserService}.
+ *
+ * @see ITextToSpeechManager
+ */
+public final class TextToSpeechManagerService extends
+        AbstractMasterSystemService<TextToSpeechManagerService,
+                TextToSpeechManagerPerUserService> {
+
+    private static final String TAG = TextToSpeechManagerService.class.getSimpleName();
+
+    public TextToSpeechManagerService(@NonNull Context context) {
+        super(context, /* serviceNameResolver= */ null,
+                /* disallowProperty = */null);
+    }
+
+    @Override // from SystemService
+    public void onStart() {
+        publishBinderService(Context.TEXT_TO_SPEECH_MANAGER_SERVICE,
+                new TextToSpeechManagerServiceStub());
+    }
+
+    @Override
+    protected TextToSpeechManagerPerUserService newServiceLocked(
+            @UserIdInt int resolvedUserId, boolean disabled) {
+        return new TextToSpeechManagerPerUserService(this, mLock, resolvedUserId);
+    }
+
+    private final class TextToSpeechManagerServiceStub extends ITextToSpeechManager.Stub {
+        @Override
+        public void createSession(String engine,
+                ITextToSpeechSessionCallback sessionCallback) {
+            synchronized (mLock) {
+                TextToSpeechManagerPerUserService perUserService = getServiceForUserLocked(
+                        UserHandle.getCallingUserId());
+                if (perUserService != null) {
+                    perUserService.createSessionLocked(engine, sessionCallback);
+                } else {
+                    runSessionCallbackMethod(
+                            () -> sessionCallback.onError("Service is not available for user"));
+                }
+            }
+        }
+    }
+}