package com.example.android.simpleai;

import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    protected static final int RESULT_SPEECH = 1;
    private ProgressBar progressBar;
    private ImageButton btnSpeak;
    private TextView txtText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{
                Manifest.permission.RECORD_AUDIO},1);

        txtText = (TextView) findViewById(R.id.txtText);
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
        btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
        //Initially progressbar is invisible
        progressBar.setVisibility(View.INVISIBLE);


        btnSpeak.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                progressBar.setVisibility(View.VISIBLE);
                Intent intent = new Intent(
                        RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");

                try {
                    startActivityForResult(intent, RESULT_SPEECH);
                    txtText.setText("");
                } catch (ActivityNotFoundException a) {
                    Toast t = Toast.makeText(getApplicationContext(),
                            "Opps! Your device doesn't support Speech to Text",
                            Toast.LENGTH_SHORT);
                    t.show();
                }
            }
        });

    }
    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case 1: {

                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(MainActivity.this, "Permission granted to record audio", Toast.LENGTH_SHORT).show();
                    // permission was granted
                } else {

                    // permission denied, Disable the
                    // functionality that depends on this permission.
                    Toast.makeText(MainActivity.this, "Permission denied to record audio", Toast.LENGTH_SHORT).show();
                }
                return;
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case RESULT_SPEECH: {
                if (resultCode == RESULT_OK && null != data) {
                    progressBar.setVisibility(View.INVISIBLE);
                    ArrayList<String> text = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

                    txtText.setText(text.get(0));
                }
                break;
            }

        }

    }
}

@@ -259,44 +259,27 @@
 ort 
-android.speech.RecognitionListener
+java.util.Locale
 ;%0A
+%0A
 impo
@@ -340,24 +340,24 @@
 ech.
-SpeechRecognizer
+tts.TextToSpeech
 ;%0Aim
@@ -427,32 +427,57 @@
 content.Intent;%0A
+import android.util.Log;%0A
 import android.v
@@ -618,16 +618,77 @@
 Toast;%0A%0A
+import org.w3c.dom.Text;%0A%0Aimport static android.R.id.input;%0A%0A
 public c
@@ -731,16 +731,55 @@
 ctivity 
+implements TextToSpeech.OnInitListener 
 %7B%0A%0A    p
@@ -923,16 +923,38 @@
 txtText;
+%0A    TextToSpeech tts;
 %0A%0A    @O
@@ -1521,17 +1521,60 @@
 SIBLE);%0A
+        tts = new TextToSpeech(this, this);
 %0A
-
 %0A       
@@ -2409,24 +2409,510 @@
  %7D);%0A%0A    %7D%0A
+    @Override%0A    public void onInit(int status) %7B%0A        if(status == TextToSpeech.SUCCESS) %7B%0A            Locale English = tts.getLanguage();%0A            int result = tts.setLanguage(English);%0A            if(result == TextToSpeech.LANG_MISSING_DATA %7C%7C result == TextToSpeech.LANG_NOT_SUPPORTED) %7B%0A                Log.e(%22TTS%22,%22this language is not supported%22);%0A            %7D else %7B%0A%0A            %7D%0A        %7D else %7B%0A            Log.e(%22TTS%22,%22Initialization failed!!%22);%0A        %7D%0A%0A%0A    %7D%0A%0A
     @Overrid
@@ -4370,24 +4370,24 @@
 _RESULTS);%0A%0A
-
             
@@ -4423,16 +4423,418 @@
 get(0));
+%0A%0A                    String txt = txtText.getText().toString();%0A                    if(txt.isEmpty())%0A                    %7B%0A                        Toast.makeText(MainActivity.this, %22You did not say anything!%22, Toast.LENGTH_SHORT).show();%0A                    %7D%0A                    else%0A                    %7B%0A                        tts.speak(txt, TextToSpeech.QUEUE_FLUSH, null);%0A                    %7D
 %0A       

implement TTS