Commit 27511e1a by Kunj Gupta

Fixed - Make mobile app stop listening to user based on server flag.

parent 0374c45b
...@@ -45,7 +45,7 @@ public class Speaker implements TextToSpeech.OnInitListener { ...@@ -45,7 +45,7 @@ public class Speaker implements TextToSpeech.OnInitListener {
} }
} }
public void speak(String text) { public void speak(String text, String utteranceId) {
//remove all special characters. //remove all special characters.
String speakText = text.replaceAll("--*", " "); String speakText = text.replaceAll("--*", " ");
// Speak only if the TTS is ready // Speak only if the TTS is ready
...@@ -54,7 +54,7 @@ public class Speaker implements TextToSpeech.OnInitListener { ...@@ -54,7 +54,7 @@ public class Speaker implements TextToSpeech.OnInitListener {
HashMap<String, String> hash = new HashMap<String, String>(); HashMap<String, String> hash = new HashMap<String, String>();
hash.put(TextToSpeech.Engine.KEY_PARAM_STREAM, hash.put(TextToSpeech.Engine.KEY_PARAM_STREAM,
String.valueOf(AudioManager.STREAM_MUSIC)); String.valueOf(AudioManager.STREAM_MUSIC));
hash.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "" + System.currentTimeMillis()); hash.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utteranceId);
tts.speak(Util.fromHtml(speakText).toString(), TextToSpeech.QUEUE_ADD, hash); tts.speak(Util.fromHtml(speakText).toString(), TextToSpeech.QUEUE_ADD, hash);
} }
} }
......
...@@ -91,6 +91,8 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD ...@@ -91,6 +91,8 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
/*According to this flag we'll close the conversation from mobile side. We'll get this flag in chat response*/ /*According to this flag we'll close the conversation from mobile side. We'll get this flag in chat response*/
private boolean mIsConversationEnd; private boolean mIsConversationEnd;
private String mNewMessageUtteranceId;
@Override @Override
public boolean onOptionsItemSelected(MenuItem menuItem) { public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == android.R.id.home) { if (menuItem.getItemId() == android.R.id.home) {
...@@ -228,7 +230,7 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD ...@@ -228,7 +230,7 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
droidSpeech.closeDroidSpeechOperations(); droidSpeech.closeDroidSpeechOperations();
} }
speaker.allow(true); speaker.allow(true);
speaker.speak(message); speaker.speak(message, mNewMessageUtteranceId);
} }
mMessages.add(chatBotHistory); mMessages.add(chatBotHistory);
...@@ -338,21 +340,23 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD ...@@ -338,21 +340,23 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
username = data.getString(NEW_MESSAGE_USER_NAME); username = data.getString(NEW_MESSAGE_USER_NAME);
message = data.getString(NEW_MESSAGE_MESSAGE); message = data.getString(NEW_MESSAGE_MESSAGE);
mIsConversationEnd = data.getBoolean(IS_END_OF_INTENT); mIsConversationEnd = data.getBoolean(IS_END_OF_INTENT);
mNewMessageUtteranceId = String.valueOf(System.currentTimeMillis());
} catch (JSONException e) { } catch (JSONException e) {
CatalogueLog.e(e.getMessage()); CatalogueLog.e(e.getMessage());
return; return;
} }
ChatBotUser localChatBotUser = ChatBotUserManager.getChatBotUsersByName(username); if(!message.isEmpty()) {
if(localChatBotUser == null) { ChatBotUser localChatBotUser = ChatBotUserManager.getChatBotUsersByName(username);
ChatBotUser chatBotUser = ChatBotUser.ChatBotUserBuilder.aChatBotUser() if (localChatBotUser == null) {
.setName(username) ChatBotUser chatBotUser = ChatBotUser.ChatBotUserBuilder.aChatBotUser()
.setUserSysId(ChatBotUser.CHAT_BOT_SYS_ID) .setName(username)
.build(); .setUserSysId(ChatBotUser.CHAT_BOT_SYS_ID)
ChatBotUserManager.save(chatBotUser); .build();
ChatBotUserManager.save(chatBotUser);
}
addMessage(username, message);
} }
addMessage(username, message);
} }
}); });
} }
...@@ -458,6 +462,18 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD ...@@ -458,6 +462,18 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
speaker.getTTS().setOnUtteranceProgressListener(new UtteranceProgressListener() { speaker.getTTS().setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override @Override
public void onDone(String utteranceId) { public void onDone(String utteranceId) {
/*Start Code
Sometime speaker is not getting stop that's why it is breaking.
* We need to forcefully stop it.*/
long utteranceIdLong = Long.parseLong(utteranceId);
long messageUtteranceId = Long.parseLong(mNewMessageUtteranceId);
if(utteranceIdLong == messageUtteranceId
&& speaker != null && speaker.isSpeaking()) {
speaker.stop();
}
/*End Code*/
if (!mIsConversationEnd && (!speaker.isSpeaking() && droidSpeech != null)) { if (!mIsConversationEnd && (!speaker.isSpeaking() && droidSpeech != null)) {
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment