PageRenderTime 28ms CodeModel.GetById 1ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 17package com.ideal.webaccess;
 18
 19import java.io.FileNotFoundException;
 20import java.io.UnsupportedEncodingException;
 21import java.net.URLDecoder;
 22import java.util.HashMap;
 23
 24import android.content.ContentProvider;
 25import android.content.ContentValues;
 26import android.database.Cursor;
 27import android.net.Uri;
 28import android.os.ParcelFileDescriptor;
 29import android.speech.tts.TextToSpeech;
 30import android.speech.tts.TextToSpeech.OnInitListener;
 31import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
 32
 33/**
 34 * Content Provider for wrapping the TextToSpeech library. Usage:
 35 * content://com.ideal.webaccess.tts/MODE/RANDOMNUMBER/STRING_TO_BE_SPOKEN where
 36 * MODE is 0 for flush, 1 for queue, RANDOMNUMBER is a random number that is
 37 * used to force the content provider to speak the message (even if it was
 38 * previously spoken because it will have a different URI), and
 39 * STRING_TO_BE_SPOKEN is the actual string that should be spoken to the user.
 40 * What is served back is currently not used, but it should be possible to use
 41 * that to communicate back whether or not the TTS is currently speaking.
 42 */
 43public class TtsContentProvider extends ContentProvider {
 44    private static final String URI_PREFIX = "content://com.ideal.webaccess.tts";
 45
 46    private TextToSpeech mTts;
 47
 48    public static String constructUri(String url) {
 49        Uri uri = Uri.parse(url);
 50        return uri.isAbsolute() ? url : URI_PREFIX + url;
 51    }
 52
 53    private String startupMessage = "";
 54
 55    private int startupQueueMode = 0;
 56    
 57    public static boolean isSpeaking = false;
 58    
 59    private HashMap<String, String> speechParams;
 60
 61    private OnInitListener mTtsInitListener = new OnInitListener() {
 62        @Override
 63        public void onInit(int status) {
 64            if (mTts == null) {
 65                mTts = new TextToSpeech(getContext(), mTtsInitListener);
 66                return;
 67            }
 68            speechParams = new HashMap<String, String>();
 69            speechParams.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "foo");
 70            mTts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener(){
 71                @Override
 72                public void onUtteranceCompleted(String utteranceId) {
 73                    isSpeaking = false;
 74                }                    
 75            });
 76            isSpeaking = true;
 77            mTts.speak(startupMessage, startupQueueMode, speechParams);
 78        }
 79    };
 80
 81    @Override
 82    public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
 83        String text = "";
 84        int queueMode = 0;
 85        try {
 86            text = uri.toString();
 87            if (text.length() > URI_PREFIX.length()) {
 88                text = text.substring(URI_PREFIX.length() + 1);
 89                if (text.startsWith("1")) {
 90                    queueMode = 1;
 91                }
 92                // Throwaway the random number generated by the JS.
 93                // We are using the random number to force a content refresh.
 94                int stringStart = text.indexOf("/", 2) + 1;
 95                text = URLDecoder.decode(text.substring(stringStart), "UTF-8");
 96                text = StringUtils.unescapeHTML(text);
 97            }
 98        } catch (UnsupportedEncodingException e) {
 99            // TODO Auto-generated catch block
100            e.printStackTrace();
101        }
102        if (mTts == null) {
103            startupMessage = text;
104            startupQueueMode = queueMode;
105            mTts = new TextToSpeech(getContext(), mTtsInitListener);
106        } else {
107            isSpeaking = true;
108            mTts.speak(text, queueMode, speechParams);
109        }
110        return null;
111    }
112
113    @Override
114    public boolean onCreate() {
115        return true;
116    }
117
118    @Override
119    public int delete(Uri uri, String s, String[] as) {
120        throw new UnsupportedOperationException("Not supported by this provider");
121    }
122
123    @Override
124    public String getType(Uri uri) {
125        throw new UnsupportedOperationException("Not supported by this provider");
126    }
127
128    @Override
129    public Uri insert(Uri uri, ContentValues contentvalues) {
130        throw new UnsupportedOperationException("Not supported by this provider");
131    }
132
133    @Override
134    public Cursor query(Uri uri, String[] as, String s, String[] as1, String s1) {
135        throw new UnsupportedOperationException("Not supported by this provider");
136    }
137
138    @Override
139    public int update(Uri uri, ContentValues contentvalues, String s, String[] as) {
140        throw new UnsupportedOperationException("Not supported by this provider");
141    }
142
143}