Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
aa516b2
Initial commit
deekshithreddyr Jul 10, 2019
ad096da
Addressed review comments
deekshithreddyr Jul 16, 2019
2a0b27f
Added README File
deekshithreddyr Jul 16, 2019
bbd9f4e
Update README.md
deekshithreddyr Jul 17, 2019
a4f9ba8
Update README.md
deekshithreddyr Jul 17, 2019
b33d644
Update README.md
deekshithreddyr Jul 17, 2019
6ce8859
refactor: refactoring the code
deekshithreddyr Jul 21, 2019
0f862d4
Merge branch 'master' of https://github.com/deekshithreddyr/android-d…
deekshithreddyr Jul 21, 2019
2e38050
Add stopwatchagent.zip file
deekshithreddyr Jul 21, 2019
c3c2b78
Update README.md
deekshithreddyr Jul 22, 2019
bba436b
Update README.md
deekshithreddyr Jul 22, 2019
b168253
Changes has per Github comments
deekshithreddyr Jul 23, 2019
e7d5487
Combine the three requests into one. Dropdown instead of multi-selec…
deekshithreddyr Aug 6, 2019
9dcf0d1
Addressed review comments
deekshithreddyr Aug 8, 2019
9ddd430
Using the Audio returned from Dialogflow.
deekshithreddyr Aug 11, 2019
1c2b79f
Addressed review comments
deekshithreddyr Aug 13, 2019
462db73
Separated the Authentication code from AppController.java to AuthUtil…
deekshithreddyr Aug 14, 2019
c8371eb
Code Refinement
deekshithreddyr Aug 18, 2019
20b798a
Update README.md
deekshithreddyr Aug 18, 2019
d31ecb6
Update README.md
deekshithreddyr Aug 19, 2019
cafff50
Addressed review comments
deekshithreddyr Aug 26, 2019
6b8e4bd
Merge branch 'master' of https://github.com/deekshithreddyr/android-d…
deekshithreddyr Aug 26, 2019
4dd38b9
function to convert the time from UTC to local TimeZone added
deekshithreddyr Aug 26, 2019
73edd3f
Code readability update
deekshithreddyr Aug 30, 2019
12e2d39
Update README.md
deekshithreddyr Aug 30, 2019
26599e8
Speech2speech Initial commit
deekshithreddyr Aug 30, 2019
32020e9
Merge branch 'master' of https://github.com/deekshithreddyr/android-d…
deekshithreddyr Aug 30, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,21 @@ public class AppController extends Application {
public static final String SESSION_ID = "sessionId";
public static String PROJECT_ID = "";

public static void playAudio(byte[] mp3SoundByteArray) {
public static void playAudio(byte[] byteArray) {
MediaPlayer mediaPlayer = new MediaPlayer();
try {
File tempMp3 = File.createTempFile("dialogFlow", "mp3", Environment.getExternalStorageDirectory());
tempMp3.deleteOnExit();
FileOutputStream fos = new FileOutputStream(tempMp3);
fos.write(mp3SoundByteArray);
File tempFile = File.createTempFile("dialogFlow", null, Environment.getExternalStorageDirectory());
tempFile.deleteOnExit();
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(byteArray);
fos.close();
mediaPlayer.reset();
FileInputStream fis = new FileInputStream(tempMp3);
FileInputStream fis = new FileInputStream(tempFile);
mediaPlayer.setDataSource(fis.getFD());

mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException ex) {
String s = ex.toString();
ex.printStackTrace();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
import android.os.Bundle;
import android.os.Handler;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
Expand All @@ -35,11 +33,9 @@

import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

public class ChatActivity extends AppCompatActivity {

private static TextToSpeech textToSpeech;
private static ChatRecyclerViewAdapter chatRecyclerViewAdapter;
private static ArrayList<ChatMsgModel> chatMsgModels;
private static RecyclerView rvChats;
Expand Down Expand Up @@ -133,33 +129,6 @@ protected void onCreate(Bundle savedInstanceState) {

apiRequest = new ApiRequest();

initTextToSpeech();

}

/**
* function to initialize the Text To Speech for voice output
*/
private void initTextToSpeech() {
textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int ttsLang = textToSpeech.setLanguage(Locale.US);

if (ttsLang == TextToSpeech.LANG_MISSING_DATA
|| ttsLang == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "The Language is not supported!");
} else {
Log.i("TTS", "Language Supported.");
}
Log.i("TTS", "Initialization success.");
} else {
Toast.makeText(getApplicationContext(), "TTS Initialization failed!", Toast.LENGTH_SHORT).show();
}
}
});

}

