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

Commit b743a23f authored by Selim Gurun's avatar Selim Gurun
Browse files

Added annotations for injected accessibility objects

Bug: 7073422

Provided @JavascriptInterface to methods that are accessible from
js.

Change-Id: If3bf8f5aa2b6ff38fe5358aad534ea31f6f546ae
parent ce3ef0ab
Loading
Loading
Loading
Loading
+39 −5
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import org.json.JSONObject;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
@@ -53,7 +54,7 @@ class AccessibilityInjector {
    private final WebView mWebView;

    // The Java objects that are exposed to JavaScript.
    private TextToSpeech mTextToSpeech;
    private TextToSpeechWrapper mTextToSpeech;
    private CallbackHandler mCallback;

    // Lazily loaded helper objects.
@@ -367,10 +368,7 @@ class AccessibilityInjector {
        if (mTextToSpeech != null) {
            return;
        }

        final String pkgName = mContext.getPackageName();

        mTextToSpeech = new TextToSpeech(mContext, null, null, pkgName + ".**webview**", true);
        mTextToSpeech = new TextToSpeechWrapper(mContext);
        mWebView.addJavascriptInterface(mTextToSpeech, ALIAS_TTS_JS_INTERFACE);
    }

@@ -525,6 +523,41 @@ class AccessibilityInjector {
        return mCallback.performAction(mWebView, jsCode);
    }

    /**
     * Used to protect the TextToSpeech class, only exposing the methods we want to expose.
     */
    private static class TextToSpeechWrapper {
        private TextToSpeech mTextToSpeech;

        public TextToSpeechWrapper(Context context) {
            final String pkgName = context.getPackageName();
            mTextToSpeech = new TextToSpeech(context, null, null, pkgName + ".**webview**", true);
        }

        @JavascriptInterface
        @SuppressWarnings("unused")
        public boolean isSpeaking() {
            return mTextToSpeech.isSpeaking();
        }

        @JavascriptInterface
        @SuppressWarnings("unused")
        public int speak(String text, int queueMode, HashMap<String, String> params) {
            return mTextToSpeech.speak(text, queueMode, params);
        }

        @JavascriptInterface
        @SuppressWarnings("unused")
        public int stop() {
            return mTextToSpeech.stop();
        }

        @SuppressWarnings("unused")
        protected void shutdown() {
            mTextToSpeech.shutdown();
        }
    }

    /**
     * Exposes result interface to JavaScript.
     */
@@ -621,6 +654,7 @@ class AccessibilityInjector {
         * @param id The result id of the request as a {@link String}.
         * @param result The result of the request as a {@link String}.
         */
        @JavascriptInterface
        @SuppressWarnings("unused")
        public void onResult(String id, String result) {
            final long resultId;