B4A Library [Lib] Contacts (OPEN SOURCE)

Hi everyone,

here is my first library "Contacts", written in the great B4A editor.
It's a redesign and enhancement of the great "ContactUtils" class from Erel.

What can you do with this library:


With this library you can add / remove / find / edit contacts.
In the contacts you can:
- add / get / remove address entries
- add / get / remove email address entries
- add / get / remove event entries
- add / get / remove im (Instant Messanger) entries
- add / get / remove nickname entries
- add / get / remove note entry
- add / get / remove phone numbers entries
- add / get / remove organization entries
- add / get / remove relation entries
- add / get / remove sip address entries
- add / get / remove thumbnail bitmap
- add / get / remove website entries
- get / set the display name
- get / set starred state
- get / set the structured name
- get the full size display photo
- get / set primary phonenumber and/or email address

Needed Permissions:

Permissions for that lib must be set in the manifest editor by your self:
B4X:
AddPermission("android.permission.READ_CONTACTS")
AddPermission("android.permission.WRITE_CONTACTS") 'if write access is required

Used libraries in this library:

- ContentResolver (>= 1.0)
- JavaObject (>= 2.05)
- SQL (>= 1.30)

How to use:

First a instance of ContactBook class is needed:
B4X:
Dim cBook As ContactBook
cBook.Initialize


Now a new contact can be added:
B4X:
Dim cItem As ContactItem
cItem = cBook.AddContact("Max Anyone")


Or a contact can be found:
B4X:
Dim cItem As ContactItem
Dim foundContactItems As List
foundContactItems = cBook.GetContactsByName("Max Anyone", False, True)
If foundContactItems.Size > 0 Then
    cItem = foundContactItems.Get(0)
End If


A list of all visible contacts:
B4X:
Dim cItem As ContactItem
Dim contactItems As List
contactItems = cBook.GetAllContacts(True) 'Unsorted
If contactItems.Size > 0 Then
    Dim cItem As ContactItem
    cItem = contactItems.Get(0)
End If

'With lib version >= 1.01, the contacts can be get sorted by displayname
contactItems = cBook.GetAllContacts2(True, True) 'Sorted
If contactItems.Size > 0 Then
    Dim cItem As ContactItem
    cItem = contactItems.Get(0)
End If


With a valid ContactItem class instance the contact can be edit directly:
B4X:
'Add a phone number simple way:
cItem.AddPhoneNumberSimple("00328888888", ContactStatics.PhoneNumberTypes.Get("1"))

'Or add a phone number like this:
Dim cPhoneNumber As ContactPhoneNumber
cPhoneNumber.Initialize
cPhoneNumber.Number = "00328888888"
cPhoneNumber.Typ = ContactStatics.PhoneNumberTypes.Get("1") ' "home" type
cItem.AddPhoneNumber(cPhoneNumber)

'After a phone number was added it can be set as primary, or use an existing phone number of this contact item:
Dim phoneNumbers() As ContactPhoneNumber
phoneNumbers = cItem.PhoneNumbers
If phoneNumbers.Length > 0 Then
    cPhoneNumber = phoneNumbers(0)
End If
cItem.SetPrimaryPhoneNumber(cPhoneNumber)


Something to know about the contact classes:
ContactAddress, ContactEmailAddress, ContactEvent, ContactIM, ContactNickname, ContactOrganization, ContactPhoneNumber, ContactRelation, ContactSipAddress and ContactWebsite.

All of these classes has a TYP property. This property will be set to a default value on initalizing a class. For example a new ContactAddress class object has "home" as default type. The TYP property must have an exact value out of the diffrent type maps that can be found under the ContactStatics module. For example:
B4X:
Dim address As ContactAddress
address.Typ = ContactStatics.AddressTypes.Get("1") ' same as "home"
address.Typ = "home" 'set one of the exact values by your self is also allowed

Dim phoneNumber As ContactPhoneNumber
phoneNumber.Typ = ContactStatics.PhoneNumberTypes.Get("2") ' "mobile"
phoneNumber.Typ = "mobile"
If you know all these exact typenames you can set it by your self to the TYP property.

The classes have also a LABEL property. This property can be used for a custom type name. But before a label can be set correctly, the TYP property must be set to "custom".
B4X:
Dim website As ContactWebsite
website.Typ = ContactStatics.WebsiteTypes.Get("0") ' same as = "custom"
website.Label = "Forum"

Versions:

V1.0 Release Version
V1.01 Fixed
- Thumbnail set error removed
- new class ContactItemSnapshot, to get a memory snapshot of a contactitem
- new function GetSnapshot at ContactItem class, to get a snapshot of the given contactitem
- new function GetAllContacts2 at ContactBook class, to get a sorted list of contactitem's
V1.02
- new functions ReadFrom and WriteTo (re)added to ContactItemSnapshot, to store a contact to stream
V>1.02
- For now the source code is free for everyone. I don't have the time to fix something.
- Please upload your bugfixes here for the community.


