org.apache.cordova.nodialogspeechrecognizer.SpeechRecognizer.java Source code

Java tutorial

Introduction

Here is the source code for org.apache.cordova.nodialogspeechrecognizer.SpeechRecognizer.java

Source

/**
 * The MIT License
 *
 *   Copyright (c) 2011-2016
 *   Colin Turner (github.com/koolspin)
 *   Guillaume Charhon (github.com/poiuytrez)
 *  Apurav Chauhan (https://github.com/apuravchauhan)
 *
 *   Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 *
 */
package org.apache.cordova.nodialogspeechrecognizer;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.apache.cordova.PermissionHelper;
import org.json.JSONArray;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.json.JSONException;

import android.Manifest;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.speech.RecognitionListener;
import android.util.Log;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.speech.RecognizerIntent;

/**
 * Style and such borrowed from the TTS ,macdonst/SpeechRecognitionPlugin and PhoneListener plugins
 */
public class SpeechRecognizer extends CordovaPlugin {
    private static final String LOG_TAG = SpeechRecognizer.class.getSimpleName();
    private static int REQUEST_CODE = 1001;
    private android.speech.SpeechRecognizer speech = null;
    private CallbackContext callbackContext;
    private LanguageDetailsChecker languageDetailsChecker;
    private static String[] permissions = { Manifest.permission.RECORD_AUDIO };
    private static int RECORD_AUDIO = 0;

    protected void getMicPermission() {
        PermissionHelper.requestPermission(this, RECORD_AUDIO, permissions[RECORD_AUDIO]);
    }

    private void promptForMic(JSONArray args) {
        if (PermissionHelper.hasPermission(this, permissions[RECORD_AUDIO])) {
            this.startSpeechRecognitionActivity(args);
        } else {
            getMicPermission();
        }

    }

