1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.texttospeech; 18 19 import static com.android.server.texttospeech.TextToSpeechManagerPerUserService.runSessionCallbackMethod; 20 21 import android.annotation.NonNull; 22 import android.annotation.UserIdInt; 23 import android.content.Context; 24 import android.os.UserHandle; 25 import android.speech.tts.ITextToSpeechManager; 26 import android.speech.tts.ITextToSpeechSessionCallback; 27 28 import com.android.server.infra.AbstractMasterSystemService; 29 30 31 /** 32 * A service that allows secured synthesizing of text to speech audio. Upon request creates a 33 * session 34 * that is managed by {@link TextToSpeechManagerPerUserService}. 35 * 36 * @see ITextToSpeechManager 37 */ 38 public final class TextToSpeechManagerService extends 39 AbstractMasterSystemService<TextToSpeechManagerService, 40 TextToSpeechManagerPerUserService> { 41 42 private static final String TAG = TextToSpeechManagerService.class.getSimpleName(); 43 TextToSpeechManagerService(@onNull Context context)44 public TextToSpeechManagerService(@NonNull Context context) { 45 super(context, /* serviceNameResolver= */ null, 46 /* disallowProperty = */null); 47 } 48 49 @Override // from SystemService onStart()50 public void onStart() { 51 publishBinderService(Context.TEXT_TO_SPEECH_MANAGER_SERVICE, 52 new TextToSpeechManagerServiceStub()); 53 } 54 55 @Override newServiceLocked( @serIdInt int resolvedUserId, boolean disabled)56 protected TextToSpeechManagerPerUserService newServiceLocked( 57 @UserIdInt int resolvedUserId, boolean disabled) { 58 return new TextToSpeechManagerPerUserService(this, mLock, resolvedUserId); 59 } 60 61 private final class TextToSpeechManagerServiceStub extends ITextToSpeechManager.Stub { 62 @Override createSession(String engine, ITextToSpeechSessionCallback sessionCallback)63 public void createSession(String engine, 64 ITextToSpeechSessionCallback sessionCallback) { 65 synchronized (mLock) { 66 if (engine == null) { 67 runSessionCallbackMethod( 68 () -> sessionCallback.onError("Engine cannot be null")); 69 return; 70 } 71 72 TextToSpeechManagerPerUserService perUserService = getServiceForUserLocked( 73 UserHandle.getCallingUserId()); 74 if (perUserService != null) { 75 perUserService.createSessionLocked(engine, sessionCallback); 76 } else { 77 runSessionCallbackMethod( 78 () -> sessionCallback.onError("Service is not available for user")); 79 } 80 } 81 } 82 } 83 } 84