@aalzehla/capacitor-contacts 0.0.5 → 8.0.0

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.
Files changed (45) hide show
  1. package/{AalzehlaCapacitorContacts.podspec → CapacitorCommunityContacts.podspec} +3 -3
  2. package/README.md +69 -45
  3. package/android/build.gradle +15 -12
  4. package/android/src/main/java/getcapacitor/community/contacts/BiMap.java +45 -0
  5. package/android/src/main/java/getcapacitor/community/contacts/ContactPayload.java +286 -0
  6. package/android/src/main/java/getcapacitor/community/contacts/Contacts.java +384 -0
  7. package/android/src/main/java/getcapacitor/community/contacts/ContactsPlugin.java +255 -0
  8. package/android/src/main/java/getcapacitor/community/contacts/CreateContactInput.java +233 -0
  9. package/android/src/main/java/getcapacitor/community/contacts/GetContactsProjectionInput.java +214 -0
  10. package/dist/esm/definitions.d.ts +285 -19
  11. package/dist/esm/definitions.js +47 -1
  12. package/dist/esm/definitions.js.map +1 -1
  13. package/dist/esm/index.d.ts +3 -3
  14. package/dist/esm/index.js +3 -3
  15. package/dist/esm/index.js.map +1 -1
  16. package/dist/esm/web.d.ts +9 -7
  17. package/dist/esm/web.js +21 -7
  18. package/dist/esm/web.js.map +1 -1
  19. package/dist/plugin.cjs.js +73 -14
  20. package/dist/plugin.cjs.js.map +1 -1
  21. package/dist/plugin.js +74 -15
  22. package/dist/plugin.js.map +1 -1
  23. package/ios/Plugin/BiMap.swift +42 -0
  24. package/ios/Plugin/ContactPayload.swift +227 -0
  25. package/ios/Plugin/Contacts.swift +227 -0
  26. package/ios/Plugin/ContactsPlugin.h +10 -0
  27. package/ios/Plugin/ContactsPlugin.m +14 -0
  28. package/ios/Plugin/ContactsPlugin.swift +221 -0
  29. package/ios/Plugin/CreateContactInput.swift +187 -0
  30. package/ios/Plugin/GetContactsProjectionInput.swift +119 -0
  31. package/ios/Plugin/Info.plist +24 -0
  32. package/package.json +33 -30
  33. package/Package.swift +0 -28
  34. package/android/src/main/java/com/aalzehla/capacitor/contacts/CapacitorContactsPlugin.java +0 -177
  35. package/android/src/main/java/com/aalzehla/capacitor/contacts/ContactDataExtractorVisitor.java +0 -122
  36. package/android/src/main/java/com/aalzehla/capacitor/contacts/ContactExtractorVisitor.java +0 -31
  37. package/android/src/main/java/com/aalzehla/capacitor/contacts/PluginContactFields.java +0 -17
  38. package/android/src/main/java/com/aalzehla/capacitor/contacts/contentQuery/ContentQuery.java +0 -83
  39. package/android/src/main/java/com/aalzehla/capacitor/contacts/contentQuery/ContentQueryService.java +0 -60
  40. package/android/src/main/java/com/aalzehla/capacitor/contacts/utils/Utils.java +0 -19
  41. package/android/src/main/java/com/aalzehla/capacitor/contacts/utils/Visitable.java +0 -5
  42. package/android/src/main/java/com/aalzehla/capacitor/contacts/utils/Visitor.java +0 -5
  43. package/dist/docs.json +0 -145
  44. package/ios/Sources/CapacitorContactsPlugin/CapacitorContactsPlugin.swift +0 -168
  45. package/ios/Tests/CapacitorContactsPluginTests/CapacitorContactsPluginTests.swift +0 -15
