2015-12-29 18:07:40 +01:00
|
|
|
package info.androidhive.speechtotext;
|
|
|
|
|
2016-01-20 17:26:12 +01:00
|
|
|
import java.io.DataOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.Reader;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
2015-12-29 18:07:40 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Locale;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.ActivityNotFoundException;
|
2016-01-20 17:26:12 +01:00
|
|
|
import android.content.Context;
|
2015-12-29 18:07:40 +01:00
|
|
|
import android.content.Intent;
|
2016-01-21 16:01:01 +01:00
|
|
|
import android.content.SharedPreferences;
|
2016-01-20 17:26:12 +01:00
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
import android.net.NetworkInfo;
|
|
|
|
import android.os.AsyncTask;
|
2015-12-29 18:07:40 +01:00
|
|
|
import android.os.Bundle;
|
2016-01-21 16:01:01 +01:00
|
|
|
import android.preference.PreferenceManager;
|
2015-12-29 18:07:40 +01:00
|
|
|
import android.speech.RecognizerIntent;
|
2016-01-20 17:26:12 +01:00
|
|
|
import android.util.Log;
|
2015-12-29 18:07:40 +01:00
|
|
|
import android.view.Menu;
|
2016-01-21 16:01:01 +01:00
|
|
|
import android.view.MenuItem;
|
2015-12-29 18:07:40 +01:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.ImageButton;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
public class MainActivity extends Activity {
|
|
|
|
|
|
|
|
private TextView txtSpeechInput;
|
|
|
|
private ImageButton btnSpeak;
|
|
|
|
private final int REQ_CODE_SPEECH_INPUT = 100;
|
2016-01-21 16:01:01 +01:00
|
|
|
private final int REQ_PREF = 200;
|
2016-01-20 17:26:12 +01:00
|
|
|
public static final String TAG = MainActivity.class.getSimpleName();
|
|
|
|
public static final int MAX_ANSWER_LEN = 500;
|
2016-01-21 16:01:01 +01:00
|
|
|
public static String Url;
|
2015-12-29 18:07:40 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_main);
|
|
|
|
|
|
|
|
txtSpeechInput = (TextView) findViewById(R.id.txtSpeechInput);
|
|
|
|
btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
|
|
|
|
|
|
|
|
// hide the action bar
|
2016-01-21 16:01:01 +01:00
|
|
|
//getActionBar().hide();
|
2015-12-29 18:07:40 +01:00
|
|
|
|
|
|
|
btnSpeak.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
promptSpeechInput();
|
|
|
|
}
|
|
|
|
});
|
2016-01-20 17:47:11 +01:00
|
|
|
new SilentTalkToMeTask().execute("goodbye");
|
2016-01-21 16:01:01 +01:00
|
|
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
Url = preferences.getString(SettingsActivity.KEY_PREF_SERVER_URL, "");
|
|
|
|
Log.d(TAG, "Server url is " + Url);
|
2015-12-29 18:07:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Showing google speech input dialog
|
|
|
|
* */
|
|
|
|
private void promptSpeechInput() {
|
|
|
|
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
|
|
|
|
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
|
|
|
|
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
|
|
|
|
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
|
|
|
|
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
|
|
|
|
getString(R.string.speech_prompt));
|
|
|
|
try {
|
|
|
|
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
|
|
|
|
} catch (ActivityNotFoundException a) {
|
|
|
|
Toast.makeText(getApplicationContext(),
|
|
|
|
getString(R.string.speech_not_supported),
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 17:26:12 +01:00
|
|
|
@Override
|
2015-12-29 18:07:40 +01:00
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
|
|
|
|
switch (requestCode) {
|
|
|
|
case REQ_CODE_SPEECH_INPUT: {
|
|
|
|
if (resultCode == RESULT_OK && null != data) {
|
|
|
|
|
|
|
|
ArrayList<String> result = data
|
|
|
|
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
|
|
|
|
txtSpeechInput.setText(result.get(0));
|
2016-01-20 17:26:12 +01:00
|
|
|
|
|
|
|
ConnectivityManager connMgr = (ConnectivityManager)
|
|
|
|
getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
|
|
|
|
if (networkInfo != null && networkInfo.isConnected()) {
|
|
|
|
new TalkToMeTask().execute(result.get(0));
|
|
|
|
} else {
|
|
|
|
txtSpeechInput.setText("No network connection available.");
|
|
|
|
}
|
2015-12-29 18:07:40 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-01-21 16:01:01 +01:00
|
|
|
case REQ_PREF:{
|
|
|
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
Url = preferences.getString(SettingsActivity.KEY_PREF_SERVER_URL, "");
|
|
|
|
}
|
|
|
|
break;
|
2015-12-29 18:07:40 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
// Inflate the menu; this adds items to the action bar if it is present.
|
|
|
|
getMenuInflater().inflate(R.menu.main, menu);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-21 16:01:01 +01:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.action_settings:
|
|
|
|
startActivityForResult(new Intent(getBaseContext(), SettingsActivity.class), REQ_PREF);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
2016-01-20 17:26:12 +01:00
|
|
|
private class TalkToMeTask extends AsyncTask<String,Void, String>{
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected String doInBackground(String... question) {
|
|
|
|
try {
|
|
|
|
return talkToServer(question[0]);
|
|
|
|
} catch (IOException e){
|
2016-01-21 16:01:01 +01:00
|
|
|
Log.d(TAG, e.toString());
|
2016-01-20 17:26:12 +01:00
|
|
|
return "Cannot connect to server";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(String s) {
|
|
|
|
txtSpeechInput.setText(s);
|
|
|
|
}
|
2016-01-20 17:47:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private class SilentTalkToMeTask extends AsyncTask<String,Void, String>{
|
2016-01-20 17:26:12 +01:00
|
|
|
|
2016-01-20 17:47:11 +01:00
|
|
|
@Override
|
|
|
|
protected String doInBackground(String... question) {
|
2016-01-20 17:26:12 +01:00
|
|
|
try {
|
2016-01-20 17:47:11 +01:00
|
|
|
return talkToServer(question[0]);
|
|
|
|
} catch (IOException e){
|
2016-01-21 16:01:01 +01:00
|
|
|
Log.d(TAG, e.toString());
|
2016-01-20 17:47:11 +01:00
|
|
|
return "Cannot connect to server";
|
2016-01-20 17:26:12 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-20 17:47:11 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(String s) {
|
2016-01-20 17:26:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 17:47:11 +01:00
|
|
|
private String talkToServer(String question) throws IOException {
|
|
|
|
InputStream is = null;
|
|
|
|
try {
|
|
|
|
URL url = new URL(Url);
|
|
|
|
String urlParameters = "msg="+question;
|
|
|
|
byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );
|
|
|
|
int postDataLength = postData.length;
|
|
|
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
|
conn.setDoOutput( true );
|
|
|
|
conn.setInstanceFollowRedirects( false );
|
|
|
|
conn.setRequestMethod("POST");
|
|
|
|
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
conn.setRequestProperty("charset", "utf-8");
|
|
|
|
conn.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
|
|
|
|
conn.setUseCaches(false);
|
|
|
|
DataOutputStream wr = new DataOutputStream( conn.getOutputStream());
|
|
|
|
wr.write(postData);
|
|
|
|
|
|
|
|
conn.connect();
|
|
|
|
int response = conn.getResponseCode();
|
|
|
|
Log.d(TAG, "The response is: " + response);
|
|
|
|
is = conn.getInputStream();
|
|
|
|
|
|
|
|
|
|
|
|
String contentAsString = readIt(is, MAX_ANSWER_LEN);
|
|
|
|
return contentAsString;
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
if (is != null){
|
|
|
|
is.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Reads an InputStream and converts it to a String.
|
|
|
|
public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
|
|
|
|
Reader reader = null;
|
|
|
|
reader = new InputStreamReader(stream, "UTF-8");
|
|
|
|
char[] buffer = new char[len];
|
|
|
|
reader.read(buffer);
|
|
|
|
return new String(buffer);
|
|
|
|
}
|
|
|
|
|
2016-01-20 17:26:12 +01:00
|
|
|
|
2015-12-29 18:07:40 +01:00
|
|
|
}
|