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 6 commits
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
9 changes: 9 additions & 0 deletions dialoflow/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.externalNativeBuild
69 changes: 69 additions & 0 deletions dialoflow/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Dialogflow Sample

This app demonstrates how to make gRPC connections to the [Dialogflow API](https://cloud.google.com/dialogflow-enterprise/)

The app demonstrates how to detect intents:
- Via Text
- Via Streaming Audio
- With Sentiment Analysis
- With Text-to-Speech
- With Knowledge Connectors

To call the Dialogflow API from Android, you need to get authorization tokens from Firebase Cloud Messaging for them to be accepted by the Dialogflow API. To get this token, this sample uses a Firebase Function (in Node.js) to generate these tokens on the behalf of a service account to be used by the app when making a request to the Dialogflow API.

## Prerequisites
- An Android device or emulator
- Android Studio 3 or later

## Setup
- Create a project (or use an existing one) in the [Google Cloud Console][cloud-console]
- Enable the [Dialogflow API](https://console.cloud.google.com/apis/library/dialogflow.googleapis.com).
- Enable the [IAM Service Account Credentials API](https://pantheon.corp.google.com/apis/library/iamcredentials.googleapis.com).
- [Enable billing][billing].
- Be sure that you have gone through the steps by expanding the [Create an agent](https://cloud.google.com/dialogflow-enterprise/docs/quickstart-console#create-an-agent) to create and configure your stopwatch agent.
- [Import the Dialogflow Agent](https://dialogflow.com/docs/agents/export-import-restore#import) using the `StopwatchAgent.zip` which is located in the `stopwatch` directory.
- [Create a Service account](https://cloud.google.com/iam/docs/creating-managing-service-accounts) with the following IAM role: `Dialogflow API Client`. Example name: `dialogflow-client`. ([For more info on: how to add roles to a Service Account](https://cloud.google.com/iam/docs/granting-roles-to-service-accounts#granting_access_to_a_service_account_for_a_resource))
- Enable beta features for:
- [Sentiment Analysis](https://cloud.google.com/dialogflow-enterprise/docs/sentiment#enable_beta_features)
- [Text-to-Speech](https://cloud.google.com/dialogflow-enterprise/docs/detect-intent-tts#enable_beta_features)
- [Knowledge Connectors](https://cloud.google.com/dialogflow-enterprise/docs/knowledge-connectors#enable_beta_features)

### Setup the app
-Clone this repository `git clone https://github.com/GoogleCloudPlatform/android-docs-samples.git`
- Replace PROJECT_ID in AppController.java with your Project ID

###  Setup Firebase on the application:
- Complete the steps for [Add Firebase to your app](https://firebase.google.com/docs/android/setup) and expand the "Create a Firebase project" section for instructions on how to add project to your Firebase console. Note: No need to complete any other sections, they are already done.
- In the [Firebase console](https://console.firebase.google.com/), open the "Authentication" section under Develop.
- On the **Sign-in Methods** page, enable the **Anonymous** sign-in method.
- Give the package name of the app as `com.google.cloud.examples.dialogflow`

###  Setup and Deploy the Firebase Function
The Firebase Function provides auth tokens to your app, You'll be using a provided sample function to be run with this app.

- Follow the steps in this [guide](https://firebase.google.com/docs/functions/get-started) for:
- "1. Set up Node.js and the Firebase CLI"
- "2. Initialize Firebase SDK for Cloud Functions".
- Replace `index.js` file with the [provided index.js](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/functions/dialogflow/functions/index.js).
- Open `index.js`, go to function "generateAccessToken", and replace “SERVICE-ACCOUNT-NAME@YOUR_PROJECT_ID.iam.gserviceaccount.com” with your Service account name (`dialogflow-client`) and project id.
- Deploy getOAuthToken method by running command:
```
firebase deploy --only functions
```
- For your "App Engine Default Service Account" add the following IAM role: `Service Account Token Creator` . ([For more info on: how to add roles to a Service Account](https://cloud.google.com/iam/docs/granting-roles-to-service-accounts#granting_access_to_a_service_account_for_a_resource))

- For more info please refer (https://firebase.google.com/docs/functions/get-started).

## Run the app
- You are now ready to build and run the project. In Android Studio you can do this by clicking the 'Play' button in the toolbar. This will launch the app on the emulator or on the device you've selected.
- As soon the app launches, it will ask for the google sign-in.
- After successful signing in, choose the option by selecting a checkbox and click on chat button
- Type the message to send and click on the send button on the bottom right.
- Alternatively tap on the mic button to speak and send the message to the Dialogflow.


[cloud-console]: https://console.cloud.google.com
[git]: https://git-scm.com/
[android-studio]: https://developer.android.com/studio
[billing]: https://console.cloud.google.com/billing?project=_
[Firebase]: https://firebase.google.com/
1 change: 1 addition & 0 deletions dialoflow/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
60 changes: 60 additions & 0 deletions dialoflow/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 28
defaultConfig {
applicationId "com.google.cloud.examples.dialogflow"
minSdkVersion 26
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/notice.txt'
exclude 'META-INF/ASL2.0'
exclude 'META-INF/INDEX.LIST'
exclude 'META-INF/io.netty.versions.properties'
}
}

dependencies {

configurations.all {
resolutionStrategy.force 'com.android.support:support-annotations:25.3.0'
}
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'

implementation 'com.google.firebase:firebase-core:17.0.0'
implementation 'com.google.firebase:firebase-auth:18.0.0'
implementation 'com.firebaseui:firebase-ui-auth:4.1.0'
implementation 'com.google.firebase:firebase-messaging:19.0.1'
implementation 'com.google.firebase:firebase-functions:18.0.0'
implementation 'com.android.volley:volley:1.1.1'
implementation 'com.google.cloud:google-cloud-dialogflow:0.96.0-alpha'
implementation group: 'io.grpc', name: 'grpc-okhttp', version: '1.21.0'
implementation group: 'io.grpc', name: 'grpc-netty', version: '1.21.0'

}


apply plugin: 'com.google.gms.google-services'
21 changes: 21 additions & 0 deletions dialoflow/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.google.cloud.examples.dialogflow;

import android.content.Context;
import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("com.example.mlapitest", appContext.getPackageName());
}
}
36 changes: 36 additions & 0 deletions dialoflow/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.google.cloud.examples.dialogflow">

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />

<application
android:name="com.google.cloud.examples.dialogflow.AppController"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.google.cloud.examples.dialogflow.ui.ChatActivity" />
<activity android:name="com.google.cloud.examples.dialogflow.ui.WelcomeActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.google.cloud.examples.dialogflow.ui.MainActivity" />
<service
android:name="com.google.cloud.examples.dialogflow.service.MyFirebaseCloudMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>

</manifest>
Binary file added dialoflow/app/src/main/assets/book_a_room.wav
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.google.cloud.examples.dialogflow;

import android.app.Application;

import com.google.firebase.functions.FirebaseFunctions;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class AppController extends Application {

public static final String TOKEN_RECEIVED = "TOKEN_RECEIVED";
public static final String PROJECT_ID = "PROJECT_ID";
public static final String SESSION_ID = "sessionId";
public static String firebaseInstanceId = "";

public static String token = "";
public static Date exipryTime;

/**
* function to call the firebase function which will send the fcm message containing token and expiry time to the device
*/
public static void callFirebaseFunction() {
Map<String, String> data = new HashMap<>();
data.put("deviceId", AppController.firebaseInstanceId);

FirebaseFunctions firebaseFunctions;
firebaseFunctions = FirebaseFunctions.getInstance();

firebaseFunctions
.getHttpsCallable("getOAuthToken")
.call(data);
}

/**
* function to convert string to date
* @param dt : string date
Copy link
Contributor

Choose a reason for hiding this comment

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

dt --> date (use descriptive variable names)

* @return : converted date object
*/
public static Date getDateFromString(String dt) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
Date date = format.parse(dt);
return date;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.google.cloud.examples.dialogflow.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.cloud.examples.dialogflow.R;
import com.google.cloud.examples.dialogflow.model.ChatMsgModel;

import java.util.ArrayList;

public class ChatRecyclerViewAdapter extends RecyclerView.Adapter<ChatRecyclerViewAdapter.MyViewHolder> {

private ArrayList<ChatMsgModel> chatMsgModels;
private Context context;

public ChatRecyclerViewAdapter(Context context, ArrayList<ChatMsgModel> chatMsgModels) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You aren't using the context can delete it.

this.context = context;
this.chatMsgModels = chatMsgModels;

}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.custom_chat_recyclerview_item, parent, false);

return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
final ChatMsgModel chatMsgModel = chatMsgModels.get(position);

if(chatMsgModel.getType()==1) { // Message Sent
holder.tvMsgSent.setText(chatMsgModel.getMsg());
holder.tvMsgSent.setVisibility(View.VISIBLE);
holder.tvMsgReceived.setVisibility(View.GONE);
} else {
holder.tvMsgReceived.setText(chatMsgModel.getMsg());
holder.tvMsgReceived.setVisibility(View.VISIBLE);
holder.tvMsgSent.setVisibility(View.GONE);
}


}

@Override
public int getItemCount() {
return chatMsgModels.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder {
RelativeLayout rlMain;
TextView tvMsgSent;
TextView tvMsgReceived;

public MyViewHolder(View view) {
super(view);
rlMain = view.findViewById(R.id.rlMain);
tvMsgSent = view.findViewById(R.id.tvMsgSent);
tvMsgReceived = view.findViewById(R.id.tvMsgReceived);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.google.cloud.examples.dialogflow.model;

public class ChatMsgModel {

private String msg;
private int type;

public ChatMsgModel(String msg, int type) {
this.msg = msg;
this.type = type;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This method isn't used.

this.msg = msg;
}

public int getType() {
return type;
}

public void setType(int type) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This method isn't used

Copy link
Author

Choose a reason for hiding this comment

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

Removed

this.type = type;
}
}
Loading