@@ -0,0 +1,233 @@
1
+ package getcapacitor.community.contacts;
2
+
3
+ import androidx.annotation.NonNull;
4
+ import java.text.ParseException;
5
+ import java.text.SimpleDateFormat;
6
+ import java.util.ArrayList;
7
+ import java.util.Date;
8
+ import java.util.Locale;
9
+ import org.json.JSONArray;
10
+ import org.json.JSONObject;
11
+
12
+ public class CreateContactInput {
13
+
14
+ // @TODO:
15
+ // Contact Type
16
+
17
+ // Name
18
+ public String nameGiven;
19
+ public String nameMiddle;
20
+ public String nameFamily;
21
+ public String namePrefix;
22
+ public String nameSuffix;
23
+ // private final String nickname;
24
+
25
+ // Organization
26
+ public String organizationName;
27
+ public String organizationJobTitle;
28
+ public String organizationDepartment;
29
+
30
+ // Birthday
31
+ public String birthday;
32
+
33
+ // Note
34
+ public String note;
35
+
36
+ // Phones
37
+ public ArrayList<PhoneInput> phones = new ArrayList<>();
38
+
39
+ // Emails
40
+ public ArrayList<EmailInput> emails = new ArrayList<>();
41
+
42
+ // URLs
43
+ public ArrayList<String> urls = new ArrayList<>();
44
+
45
+ // Postal Addresses
46
+ public ArrayList<PostalAddressInput> postalAddresses = new ArrayList<>();
47
+
48
+ // Image
49
+ public ImageInput image;
50
+
51
+ CreateContactInput(JSONObject fromJSONObject) {
52
+ // Name
53
+ JSONObject nameObject = fromJSONObject.optJSONObject("name");
54
+ if (nameObject != null) {
55
+ this.nameGiven = nameObject.has("given") ? nameObject.optString("given") : null;
56
+ this.nameMiddle = nameObject.has("middle") ? nameObject.optString("middle") : null;
57
+ this.nameFamily = nameObject.has("family") ? nameObject.optString("family") : null;
58
+ this.namePrefix = nameObject.has("prefix") ? nameObject.optString("prefix") : null;
59
+ this.nameSuffix = nameObject.has("suffix") ? nameObject.optString("suffix") : null;
60
+ }
61
+
62
+ // Organization
63
+ JSONObject organizationObject = fromJSONObject.optJSONObject("organization");
64
+ if (organizationObject != null) {
65
+ this.organizationName = organizationObject.has("company") ? organizationObject.optString("company") : null;
66
+ this.organizationJobTitle = organizationObject.has("jobTitle") ? organizationObject.optString("jobTitle") : null;
67
+ this.organizationDepartment = organizationObject.has("department") ? organizationObject.optString("department") : null;
68
+ }
69
+
70
+ // Birthday
71
+ JSONObject birthdayObject = fromJSONObject.optJSONObject("birthday");
72
+ if (birthdayObject != null) {
73
+ if (birthdayObject.has("month") && birthdayObject.has("day")) {
74
+ String month = birthdayObject.optString("month");
75
+ String day = birthdayObject.optString("day");
76
+
77
+ @NonNull
78
+ String birthdayFormat = "MM-dd";
79
+ @NonNull
80
+ String birthdayString = month + "-" + day;
81
+
82
+ if (birthdayObject.has("year")) {
83
+ String year = birthdayObject.optString("year");
84
+
85
+ birthdayFormat = "yyyy-MM-dd";
86
+ birthdayString = year + "-" + birthdayString;
87
+ }
88
+
89
+ try {
90
+ SimpleDateFormat dateFormat = new SimpleDateFormat(birthdayFormat, Locale.getDefault());
91
+ // `setLenient` makes sure input like "2020-06-50" does not get parsed to a valid date.
92
+ dateFormat.setLenient(false);
93
+ Date date = dateFormat.parse(birthdayString);
94
+ if (date != null) {
95
+ if (birthdayFormat.equals("MM-dd")) {
96
+ // We have to prefix with "--",
97
+ // because otherwise it may get saved with the current year attached instead of no year at all.
98
+ this.birthday = "--" + birthdayString;
99
+ } else {
100
+ this.birthday = birthdayString;
101
+ }
102
+ }
103
+ } catch (ParseException e) {
104
+ e.printStackTrace();
105
+ // Something wrong with this date format
106
+ }
107
+ }
108
+ }
109
+
110
+ // Note
111
+ this.note = fromJSONObject.has("note") ? fromJSONObject.optString("note") : null;
112
+
113
+ // Phones
114
+ JSONArray phonesArray = fromJSONObject.optJSONArray("phones");
115
+ if (phonesArray != null && phonesArray.length() > 0) {
116
+ for (int n = 0; n < phonesArray.length(); n++) {
117
+ JSONObject phoneObject = phonesArray.optJSONObject(n);
118
+ if (phoneObject != null) {
119
+ this.phones.add(new PhoneInput(phoneObject));
120
+ }
121
+ }
122
+ }
123
+
124
+ // Emails
125
+ JSONArray emailsArray = fromJSONObject.optJSONArray("emails");
126
+ if (emailsArray != null && emailsArray.length() > 0) {
127
+ for (int n = 0; n < emailsArray.length(); n++) {
128
+ JSONObject emailObject = emailsArray.optJSONObject(n);
129
+ if (emailObject != null) {
130
+ this.emails.add(new EmailInput(emailObject));
131
+ }
132
+ }
133
+ }
134
+
135
+ // URLs
136
+ JSONArray urlsArray = fromJSONObject.optJSONArray("urls");
137
+ if (urlsArray != null && urlsArray.length() > 0) {
138
+ for (int n = 0; n < urlsArray.length(); n++) {
139
+ String url = urlsArray.optString(n);
140
+ if (url != null) {
141
+ this.urls.add(url);
142
+ }
143
+ }
144
+ }
145
+
146
+ // Postal Addresses
147
+ JSONArray postalAddressesArray = fromJSONObject.optJSONArray("postalAddresses");
148
+ if (postalAddressesArray != null && postalAddressesArray.length() > 0) {
149
+ for (int n = 0; n < postalAddressesArray.length(); n++) {
150
+ JSONObject postalAddressObject = postalAddressesArray.optJSONObject(n);
151
+ if (postalAddressObject != null) {
152
+ this.postalAddresses.add(new PostalAddressInput(postalAddressObject));
153
+ }
154
+ }
155
+ }
156
+
157
+ // Image
158
+ JSONObject imageObject = fromJSONObject.optJSONObject("image");
159
+ if (imageObject != null) {
160
+ this.image = new ImageInput(imageObject);
161
+ }
162
+ }
163
+
164
+ public static class PhoneInput {
165
+
166
+ public final int type;
167
+ public final String label;
168
+ public final Boolean isPrimary;
169
+
170
+ public final String number;
171
+
172
+ PhoneInput(JSONObject fromJSONObject) {
173
+ this.type = Contacts.phoneTypeMap.getValue(fromJSONObject.optString("type"));
174
+ this.label = fromJSONObject.has("label") ? fromJSONObject.optString("label") : null;
175
+ this.isPrimary = fromJSONObject.optBoolean("isPrimary", false);
176
+
177
+ this.number = fromJSONObject.has("number") ? fromJSONObject.optString("number") : null;
178
+ }
179
+ }
180
+
181
+ public static class EmailInput {
182
+
183
+ public final int type;
184
+ public final String label;
185
+ public final Boolean isPrimary;
186
+
187
+ public final String address;
188
+
189
+ EmailInput(JSONObject fromJSONObject) {
190
+ this.type = Contacts.emailTypeMap.getValue(fromJSONObject.optString("type"));
191
+ this.label = fromJSONObject.has("label") ? fromJSONObject.optString("label") : null;
192
+ this.isPrimary = fromJSONObject.optBoolean("isPrimary", false);
193
+
194
+ this.address = fromJSONObject.has("address") ? fromJSONObject.optString("address") : null;
195
+ }
196
+ }
197
+
198
+ public static class PostalAddressInput {
199
+
200
+ public final int type;
201
+ public final String label;
202
+ public final Boolean isPrimary;
203
+
204
+ public final String street;
205
+ public final String neighborhood;
206
+ public final String city;
207
+ public final String region;
208
+ public final String postcode;
209
+ public final String country;
210
+
211
+ PostalAddressInput(JSONObject fromJSONObject) {
212
+ this.type = Contacts.postalAddressTypeMap.getValue(fromJSONObject.optString("type"));
213
+ this.label = fromJSONObject.has("label") ? fromJSONObject.optString("label") : null;
214
+ this.isPrimary = fromJSONObject.optBoolean("isPrimary", false);
215
+
216
+ this.street = fromJSONObject.has("street") ? fromJSONObject.optString("street") : null;
217
+ this.neighborhood = fromJSONObject.has("neighborhood") ? fromJSONObject.optString("neighborhood") : null;
218
+ this.city = fromJSONObject.has("city") ? fromJSONObject.optString("city") : null;
219
+ this.region = fromJSONObject.has("region") ? fromJSONObject.optString("region") : null;
220
+ this.postcode = fromJSONObject.has("postcode") ? fromJSONObject.optString("postcode") : null;
221
+ this.country = fromJSONObject.has("country") ? fromJSONObject.optString("country") : null;
222
+ }
223
+ }
224
+
225
+ public static class ImageInput {
226
+
227
+ public final String base64String;
228
+
229
+ ImageInput(JSONObject fromJSONObject) {
230
+ this.base64String = fromJSONObject.has("base64String") ? fromJSONObject.optString("base64String") : null;
231
+ }
232
+ }
233
+ }
@@ -0,0 +1,214 @@
1
+ package getcapacitor.community.contacts;
2
+
3
+ import android.provider.ContactsContract;
4
+ import android.util.Log;
5
+ import androidx.annotation.Nullable;
6
+ import java.util.ArrayList;
7
+ import org.json.JSONObject;
8
+
9
+ public class GetContactsProjectionInput {
10
+
11
+ // Name
12
+ private final boolean name;
13
+
14
+ // Organization
15
+ private final boolean organization;
16
+
17
+ // Birthday
18
+ private final boolean birthday;
19
+
20
+ // Note
21
+ private final boolean note;
22
+
23
+ // Phones
24
+ private final boolean phones;
25
+
26
+ // Emails
27
+ private final boolean emails;
28
+
29
+ // URLs
30
+ private final boolean urls;
31
+
32
+ // Postal Addresses
33
+ private final boolean postalAddresses;
34
+
35
+ // Image
36
+ private final boolean image;
37
+
38
+ GetContactsProjectionInput(JSONObject fromJSONObject) {
39
+ // Name
40
+ this.name = fromJSONObject.optBoolean("name");
41
+
42
+ // Organization
43
+ this.organization = fromJSONObject.optBoolean("organization");
44
+
45
+ // Birthday
46
+ this.birthday = fromJSONObject.optBoolean("birthday");
47
+
48
+ // Note
49
+ this.note = fromJSONObject.optBoolean("note");
50
+
51
+ // Phones
52
+ this.phones = fromJSONObject.optBoolean("phones");
53
+
54
+ // Emails
55
+ this.emails = fromJSONObject.optBoolean("emails");
56
+
57
+ // URLs
58
+ this.urls = fromJSONObject.optBoolean("urls");
59
+
60
+ // Postal Addresses
61
+ this.postalAddresses = fromJSONObject.optBoolean("postalAddresses");
62
+
63
+ // Image
64
+ this.image = fromJSONObject.optBoolean("image");
65
+ }
66
+
67
+ public String[] getProjection() {
68
+ ArrayList<String> projection = new ArrayList<>();
69
+
70
+ projection.add(ContactsContract.Data.MIMETYPE);
71
+ projection.add(ContactsContract.Data._ID);
72
+ projection.add(ContactsContract.Data.CONTACT_ID);
73
+
74
+ // Name
75
+ if (this.name) {
76
+ projection.add(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME);
77
+ projection.add(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
78
+ projection.add(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME);
79
+ projection.add(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME);
80
+ projection.add(ContactsContract.CommonDataKinds.StructuredName.PREFIX);
81
+ projection.add(ContactsContract.CommonDataKinds.StructuredName.SUFFIX);
82
+ }
83
+
84
+ // Organization
85
+ if (this.organization) {
86
+ projection.add(ContactsContract.CommonDataKinds.Organization.COMPANY);
87
+ projection.add(ContactsContract.CommonDataKinds.Organization.TITLE);
88
+ projection.add(ContactsContract.CommonDataKinds.Organization.DEPARTMENT);
89
+ }
90
+
91
+ // Birthday
92
+ if (this.birthday) {
93
+ projection.add(ContactsContract.CommonDataKinds.Event.START_DATE);
94
+ projection.add(ContactsContract.CommonDataKinds.Event.TYPE);
95
+ }
96
+
97
+ // Note
98
+ if (this.note) {
99
+ projection.add(ContactsContract.CommonDataKinds.Note.NOTE);
100
+ }
101
+
102
+ // Phones
103
+ if (this.phones) {
104
+ projection.add(ContactsContract.CommonDataKinds.Phone.TYPE);
105
+ projection.add(ContactsContract.CommonDataKinds.Phone.LABEL);
106
+ projection.add(ContactsContract.CommonDataKinds.Phone.IS_PRIMARY);
107
+ projection.add(ContactsContract.CommonDataKinds.Phone.NUMBER);
108
+ // projection.add(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER);
109
+ }
110
+
111
+ // Emails
112
+ if (this.emails) {
113
+ projection.add(ContactsContract.CommonDataKinds.Email.TYPE);
114
+ projection.add(ContactsContract.CommonDataKinds.Email.LABEL);
115
+ projection.add(ContactsContract.CommonDataKinds.Email.IS_PRIMARY);
116
+ projection.add(ContactsContract.CommonDataKinds.Email.ADDRESS);
117
+ }
118
+
119
+ // URLs
120
+ if (this.urls) {
121
+ projection.add(ContactsContract.CommonDataKinds.Website.URL);
122
+ }
123
+
124
+ // Postal Addresses
125
+ if (this.postalAddresses) {
126
+ projection.add(ContactsContract.CommonDataKinds.StructuredPostal.TYPE);
127
+ projection.add(ContactsContract.CommonDataKinds.StructuredPostal.LABEL);
128
+ projection.add(ContactsContract.CommonDataKinds.StructuredPostal.IS_PRIMARY);
129
+ projection.add(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS);
130
+ projection.add(ContactsContract.CommonDataKinds.StructuredPostal.STREET);
131
+ projection.add(ContactsContract.CommonDataKinds.StructuredPostal.NEIGHBORHOOD);
132
+ projection.add(ContactsContract.CommonDataKinds.StructuredPostal.CITY);
133
+ projection.add(ContactsContract.CommonDataKinds.StructuredPostal.REGION);
134
+ projection.add(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE);
135
+ projection.add(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY);
136
+ }
137
+
138
+ // Image
139
+ if (this.image) {
140
+ projection.add(ContactsContract.Contacts.Photo.PHOTO);
141
+ }
142
+
143
+ return projection.toArray(new String[projection.size()]);
144
+ }
145
+
146
+ public String[] getSelectionArgs() {
147
+ ArrayList<String> selectionArgs = new ArrayList<>();
148
+
149
+ // Name
150
+ if (this.name) {
151
+ selectionArgs.add(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
152
+ }
153
+
154
+ // Organization
155
+ if (this.organization) {
156
+ selectionArgs.add(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
157
+ }
158
+
159
+ // Birthday
160
+ if (this.birthday) {
161
+ selectionArgs.add(ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE);
162
+ }
163
+
164
+ // Note
165
+ if (this.note) {
166
+ selectionArgs.add(ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE);
167
+ }
168
+
169
+ // Phones
170
+ if (this.phones) {
171
+ selectionArgs.add(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
172
+ }
173
+
174
+ // Emails
175
+ if (this.emails) {
176
+ selectionArgs.add(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
177
+ }
178
+
179
+ // URLs
180
+ if (this.urls) {
181
+ selectionArgs.add(ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
182
+ }
183
+
184
+ // Postal Addresses
185
+ if (this.postalAddresses) {
186
+ selectionArgs.add(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);
187
+ }
188
+
189
+ // Image
190
+ if (this.image) {
191
+ selectionArgs.add(ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
192
+ }
193
+
194
+ return selectionArgs.toArray(new String[selectionArgs.size()]);
195
+ }
196
+
197
+ public static String getSelection(String[] selectionArgs) {
198
+ String selection = "";
199
+
200
+ if (selectionArgs.length > 0) {
201
+ selection = selection.concat(ContactsContract.Data.MIMETYPE + " in (");
202
+ for (int i = 0; i < selectionArgs.length; i++) {
203
+ if (i == 0) {
204
+ selection = selection.concat("?");
205
+ } else {
206
+ selection = selection.concat(", ?");
207
+ }
208
+ }
209
+ selection = selection.concat(")");
210
+ }
211
+
212
+ return selection;
213
+ }
214
+ }