package com.sexy.sexypa;␊ |
␊ |
import android.app.Activity;␊ |
import android.content.Context;␊ |
import android.content.Intent;␊ |
import android.graphics.Typeface;␊ |
import android.os.Bundle;␊ |
import android.os.Environment;␊ |
import android.speech.RecognizerIntent;␊ |
import android.speech.tts.TextToSpeech;␊ |
import android.view.View;␊ |
import android.widget.EditText;␊ |
import android.widget.TextView;␊ |
import android.widget.Toast;␊ |
␊ |
␊ |
import java.io.File;␊ |
import java.io.FileWriter;␊ |
import java.io.IOException;␊ |
import java.util.ArrayList;␊ |
import java.util.Locale;␊ |
import java.util.logging.Logger;␊ |
␊ |
public class SexyPAActivity extends Activity implements View.OnClickListener, TextToSpeech.OnInitListener {␊ |
␊ |
protected static final int REQUEST_OK = 1;␊ |
private TextToSpeech tts;␊ |
private String spoken = "";␊ |
␊ |
@Override␊ |
protected void onCreate(Bundle savedInstanceState) {␊ |
super.onCreate(savedInstanceState);␊ |
setContentView(R.layout.sexy_pa);␊ |
findViewById(R.id.speak).setOnClickListener(this);␊ |
␊ |
tts = new TextToSpeech(this, this);␊ |
findViewById(R.id.play).setOnClickListener(this);␊ |
//Typeface myFont = Typeface.createFromAsset(getAssets(), "fonts/Prata-Regular.ttf");␊ |
Typeface myFont = Typeface.createFromAsset(getAssets(), "fonts/AdventPro-Medium.ttf");␊ |
␊ |
EditText spokenWord = (EditText)findViewById(R.id.editText1);␊ |
spokenWord.setTypeface(myFont);␊ |
␊ |
createBookFile();␊ |
}␊ |
␊ |
@Override␊ |
public void onClick(View v)␊ |
{␊ |
switch (v.getId())␊ |
{␊ |
case R.id.speak:␊ |
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);␊ |
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");␊ |
try␊ |
{␊ |
startActivityForResult(i, REQUEST_OK);␊ |
} catch (Exception e)␊ |
{␊ |
Toast.makeText(this, "Error initializing speech to text engine.", Toast.LENGTH_LONG).show();␊ |
}␊ |
break;␊ |
case R.id.play:␊ |
if (tts!=null)␊ |
{␊ |
String text = ((EditText)findViewById(R.id.editText1)).getText().toString();␊ |
if(text != null && text.equalsIgnoreCase("hello"))␊ |
{␊ |
if (!tts.isSpeaking())␊ |
{␊ |
tts.speak("Hello Scott, how was your night?", TextToSpeech.QUEUE_FLUSH, null);␊ |
}␊ |
␊ |
}else if(text != null && text.equalsIgnoreCase("good"))␊ |
{␊ |
if(!tts.isSpeaking())␊ |
{␊ |
tts.speak("Cool, it is nice to see you today, man.", TextToSpeech.QUEUE_FLUSH, null);␊ |
}␊ |
}␊ |
else{␊ |
if(!tts.isSpeaking())␊ |
{␊ |
tts.speak("What? What? What? What?", TextToSpeech.QUEUE_FLUSH, null);␊ |
}␊ |
}␊ |
␊ |
}␊ |
}␊ |
␊ |
}␊ |
␊ |
@Override␊ |
protected void onDestroy()␊ |
{␊ |
if (tts!=null)␊ |
{␊ |
tts.stop();␊ |
tts.shutdown();␊ |
}␊ |
super.onDestroy();␊ |
}␊ |
␊ |
@Override␊ |
protected void onActivityResult(int requestCode, int resultCode, Intent data)␊ |
{␊ |
super.onActivityResult(requestCode, resultCode, data);␊ |
if (requestCode==REQUEST_OK && resultCode==RESULT_OK)␊ |
{␊ |
ArrayList<String> thingsYouSaid = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);␊ |
spoken = thingsYouSaid.get(0);␊ |
((TextView)findViewById(R.id.editText1)).setText(spoken);␊ |
}␊ |
}␊ |
␊ |
␊ |
@Override␊ |
public void onInit(int code)␊ |
{␊ |
if (code == TextToSpeech.SUCCESS)␊ |
{␊ |
tts.setLanguage(Locale.getDefault());␊ |
␊ |
} else␊ |
{␊ |
tts = null;␊ |
Toast.makeText(this, "Failed to initialize TTS engine.",␊ |
Toast.LENGTH_SHORT).show();␊ |
}␊ |
}␊ |
␊ |
public void createBookFile()␊ |
{␊ |
File folder = new File(Environment.getExternalStorageDirectory() + "/draft");␊ |
boolean success = true;␊ |
if (!folder.exists()) {␊ |
success = folder.mkdir();␊ |
}␊ |
if (success) {␊ |
writeToFile("Some Value", "book.txt");␊ |
} else {␊ |
// Do something else on failure␊ |
}␊ |
}␊ |
␊ |
public void writeToFile(String text, String fileName)␊ |
{␊ |
Context context = getBaseContext();␊ |
try {␊ |
FileWriter out = new FileWriter(new File(context.getFilesDir(), fileName));␊ |
out.write(text);␊ |
out.close();␊ |
} catch (IOException e) {␊ |
␊ |
}␊ |
}␊ |
␊ |
␊ |
␊ |
␊ |
}␊ |