diff --git a/android/src/main/java/getcapacitor/community/contacts/ContactsPlugin.java b/android/src/main/java/getcapacitor/community/contacts/ContactsPlugin.java index afa0aa5..82d3dd2 100644 --- a/android/src/main/java/getcapacitor/community/contacts/ContactsPlugin.java +++ b/android/src/main/java/getcapacitor/community/contacts/ContactsPlugin.java @@ -18,6 +18,8 @@ import com.getcapacitor.annotation.PermissionCallback; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; @CapacitorPlugin( name = "Contacts", @@ -102,20 +104,34 @@ public void getContacts(PluginCall call) { if (!isContactsPermissionGranted()) { requestContactsPermission(call); } else { - HashMap contacts = implementation.getContacts( - new GetContactsProjectionInput(call.getObject("projection")) - ); - - JSArray contactsJSArray = new JSArray(); - - for (Map.Entry entry : contacts.entrySet()) { - ContactPayload value = entry.getValue(); - contactsJSArray.put(value.getJSObject()); - } - - JSObject result = new JSObject(); - result.put("contacts", contactsJSArray); - call.resolve(result); + ExecutorService executor = Executors.newSingleThreadExecutor(); + + executor.execute(new Runnable() { + @Override + public void run() { + HashMap contacts = implementation.getContacts( + new GetContactsProjectionInput(call.getObject("projection")) + ); + + JSArray contactsJSArray = new JSArray(); + for (Map.Entry entry : contacts.entrySet()) { + ContactPayload value = entry.getValue(); + contactsJSArray.put(value.getJSObject()); + } + + JSObject result = new JSObject(); + result.put("contacts", contactsJSArray); + + bridge.getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + call.resolve(result); + } + }); + } + }); + + executor.shutdown(); } }