The attached test project will list all visible contacts and its data entries. A long press will change the
contacts starred state.
 

Attachments

  • ContactsLibTest.zip
    8.6 KB · Views: 514
  • ContactsLib1.0.zip
    54.3 KB · Views: 477
  • ContactsLib1.01fixed.zip
    55.7 KB · Views: 412
  • ContactsLib1.02.zip
    71 KB · Views: 571
  • Lib Contacts 1.02 Src Code.zip
    36.3 KB · Views: 499
Last edited:

Tayfur

Well-Known Member
Licensed User
Longtime User
Hello;
firstly; i apologize for disturbing when you on vacation. :oops:

I cancelled facerecord method.

This is very same my orginal code. it has only one button. This button save 4 records ( code added ".Demo."&Name, Therefore; you can find easly fake records. )
and yopu please check logs.

Very important issue @Serjoscha Bald , AFTER HOLIDAYS...;)
 

Attachments

  • Contac_backup_fake.zip
    187 KB · Views: 254

Serjoscha Bald

Member
Licensed User
Longtime User
Hi Tayfur,

i had wrote my own test program, and yes i get the same error like you. I had tested a little bit around and found something that should be interesting.
The app i wrote backups all contacts that was found by the contact book class and it can restore these contacts.

So first i have create a backup of all my contacts on the real android device. Next i have copied the created file to a virtual android (5.1.1).
Then i had started the restore process. While restoring the contacts i get an exception on the new function ReadFrom in ContactItemSnapshot class.
It should be a bug, i will look later whats going wrong here. Some of the contacts are restored correctly. No error "raw contact id".
Now i start the restoring again. And yes there is the error. It seems to be that when you add a contact twice android removes it while you adding all entries
to the contact. After that i removed all contacts on the virtual android and add only one contact. While the app adds the contact again i get an error "raw id".
The app adds first the display name, then the structured name, addresses, email addresses and before events can be added the contact was removed by android.

I had not tested yet if it will be when a fake/other name will be used then the existing contact.
 

Tayfur

Well-Known Member
Licensed User
Longtime User
Hi Tayfur,

i had wrote my own test program, and yes i get the same error like you. I had tested a little bit around and found something that should be interesting.
The app i wrote backups all contacts that was found by the contact book class and it can restore these contacts.

So first i have create a backup of all my contacts on the real android device. Next i have copied the created file to a virtual android (5.1.1).
Then i had started the restore process. While restoring the contacts i get an exception on the new function ReadFrom in ContactItemSnapshot class.
It should be a bug, i will look later whats going wrong here. Some of the contacts are restored correctly. No error "raw contact id".
Now i start the restoring again. And yes there is the error. It seems to be that when you add a contact twice android removes it while you adding all entries
to the contact. After that i removed all contacts on the virtual android and add only one contact. While the app adds the contact again i get an error "raw id".
The app adds first the display name, then the structured name, addresses, email addresses and before events can be added the contact was removed by android.

I had not tested yet if it will be when a fake/other name will be used then the existing contact.

Thank you for feedback @Serjoscha Bald
when you fix it? Next week?
 

Serjoscha Bald

Member
Licensed User
Longtime User
Hi Tayfur,

i will fix the bug in ReadFrom method of ContactItemSnapshot class. But the "RAW ID" problem can not be fixed. I think the android system check's the contacts and removes a contact automaticly if it exists already. I don't know in which cases. On my last tests the android system removes the contact after adding a already exisiting email address. What will the android system do when the email addresses will be added at last? That should be tested.

What i can do is to add an exception "RAW ID LOST" or an event so that your app knows that the ContactItem was now invalid / removed.
 

Tayfur

Well-Known Member
Licensed User
Longtime User
Hi Tayfur,

i will fix the bug in ReadFrom method of ContactItemSnapshot class. But the "RAW ID" problem can not be fixed. I think the android system check's the contacts and removes a contact automaticly if it exists already. I don't know in which cases. On my last tests the android system removes the contact after adding a already exisiting email address. What will the android system do when the email addresses will be added at last? That should be tested.

What i can do is to add an exception "RAW ID LOST" or an event so that your app knows that the ContactItem was now invalid / removed.

hello;

do you have anyway a new record or change with ID.
I think, OS works with ID number.

do @Erel have any idea on "contactsutils class (if your lib has this class)"
 

Serjoscha Bald

Member
Licensed User
Longtime User
Hello Community,

Lib Contacts is now open source. I have no time to fix or add features because work and family needs the time.
Feel free to add, change or use the code in your code. Please upload bugfixes for the community here.

I hope you will enjoy it.

Most of the functions are commented.

Have a nice day.
 

vvg

Member
Licensed User
Longtime User
Hello Community,

Lib Contacts is now open source. I have no time to fix or add features because work and family needs the time.
Feel free to add, change or use the code in your code. Please upload bugfixes for the community here.

I hope you will enjoy it.

Most of the functions are commented.

Have a nice day.

Problem Resolved, Thank You
 
Top