Ch15 Listings


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnInitListener {
    private EditText words = null;
    private Button speakBtn = null;
    private static final int REQ_TTS_STATUS_CHECK = 0;
    private static final String TAG = "TTS Demo";
    private TextToSpeech mTts;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        words = (EditText)findViewById(R.id.wordsToSpeak);
        speakBtn = (Button)findViewById(R.id.speak);
        speakBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mTts.speak(words.getText().toString(), TextToSpeech.QUEUE_ADD, null);
            }});

        // Check to be sure that TTS exists and is okay to use
        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, REQ_TTS_STATUS_CHECK);
    }
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQ_TTS_STATUS_CHECK) {
            switch (resultCode) {
            case TextToSpeech.Engine.CHECK_VOICE_DATA_PASS:
                // TTS is up and running
                mTts = new TextToSpeech(this, this);
                Log.v(TAG, "Pico is installed okay");
                break;
            case TextToSpeech.Engine.CHECK_VOICE_DATA_BAD_DATA:
            case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_DATA:
            case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_VOLUME:
                // missing data, install it
                Log.v(TAG, "Need language stuff: " + resultCode);
                Intent installIntent = new Intent();
                installIntent.setAction(
                    TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installIntent);
                break;
            case TextToSpeech.Engine.CHECK_VOICE_DATA_FAIL:
            default:
                Log.e(TAG, "Got a failure. TTS apparently not available");
            }
        }
        else {
            // Got something else
        }
    }

    @Override
    public void onInit(int status) {
        // Now that the TTS engine is ready, we enable the button
        if( status == TextToSpeech.SUCCESS) {
            speakBtn.setEnabled(true);
        }
    }

    @Override
    public void onPause()
    {
        super.onPause();
        // if we're losing focus, stop talking
        if( mTts != null)
            mTts.stop();
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        mTts.shutdown();
    }
}

<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">

  <EditText android:id="@+id/wordsToSpeak"
    android:hinttext="Type words to speak here"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

  <Button android:id="@+id/speak"
    android:text="Speak"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:enabled="false" />

</LinearLayout>

import java.util.HashMap;
import java.util.StringTokenizer;

public class MainActivity extends Activity implements OnInitListener, OnUtteranceCompletedListener

    private int uttCount = 0;
    private int lastUtterance = -1;
    private HashMap<String, String> params = new HashMap<String, String>();

        @Override
        public void onClick(View view) {
            StringTokenizer st = new StringTokenizer(words.getText().toString(),",.");
            while (st.hasMoreTokens()) {
                params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,
                            String.valueOf(uttCount++));
                mTts.speak(st.nextToken(), TextToSpeech.QUEUE_ADD, params);;
           }

            @Override
            public void onInit(int status) {
                // Now that the TTS engine is ready, we enable the button
                if( status == TextToSpeech.SUCCESS) {
                    speakBtn.setEnabled(true);
                    mTts.setOnUtteranceCompletedListener(this);
                }
            }

            @Override
            public void onUtteranceCompleted(String uttId) {
                Log.v(TAG, "Got completed message for uttId: " + uttId);
                lastUtterance = Integer.parseInt(uttId);
            }

<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

  <EditText android:id="@+id/wordsToSpeak"
    android:text="Dohn Keyhotay"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

  <Button android:id="@+id/speakBtn"
    android:text="Speak"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:enabled="false" />

  <TextView android:id="@+id/filenameLabel"
    android:text="Filename:"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

  <EditText android:id="@+id/filename"
    android:text="/sdcard/donquixote.wav"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

  <Button android:id="@+id/recordBtn"
    android:text="Record"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:enabled="false" />

  <Button android:id="@+id/playBtn"
    android:text="Play"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:enabled="false" />

  <TextView android:id="@+id/useWithLabel"
    android:text="Use with:"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

  <EditText android:id="@+id/realText"
    android:text="Don Quixote"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

  <Button android:id="@+id/assocBtn"
    android:text="Associate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:enabled="false" />

