Skip to content

Commit fb1f70d

Browse files
committed
Wait for connection result before sending plugin result on "connectToService" call
1 parent ee41062 commit fb1f70d

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

src/android/ChromeCustomTabPlugin.java

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
116116
return true;
117117
}
118118
case "connectToService": {
119-
if (bindCustomTabsService())
120-
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, true));
121-
else
122-
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "Failed to connect to service"));
119+
bindCustomTabsService(
120+
() -> callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, true)),
121+
() -> callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "Failed to connect to service"))
122+
);
123123
return true;
124124
}
125125
case "warmUp": {
@@ -219,8 +219,32 @@ private CustomTabsSession getSession() {
219219
return mCustomTabPluginHelper.getSession();
220220
}
221221

222-
private boolean bindCustomTabsService() {
223-
return mCustomTabPluginHelper.bindCustomTabsService(cordova.getActivity());
222+
@FunctionalInterface
223+
private interface Thunk {
224+
void execute();
225+
}
226+
227+
private void bindCustomTabsService(
228+
Thunk successCb,
229+
Thunk failureCb
230+
) {
231+
if (mCustomTabPluginHelper.getClient() == null) {
232+
mCustomTabPluginHelper.setConnectionCallback(new CustomTabServiceHelper.ConnectionCallback() {
233+
@Override
234+
public void onCustomTabsConnected() {
235+
successCb.execute();
236+
}
237+
238+
@Override
239+
public void onCustomTabsDisconnected() {
240+
}
241+
});
242+
if (!mCustomTabPluginHelper.bindCustomTabsService(cordova.getActivity())) {
243+
failureCb.execute();
244+
}
245+
} else {
246+
successCb.execute();
247+
}
224248
}
225249

226250
private boolean unbindCustomTabsService() {
@@ -258,8 +282,8 @@ public void onStop() {
258282

259283
@Override
260284
public void onStart() {
261-
if(wasConnected){
262-
bindCustomTabsService();
285+
if (wasConnected) {
286+
bindCustomTabsService(() -> {}, () -> {});
263287
}
264288
super.onStart();
265289
}

0 commit comments

Comments
 (0)