| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.speech.tts; |
| 17 |
|
| 18 |
import android.util.Log; |
| 19 |
|
| 20 |
import java.util.Iterator; |
| 21 |
import java.util.concurrent.LinkedBlockingQueue; |
| 22 |
|
| 23 |
class AudioPlaybackHandler { |
| 24 |
private static final String TAG = "TTS.AudioPlaybackHandler"; |
| 25 |
private static final boolean DBG = false; |
| 26 |
|
| 27 |
private final LinkedBlockingQueue<PlaybackQueueItem> mQueue = |
| 28 |
new LinkedBlockingQueue<PlaybackQueueItem>(); |
| 29 |
private final Thread mHandlerThread; |
| 30 |
|
| 31 |
private volatile PlaybackQueueItem mCurrentWorkItem = null; |
| 32 |
|
| 33 |
AudioPlaybackHandler() { |
| 34 |
mHandlerThread = new Thread(new MessageLoop(), "TTS.AudioPlaybackThread"); |
| 35 |
} |
| 36 |
|
| 37 |
public void start() { |
| 38 |
mHandlerThread.start(); |
| 39 |
} |
| 40 |
|
| 41 |
private void stop(PlaybackQueueItem item) { |
| 42 |
if (item == null) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
item.stop(TextToSpeech.STOPPED); |
| 47 |
} |
| 48 |
|
| 49 |
public void enqueue(PlaybackQueueItem item) { |
| 50 |
try { |
| 51 |
mQueue.put(item); |
| 52 |
} catch (InterruptedException ie) { |
| 53 |
|
| 54 |
|
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
public void stopForApp(Object callerIdentity) { |
| 59 |
if (DBG) Log.d(TAG, "Removing all callback items for : " + callerIdentity); |
| 60 |
removeWorkItemsFor(callerIdentity); |
| 61 |
|
| 62 |
final PlaybackQueueItem current = mCurrentWorkItem; |
| 63 |
if (current != null && (current.getCallerIdentity() == callerIdentity)) { |
| 64 |
stop(current); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
public void stop() { |
| 69 |
if (DBG) Log.d(TAG, "Stopping all items"); |
| 70 |
removeAllMessages(); |
| 71 |
|
| 72 |
stop(mCurrentWorkItem); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public boolean isSpeaking() { |
| 80 |
return (mQueue.peek() != null) || (mCurrentWorkItem != null); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public void quit() { |
| 87 |
removeAllMessages(); |
| 88 |
stop(mCurrentWorkItem); |
| 89 |
mHandlerThread.interrupt(); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
private void removeAllMessages() { |
| 96 |
mQueue.clear(); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
private void removeWorkItemsFor(Object callerIdentity) { |
| 103 |
Iterator<PlaybackQueueItem> it = mQueue.iterator(); |
| 104 |
|
| 105 |
while (it.hasNext()) { |
| 106 |
final PlaybackQueueItem item = it.next(); |
| 107 |
if (item.getCallerIdentity() == callerIdentity) { |
| 108 |
it.remove(); |
| 109 |
stop(item); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
private final class MessageLoop implements Runnable { |
| 119 |
@Override |
| 120 |
public void run() { |
| 121 |
while (true) { |
| 122 |
PlaybackQueueItem item = null; |
| 123 |
try { |
| 124 |
item = mQueue.take(); |
| 125 |
} catch (InterruptedException ie) { |
| 126 |
if (DBG) Log.d(TAG, "MessageLoop : Shutting down (interrupted)"); |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
mCurrentWorkItem = item; |
| 135 |
item.run(); |
| 136 |
mCurrentWorkItem = null; |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
} |