    public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults)
            throws JSONException {
        for (int r : grantResults) {
            if (r == PackageManager.PERMISSION_DENIED) {
                this.callbackContext.error("No permission!");

                return;
            }
        }
        promptForMic(new JSONArray());
    }

    // @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
        Boolean isValidAction = true;
        try {
            if (speech == null) {

                Handler loopHandler = new Handler(Looper.getMainLooper());
                loopHandler.post(new Runnable() {

                    @Override
                    public void run() {
                        speech = android.speech.SpeechRecognizer
                                .createSpeechRecognizer(cordova.getActivity().getBaseContext());
                        speech.setRecognitionListener(new SpeechRecognitionListner());
                    }

                });

            }
        } catch (Exception e) {
            Log.e(LOG_TAG, String.format("execute SpeechRecognitionActivity exception: %s", e.toString()), e);
        }
        this.callbackContext = callbackContext;

        // Action selector
        if ("startRecognize".equals(action)) {
            // recognize speech
            this.promptForMic(args);
            // startSpeechRecognitionActivity(args);
        } else if ("getSupportedLanguages".equals(action)) {
            getSupportedLanguages();
        } else if ("checkSpeechRecognition".equals(action)) {
            checkSpeechRecognition();
        } else {
            // Invalid action
            this.callbackContext.error("Unknown action: " + action);
            isValidAction = false;
        }

        return isValidAction;

    }

    // Get the list of supported languages
    private void getSupportedLanguages() {
        if (languageDetailsChecker == null) {
            languageDetailsChecker = new LanguageDetailsChecker(callbackContext);
        }
        // Create and launch get languages intent
        Intent detailsIntent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
        cordova.getActivity().sendOrderedBroadcast(detailsIntent, null, languageDetailsChecker, null,
                Activity.RESULT_OK, null, null);

    }

    // Check to see if a recognition activity is present
    private void checkSpeechRecognition() {
        PackageManager pm = cordova.getActivity().getPackageManager();
        List activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);

        if (activities.size() != 0) {
            // is present
            this.callbackContext.success("RecognizerIntent.ACTION_RECOGNIZE_SPEECH found!");
        } else {
            // don't exist
            this.callbackContext.error("RecognizerIntent.ACTION_RECOGNIZE_SPEECH not found...");
        }
    }

    /**
     * Fire an intent to start the speech recognition activity.
     *
     * @param args
     *          Argument array with the following string args: [req code][number
     *          of matches][prompt string]
     */
    private void startSpeechRecognitionActivity(JSONArray args) {
        int maxMatches = 1;
        String prompt = "";
        String language = Locale.getDefault().toString();

        try {
            if (args.length() > 0) {
                // Maximum number of matches, 0 means the recognizer decides
                String temp = args.getString(0);
                maxMatches = Integer.parseInt(temp);
            }
            if (args.length() > 1) {
                // Optional text prompt
                prompt = args.getString(1);
            }
            if (args.length() > 2) {
                // Optional language specified
                language = args.getString(2);
            }
        } catch (Exception e) {
            Log.e(LOG_TAG, String.format("startSpeechRecognitionActivity exception: %s", e.toString()));
        }

        // Create the intent and set parameters
        final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, cordova.getActivity().getPackageName());
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);

        if (maxMatches > 0)
            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, maxMatches);
        if (!prompt.equals(""))
            intent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt);
        try {
            Handler loopHandler = new Handler(Looper.getMainLooper());
            loopHandler.post(new Runnable() {

                @Override
                public void run() {
                    speech.startListening(intent);
                }

            });
        } catch (Exception e) {
            Log.e("error", "er", e);
        }
        // cordova.startActivityForResult(this, intent, REQUEST_CODE);
    }

    /**
     * Handle the results from the recognition activity.
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            // Fill the list view with the strings the recognizer thought it could
            // have heard
            ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

            returnSpeechResults(matches);
        } else {
            // Failure - Let the caller know
            this.callbackContext.error(Integer.toString(resultCode));
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    private void returnSpeechResults(ArrayList<String> matches) {
        JSONArray jsonMatches = new JSONArray(matches);
        this.callbackContext.success(jsonMatches);
    }

    class SpeechRecognitionListner implements RecognitionListener {

        @Override
        public void onBeginningOfSpeech() {
            Log.d(LOG_TAG, "begin speech");

        }

        @Override
        public void onBufferReceived(byte[] buffer) {
            Log.d(LOG_TAG, "buffer received");
        }

        @Override
        public void onEndOfSpeech() {
            Log.d(LOG_TAG, "end speech");

        }

        @Override
        public void onError(int error) {

            Log.d(LOG_TAG, "error speech " + error);
            callbackContext.error(error);

        }

        @Override
        public void onEvent(int eventType, Bundle params) {
            Log.d(LOG_TAG, "event speech");
        }

        @Override
        public void onPartialResults(Bundle partialResults) {
            Log.d(LOG_TAG, "partial results");
        }

        @Override
        public void onReadyForSpeech(Bundle params) {
            Log.d(LOG_TAG, "ready for speech");
        }

        @Override
        public void onResults(Bundle results) {
            Log.d(LOG_TAG, "results");
            String str = new String();
            Log.d(LOG_TAG, "onResults " + results);
            ArrayList<String> transcript = results
                    .getStringArrayList(android.speech.SpeechRecognizer.RESULTS_RECOGNITION);
            JSONArray jsonMatches = new JSONArray(transcript);
            callbackContext.success(jsonMatches);
            float[] confidence = results.getFloatArray(android.speech.SpeechRecognizer.CONFIDENCE_SCORES);
            if (transcript.size() > 0) {
                Log.d(LOG_TAG, "fire recognition event");
                // fireRecognitionEvent(transcript, confidence);
            } else {
                Log.d(LOG_TAG, "fire no match event");
            }
        }

        @Override
        public void onRmsChanged(float rmsdB) {
            Log.d(LOG_TAG, "rms changed");
        }

    }
}