Skip to content

Commit 0acc4a0

Browse files
authored
Merge pull request #1 from getyoti/ED-553
[TASK] [ED-553] Adding YotiCalled callback
2 parents 57b2726 + 40e6c1b commit 0acc4a0

File tree

7 files changed

+69
-15
lines changed

7 files changed

+69
-15
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,19 @@ yotiSDKButton.setOnYotiButtonListener(new YotiSDKButton.OnYotiButtonClickListene
179179
});
180180
```
181181

182+
There is also a listener that you can set to be notified when the intent has been sent to the Yoti app.
183+
When this happens you would probably want to restore your state.
184+
185+
186+
```java
187+
yotiSDKButton.setOnYotiCalledListener(new YotiSDKButton.OnYotiCalledListener() {
188+
@Override
189+
public void onYotiCalled() {
190+
// Restore the original state
191+
}
192+
});
193+
```
194+
[See this code in one of our sample apps](./sample-app/src/main/java/com/yoti/mobile/android/sdk/sampleapp/MainActivity.java)
182195

183196
You can activate a verbose mode for the SDK by using this method :
184197
```java

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ allprojects {
3333

3434
// Testing
3535
libJunit = "junit:junit:${libJunitVersion}"
36-
libMockito = "org.mockito:mockito-all:${libMockitoVersion}"
36+
libMockito = "org.mockito:mockito-core:${libMockitoVersion}"
3737
}
3838
}

sample-app/src/main/java/com/yoti/mobile/android/sdk/sampleapp/MainActivity.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ public void onYotiAppNotInstalledError(YotiSDKNoYotiAppException cause) {
4747
}
4848
});
4949

