/WebAccess/src/com/ideal/webaccess/TtsContentProvider.java

http://eyes-free.googlecode.com/ · Java · 143 lines · 96 code · 19 blank · 28 comment · 7 complexity · 449ac6ded497d2277437252a0c98e5f0 MD5 · raw file

  1. /*
  2. * Copyright (C) 2010 The IDEAL Group
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.ideal.webaccess;
  17. import java.io.FileNotFoundException;
  18. import java.io.UnsupportedEncodingException;
  19. import java.net.URLDecoder;
  20. import java.util.HashMap;
  21. import android.content.ContentProvider;
  22. import android.content.ContentValues;
  23. import android.database.Cursor;
  24. import android.net.Uri;
  25. import android.os.ParcelFileDescriptor;
  26. import android.speech.tts.TextToSpeech;
  27. import android.speech.tts.TextToSpeech.OnInitListener;
  28. import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
  29. /**
  30. * Content Provider for wrapping the TextToSpeech library. Usage:
  31. * content://com.ideal.webaccess.tts/MODE/RANDOMNUMBER/STRING_TO_BE_SPOKEN where
  32. * MODE is 0 for flush, 1 for queue, RANDOMNUMBER is a random number that is
  33. * used to force the content provider to speak the message (even if it was
  34. * previously spoken because it will have a different URI), and
  35. * STRING_TO_BE_SPOKEN is the actual string that should be spoken to the user.
  36. * What is served back is currently not used, but it should be possible to use
  37. * that to communicate back whether or not the TTS is currently speaking.
  38. */
  39. public class TtsContentProvider extends ContentProvider {
  40. private static final String URI_PREFIX = "content://com.ideal.webaccess.tts";
  41. private TextToSpeech mTts;
  42. public static String constructUri(String url) {
  43. Uri uri = Uri.parse(url);
  44. return uri.isAbsolute() ? url : URI_PREFIX + url;
  45. }
  46. private String startupMessage = "";
  47. private int startupQueueMode = 0;
  48. public static boolean isSpeaking = false;
  49. private HashMap<String, String> speechParams;
  50. private OnInitListener mTtsInitListener = new OnInitListener() {
  51. @Override
  52. public void onInit(int status) {
  53. if (mTts == null) {
  54. mTts = new TextToSpeech(getContext(), mTtsInitListener);
  55. return;
  56. }
  57. speechParams = new HashMap<String, String>();
  58. speechParams.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "foo");
  59. mTts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener(){
  60. @Override
  61. public void onUtteranceCompleted(String utteranceId) {
  62. isSpeaking = false;
  63. }
  64. });
  65. isSpeaking = true;
  66. mTts.speak(startupMessage, startupQueueMode, speechParams);
  67. }
  68. };
  69. @Override
  70. public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
  71. String text = "";
  72. int queueMode = 0;
  73. try {
  74. text = uri.toString();
  75. if (text.length() > URI_PREFIX.length()) {
  76. text = text.substring(URI_PREFIX.length() + 1);
  77. if (text.startsWith("1")) {
  78. queueMode = 1;
  79. }
  80. // Throwaway the random number generated by the JS.
  81. // We are using the random number to force a content refresh.
  82. int stringStart = text.indexOf("/", 2) + 1;
  83. text = URLDecoder.decode(text.substring(stringStart), "UTF-8");
  84. text = StringUtils.unescapeHTML(text);
  85. }
  86. } catch (UnsupportedEncodingException e) {
  87. // TODO Auto-generated catch block
  88. e.printStackTrace();
  89. }
  90. if (mTts == null) {
  91. startupMessage = text;
  92. startupQueueMode = queueMode;
  93. mTts = new TextToSpeech(getContext(), mTtsInitListener);
  94. } else {
  95. isSpeaking = true;
  96. mTts.speak(text, queueMode, speechParams);
  97. }
  98. return null;
  99. }
  100. @Override
  101. public boolean onCreate() {
  102. return true;
  103. }
  104. @Override
  105. public int delete(Uri uri, String s, String[] as) {
  106. throw new UnsupportedOperationException("Not supported by this provider");
  107. }
  108. @Override
  109. public String getType(Uri uri) {
  110. throw new UnsupportedOperationException("Not supported by this provider");
  111. }
  112. @Override
  113. public Uri insert(Uri uri, ContentValues contentvalues) {
  114. throw new UnsupportedOperationException("Not supported by this provider");
  115. }
  116. @Override
  117. public Cursor query(Uri uri, String[] as, String s, String[] as1, String s1) {
  118. throw new UnsupportedOperationException("Not supported by this provider");
  119. }
  120. @Override
  121. public int update(Uri uri, ContentValues contentvalues, String s, String[] as) {
  122. throw new UnsupportedOperationException("Not supported by this provider");
  123. }
  124. }