</LinearLayout>

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnInitListener {
    private EditText words = null;
    private Button speakBtn = null;
    private EditText filename = null;
    private Button recordBtn = null;
    private Button playBtn = null;
    private EditText useWith = null;
    private Button assocBtn = null;
    private String soundFilename = null;
    private File soundFile = null;
    private static final int REQ_TTS_STATUS_CHECK = 0;
    private static final String TAG = "TTS Demo";
    private TextToSpeech mTts = null;
    private MediaPlayer player = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        words = (EditText)findViewById(R.id.wordsToSpeak);
        filename = (EditText)findViewById(R.id.filename);
        useWith = (EditText)findViewById(R.id.realText);

        speakBtn = (Button)findViewById(R.id.speakBtn);
        speakBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mTts.speak(words.getText().toString(), TextToSpeech.QUEUE_ADD, null);
            }});

        recordBtn = (Button)findViewById(R.id.recordBtn);
        recordBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                soundFilename = filename.getText().toString();
                soundFile = new File(soundFilename);
                if (soundFile.exists())
                    soundFile.delete();

                if(mTts.synthesizeToFile(words.getText().toString(), null, soundFilename)
                        == TextToSpeech.SUCCESS) {
                    Toast.makeText(getBaseContext(),
                            "Sound file created",
                            Toast.LENGTH_SHORT).show();
                    playBtn.setEnabled(true);
                    assocBtn.setEnabled(true);
                }
                else {
                    Toast.makeText(getBaseContext(),
                            "Oops! Sound file not created",
                            Toast.LENGTH_SHORT).show();
                }
            }});

        playBtn = (Button)findViewById(R.id.playBtn);
        playBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    player = new MediaPlayer();
                    player.setDataSource(soundFilename);
                    player.prepare();
                    player.start();
                }
                catch(Exception e) {
                    Toast.makeText(getBaseContext(),
                            "Hmmmmm. Can't play file",
                            Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }});

        assocBtn = (Button)findViewById(R.id.assocBtn);
        assocBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mTts.addSpeech(useWith.getText().toString(), soundFilename);
                Toast.makeText(getBaseContext(),
                     "Associated!",
                     Toast.LENGTH_SHORT).show();
            }});

        // Check to be sure that TTS exists and is okay to use
        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, REQ_TTS_STATUS_CHECK);
    }
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQ_TTS_STATUS_CHECK) {
            switch (resultCode) {
            case TextToSpeech.Engine.CHECK_VOICE_DATA_PASS:
                // TTS is up and running
                mTts = new TextToSpeech(this, this);
                Log.v(TAG, "Pico is installed okay");
                break;
            case TextToSpeech.Engine.CHECK_VOICE_DATA_BAD_DATA:
            case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_DATA:
            case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_VOLUME:
                // missing data, install it
                Log.v(TAG, "Need language stuff: " + resultCode);
                Intent installIntent = new Intent();
                installIntent.setAction(
                    TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installIntent);
                break;
            case TextToSpeech.Engine.CHECK_VOICE_DATA_FAIL:
            default:
                Log.e(TAG, "Got a failure. TTS apparently not available");
            }
        }
        else {
            // Got something else
        }
    }

    @Override
    public void onInit(int status) {
        // Now that the TTS engine is ready, we enable buttons
        if( status == TextToSpeech.SUCCESS) {
            speakBtn.setEnabled(true);
            recordBtn.setEnabled(true);
        }
    }

    @Override
    public void onPause()
    {
        super.onPause();
        // if we're losing focus, stop playing
        if(player != null) {
            player.stop();
        }
        // if we're losing focus, stop talking
        if( mTts != null)
            mTts.stop();
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        if(player != null) {
            player.release();
        }
        if( mTts != null) {
            mTts.shutdown();
        }
    }
}

<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/main.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_height="fill_parent"
       android:layout_width="fill_parent">

        <EditText android:id="@+id/input"
       android:hint="@string/input"
       android:layout_height="wrap_content"
       android:layout_width="fill_parent" />

        <Spinner android:id="@+id/from"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/input"
            android:prompt="@string/prompt" />

        <Button android:id="@+id/translateBtn"
            android:text="@string/translateBtn"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/input"
            android:layout_toRightOf="@id/from"
            android:enabled="false" />

        <Spinner android:id="@+id/to"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/input"
            android:layout_toRightOf="@id/translateBtn"
            android:prompt="@string/prompt" />

    <EditText android:id="@+id/translation"
        android:hint="@string/translation"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:editable="false"
        android:layout_below="@id/from" />

    <TextView android:id="@+id/poweredBy"
        android:text="powered by Google"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/values/strings.xml -->