/**
Expand Down Expand Up @@ -318,15 +287,6 @@ protected void onPause() {
}
}

@Override
protected void onDestroy() {
super.onDestroy();
if (textToSpeech != null) {
textToSpeech.stop();
textToSpeech.shutdown();
}
}

public void showMorePopup() {
PopupMenu popup = new PopupMenu(this, ibMore);
popup.inflate(R.menu.main_menu);
Expand Down Expand Up @@ -380,7 +340,6 @@ public APIRequest(String token, Date expiryTime, String msg, boolean tts, boolea
@Override
protected void onPreExecute() {
super.onPreExecute();
showProgressDialog();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this line gone, you can remove the onPreExecute() method override now that we just use the default.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

}

@Override
Expand All @@ -391,7 +350,6 @@ protected String doInBackground(Void... voids) {
@Override
protected void onPostExecute(String response) {
super.onPostExecute(response);
alert.dismiss();
addMsg(response, 0);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.google.cloud.examples.dialogflow.utils;

import android.content.Context;
import android.widget.Toast;

import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.Credentials;
import com.google.auth.oauth2.AccessToken;
Expand Down Expand Up @@ -49,7 +46,7 @@ public ApiRequest() {
* @param tts : send message to text to speech if true
* @param sentiment : send message to sentiment analysis if true
* @param knowledge : send message to knowledge base if true
* @return : response from the server
* @return : response from the server
*/
public String callAPI(String accessToken, Date expiryTime, String msg, boolean tts, boolean sentiment, boolean knowledge) {
this.token = accessToken;
Expand All @@ -64,7 +61,7 @@ public String callAPI(String accessToken, Date expiryTime, String msg, boolean t
* @param tts : send message to text to speech if true
* @param sentiment : send message to sentiment analysis if true
* @param knowledge : send message to knowledge base if true
* @return : response from the server
* @return : response from the server
*/
private String detectIntent(String msg, boolean tts, boolean sentiment, boolean knowledge) {
try {
Expand All @@ -91,7 +88,7 @@ private String detectIntent(String msg, boolean tts, boolean sentiment, boolean

sessionsClient.close();

if(tts) {
if (tts) {
AppController.playAudio(detectIntentResponse.getOutputAudio().toByteArray());
}

Expand All @@ -104,8 +101,9 @@ private String detectIntent(String msg, boolean tts, boolean sentiment, boolean

/**
* function to handle the results
* @param detectIntentResponse : detectIntentResponse object
* @return : String response
*
* @param detectIntentResponse : detectIntentResponse object
* @return : String response
*/
private String handleResults(DetectIntentResponse detectIntentResponse) {
QueryResult queryResult = detectIntentResponse.getQueryResult();
Expand Down Expand Up @@ -143,54 +141,52 @@ private String handleResults(DetectIntentResponse detectIntentResponse) {
* @return : DetectIntentRequest object
*/
private DetectIntentRequest getDetectIntentRequest(SessionName sessionName, QueryInput queryInput, boolean tts, boolean sentiment, boolean knowledge, FixedCredentialsProvider fixedCredentialsProvider) throws Exception {
DetectIntentRequest detectIntentRequest = DetectIntentRequest.newBuilder()
DetectIntentRequest.Builder detectIntentRequestBuilder = DetectIntentRequest.newBuilder()
.setSession(sessionName.toString())
.setQueryInput(queryInput)
.build();
QueryParameters queryParameters = QueryParameters.newBuilder().build();
.setQueryInput(queryInput);

QueryParameters.Builder queryParametersBuilder = QueryParameters.newBuilder();

if (tts) {
OutputAudioEncoding audioEncoding = OutputAudioEncoding.OUTPUT_AUDIO_ENCODING_LINEAR_16;
OutputAudioEncoding audioEncoding = OutputAudioEncoding.OUTPUT_AUDIO_ENCODING_MP3;
int sampleRateHertz = 16000;
OutputAudioConfig outputAudioConfig = OutputAudioConfig.newBuilder()
.setAudioEncoding(audioEncoding)
.setSampleRateHertz(sampleRateHertz)
.build();

detectIntentRequest.toBuilder()
.setOutputAudioConfig(outputAudioConfig)
.build();
detectIntentRequestBuilder.setOutputAudioConfig(outputAudioConfig);
}

if (sentiment) {
SentimentAnalysisRequestConfig sentimentAnalysisRequestConfig = SentimentAnalysisRequestConfig.newBuilder().setAnalyzeQueryTextSentiment(true).build();
SentimentAnalysisRequestConfig sentimentAnalysisRequestConfig =
SentimentAnalysisRequestConfig.newBuilder()
.setAnalyzeQueryTextSentiment(true).build();

queryParameters.toBuilder()
.setSentimentAnalysisRequestConfig(sentimentAnalysisRequestConfig)
.build();
detectIntentRequest.toBuilder()
.setQueryParams(queryParameters)
.build();
queryParametersBuilder
.setSentimentAnalysisRequestConfig(sentimentAnalysisRequestConfig);
}


if (knowledge) {
KnowledgeBasesSettings knowledgeSessionsSettings = KnowledgeBasesSettings.newBuilder().setCredentialsProvider(fixedCredentialsProvider).build();
ArrayList<String> knowledgeBaseNames = KnowledgeBaseUtils.listKnowledgeBases(AppController.PROJECT_ID, knowledgeSessionsSettings);
KnowledgeBasesSettings knowledgeSessionsSettings = KnowledgeBasesSettings.newBuilder()
.setCredentialsProvider(fixedCredentialsProvider).build();
ArrayList<String> knowledgeBaseNames =
KnowledgeBaseUtils.listKnowledgeBases(
AppController.PROJECT_ID, knowledgeSessionsSettings);

if (knowledgeBaseNames.size() > 0) {
// As an example, we'll only grab the first Knowledge Base
queryParameters.toBuilder()
.addKnowledgeBaseNames(knowledgeBaseNames.get(0))
.build();
detectIntentRequest.toBuilder()
.setQueryParams(queryParameters)
.build();
queryParametersBuilder.addKnowledgeBaseNames(knowledgeBaseNames.get(0));
}
}

return detectIntentRequest;
QueryParameters queryParameters = queryParametersBuilder.build();
detectIntentRequestBuilder.setQueryParams(queryParameters);

return detectIntentRequestBuilder.build();
}

}