Skip to content

Commit efb9376

Browse files
authored
fix(android): catch native exceptions (#128)
Prevent app crashes because of this plugin. Close #127
1 parent ea771d9 commit efb9376

File tree

1 file changed

+111
-74
lines changed

1 file changed

+111
-74
lines changed

android/src/main/java/getcapacitor/community/contacts/ContactsPlugin.java

+111-74
Original file line numberDiff line numberDiff line change
@@ -74,114 +74,144 @@ private void permissionCallback(PluginCall call) {
7474

7575
@PluginMethod
7676
public void getContact(PluginCall call) {
77-
if (!isContactsPermissionGranted()) {
78-
requestContactsPermission(call);
79-
} else {
80-
String contactId = call.getString("contactId");
77+
try {
78+
if (!isContactsPermissionGranted()) {
79+
requestContactsPermission(call);
80+
} else {
81+
String contactId = call.getString("contactId");
82+
83+
if (contactId == null) {
84+
call.reject("Parameter `contactId` not provided.");
85+
return;
86+
}
8187

82-
if (contactId == null) {
83-
call.reject("Parameter `contactId` not provided.");
84-
return;
85-
}
88+
GetContactsProjectionInput projectionInput = new GetContactsProjectionInput(call.getObject("projection"));
8689

87-
GetContactsProjectionInput projectionInput = new GetContactsProjectionInput(call.getObject("projection"));
90+
ContactPayload contact = implementation.getContact(contactId, projectionInput);
8891

89-
ContactPayload contact = implementation.getContact(contactId, projectionInput);
92+
if (contact == null) {
93+
call.reject("Contact not found.");
94+
return;
95+
}
9096

91-
if (contact == null) {
92-
call.reject("Contact not found.");
93-
return;
97+
JSObject result = new JSObject();
98+
result.put("contact", contact.getJSObject());
99+
call.resolve(result);
94100
}
95-
96-
JSObject result = new JSObject();
97-
result.put("contact", contact.getJSObject());
98-
call.resolve(result);
101+
} catch (Exception exception) {
102+
rejectCall(call, exception);
99103
}
100104
}
101105

102106
@PluginMethod
103107
public void getContacts(PluginCall call) {
104-
if (!isContactsPermissionGranted()) {
105-
requestContactsPermission(call);
106-
} else {
107-
ExecutorService executor = Executors.newSingleThreadExecutor();
108-
109-
executor.execute(new Runnable() {
110-
@Override
111-
public void run() {
112-
HashMap<String, ContactPayload> contacts = implementation.getContacts(
113-
new GetContactsProjectionInput(call.getObject("projection"))
114-
);
115-
116-
JSArray contactsJSArray = new JSArray();
117-
for (Map.Entry<String, ContactPayload> entry : contacts.entrySet()) {
118-
ContactPayload value = entry.getValue();
119-
contactsJSArray.put(value.getJSObject());
120-
}
121-
122-
JSObject result = new JSObject();
123-
result.put("contacts", contactsJSArray);
124-
125-
bridge.getActivity().runOnUiThread(new Runnable() {
108+
try {
109+
if (!isContactsPermissionGranted()) {
110+
requestContactsPermission(call);
111+
} else {
112+
ExecutorService executor = Executors.newSingleThreadExecutor();
113+
114+
executor.execute(
115+
new Runnable() {
126116
@Override
127117
public void run() {
128-
call.resolve(result);
118+
try {
119+
HashMap<String, ContactPayload> contacts = implementation.getContacts(
120+
new GetContactsProjectionInput(call.getObject("projection"))
121+
);
122+
123+
JSArray contactsJSArray = new JSArray();
124+
for (Map.Entry<String, ContactPayload> entry : contacts.entrySet()) {
125+
ContactPayload value = entry.getValue();
126+
contactsJSArray.put(value.getJSObject());
127+
}
128+
129+
JSObject result = new JSObject();
130+
result.put("contacts", contactsJSArray);
131+
132+
bridge
133+
.getActivity()
134+
.runOnUiThread(
135+
new Runnable() {
136+
@Override
137+
public void run() {
138+
call.resolve(result);
139+
}
140+
}
141+
);
142+
} catch (Exception exception) {
143+
rejectCall(call, exception);
144+
}
129145
}
130-
});
131-
}
132-
});
146+
}
147+
);
133148

134-
executor.shutdown();
149+
executor.shutdown();
150+
}
151+
} catch (Exception exception) {
152+
rejectCall(call, exception);
135153
}
136154
}
137155

138156
@PluginMethod
139157
public void createContact(PluginCall call) {
140-
if (!isContactsPermissionGranted()) {
141-
requestContactsPermission(call);
142-
} else {
143-
String contactId = implementation.createContact(new CreateContactInput(call.getObject("contact")));
158+
try {
159+
if (!isContactsPermissionGranted()) {
160+
requestContactsPermission(call);
161+
} else {
162+
String contactId = implementation.createContact(new CreateContactInput(call.getObject("contact")));
163+
164+
if (contactId == null) {
165+
call.reject("Something went wrong.");
166+
return;
167+
}
144168

145-
if (contactId == null) {
146-
call.reject("Something went wrong.");
147-
return;
148-
}
169+
JSObject result = new JSObject();
170+
result.put("contactId", contactId);
149171

150-
JSObject result = new JSObject();
151-
result.put("contactId", contactId);
152-
153-
call.resolve(result);
172+
call.resolve(result);
173+
}
174+
} catch (Exception exception) {
175+
rejectCall(call, exception);
154176
}
155177
}
156178

157179
@PluginMethod
158180
public void deleteContact(PluginCall call) {
159-
if (!isContactsPermissionGranted()) {
160-
requestContactsPermission(call);
161-
} else {
162-
String contactId = call.getString("contactId");
181+
try {
182+
if (!isContactsPermissionGranted()) {
183+
requestContactsPermission(call);
184+
} else {
185+
String contactId = call.getString("contactId");
186+
187+
if (contactId == null) {
188+
call.reject("Parameter `contactId` not provided.");
189+
return;
190+
}
163191

164-
if (contactId == null) {
165-
call.reject("Parameter `contactId` not provided.");
166-
return;
167-
}
192+
if (!implementation.deleteContact(contactId)) {
193+
call.reject("Something went wrong.");
194+
return;
195+
}
168196

169-
if (!implementation.deleteContact(contactId)) {
170-
call.reject("Something went wrong.");
171-
return;
197+
call.resolve();
172198
}
173-
174-
call.resolve();
199+
} catch (Exception exception) {
200+
rejectCall(call, exception);
175201
}
176202
}
177203

178204
@PluginMethod
179205
public void pickContact(PluginCall call) {
180-
if (!isContactsPermissionGranted()) {
181-
requestContactsPermission(call);
182-
} else {
183-
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
184-
startActivityForResult(call, contactPickerIntent, "pickContactResult");
206+
try {
207+
if (!isContactsPermissionGranted()) {
208+
requestContactsPermission(call);
209+
} else {
210+
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
211+
startActivityForResult(call, contactPickerIntent, "pickContactResult");
212+
}
213+
} catch (Exception exception) {
214+
rejectCall(call, exception);
185215
}
186216
}
187217

@@ -212,4 +242,11 @@ private void pickContactResult(PluginCall call, ActivityResult activityResult) {
212242
call.resolve(result);
213243
}
214244
}
245+
246+
private void rejectCall(PluginCall call, Exception exception) {
247+
String message = exception.getMessage();
248+
message = (message != null) ? message : "An error occurred.";
249+
Logger.error(TAG, message, exception);
250+
call.reject(message);
251+
}
215252
}

0 commit comments

Comments
 (0)