Skip to content

Commit

Permalink
fix(android): resolve ANR issue when calling getContacts
Browse files Browse the repository at this point in the history
  • Loading branch information
trinitiwowka authored Mar 13, 2024
1 parent 79f77cc commit 52179dd
Showing 1 changed file with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -102,20 +104,34 @@ public void getContacts(PluginCall call) {
if (!isContactsPermissionGranted()) {
requestContactsPermission(call);
} else {
HashMap<String, ContactPayload> contacts = implementation.getContacts(
new GetContactsProjectionInput(call.getObject("projection"))
);

JSArray contactsJSArray = new JSArray();

for (Map.Entry<String, ContactPayload> 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<String, ContactPayload> contacts = implementation.getContacts(
new GetContactsProjectionInput(call.getObject("projection"))
);

JSArray contactsJSArray = new JSArray();
for (Map.Entry<String, ContactPayload> 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();
}
}

Expand Down

0 comments on commit 52179dd

Please sign in to comment.