@capacitor/contacts 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,27 @@
1
+ package com.capacitorjs.plugins.contacts
2
+
3
+ /**
4
+ * OutSystems unified error codes for the Contacts plugin, in the
5
+ * `OS-PLUG-CONT-NNNN` format. The numeric suffix mirrors the legacy Cordova
6
+ * `ContactError` codes so both stacks report equivalent failures.
7
+ */
8
+ enum class ContactsError(val code: String, val message: String) {
9
+ UNKNOWN("OS-PLUG-CONT-0000", "An unknown error occurred."),
10
+ INVALID_ARGUMENT("OS-PLUG-CONT-0001", "Invalid arguments were provided."),
11
+ TIMEOUT("OS-PLUG-CONT-0002", "The operation timed out."),
12
+ PENDING_OPERATION("OS-PLUG-CONT-0003", "A pending operation is already in progress."),
13
+ IO_ERROR("OS-PLUG-CONT-0004", "An I/O error occurred while accessing contacts."),
14
+ NOT_SUPPORTED("OS-PLUG-CONT-0005", "The operation is not supported on this device."),
15
+ OPERATION_CANCELLED("OS-PLUG-CONT-0006", "The operation was cancelled."),
16
+ PERMISSION_DENIED("OS-PLUG-CONT-0020", "Contacts permission was denied.")
17
+ }
18
+
19
+ /**
20
+ * Throwable wrapper for [ContactsError]. Kotlin enums cannot themselves be
21
+ * [Throwable], so business logic throws this and the bridge reads `error` to
22
+ * reject the call with the right code.
23
+ */
24
+ class ContactsException(
25
+ val error: ContactsError,
26
+ cause: Throwable? = null
27
+ ) : Exception(error.message, cause)
@@ -0,0 +1,278 @@
1
+ package com.capacitorjs.plugins.contacts
2
+
3
+ import android.Manifest
4
+ import android.app.Activity
5
+ import android.content.Intent
6
+ import android.provider.ContactsContract
7
+ import com.getcapacitor.JSObject
8
+ import com.getcapacitor.PermissionState
9
+ import com.getcapacitor.Plugin
10
+ import com.getcapacitor.PluginCall
11
+ import com.getcapacitor.PluginMethod
12
+ import com.getcapacitor.annotation.ActivityCallback
13
+ import com.getcapacitor.annotation.CapacitorPlugin
14
+ import com.getcapacitor.annotation.Permission
15
+ import com.getcapacitor.annotation.PermissionCallback
16
+ import org.json.JSONArray
17
+ import java.util.concurrent.ExecutorService
18
+ import java.util.concurrent.Executors
19
+
20
+ /**
21
+ * Capacitor bridge for the Contacts plugin.
22
+ *
23
+ * Permission model is **implicit**: there are no `checkPermissions` /
24
+ * `requestPermissions` methods. Each method ensures the permission it needs
25
+ * before touching the provider: read for `find`/`pickContact`, read+write
26
+ * for `save`/`remove`. All provider work runs off the main thread.
27
+ */
28
+ @CapacitorPlugin(
29
+ name = "Contacts",
30
+ permissions = [
31
+ Permission(
32
+ alias = ContactsPlugin.READ,
33
+ strings = [Manifest.permission.READ_CONTACTS]
34
+ ),
35
+ Permission(
36
+ alias = ContactsPlugin.WRITE,
37
+ strings = [
38
+ Manifest.permission.READ_CONTACTS,
39
+ Manifest.permission.WRITE_CONTACTS
40
+ ]
41
+ )
42
+ ]
43
+ )
44
+ class ContactsPlugin : Plugin() {
45
+
46
+ companion object {
47
+ const val READ = "readContacts"
48
+ const val WRITE = "writeContacts"
49
+ }
50
+
51
+ private val implementation by lazy { Contacts(context) }
52
+
53
+ /** Serializes provider work off the main thread. */
54
+ private val executor: ExecutorService = Executors.newSingleThreadExecutor()
55
+
56
+ override fun handleOnDestroy() {
57
+ executor.shutdown()
58
+ super.handleOnDestroy()
59
+ }
60
+
61
+ // ------------------------------------------------------------------
62
+ // find
63
+ // ------------------------------------------------------------------
64
+
65
+ @PluginMethod
66
+ fun find(call: PluginCall) {
67
+ val fields = call.getArray("fields")
68
+ if (fields == null || fields.length() == 0) {
69
+ reject(call, ContactsError.INVALID_ARGUMENT)
70
+ return
71
+ }
72
+ ensurePermission(call)
73
+ }
74
+
75
+ private fun runFind(call: PluginCall) {
76
+ executor.execute {
77
+ try {
78
+ val fields = toStringList(call.getArray("fields"))
79
+ val filter = call.getString("filter", "") ?: ""
80
+ val multiple = call.getBoolean("multiple", false) ?: false
81
+ val desired = toStringList(call.getArray("desiredFields"))
82
+ val hasPhoneNumber = call.getBoolean("hasPhoneNumber", false) ?: false
83
+
84
+ val contacts = implementation.search(fields, filter, multiple, desired, hasPhoneNumber)
85
+ val ret = JSObject()
86
+ ret.put("contacts", contacts)
87
+ call.resolve(ret)
88
+ } catch (e: ContactsException) {
89
+ reject(call, e.error)
90
+ } catch (e: Exception) {
91
+ reject(call, ContactsError.UNKNOWN, e)
92
+ }
93
+ }
94
+ }
95
+
96
+ // ------------------------------------------------------------------
97
+ // save
98
+ // ------------------------------------------------------------------
99
+
100
+ @PluginMethod
101
+ fun save(call: PluginCall) {
102
+ if (call.getObject("contact") == null) {
103
+ reject(call, ContactsError.INVALID_ARGUMENT)
104
+ return
105
+ }
106
+ ensurePermission(call)
107
+ }
108
+
109
+ private fun runSave(call: PluginCall) {
110
+ executor.execute {
111
+ try {
112
+ val contact = call.getObject("contact")
113
+ if (contact == null) {
114
+ reject(call, ContactsError.INVALID_ARGUMENT)
115
+ return@execute
116
+ }
117
+ val rawId = implementation.save(contact)
118
+ val saved = implementation.getContactByRawId(rawId)
119
+ if (saved != null) {
120
+ call.resolve(saved)
121
+ } else {
122
+ reject(call, ContactsError.UNKNOWN)
123
+ }
124
+ } catch (e: ContactsException) {
125
+ reject(call, e.error)
126
+ } catch (e: Exception) {
127
+ reject(call, ContactsError.UNKNOWN, e)
128
+ }
129
+ }
130
+ }
131
+
132
+ // ------------------------------------------------------------------
133
+ // remove
134
+ // ------------------------------------------------------------------
135
+
136
+ @PluginMethod
137
+ fun remove(call: PluginCall) {
138
+ if (call.getString("id").isNullOrEmpty()) {
139
+ reject(call, ContactsError.INVALID_ARGUMENT)
140
+ return
141
+ }
142
+ ensurePermission(call)
143
+ }
144
+
145
+ private fun runRemove(call: PluginCall) {
146
+ executor.execute {
147
+ try {
148
+ val id = call.getString("id") ?: run {
149
+ reject(call, ContactsError.INVALID_ARGUMENT)
150
+ return@execute
151
+ }
152
+ if (implementation.remove(id)) {
153
+ call.resolve()
154
+ } else {
155
+ // No accessible contact has this id.
156
+ reject(call, ContactsError.INVALID_ARGUMENT)
157
+ }
158
+ } catch (e: Exception) {
159
+ reject(call, ContactsError.UNKNOWN, e)
160
+ }
161
+ }
162
+ }
163
+
164
+ // ------------------------------------------------------------------
165
+ // pickContact
166
+ // ------------------------------------------------------------------
167
+
168
+ @PluginMethod
169
+ fun pickContact(call: PluginCall) {
170
+ ensurePermission(call)
171
+ }
172
+
173
+ /** True while the system picker is on screen. */
174
+ @Volatile
175
+ private var pickInProgress = false
176
+
177
+ private fun runPickContact(call: PluginCall) {
178
+ if (pickInProgress) {
179
+ reject(call, ContactsError.PENDING_OPERATION)
180
+ return
181
+ }
182
+ pickInProgress = true
183
+ val intent = Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI)
184
+ try {
185
+ startActivityForResult(call, intent, "pickContactResult")
186
+ } catch (e: Exception) {
187
+ // No contacts app / picker activity on this device.
188
+ pickInProgress = false
189
+ reject(call, ContactsError.NOT_SUPPORTED, e)
190
+ }
191
+ }
192
+
193
+ @ActivityCallback
194
+ private fun pickContactResult(call: PluginCall?, result: androidx.activity.result.ActivityResult) {
195
+ pickInProgress = false
196
+ if (call == null) return
197
+ when (result.resultCode) {
198
+ Activity.RESULT_OK -> {
199
+ val uri = result.data?.data
200
+ val contactId = uri?.lastPathSegment
201
+ if (contactId == null) {
202
+ reject(call, ContactsError.UNKNOWN)
203
+ return
204
+ }
205
+ executor.execute {
206
+ try {
207
+ val contact = implementation.getContactById(contactId)
208
+ if (contact != null) call.resolve(contact) else reject(call, ContactsError.UNKNOWN)
209
+ } catch (e: Exception) {
210
+ reject(call, ContactsError.UNKNOWN, e)
211
+ }
212
+ }
213
+ }
214
+ Activity.RESULT_CANCELED -> reject(call, ContactsError.OPERATION_CANCELLED)
215
+ else -> reject(call, ContactsError.UNKNOWN)
216
+ }
217
+ }
218
+
219
+ // ------------------------------------------------------------------
220
+ // Implicit permission handling
221
+ // ------------------------------------------------------------------
222
+
223
+ /** The permission alias each method needs. */
224
+ private fun aliasFor(call: PluginCall): String = when (call.methodName) {
225
+ "save", "remove" -> WRITE
226
+ else -> READ
227
+ }
228
+
229
+ /** Ensures the method's permission alias, then dispatches the saved call. */
230
+ private fun ensurePermission(call: PluginCall) {
231
+ if (getPermissionState(aliasFor(call)) == PermissionState.GRANTED) {
232
+ dispatch(call)
233
+ } else {
234
+ requestPermissionForAlias(aliasFor(call), call, "permissionCallback")
235
+ }
236
+ }
237
+
238
+ @PermissionCallback
239
+ private fun permissionCallback(call: PluginCall) {
240
+ if (getPermissionState(aliasFor(call)) == PermissionState.GRANTED) {
241
+ dispatch(call)
242
+ } else {
243
+ reject(call, ContactsError.PERMISSION_DENIED)
244
+ }
245
+ }
246
+
247
+ /** Routes a permission-cleared call to its implementation runner. */
248
+ private fun dispatch(call: PluginCall) {
249
+ when (call.methodName) {
250
+ "find" -> runFind(call)
251
+ "save" -> runSave(call)
252
+ "remove" -> runRemove(call)
253
+ "pickContact" -> runPickContact(call)
254
+ else -> reject(call, ContactsError.NOT_SUPPORTED)
255
+ }
256
+ }
257
+
258
+ // ------------------------------------------------------------------
259
+ // Helpers
260
+ // ------------------------------------------------------------------
261
+
262
+ private fun toStringList(array: JSONArray?): List<String> {
263
+ if (array == null) return emptyList()
264
+ val out = ArrayList<String>(array.length())
265
+ for (i in 0 until array.length()) {
266
+ array.optString(i, null)?.let { out.add(it) }
267
+ }
268
+ return out
269
+ }
270
+
271
+ private fun reject(call: PluginCall, error: ContactsError, cause: Exception? = null) {
272
+ if (cause != null) {
273
+ call.reject(error.message, error.code, cause)
274
+ } else {
275
+ call.reject(error.message, error.code)
276
+ }
277
+ }
278
+ }
File without changes