3434
3535import java .net .URI ;
3636import java .net .URISyntaxException ;
37+ import java .util .HashMap ;
3738import java .util .Iterator ;
3839import java .util .List ;
3940import java .util .concurrent .atomic .AtomicInteger ;
@@ -53,7 +54,7 @@ class AccessibilityInjector {
5354 private final WebView mWebView ;
5455
5556 // The Java objects that are exposed to JavaScript.
56- private TextToSpeech mTextToSpeech ;
57+ private TextToSpeechWrapper mTextToSpeech ;
5758 private CallbackHandler mCallback ;
5859
5960 // Lazily loaded helper objects.
@@ -367,10 +368,7 @@ private void addTtsApis() {
367368 if (mTextToSpeech != null ) {
368369 return ;
369370 }
370-
371- final String pkgName = mContext .getPackageName ();
372-
373- mTextToSpeech = new TextToSpeech (mContext , null , null , pkgName + ".**webview**" , true );
371+ mTextToSpeech = new TextToSpeechWrapper (mContext );
374372 mWebView .addJavascriptInterface (mTextToSpeech , ALIAS_TTS_JS_INTERFACE );
375373 }
376374
@@ -525,6 +523,41 @@ private boolean sendActionToAndroidVox(int action, Bundle arguments) {
525523 return mCallback .performAction (mWebView , jsCode );
526524 }
527525
526+ /**
527+ * Used to protect the TextToSpeech class, only exposing the methods we want to expose.
528+ */
529+ private static class TextToSpeechWrapper {
530+ private TextToSpeech mTextToSpeech ;
531+
532+ public TextToSpeechWrapper (Context context ) {
533+ final String pkgName = context .getPackageName ();
534+ mTextToSpeech = new TextToSpeech (context , null , null , pkgName + ".**webview**" , true );
535+ }
536+
537+ @ JavascriptInterface
538+ @ SuppressWarnings ("unused" )
539+ public boolean isSpeaking () {
540+ return mTextToSpeech .isSpeaking ();
541+ }
542+
543+ @ JavascriptInterface
544+ @ SuppressWarnings ("unused" )
545+ public int speak (String text , int queueMode , HashMap <String , String > params ) {
546+ return mTextToSpeech .speak (text , queueMode , params );
547+ }
548+
549+ @ JavascriptInterface
550+ @ SuppressWarnings ("unused" )
551+ public int stop () {
552+ return mTextToSpeech .stop ();
553+ }
554+
555+ @ SuppressWarnings ("unused" )
556+ protected void shutdown () {
557+ mTextToSpeech .shutdown ();
558+ }
559+ }
560+
528561 /**
529562 * Exposes result interface to JavaScript.
530563 */
@@ -621,6 +654,7 @@ private boolean waitForResultTimedLocked(int resultId) {
621654 * @param id The result id of the request as a {@link String}.
622655 * @param result The result of the request as a {@link String}.
623656 */
657+ @ JavascriptInterface
624658 @ SuppressWarnings ("unused" )
625659 public void onResult (String id , String result ) {
626660 final long resultId ;
0 commit comments