<resources>
    <string name="translateBtn">>  Translate  ></string>
    <string name="input">Enter the text to translate</string>
    <string name="translation">The translation will appear here</string>
    <string name="prompt">Choose a language</string>
</resources>

<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/values/arrays.xml -->
<resources>
<string-array name="languages">
    <item>Chinese</item>
    <item>English</item>
    <item>French</item>
    <item>German</item>
    <item>Japanese</item>
    <item>Spanish</item>
</string-array>
<string-array name="language_values">
    <item>zh</item>
    <item>en</item>
    <item>fr</item>
    <item>de</item>
    <item>ja</item>
    <item>es</item>
</string-array>
</resources>

// This file is ITranslate.aidl under /src
interface ITranslate {
    String translate(in String text, in String from, in String to);
}

// This file is TranslateService.java
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class TranslateService extends Service {
    public static final String TAG = "TranslateService";

    private final ITranslate.Stub mBinder = new ITranslate.Stub() {
        public String translate(String text, String from, String to) {
            try {
                return Translator.translate(text, from, to);
            } catch (Exception e) {
                Log.e(TAG, "Failed to translate: " + e.getMessage());
                return null;
            }
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
}

// This file is Translator.java
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import org.apache.commons.lang.StringEscapeUtils;
import org.json.JSONObject;
import android.util.Log;


public class Translator {
    private static final String ENCODING = "UTF-8";
    private static final String URL_BASE = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&langpair=";
    private static final String INPUT_TEXT = "&q=";
    private static final String MY_SITE = �??http://my.website.com�?�;
    private static final String TAG = "Translator";

    public static String translate(String text, String from, String to) throws Exception {
        try {
            StringBuilder url = new StringBuilder();
            url.append(URL_BASE).append(from).append("%7C").append(to);
            url.append(INPUT_TEXT).append(URLEncoder.encode(text, ENCODING));

            HttpURLConnection conn = (HttpURLConnection) new URL(url.toString())
                                 .openConnection();
            conn.setRequestProperty("REFERER", MY_SITE);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            try {
                InputStream is= conn.getInputStream();
                String rawResult = makeResult(is);
                
                JSONObject json = new JSONObject(rawResult);
                String result = ((JSONObject)json.get("responseData"))
                                             .getString("translatedText");
                return (StringEscapeUtils.unescapeXml(result));
            } finally {
                conn.getInputStream().close();
                if(conn.getErrorStream() != null)
                    conn.getErrorStream().close();
            }
        } catch (Exception ex) {
            throw ex;
        }
    }

    private static String makeResult(InputStream inputStream) throws Exception {
        StringBuilder outputString = new StringBuilder();
        try {
            String string;
            if (inputStream != null) {
                BufferedReader reader =
                        new BufferedReader(new InputStreamReader(inputStream, ENCODING));
                while (null != (string = reader.readLine())) {
                    outputString.append(string).append('\n');
                }
            }
        } catch (Exception ex) {
            Log.e(TAG, "Error reading translation stream.", ex);
        }
        return outputString.toString();
    }
}

// This file is MainActivity.java
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
    static final String TAG = "Translator";
    private EditText inputText = null;
    private TextView outputText = null;
    private Spinner fromLang = null;
    private Spinner toLang = null;
    private Button translateBtn = null;
    private String[] langShortNames = null;
    private Handler mHandler = new Handler();
    
    private ITranslate mTranslateService;

    private ServiceConnection mTranslateConn = new ServiceConnection() {
        public void onServiceConnected(ComponentName name, IBinder service) {
            mTranslateService = ITranslate.Stub.asInterface(service);
            if (mTranslateService != null) {
                translateBtn.setEnabled(true);
            } else {
                translateBtn.setEnabled(false);
                Log.e(TAG, "Unable to acquire TranslateService");
            }
        }

        public void onServiceDisconnected(ComponentName name) {
            translateBtn.setEnabled(false);
            mTranslateService = null;
        }
    };

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        setContentView(R.layout.main);
        inputText = (EditText) findViewById(R.id.input);
        outputText = (EditText) findViewById(R.id.translation);
        fromLang = (Spinner) findViewById(R.id.from);
        toLang = (Spinner) findViewById(R.id.to);

        langShortNames = getResources().getStringArray(R.array.language_values);

        translateBtn = (Button) findViewById(R.id.translateBtn);
        translateBtn.setOnClickListener(this);
        
        ArrayAdapter<?> fromAdapter = ArrayAdapter.createFromResource(this,
                      R.array.languages, android.R.layout.simple_spinner_item);
        fromAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        fromLang.setAdapter(fromAdapter);
        fromLang.setSelection(1); // English
        
        ArrayAdapter<?> toAdapter = ArrayAdapter.createFromResource(this,
                      R.array.languages,android.R.layout.simple_spinner_item);
        toAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        toLang.setAdapter(toAdapter);
        toLang.setSelection(3); // German
        
        inputText.selectAll();

        Intent intent = new Intent(Intent.ACTION_VIEW);
        bindService(intent, mTranslateConn, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(mTranslateConn);
    }

    public void onClick(View v) {
        if (inputText.getText().length() > 0) {
            doTranslate();
        }
    }
    
    private void doTranslate() {
        mHandler.post(new Runnable() {
            public void run() {
                String result = "";
                try {
                    int fromPosition = fromLang.getSelectedItemPosition();
                    int toPosition = toLang.getSelectedItemPosition();
                    String input = inputText.getText().toString();
                    if(input.length() > 5000)
                           input = input.substring(0,5000);
                    Log.v(TAG,"Translating from " + langShortNames[fromPosition] + " to " +
                                langShortNames[toPosition]);
                    result = mTranslateService.translate(input,
                                      langShortNames[fromPosition],
                                     langShortNames[toPosition]);
                    if (result == null) {
                        throw new Exception("Failed to get a translation");
                    }
                    outputText.setText(result);
                    inputText.selectAll();
                } catch (Exception e) {
                    Log.e(TAG, "Error: " + e.getMessage());
                }
            }
        });
    }
}

<?xml version="1.0" encoding="utf-8"?>
<!-- This file is AndroidManifest.xml -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.androidbook.translation"
      android:versionName="1.0"
      android:versionCode="1" >

    <application android:label="Translate" 
        android:icon="@drawable/icon">

        <activity android:name="MainActivity" android:label="Translate">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name="TranslateService" android:label="Translate">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </service>
    </application>
    <uses-permission android:name="android.permission.INTERNET" />
</manifest>

// This file is TranslateService.java
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class TranslateService extends Service {
    public static final String TAG = "TranslateService";

    private final ITranslate.Stub mBinder = new ITranslate.Stub() {
        public String translate(String text, String from, String to) {
            try {
                return Translator.translate(text, from, to);
            } catch (Exception e) {
                Log.e(TAG, "Failed to translate: " + e.getMessage());
                return null;
            }
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
}

// This file is Translator.java
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import org.apache.commons.lang.StringEscapeUtils;
import org.json.JSONObject;
import android.util.Log;


public class Translator {
    private static final String ENCODING = "UTF-8";
    private static final String URL_BASE = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&langpair=";
    private static final String INPUT_TEXT = "&q=";
    private static final String MY_SITE = �??http://my.website.com�?�;
    private static final String TAG = "Translator";

    public static String translate(String text, String from, String to) throws Exception {
        try {
            StringBuilder url = new StringBuilder();
            url.append(URL_BASE).append(from).append("%7C").append(to);
            url.append(INPUT_TEXT).append(URLEncoder.encode(text, ENCODING));

            HttpURLConnection conn = (HttpURLConnection) new URL(url.toString()).openConnection();
            conn.setRequestProperty("REFERER", MY_SITE);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            try {
                InputStream is= conn.getInputStream();
                String rawResult = makeResult(is);
                
                JSONObject json = new JSONObject(rawResult);
                String result = ((JSONObject)json.get("responseData"))
                                             .getString("translatedText");
                return (StringEscapeUtils.unescapeXml(result));
            } finally {
                conn.getInputStream().close();
                if(conn.getErrorStream() != null)
                       conn.getErrorStream().close();
            }
        } catch (Exception ex) {
            throw ex;
        }
    }

    private static String makeResult(InputStream inputStream) throws Exception {
        StringBuilder outputString = new StringBuilder();
        try {
            String string;
            if (inputStream != null) {
                BufferedReader reader =
                        new BufferedReader(new InputStreamReader(inputStream, ENCODING));
                while (null != (string = reader.readLine())) {
                    outputString.append(string).append('\n');
                }
            }
        } catch (Exception ex) {
            Log.e(TAG, "Error reading translation stream.", ex);
        }
        return outputString.toString();
    }
}