Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 17e13610 authored by Jean-Michel Trivi's avatar Jean-Michel Trivi
Browse files

Addressed comments of change 2515 for the TtsService class:

 - made the SpeechItem and SoundResource inner classes static,
 - prefixed the TtsService member variables by 'm',
 - changed indentation from 2 to 4 characters.
parent 446393ba
Loading
Loading
Loading
Loading
+635 −635
Original line number Diff line number Diff line
@@ -45,7 +45,7 @@ import java.util.concurrent.locks.ReentrantLock;
 */
public class TtsService extends Service implements OnCompletionListener {

  private class SpeechItem {
    private static class SpeechItem {
        public static final int SPEECH = 0;
        public static final int EARCON = 1;
        public static final int SILENCE = 2;
@@ -70,7 +70,7 @@ public class TtsService extends Service implements OnCompletionListener {
     * the package that contains the resource and the resID of the resource
     * within that package.
     */
  private class SoundResource {
    private static class SoundResource {
        public String mSourcePackageName = null;
        public int mResId = -1;
        public String mFilename = null;
@@ -94,12 +94,12 @@ public class TtsService extends Service implements OnCompletionListener {

    final RemoteCallbackList<ITtsCallback> mCallbacks = new RemoteCallbackList<ITtsCallback>();

  private Boolean isSpeaking;
  private ArrayList<SpeechItem> speechQueue;
  private HashMap<String, SoundResource> earcons;
  private HashMap<String, SoundResource> utterances;
  private MediaPlayer player;
  private TtsService self;
    private Boolean mIsSpeaking;
    private ArrayList<SpeechItem> mSpeechQueue;
    private HashMap<String, SoundResource> mEarcons;
    private HashMap<String, SoundResource> mUtterances;
    private MediaPlayer mPlayer;
    private TtsService mSelf;

    private SharedPreferences prefs;

@@ -124,14 +124,14 @@ public class TtsService extends Service implements OnCompletionListener {
        nativeSynth = new SynthProxy(prefs.getString("engine_pref", ""));


    self = this;
    isSpeaking = false;
        mSelf = this;
        mIsSpeaking = false;

    earcons = new HashMap<String, SoundResource>();
    utterances = new HashMap<String, SoundResource>();
        mEarcons = new HashMap<String, SoundResource>();
        mUtterances = new HashMap<String, SoundResource>();

    speechQueue = new ArrayList<SpeechItem>();
    player = null;
        mSpeechQueue = new ArrayList<SpeechItem>();
        mPlayer = null;

        setLanguage(prefs.getString("lang_pref", "en-rUS"));
        setSpeechRate(Integer.parseInt(prefs.getString("rate_pref", "140")));
@@ -210,7 +210,7 @@ public class TtsService extends Service implements OnCompletionListener {
     *            The resource ID of the sound within its package
     */
    private void addSpeech(String text, String packageName, int resId) {
    utterances.put(text, new SoundResource(packageName, resId));
        mUtterances.put(text, new SoundResource(packageName, resId));
    }

    /**
@@ -223,7 +223,7 @@ public class TtsService extends Service implements OnCompletionListener {
     *            path like: (/sdcard/mysounds/mysoundbite.mp3).
     */
    private void addSpeech(String text, String filename) {
    utterances.put(text, new SoundResource(filename));
        mUtterances.put(text, new SoundResource(filename));
    }

    /**
@@ -237,7 +237,7 @@ public class TtsService extends Service implements OnCompletionListener {
     *            The resource ID of the sound within its package
     */
    private void addEarcon(String earcon, String packageName, int resId) {
    earcons.put(earcon, new SoundResource(packageName, resId));
        mEarcons.put(earcon, new SoundResource(packageName, resId));
    }

    /**
@@ -250,7 +250,7 @@ public class TtsService extends Service implements OnCompletionListener {
     *            path like: (/sdcard/mysounds/mysoundbite.mp3).
     */
    private void addEarcon(String earcon, String filename) {
    earcons.put(earcon, new SoundResource(filename));
        mEarcons.put(earcon, new SoundResource(filename));
    }

    /**
@@ -269,8 +269,8 @@ public class TtsService extends Service implements OnCompletionListener {
        if (queueMode == 0) {
            stop();
        }
    speechQueue.add(new SpeechItem(text, params, SpeechItem.SPEECH));
    if (!isSpeaking) {
        mSpeechQueue.add(new SpeechItem(text, params, SpeechItem.SPEECH));
        if (!mIsSpeaking) {
            processSpeechQueue();
        }
    }
@@ -292,8 +292,8 @@ public class TtsService extends Service implements OnCompletionListener {
        if (queueMode == 0) {
            stop();
        }
    speechQueue.add(new SpeechItem(earcon, params, SpeechItem.EARCON));
    if (!isSpeaking) {
        mSpeechQueue.add(new SpeechItem(earcon, params, SpeechItem.EARCON));
        if (!mIsSpeaking) {
            processSpeechQueue();
        }
    }
@@ -303,13 +303,13 @@ public class TtsService extends Service implements OnCompletionListener {
     */
    private void stop() {
        Log.i("TTS", "Stopping");
    speechQueue.clear();
        mSpeechQueue.clear();

        nativeSynth.stop();
    isSpeaking = false;
    if (player != null) {
        mIsSpeaking = false;
        if (mPlayer != null) {
            try {
        player.stop();
                mPlayer.stop();
            } catch (IllegalStateException e) {
                // Do nothing, the player is already stopped.
            }
@@ -326,8 +326,8 @@ public class TtsService extends Service implements OnCompletionListener {
        if (queueMode == 0) {
            stop();
        }
    speechQueue.add(new SpeechItem(duration));
    if (!isSpeaking) {
        mSpeechQueue.add(new SpeechItem(duration));
        if (!mIsSpeaking) {
            processSpeechQueue();
        }
    }
@@ -387,9 +387,9 @@ public class TtsService extends Service implements OnCompletionListener {
        if (speechItem.mType == SpeechItem.SILENCE) {
            // Do nothing if this is just silence
        } else if (speechItem.mType == SpeechItem.EARCON) {
      sr = earcons.get(text);
            sr = mEarcons.get(text);
        } else {
      sr = utterances.get(text);
            sr = mUtterances.get(text);
        }
        return sr;
    }
@@ -417,8 +417,8 @@ public class TtsService extends Service implements OnCompletionListener {
            if (!speechQueueAvailable) {
                return;
            }
      if (speechQueue.size() < 1) {
        isSpeaking = false;
            if (mSpeechQueue.size() < 1) {
                mIsSpeaking = false;
                // Dispatch a completion here as this is the
                // only place where speech completes normally.
                // Nothing left to say in the queue is a special case
@@ -427,8 +427,8 @@ public class TtsService extends Service implements OnCompletionListener {
                return;
            }

      SpeechItem currentSpeechItem = speechQueue.get(0);
      isSpeaking = true;
            SpeechItem currentSpeechItem = mSpeechQueue.get(0);
            mIsSpeaking = true;
            SoundResource sr = getSoundResource(currentSpeechItem);
            // Synth speech as needed - synthesizer should call
            // processSpeechQueue to continue running the queue
@@ -448,7 +448,7 @@ public class TtsService extends Service implements OnCompletionListener {
                cleanUpPlayer();
                if (sr.mSourcePackageName == PKGNAME) {
                    // Utterance is part of the TTS library
          player = MediaPlayer.create(this, sr.mResId);
                    mPlayer = MediaPlayer.create(this, sr.mResId);
                } else if (sr.mSourcePackageName != null) {
                    // Utterance is part of the app calling the library
                    Context ctx;
@@ -457,36 +457,36 @@ public class TtsService extends Service implements OnCompletionListener {
                                0);
                    } catch (NameNotFoundException e) {
                        e.printStackTrace();
            speechQueue.remove(0); // Remove it from the queue and
                        mSpeechQueue.remove(0); // Remove it from the queue and
                        // move on
            isSpeaking = false;
                        mIsSpeaking = false;
                        return;
                    }
          player = MediaPlayer.create(ctx, sr.mResId);
                    mPlayer = MediaPlayer.create(ctx, sr.mResId);
                } else {
                    // Utterance is coming from a file
          player = MediaPlayer.create(this, Uri.parse(sr.mFilename));
                    mPlayer = MediaPlayer.create(this, Uri.parse(sr.mFilename));
                }

                // Check if Media Server is dead; if it is, clear the queue and
                // give up for now - hopefully, it will recover itself.
        if (player == null) {
          speechQueue.clear();
          isSpeaking = false;
                if (mPlayer == null) {
                    mSpeechQueue.clear();
                    mIsSpeaking = false;
                    return;
                }
        player.setOnCompletionListener(this);
                mPlayer.setOnCompletionListener(this);
                try {
          player.start();
                    mPlayer.start();
                } catch (IllegalStateException e) {
          speechQueue.clear();
          isSpeaking = false;
                    mSpeechQueue.clear();
                    mIsSpeaking = false;
                    cleanUpPlayer();
                    return;
                }
            }
      if (speechQueue.size() > 0) {
        speechQueue.remove(0);
            if (mSpeechQueue.size() > 0) {
                mSpeechQueue.remove(0);
            }
        } finally {
            // This check is needed because finally will always run; even if the
@@ -498,9 +498,9 @@ public class TtsService extends Service implements OnCompletionListener {
    }

    private void cleanUpPlayer() {
    if (player != null) {
      player.release();
      player = null;
        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }

@@ -583,7 +583,7 @@ public class TtsService extends Service implements OnCompletionListener {
         */
        public void setEngine(String engineName, String[] supportedLanguages,
                int strictness) {
      self.setEngine(engineName, supportedLanguages, strictness);
            mSelf.setEngine(engineName, supportedLanguages, strictness);
        }

        /**
@@ -596,7 +596,7 @@ public class TtsService extends Service implements OnCompletionListener {
         *            support.
         */
        public void setEngineWithIntent(Intent engineIntent) {
      self.setEngine(engineIntent);
            mSelf.setEngine(engineIntent);
        }

        /**
@@ -617,7 +617,7 @@ public class TtsService extends Service implements OnCompletionListener {
            if (params != null) {
                speakingParams = new ArrayList<String>(Arrays.asList(params));
            }
      self.speak(text, queueMode, speakingParams);
            mSelf.speak(text, queueMode, speakingParams);
        }

        /**
@@ -636,7 +636,7 @@ public class TtsService extends Service implements OnCompletionListener {
            if (params != null) {
                speakingParams = new ArrayList<String>(Arrays.asList(params));
            }
      self.playEarcon(earcon, queueMode, speakingParams);
            mSelf.playEarcon(earcon, queueMode, speakingParams);
        }

        /**
@@ -655,7 +655,7 @@ public class TtsService extends Service implements OnCompletionListener {
            if (params != null) {
                speakingParams = new ArrayList<String>(Arrays.asList(params));
            }
      self.playSilence(duration, queueMode, speakingParams);
            mSelf.playSilence(duration, queueMode, speakingParams);
        }


@@ -664,7 +664,7 @@ public class TtsService extends Service implements OnCompletionListener {
         * queue.
         */
        public void stop() {
      self.stop();
            mSelf.stop();
        }

        /**
@@ -673,7 +673,7 @@ public class TtsService extends Service implements OnCompletionListener {
         * @return Boolean to indicate whether or not the TTS is speaking
         */
        public boolean isSpeaking() {
      return (self.isSpeaking && (speechQueue.size() < 1));
            return (mSelf.mIsSpeaking && (mSpeechQueue.size() < 1));
        }

        /**
@@ -687,7 +687,7 @@ public class TtsService extends Service implements OnCompletionListener {
         *            The resource ID of the sound within its package
         */
        public void addSpeech(String text, String packageName, int resId) {
      self.addSpeech(text, packageName, resId);
            mSelf.addSpeech(text, packageName, resId);
        }

        /**
@@ -700,7 +700,7 @@ public class TtsService extends Service implements OnCompletionListener {
         *            complete path like: (/sdcard/mysounds/mysoundbite.mp3).
         */
        public void addSpeechFile(String text, String filename) {
      self.addSpeech(text, filename);
            mSelf.addSpeech(text, filename);
        }

        /**
@@ -714,7 +714,7 @@ public class TtsService extends Service implements OnCompletionListener {
         *            The resource ID of the sound within its package
         */
        public void addEarcon(String earcon, String packageName, int resId) {
      self.addEarcon(earcon, packageName, resId);
            mSelf.addEarcon(earcon, packageName, resId);
        }

        /**
@@ -727,7 +727,7 @@ public class TtsService extends Service implements OnCompletionListener {
         *            complete path like: (/sdcard/mysounds/mysoundbite.mp3).
         */
        public void addEarconFile(String earcon, String filename) {
      self.addEarcon(earcon, filename);
            mSelf.addEarcon(earcon, filename);
        }

        /**
@@ -738,7 +738,7 @@ public class TtsService extends Service implements OnCompletionListener {
         *            The speech rate that should be used
         */
        public void setSpeechRate(int speechRate) {
      self.setSpeechRate(speechRate);
            mSelf.setSpeechRate(speechRate);
        }

        // TODO: Fix comment about language
@@ -753,7 +753,7 @@ public class TtsService extends Service implements OnCompletionListener {
         *            http://en.wikipedia.org/wiki/IETF_language_tag
         */
        public void setLanguage(String language) {
      self.setLanguage(language);
            mSelf.setLanguage(language);
        }

        /**
@@ -776,7 +776,7 @@ public class TtsService extends Service implements OnCompletionListener {
            if (params != null) {
                speakingParams = new ArrayList<String>(Arrays.asList(params));
            }
      return self.synthesizeToFile(text, speakingParams, filename, true);
            return mSelf.synthesizeToFile(text, speakingParams, filename, true);
        }
    };