Skip to content

Commit

Permalink
resolve false for all unsupoorted function in android. refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
Winglonelion committed Jun 17, 2018
1 parent d5ad082 commit 5f3ebe6
Showing 1 changed file with 15 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,19 @@ protected void doInBackgroundGuarded(Void... params){

@ReactMethod
public void isPaused(final Promise promise) {
promise.reject("This function doesn\'t exists on android !");
promise.resolve(false);
}

@ReactMethod
public void resume(final Promise promise) {
promise.reject("This function doesn\'t exists on android !");
FLog.e(ReactConstants.TAG, "resume function doesn\'t exists on android !");
promise.resolve(false);
}

@ReactMethod
public void pause(final Promise promise) {
promise.reject("This function doesn\'t exists on android !");
FLog.e(ReactConstants.TAG, "pause function doesn\'t exists on android !");
promise.resolve(false);
}

@ReactMethod
Expand All @@ -169,8 +171,7 @@ protected void doInBackgroundGuarded(Void... params){
}.execute();
}

@ReactMethod
public void speak(final ReadableMap args, final Promise promise) {
private void _speak(final ReadableMap args, final Promise promise) {
new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
@Override
protected void doInBackgroundGuarded(Void... params) {
Expand All @@ -191,9 +192,11 @@ protected void doInBackgroundGuarded(Void... params) {
queueMethod = TextToSpeech.QUEUE_ADD;
}
}

if(args.getString("text") == null || text == ""){
promise.reject("Text cannot be blank");
}

try {
if (voice != null && voice != "") {
tts.setLanguage(new Locale(voice));
Expand Down Expand Up @@ -231,65 +234,13 @@ protected void doInBackgroundGuarded(Void... params) {
}

@ReactMethod
public void speakWithFinish(final ReadableMap args, final Promise promise) {
new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
@Override
protected void doInBackgroundGuarded(Void... params) {
if(tts == null){
init();
}
String text = args.hasKey("text") ? args.getString("text") : null;
String voice = args.hasKey("voice") ? args.getString("voice") : null;
Boolean forceStop = args.hasKey("forceStop") ? args.getBoolean("forceStop") : true;
Float rate = args.hasKey("rate") ? (float) args.getDouble("rate") : null;
int queueMethod = TextToSpeech.QUEUE_FLUSH;

if(tts.isSpeaking()){
//Force to stop and start new speech
if(forceStop != null && forceStop){
tts.stop();
} else {
queueMethod = TextToSpeech.QUEUE_ADD;
}
}

if(args.getString("text") == null || text == ""){
promise.reject("Text cannot be blank");
}

try {
if (voice != null && voice != "") {
tts.setLanguage(new Locale(voice));
} else {
//Setting up default voice
tts.setLanguage(new Locale("en"));
}
//Set the rate if provided by the user
if(rate != null){
tts.setPitch(rate);
}

int speakResult = 0;
String speechUUID = UUID.randomUUID().toString();
if(Build.VERSION.SDK_INT >= 21) {
Bundle bundle = new Bundle();
bundle.putCharSequence(Engine.KEY_PARAM_UTTERANCE_ID, "");
ttsPromises.put(speechUUID, promise);
speakResult = tts.speak(text, queueMethod, bundle, speechUUID);
} else {
HashMap<String, String> map = new HashMap<String, String>();
map.put(Engine.KEY_PARAM_UTTERANCE_ID, speechUUID);
ttsPromises.put(speechUUID, promise);
speakResult = tts.speak(text, queueMethod, map);
}
public void speak(final ReadableMap args, final Promise promise) {
return _speak(args, promise);
}


if(speakResult < 0) {
throw new Exception("Speak with finsh failed, make sure that TTS service is installed on you device");
}
} catch (Exception e) {
promise.reject(e.getMessage());
}
}
}.execute();
@ReactMethod
public void speakWithFinish(final ReadableMap args, final Promise promise) {
return _speak(args, promise);
}
}

0 comments on commit 5f3ebe6

Please sign in to comment.