@@ -74,114 +74,144 @@ private void permissionCallback(PluginCall call) {
74
74
75
75
@ PluginMethod
76
76
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
+ }
81
87
82
- if (contactId == null ) {
83
- call .reject ("Parameter `contactId` not provided." );
84
- return ;
85
- }
88
+ GetContactsProjectionInput projectionInput = new GetContactsProjectionInput (call .getObject ("projection" ));
86
89
87
- GetContactsProjectionInput projectionInput = new GetContactsProjectionInput ( call . getObject ( "projection" ) );
90
+ ContactPayload contact = implementation . getContact ( contactId , projectionInput );
88
91
89
- ContactPayload contact = implementation .getContact (contactId , projectionInput );
92
+ if (contact == null ) {
93
+ call .reject ("Contact not found." );
94
+ return ;
95
+ }
90
96
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 ) ;
94
100
}
95
-
96
- JSObject result = new JSObject ();
97
- result .put ("contact" , contact .getJSObject ());
98
- call .resolve (result );
101
+ } catch (Exception exception ) {
102
+ rejectCall (call , exception );
99
103
}
100
104
}
101
105
102
106
@ PluginMethod
103
107
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 () {
126
116
@ Override
127
117
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
+ }
129
145
}
130
- });
131
- }
132
- });
146
+ }
147
+ );
133
148
134
- executor .shutdown ();
149
+ executor .shutdown ();
150
+ }
151
+ } catch (Exception exception ) {
152
+ rejectCall (call , exception );
135
153
}
136
154
}
137
155
138
156
@ PluginMethod
139
157
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
+ }
144
168
145
- if (contactId == null ) {
146
- call .reject ("Something went wrong." );
147
- return ;
148
- }
169
+ JSObject result = new JSObject ();
170
+ result .put ("contactId" , contactId );
149
171
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 );
154
176
}
155
177
}
156
178
157
179
@ PluginMethod
158
180
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
+ }
163
191
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
+ }
168
196
169
- if (!implementation .deleteContact (contactId )) {
170
- call .reject ("Something went wrong." );
171
- return ;
197
+ call .resolve ();
172
198
}
173
-
174
- call . resolve ( );
199
+ } catch ( Exception exception ) {
200
+ rejectCall ( call , exception );
175
201
}
176
202
}
177
203
178
204
@ PluginMethod
179
205
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 );
185
215
}
186
216
}
187
217
@@ -212,4 +242,11 @@ private void pickContactResult(PluginCall call, ActivityResult activityResult) {
212
242
call .resolve (result );
213
243
}
214
244
}
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
+ }
215
252
}
0 commit comments