Skip to content

Commit 716b787

Browse files
authored
Merge pull request #1 from verloop/button_listeners
Adding a way to get the click events in the mobile app
2 parents 92cb43f + 94acc46 commit 716b787

6 files changed

Lines changed: 83 additions & 18 deletions

File tree

app/build.gradle

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ android {
1616
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
1717
}
1818
}
19+
compileOptions {
20+
sourceCompatibility = 1.8
21+
targetCompatibility = 1.8
22+
}
1923
}
2024

2125
dependencies {
@@ -25,10 +29,7 @@ dependencies {
2529
implementation 'com.android.support:support-v4:28.0.0'
2630
implementation 'com.google.firebase:firebase-messaging:17.3.3'
2731

28-
// implementation project(':sdk')
29-
// implementation 'com.gitlab.indusbit:verloop-android-app-for-sdk:1.0.2'
30-
implementation 'com.gitlab.indusbit:verloop-android-app-for-sdk:master-SNAPSHOT'
31-
// implementation 'io.verloop.sdk:livechat:0.0.1.0'
32+
implementation 'com.github.verloop:android-sdk:1.0.4-rc08'
3233
testImplementation 'junit:junit:4.12'
3334
androidTestImplementation 'com.android.support.test:runner:1.0.2'
3435
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

app/src/main/java/io/verloop/MainActivity.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected void onCreate(Bundle savedInstanceState) {
3434
super.onCreate(savedInstanceState);
3535
setContentView(R.layout.activity_main);
3636

37-
Button button = findViewById(R.id.button);
37+
final Button button = findViewById(R.id.button);
3838
findViewById(R.id.logout).setOnClickListener(new View.OnClickListener() {
3939
@Override
4040
public void onClick(View v) {
@@ -83,6 +83,10 @@ public void onComplete(@NonNull Task<InstanceIdResult> task) {
8383
config.setUserEmail("anthony@gfam.com");
8484
config.setUserName("Anthony Gonsalves");
8585
config.setUserPhone("8890656400");
86+
config.setButtonOnClickListener((title, type, payload) -> {
87+
// Add the callbacks on button clicks
88+
Log.d(TAG, "title is " + title);
89+
});
8690
verloop = new Verloop(MainActivity.this, config);
8791

8892
// verloop.login("");
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.verloop.sdk;
2+
3+
public interface LiveChatButtonClickListener {
4+
void buttonClicked(String title, String type, String payload);
5+
}

sdk/src/main/java/io/verloop/sdk/Verloop.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package io.verloop.sdk;
22

3+
import android.content.BroadcastReceiver;
34
import android.content.Context;
45
import android.content.Intent;
6+
import android.content.IntentFilter;
57
import android.content.SharedPreferences;
6-
import android.os.Build;
8+
import android.net.ConnectivityManager;
9+
import android.support.v4.content.LocalBroadcastManager;
710
import android.util.Log;
811

912
import java.util.UUID;
@@ -37,12 +40,12 @@ public class Verloop {
3740
private String clientId;
3841
private String fcmToken;
3942
private boolean isStaging;
43+
private LiveChatButtonClickListener buttonOnClickListener;
44+
4045

4146
/**
42-
*
43-
*
4447
* @param context Context of an activity/service.
45-
* @param config The <code>VerloopConfig</code> object for the current user.
48+
* @param config The <code>VerloopConfig</code> object for the current user.
4649
*/
4750
public Verloop(Context context, VerloopConfig config) {
4851
this.context = context;
@@ -51,30 +54,29 @@ public Verloop(Context context, VerloopConfig config) {
5154
this.clientId = config.getClientId();
5255
this.fcmToken = config.getFcmToken();
5356
this.isStaging = config.getStaging();
57+
this.buttonOnClickListener = config.getButtonOnClickListener();
5458

5559
config.save(getPreferences());
5660

5761
this.startService();
5862
}
5963

6064
/**
65+
* @param userId User ID of the user you want to log in the app.
6166
* @deprecated Use {@link #login(VerloopConfig)} instead.
62-
*
67+
* <p>
6368
* Login a different user than the one that this object was initialized with.
64-
*
65-
* @param userId User ID of the user you want to log in the app.
6669
*/
6770
public void login(String userId) {
6871
login(userId, null);
6972
}
7073

7174
/**
75+
* @param userId User ID of the user you want to log in the app.
76+
* @param fcmToken FCM Token of the user being logged in.
7277
* @deprecated Use {@link #login(VerloopConfig)} instead.
73-
*
78+
* <p>
7479
* Login a different user than the one that this object was initialized with.
75-
*
76-
* @param userId User ID of the user you want to log in the app.
77-
* @param fcmToken FCM Token of the user being logged in.
7880
*/
7981
public void login(String userId, String fcmToken) {
8082
stopService();
@@ -129,6 +131,24 @@ public void showChat() {
129131

130132
Intent i = new Intent(context, VerloopActivity.class);
131133
context.startActivity(i);
134+
135+
if(buttonOnClickListener != null){
136+
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
137+
filter.addAction(context.getPackageName() + ".BUTTON_CLICK_LISTENER_VERLOOP_INTERFACE");
138+
LocalBroadcastManager.getInstance(context).registerReceiver(new BroadcastReceiver() {
139+
@Override
140+
public void onReceive(Context context, Intent intent) {
141+
142+
String title = intent.getStringExtra(VerloopInterface.BUTTON_TITLE);
143+
String type = intent.getStringExtra(VerloopInterface.BUTTON_TYPE);
144+
String payload = intent.getStringExtra(VerloopInterface.BUTTON_PAYLOAD);
145+
146+
Log.d(TAG, "Button click event received Title: " + title + " Type: " + type + " Payload " + payload);
147+
148+
buttonOnClickListener.buttonClicked(title, type, payload);
149+
}
150+
}, filter);
151+
}
132152
}
133153

134154
private String retrieveUserId(VerloopConfig config) {

sdk/src/main/java/io/verloop/sdk/VerloopConfig.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public class VerloopConfig {
2424

2525
private String recipeId;
2626

27+
private LiveChatButtonClickListener buttonOnClickListener;
28+
2729
public enum Scope {
2830
USER,
2931
ROOM
@@ -54,6 +56,10 @@ boolean getStaging() {
5456
return isStaging;
5557
}
5658

59+
LiveChatButtonClickListener getButtonOnClickListener() {
60+
return this.buttonOnClickListener;
61+
}
62+
5763
void setUserId(String userId) {
5864
this.userId = userId;
5965
}
@@ -78,6 +84,10 @@ public void setUserPhone(String userPhone) {
7884
this.userPhone = userPhone;
7985
}
8086

87+
public void setButtonOnClickListener(LiveChatButtonClickListener buttonOnClickListener) {
88+
this.buttonOnClickListener = buttonOnClickListener;
89+
}
90+
8191
public void putCustomField(String key, String value, Scope scope) {
8292
fields.add(new CustomField(key, value, scope));
8393
}
@@ -103,7 +113,6 @@ void save(SharedPreferences preferences) {
103113
editor.putString(Verloop.CONFIG_RECIPE_ID, this.recipeId);
104114

105115

106-
107116
JSONObject object = new JSONObject();
108117
for (CustomField field : fields) {
109118
try {

sdk/src/main/java/io/verloop/sdk/VerloopInterface.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111

1212
public class VerloopInterface {
1313

14+
private static final String TAG = "VerloopInterface";
15+
16+
static String BUTTON_TITLE = "BUTTON_TITLE";
17+
static String BUTTON_TYPE = "BUTTON_TYPE";
18+
static String BUTTON_PAYLOAD = "BUTTON_PAYLOAD";
19+
1420
private VerloopFragment fragment;
1521
private Context context;
1622

@@ -21,7 +27,7 @@ public class VerloopInterface {
2127

2228
@JavascriptInterface
2329
public void clientInfo(String json) throws JSONException {
24-
Log.d("VerloopInterface ", "DDD clientInfo " + json);
30+
Log.d(TAG, "DDD clientInfo " + json);
2531
JSONObject jsonObject = new JSONObject(json);
2632

2733
String title = jsonObject.getString("title");
@@ -38,4 +44,24 @@ public void clientInfo(String json) throws JSONException {
3844
intent.setAction(action);
3945
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
4046
}
47+
48+
@JavascriptInterface
49+
public void onButtonClick(String json) throws JSONException {
50+
Log.d(TAG, " onButtonClick " + json);
51+
52+
JSONObject jsonObject = new JSONObject(json);
53+
54+
String type = jsonObject.getString("type");
55+
String title = jsonObject.getString("title");
56+
String payload = jsonObject.getString("payload");
57+
58+
String action = context.getPackageName() + ".BUTTON_CLICK_LISTENER_VERLOOP_INTERFACE";
59+
60+
Intent intent = new Intent();
61+
intent.setAction(action);
62+
intent.putExtra(BUTTON_TITLE, title);
63+
intent.putExtra(BUTTON_TYPE, type);
64+
intent.putExtra(BUTTON_PAYLOAD, payload);
65+
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
66+
}
4167
}

0 commit comments

Comments
 (0)