50+
yotiSDKButton.setOnYotiCalledListener(new YotiSDKButton.OnYotiCalledListener() {
51+
@Override
52+
public void onYotiCalled() {
53+
// Restore the original state
54+
yotiSDKButton.setVisibility(View.VISIBLE);
55+
progress.setVisibility(View.GONE);
56+
}
57+
});
58+
5059
if (getIntent().hasExtra(ShareAttributesResultBroadcastReceiver.EXTRA_CANCELLED_BY_USER)) {
5160
yotiSDKButton.setVisibility(View.VISIBLE);
5261
progress.setVisibility(View.GONE);

yoti-sdk/src/main/java/com/yoti/mobile/android/sdk/YotiSDK.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.content.Context;
44
import android.content.pm.PackageInfo;
55
import android.content.pm.PackageManager;
6+
import android.os.ResultReceiver;
67
import android.support.annotation.Nullable;
78

89
import com.yoti.mobile.android.sdk.exceptions.YotiSDKException;
@@ -92,7 +93,9 @@ public static Scenario getScenario(String useCaseId) {
9293
* @throws YotiSDKException
9394
*/
9495
/*package*/
95-
static void startScenario(final Context context, final String useCaseId, final boolean handleNoYotiAppError) throws YotiSDKException {
96+
static void startScenario(final Context context, final String useCaseId,
97+
final boolean handleNoYotiAppError,
98+
final ResultReceiver onYotiCalledResultReceiver) throws YotiSDKException {
9699

97100
YotiSDKLogger.debug("Starting scenario " + useCaseId);
98101

@@ -127,7 +130,7 @@ static void startScenario(final Context context, final String useCaseId, final b
127130
}
128131

129132
YotiSDKLogger.debug("Started scenario " + useCaseId);
130-
KernelSDKIntentService.startActionStartScenario(context, useCaseId);
133+
KernelSDKIntentService.startActionStartScenario(context, useCaseId, onYotiCalledResultReceiver);
131134
}
132135

133136
/**

yoti-sdk/src/main/java/com/yoti/mobile/android/sdk/YotiSDKButton.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import android.content.Context;
44
import android.content.res.TypedArray;
5+
import android.os.Bundle;
6+
import android.os.Handler;
7+
import android.os.ResultReceiver;
8+
import android.support.annotation.NonNull;
59
import android.support.annotation.Nullable;
610
import android.support.v7.widget.AppCompatButton;
711
import android.util.AttributeSet;
@@ -19,6 +23,17 @@ public class YotiSDKButton extends YotiButton implements View.OnClickListener {
1923
private String mUseCaseId;
2024
private OnYotiButtonClickListener mOnYotiButtonClickListener;
2125
private OnYotiAppNotInstalledListener mOnYotiAppNotInstalledListener;
26+
private OnYotiCalledListener mOnYotiCalledListener;
27+
28+
private ResultReceiver mYotiCallResultReceiver = new ResultReceiver(new Handler()) {
29+
@Override
30+
protected void onReceiveResult(int resultCode, Bundle resultData) {
31+
super.onReceiveResult(resultCode, resultData);
32+
if (mOnYotiCalledListener != null) {
33+
mOnYotiCalledListener.onYotiCalled();
34+
}
35+
}
36+
};
2237

2338
public YotiSDKButton(Context context) {
2439
super(context);
@@ -72,6 +87,10 @@ public void setOnYotiAppNotInstalledListener(@Nullable OnYotiAppNotInstalledList
7287
mOnYotiAppNotInstalledListener = listener;
7388
}
7489

90+
public void setOnYotiCalledListener(@Nullable OnYotiCalledListener listener) {
91+
mOnYotiCalledListener = listener;
92+
}
93+
7594
@Override
7695
public void onClick(View v) {
7796

@@ -80,7 +99,7 @@ public void onClick(View v) {
8099
}
81100

82101
try {
83-
YotiSDK.startScenario(getContext(), mUseCaseId, mOnYotiAppNotInstalledListener == null);
102+
YotiSDK.startScenario(getContext(), mUseCaseId, mOnYotiAppNotInstalledListener == null, mYotiCallResultReceiver);
84103
} catch (YotiSDKException cause) {
85104

86105
YotiSDKLogger.error(cause.getMessage(), cause);
@@ -104,4 +123,8 @@ public interface OnYotiButtonClickListener {
104123
public interface OnYotiAppNotInstalledListener {
105124
void onYotiAppNotInstalledError(YotiSDKNoYotiAppException cause);
106125
}
126+
127+
public interface OnYotiCalledListener {
128+
void onYotiCalled();
129+
}
107130
}

yoti-sdk/src/main/java/com/yoti/mobile/android/sdk/kernelSDK/KernelSDKIntentService.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.content.Intent;
66
import android.content.pm.ApplicationInfo;
77
import android.net.Uri;
8+
import android.os.ResultReceiver;
89
import android.text.TextUtils;
910

1011
import com.yoti.mobile.android.sdk.YotiSDK;
@@ -35,6 +36,7 @@ public class KernelSDKIntentService extends IntentService {
3536

3637
private static final String ACTION_BACKEND_CALL = "com.yoti.mobile.android.sdk.network.action.BACKEND_CALL";
3738
private static final String ACTION_START_SCENARIO = "com.yoti.mobile.android.sdk.network.action.START_SCENARIO";
39+
private static final String YOTI_CALLED_RESULT_RECEIVER = "com.yoti.mobile.android.sdk.network.action.YOTI_CALLED_RESULT_RECEIVER";
3840

3941
private static final String EXTRA_USE_CASE_ID = "com.yoti.mobile.android.sdk.network.extra.USE_CASE_ID";
4042
private KernelSDK mKernelSDK;
@@ -67,10 +69,11 @@ public static void startActionBackendCall(Context context, String useCaseId) {
6769
*
6870
* @see IntentService
6971
*/
70-
public static void startActionStartScenario(Context context, String useCaseId) {
72+
public static void startActionStartScenario(Context context, String useCaseId, ResultReceiver resultReceiver) {
7173
Intent intent = new Intent(context, KernelSDKIntentService.class);
7274
intent.setAction(ACTION_START_SCENARIO);
7375
intent.putExtra(EXTRA_USE_CASE_ID, useCaseId);
76+
intent.putExtra(YOTI_CALLED_RESULT_RECEIVER, resultReceiver);
7477
context.startService(intent);
7578
}
7679

@@ -82,12 +85,13 @@ protected void onHandleIntent(Intent intent) {
8285
if (ACTION_BACKEND_CALL.equals(action)) {
8386
handleActionBackendCall(useCaseId);
8487
} else if (ACTION_START_SCENARIO.equals(action)) {
85-
handleActionStartScenario(useCaseId);
88+
ResultReceiver resultReceiver = intent.getParcelableExtra(YOTI_CALLED_RESULT_RECEIVER);
89+
handleActionStartScenario(useCaseId, resultReceiver);
8690
}
8791
}
8892
}
8993

90-
private void handleActionStartScenario(String useCaseId) {
94+
private void handleActionStartScenario(String useCaseId, ResultReceiver resultReceiver) {
9195

9296
Scenario currentScenario = YotiSDK.getScenario(useCaseId);
9397

@@ -123,6 +127,8 @@ private void handleActionStartScenario(String useCaseId) {
123127
.appendQueryParameter(APP_NAME_PARAM, appName)
124128
.build();
125129

130+
resultReceiver.send(0, null);
131+
126132
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
127133
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
128134
startActivity(intent);

yoti-sdk/src/test/java/com/yoti/mobile/android/sdk/YotiSDKTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void testStartScenario_withNotInitiatedSDK() throws Throwable {
5959
exception.expect(YotiSDKException.class);
6060
exception.expectMessage("SDK not initialised");
6161

62-
YotiSDK.startScenario(mMockContext, "a_scenario", true);
62+
YotiSDK.startScenario(mMockContext, "a_scenario", true, null);
6363
}
6464

6565
@Test
@@ -76,7 +76,7 @@ public void testStartScenario_withNotScenariosSDK() throws Throwable {
7676
.create();
7777

7878
YotiSDK.addScenario(scenario);
79-
YotiSDK.startScenario(mMockContext, "a_scenario", true);
79+
YotiSDK.startScenario(mMockContext, "a_scenario", true, null);
8080
}
8181

8282
@Test
@@ -93,7 +93,7 @@ public void testStartScenario_withWrongScenario() throws Throwable {
9393
.create();
9494

9595
YotiSDK.addScenario(scenario);
96-
YotiSDK.startScenario(mMockContext, "a_scenario", true);
96+
YotiSDK.startScenario(mMockContext, "a_scenario", true, null);
9797
}
9898

9999
@Test
@@ -106,7 +106,7 @@ public void testStartScenario_withNotValidScenario() throws Throwable {
106106
.create();
107107

108108
YotiSDK.addScenario(scenario);
109-
YotiSDK.startScenario(mMockContext, "b_scenario", true);
109+
YotiSDK.startScenario(mMockContext, "b_scenario", true, null);
110110
}
111111

112112
@Test
@@ -126,7 +126,7 @@ public void testStartScenario_withWrongYotiAppVersion() throws Throwable {
126126

127127

128128
YotiSDK.addScenario(scenario);
129-
YotiSDK.startScenario(mMockContext, "a_scenario", true);
129+
YotiSDK.startScenario(mMockContext, "a_scenario", true, null);
130130
}
131131

132132
@Test
@@ -148,7 +148,7 @@ public void testStartScenario_withNoYotiApp() throws Throwable {
148148

149149

150150
YotiSDK.addScenario(scenario);
151-
YotiSDK.startScenario(mMockContext, "a_scenario", false);
151+
YotiSDK.startScenario(mMockContext, "a_scenario", false, null);
152152
}
153153

154154
@Test
@@ -168,7 +168,7 @@ public void testStartScenario_whenHandlingNoYotiApp() throws Throwable {
168168

169169

170170
YotiSDK.addScenario(scenario);
171-
YotiSDK.startScenario(mMockContext, "a_scenario", true);
171+
YotiSDK.startScenario(mMockContext, "a_scenario", true, null);
172172
}
173173

174174
@Test
@@ -190,6 +190,6 @@ public void testStartScenario_withoutHandlingNoYotiApp() throws Throwable {
190190

191191

192192
YotiSDK.addScenario(scenario);
193-
YotiSDK.startScenario(mMockContext, "a_scenario", false);
193+
YotiSDK.startScenario(mMockContext, "a_scenario", false, null);
194194
}
195195
}

0 commit comments

Comments
 (0)