@@ -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