AI2 Component  (Version nb184)
ExternalTextToSpeech.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2012 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime.util;
8 
11 
12 import android.app.Activity;
13 import android.content.Intent;
14 import android.speech.tts.TextToSpeech;
15 
16 import java.util.Locale;
17 
25 // Note(Hal): I have not looked at making the changes for retries, similar to the ones in
26 // InternalTextToSpeech. The use of ExternalTTS is for pre-1.6 systems only, so is obsolete.
27 // And I don't have an old external TTS library to use in testing, anyway.
28 
30 
31  private static final String TTS_INTENT = "com.google.tts.makeBagel";
32 
33  /* Used to identify the call to startActivityForResult. Will be passed back
34  into the resultReturned() callback method. */
35  private int requestCode;
36 
37  private final ComponentContainer container;
38 
39  private final TextToSpeechCallback callback;
40 
42  TextToSpeechCallback callback) {
43  this.container = container;
44  this.callback = callback;
45  }
46 
47  @Override
48  public void speak(String message, Locale loc) {
49  Intent intent = new Intent(TTS_INTENT);
50  intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
51  intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
52  intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
53  intent.putExtra("message", message);
54  intent.putExtra("language", loc.getISO3Language());
55  intent.putExtra("country", loc.getISO3Country());
56  if (requestCode == 0) {
57  requestCode = container.$form().registerForActivityResult(this);
58  }
59  container.$context().startActivityForResult(intent, requestCode);
60  }
61 
62  @Override
63  public void onDestroy() {
64  // nothing to do
65  }
66 
67  @Override
68  public void onStop() {
69  // nothing to do
70  }
71 
72  @Override
73  public void onResume() {
74  // nothing to do
75  }
76 
77  @Override
78  public void setPitch(float pitch) {
79  // nothing to do
80  }
81 
82  @Override
83  public void setSpeechRate(float speechRate) {
84  // nothing to do
85  }
86 
87  @Override
88  public void resultReturned(int requestCode, int resultCode, Intent data) {
89  boolean isSuccess = (requestCode == this.requestCode) && (resultCode == Activity.RESULT_OK);
90  if (isSuccess) {
91  callback.onSuccess();
92  } else {
93  callback.onFailure();
94  }
95  }
96 
97  // External TextToSpeech is obsolete, so we'll just report that no languages are available
98  // In reality, we won't call this from TextToSpeech in pre-Gingerbread systems
99  public int isLanguageAvailable(Locale loc) {
100  return TextToSpeech.LANG_MISSING_DATA;
101  }
102 
103 // this is a dummy. We'll never use this for external TTS engines
104  public boolean isInitialized() {
105  return true;
106  }
107 }
com.google.appinventor.components.runtime.util.ExternalTextToSpeech.onDestroy
void onDestroy()
Definition: ExternalTextToSpeech.java:63
com.google.appinventor.components.runtime.util.ITextToSpeech.TextToSpeechCallback
Definition: ITextToSpeech.java:24
com.google.appinventor.components
com.google.appinventor.components.runtime.util.ExternalTextToSpeech.setSpeechRate
void setSpeechRate(float speechRate)
Definition: ExternalTextToSpeech.java:83
com.google.appinventor.components.runtime.util.ExternalTextToSpeech.ExternalTextToSpeech
ExternalTextToSpeech(ComponentContainer container, TextToSpeechCallback callback)
Definition: ExternalTextToSpeech.java:41
com.google.appinventor.components.runtime.util.ExternalTextToSpeech.isLanguageAvailable
int isLanguageAvailable(Locale loc)
Definition: ExternalTextToSpeech.java:99
com.google.appinventor.components.runtime.util.ExternalTextToSpeech.speak
void speak(String message, Locale loc)
Definition: ExternalTextToSpeech.java:48
com.google.appinventor.components.runtime.util.ExternalTextToSpeech
Definition: ExternalTextToSpeech.java:29
com.google.appinventor.components.runtime.util.ExternalTextToSpeech.isInitialized
boolean isInitialized()
Definition: ExternalTextToSpeech.java:104
com.google.appinventor.components.runtime.util.ExternalTextToSpeech.onResume
void onResume()
Definition: ExternalTextToSpeech.java:73
com.google.appinventor.components.runtime.util.ITextToSpeech
Definition: ITextToSpeech.java:19
com.google.appinventor.components.runtime.util.ExternalTextToSpeech.resultReturned
void resultReturned(int requestCode, int resultCode, Intent data)
Definition: ExternalTextToSpeech.java:88
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.util.ITextToSpeech.TextToSpeechCallback.onFailure
void onFailure()
com.google.appinventor.components.runtime.ActivityResultListener
Definition: ActivityResultListener.java:16
com.google.appinventor.components.runtime.util.ExternalTextToSpeech.onStop
void onStop()
Definition: ExternalTextToSpeech.java:68
com.google
com
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.runtime.ComponentContainer.$context
Activity $context()
com.google.appinventor.components.runtime.util.ExternalTextToSpeech.setPitch
void setPitch(float pitch)
Definition: ExternalTextToSpeech.java:78
com.google.appinventor.components.runtime.Form.registerForActivityResult
int registerForActivityResult(ActivityResultListener listener)
Definition: Form.java:638
com.google.appinventor
com.google.appinventor.components.runtime.util.ITextToSpeech.TextToSpeechCallback.onSuccess
void onSuccess()