Android Tutorial [B4A] [Class] [Contacts] wmContactsUtils - enhanced ContactsUtils

This is an enhanced version based on @Erel 's ContactsUtils v1.20.

NOTE: DeleteContact didn't work on an Amazon Fire tablet with Android 5.1.1 but did on a Moto X Style phone with Android 7. I wasn't able to find the cause; possibly it's related to the ROM I installed on that tablet or to the older Android version - no idea. No error was returned and 'cr.Delete' returned 1 indicating that the deletion had taken place; the contact was just still there after the call.

Changes to ContactsUtils v1.20 code (all changes are marked with a 'WM' comment in the code):
- Private Sub GetKeyFromValue: changed the lookup to be case-insensitive
- Private Sub SetData: small changes that don't impact existing code
- Public Sub DeleteContact: added return type Int that shows how many contacts were deleted

New public methods from or based on ddahan's code from https://www.b4x.com/android/forum/threads/create-contact-missing-fields.40791/:
- AddOrganization: Adds an organisation to a contact
- AddPhoto: Adds a photo to a contact from a bitmap
- AddPostalAddress: Adds an unstructured postal address for a contact
- AddPostalAddress1: Adds an unstructured postal address for a contact
- GetDetailedName: Returns Detailed Contact Name as cuContactName
- GetOrganization: Returns a contact's organisation data as a cuOrganization item
- GetPostalAddresses: Returns a List with cuPostalAddr items
- GetPostalAddresses1: Returns a Map with the postal addresses as keys and the address types as values
- InsertContactWithDetailedName: Inserts a new contact and returns the cuContact object of this contact
- SetOrganization: Sets a contact's organisation
- SetPhoto: Sets a contact's photo from a bitmap
- SetPostalAddress: Sets an unstructured postal address for a contact

New public methods added by me:
- AddEvent: Adds an event field to the given contact id; Events are only available as of Android API level 5
- AddIM: Adds an IM field to the given contact id
- AddNickname: Adds a nickname field to the given contact id
- AddPostalAddress2: Adds a structured postal address
- AddWebsite: Adds a website field to the given contact id
- DeleteContact2: Deletes the contact with the given id and catches errors
- DeleteWebsite: Deletes the given website
- FindContactsByGroupName: Returns all contacts that are member of the specified group name
- FindContactsByGroupRowId: Returns all contacts that are member of the specified group Row ID
- GetAllGroupsByRowId: Returns a map with all groups; the key is the group's Row ID, the value is the group name
- GetAllGroupsByRowName: Returns a map with all groups; the key is the group name, the value is the group Row ID
- GetAllGroupsBySourceId: Returns a map with all groups; the key is the group's source ID, the value is the group name (you probably shouldn't be using this one, but use GetAllGroupsByRowId instead)
- GetAllGroupsBySourceName: Returns a map with all groups; the key is the group name, the value is the group source ID (you probably shouldn't be using this one, but use GetAllGroupsByRowName instead)
- GetAllContactsThunderbird: Returns all contacts as a list of cuContactDetailsThunderbird items (this is a Type I added which I needed for another project)
- GetContactDetailsThunderbird: Returns a contact's data as a cuContactDetailsThunderbird item
- GetFullSizePhoto: Returns the full size photo of the given contact; returns an uninitialized bitmap if no photo is available
- GetIMs: Returns a List with cuIM items
- GetNicknames: Returns a List with cuNickname items
- GetWebsites: Returns a List with cuWebsite items
- SetDisplayName: Sets an existing contact's DisplayName
- SetPhotoFromFile: Sets a contact's photo from a file

Enjoy !

Updates:
- 2021-10-12: bug fix in the new methods, attached class updated
- 2021-10-12: added methods FindContactsByGroupRowId, FindContactsByGroupName, GetAllGroupsByRowId, GetAllGroupsByRowName, GetAllGroupsBySourceId, GetAllGroupsBySourceName
- 2021-06-24: added method DeleteContact2
 

Attachments

  • wmContactsUtils.bas
    37.1 KB · Views: 447
Last edited:

SMR

New Member
Hi dear

Thanks for your very nice class ;

i have a problem in get accounts with contact id;

when i get accounts of o contact , this feature just return name of account like telegram & whatsapp &.... but i need get whats number of contact have a acount,

for example :

a contact have 3 mobile number but just a number of contact have whatsapp & telegram and any ....
how i can find out what number have telegram &... ?

thanks.
 

adriano.freitas

Active Member
I have huge contact list accounts from which I need to show contacts based on choice of groups. Currently I have taken all the contacts and made a loop to go through them and look for the ones that are in the desired groups. This makes processing extremely slow. Is it possible to add a method that returns the contact list based on the name of a group?
 

walt61

Active Member
Licensed User
Longtime User
That kept me going for a significant part of the day with research, trial, and a lot of error - but it seems I got there. See attached demo project; the first post and its attached class have been updated too.

EDIT: updated the example to demonstrate 'find by name' as well as 'find by ID'
 

Attachments

  • ContactsByGroup.zip
    17.5 KB · Views: 296
Last edited:

adriano.freitas

Active Member
Wow! I'm surprised! Fast and very effective! I really appreciate the help! I've been trying to solve for several days without success (I didn't know how the storage of contacts on Android works, which made it difficult). Again, thank you so much! Great!
 
Top