Skip to content

Commit

Permalink
fix(android): catch native exceptions (#128)
Browse files Browse the repository at this point in the history
Prevent app crashes because of this plugin. 

Close #127
  • Loading branch information
robingenz authored Sep 19, 2024
1 parent ea771d9 commit efb9376
Showing 1 changed file with 111 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,114 +74,144 @@ private void permissionCallback(PluginCall call) {

@PluginMethod
public void getContact(PluginCall call) {
if (!isContactsPermissionGranted()) {
requestContactsPermission(call);
} else {
String contactId = call.getString("contactId");
try {
if (!isContactsPermissionGranted()) {
requestContactsPermission(call);
} else {
String contactId = call.getString("contactId");

if (contactId == null) {
call.reject("Parameter `contactId` not provided.");
return;
}

if (contactId == null) {
call.reject("Parameter `contactId` not provided.");
return;
}
GetContactsProjectionInput projectionInput = new GetContactsProjectionInput(call.getObject("projection"));

GetContactsProjectionInput projectionInput = new GetContactsProjectionInput(call.getObject("projection"));
ContactPayload contact = implementation.getContact(contactId, projectionInput);

ContactPayload contact = implementation.getContact(contactId, projectionInput);
if (contact == null) {
call.reject("Contact not found.");
return;
}

if (contact == null) {
call.reject("Contact not found.");
return;
JSObject result = new JSObject();
result.put("contact", contact.getJSObject());
call.resolve(result);
}

JSObject result = new JSObject();
result.put("contact", contact.getJSObject());
call.resolve(result);
} catch (Exception exception) {
rejectCall(call, exception);
}
}

@PluginMethod
public void getContacts(PluginCall call) {
if (!isContactsPermissionGranted()) {
requestContactsPermission(call);
} else {
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() {
try {
if (!isContactsPermissionGranted()) {
requestContactsPermission(call);
} else {
ExecutorService executor = Executors.newSingleThreadExecutor();

executor.execute(
new Runnable() {
@Override
public void run() {
call.resolve(result);
try {
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);
}
}
);
} catch (Exception exception) {
rejectCall(call, exception);
}
}
});
}
});
}
);

executor.shutdown();
executor.shutdown();
}
} catch (Exception exception) {
rejectCall(call, exception);
}
}

@PluginMethod
public void createContact(PluginCall call) {
if (!isContactsPermissionGranted()) {
requestContactsPermission(call);
} else {
String contactId = implementation.createContact(new CreateContactInput(call.getObject("contact")));
try {
if (!isContactsPermissionGranted()) {
requestContactsPermission(call);
} else {
String contactId = implementation.createContact(new CreateContactInput(call.getObject("contact")));

if (contactId == null) {
call.reject("Something went wrong.");
return;
}

if (contactId == null) {
call.reject("Something went wrong.");
return;
}
JSObject result = new JSObject();
result.put("contactId", contactId);

JSObject result = new JSObject();
result.put("contactId", contactId);

call.resolve(result);
call.resolve(result);
}
} catch (Exception exception) {
rejectCall(call, exception);
}
}

@PluginMethod
public void deleteContact(PluginCall call) {
if (!isContactsPermissionGranted()) {
requestContactsPermission(call);
} else {
String contactId = call.getString("contactId");
try {
if (!isContactsPermissionGranted()) {
requestContactsPermission(call);
} else {
String contactId = call.getString("contactId");

if (contactId == null) {
call.reject("Parameter `contactId` not provided.");
return;
}

if (contactId == null) {
call.reject("Parameter `contactId` not provided.");
return;
}
if (!implementation.deleteContact(contactId)) {
call.reject("Something went wrong.");
return;
}

if (!implementation.deleteContact(contactId)) {
call.reject("Something went wrong.");
return;
call.resolve();
}

call.resolve();
} catch (Exception exception) {
rejectCall(call, exception);
}
}

@PluginMethod
public void pickContact(PluginCall call) {
if (!isContactsPermissionGranted()) {
requestContactsPermission(call);
} else {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(call, contactPickerIntent, "pickContactResult");
try {
if (!isContactsPermissionGranted()) {
requestContactsPermission(call);
} else {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(call, contactPickerIntent, "pickContactResult");
}
} catch (Exception exception) {
rejectCall(call, exception);
}
}

Expand Down Expand Up @@ -212,4 +242,11 @@ private void pickContactResult(PluginCall call, ActivityResult activityResult) {
call.resolve(result);
}
}

private void rejectCall(PluginCall call, Exception exception) {
String message = exception.getMessage();
message = (message != null) ? message : "An error occurred.";
Logger.error(TAG, message, exception);
call.reject(message);
}
}

0 comments on commit efb9376

Please sign in to comment.