From 1ca1d886588ce54ab0c0229eabc49fa8dff40bc5 Mon Sep 17 00:00:00 2001 From: Przemyslaw Szczepaniak Date: Wed, 29 Jan 2014 15:20:06 +0000 Subject: [PATCH] Allow clients to extend the TTS UtteranceId class. This change allows TTS clients to create (and use) classes derived from the UtteranceId class. This allows to attach a custom data and methods that can be reached later in callbacks that take the UtteranceId instance as parameter. Also, since we can't depend on the identityHashCode results being unique, this change adds AtomicInteger to generate unique identifiers for UtteranceId instances. Bug: 8259486 Change-Id: Id1e9eabc890ec585a7f8570fd20e287dcda9a11d --- api/current.txt | 5 +-- .../speech/tts/TextToSpeechClient.java | 37 ++++++++----------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/api/current.txt b/api/current.txt index 9b6ee472b0f1d..2175da6b5e56c 100644 --- a/api/current.txt +++ b/api/current.txt @@ -23954,10 +23954,9 @@ package android.speech.tts { field public static final int SUCCESS = 0; // 0x0 } - public static final class TextToSpeechClient.UtteranceId { + public static class TextToSpeechClient.UtteranceId { ctor public TextToSpeechClient.UtteranceId(); - ctor public TextToSpeechClient.UtteranceId(java.lang.String); - method public java.lang.String toUniqueString(); + method public final java.lang.String toUniqueString(); } public abstract class TextToSpeechService extends android.app.Service { diff --git a/core/java/android/speech/tts/TextToSpeechClient.java b/core/java/android/speech/tts/TextToSpeechClient.java index 0d8d42ceacc81..c6a14f2937dd2 100644 --- a/core/java/android/speech/tts/TextToSpeechClient.java +++ b/core/java/android/speech/tts/TextToSpeechClient.java @@ -39,6 +39,7 @@ import java.io.IOException; import java.util.Collections; import java.util.HashMap; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; /** * Synthesizes speech from text for immediate playback or to create a sound @@ -292,7 +293,8 @@ public final class TextToSpeechClient { * @param msFromStart * Miliseconds from the start of the synthesis. */ - public void onSynthesisProgress(UtteranceId utteranceId, int charIndex, int msFromStart) {} + public void onSynthesisProgress(UtteranceId utteranceId, int charIndex, + int msFromStart) {} } /** @@ -366,38 +368,28 @@ public final class TextToSpeechClient { } /** Unique synthesis request identifier. */ - public static final class UtteranceId { - private final String mDescription; + public static class UtteranceId { + /** Unique identifier */ + private final int id; + + /** Unique identifier generator */ + private static final AtomicInteger ID_GENERATOR = new AtomicInteger(); + /** * Create new, unique UtteranceId instance. */ public UtteranceId() { - mDescription = null; - } - - /** - * Create new, unique UtteranceId instance. - * - * @param description Additional string, that will be appended to - * {@link #toUniqueString()} output, allowing easier identification of the utterance in - * callbacks. - */ - public UtteranceId(String description) { - mDescription = description; + id = ID_GENERATOR.getAndIncrement(); } /** * Returns a unique string associated with an instance of this object. * - * If you subclass {@link UtteranceId} make sure that output of this method is - * consistent across multiple calls and unique for the instance. - * * This string will be used to identify the synthesis request/utterance inside the * TTS service. */ - public String toUniqueString() { - return mDescription == null ? "UtteranceId" + System.identityHashCode(this) : - "UtteranceId" + System.identityHashCode(this) + ": " + mDescription; + public final String toUniqueString() { + return "UID" + id; } } @@ -680,7 +672,8 @@ public final class TextToSpeechClient { public void onFallback(String utteranceIdStr) { synchronized (mLock) { - Pair callbacks = getCallback(utteranceIdStr); + Pair callbacks = getCallback( + utteranceIdStr); callbacks.second.onSynthesisFallback(callbacks.first); } };