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: 420
  • ContactsLib1.0.zip
    54.3 KB · Views: 372
  • ContactsLib1.01fixed.zip
    55.7 KB · Views: 328
  • ContactsLib1.02.zip
    71 KB · Views: 465
  • Lib Contacts 1.02 Src Code.zip
    36.3 KB · Views: 412
Last edited:

Tayfur

Well-Known Member
Licensed User
Longtime User
hi;

I used your lb. First Thank you for lib. @Serjoscha Bald

I have some problem I cant add picture. (and i cant recive any error logs ???)

please hare full records sample for add functions.
B4X:
Sub Fake_record(cbook As ContactBook)
 
    Dim Citem As ContactItem
Citem=cbook.AddContact(".XX."&"Fake Name")

Dim Cnn As ContactNickname
Cnn.Initialize
        Cnn.Name=".Xnick name"'Kayit.Nick_Names(i).Name&"-"
        Cnn.Label=""'Kayit.Nick_Names(i).Label
        Cnn.Typ="sfasdfa"'Kayit.Nick_Names(i).Typ
        Citem.AddNickname(Cnn)

Dim Cadress2 As ContactAddress
Cadress2.Initialize
        Cadress2.City="istanbul"
        Cadress2.Country="turkey"
        Cadress2.Formatted=""'Kayit.Local_Addresses(i).Formatted
        Cadress2.Label=""'Kayit.Local_Addresses(i).Label
        Cadress2.Typ="sallamöa"'Kayit.Local_Addresses(i).Typ
        Cadress2.Neighborhood=""'Kayit.Local_Addresses(i).Neighborhood
        Cadress2.POBox="12345"'Kayit.Local_Addresses(i).Pobox
        Cadress2.Postcode=""'Kayit.Local_Addresses(i).Postcode
        Cadress2.Region=""'Kayit.Local_Addresses(i).Region
        Cadress2.Street=""'Kayit.Local_Addresses(i).Street
        Citem.AddAddress(Cadress2)

Dim Cemail As ContactEmailAddress
Cemail.Initialize
        Cemail.Address ="[email protected]"'Kayit.Email_Addresses(i).E_Mail_Address
     
        Cemail.Label=""'Kayit.Email_Addresses(i).Label
        Cemail.Typ=""'Kayit.Email_Addresses(i).Typ
        If True Then
            Citem.SetPrimaryEmailAddress(Cemail)
        Else
            Citem.AddEmailAddress(Cemail)
        End If

Dim Cim As ContactIM
Cim.Initialize
        Cim.Data="data cim"'Kayit.Co_IMs(i).Data
        Cim.CustomProtocol =""'Kayit.Co_IMs(i).CustomProtocol
        Cim.Label=""'Kayit.Co_IMs(i).Label
        Cim.Typ=""'Kayit.Co_IMs(i).Typ
        Citem.AddIM(Cim)

Dim Crel As ContactRelation
Crel.Initialize
        Crel.Name=".xx crelatiiiion name"'Kayit.Co_Relations(i).Name
        Crel.Label=""'Kayit.Co_Relations(i).Label
        Crel.Typ=""'Kayit.Co_Relations(i).Typ
        Citem.AddRelation(Crel)

Dim Corg As ContactOrganization
Corg.Initialize
        Corg.Company="company"'Kayit.Contac_Organizations(i).Company
        Corg.Label=""'Kayit.Contac_Organizations(i).Label
        Corg.Typ=""'Kayit.Contac_Organizations(i).Typ
        Corg.Department="dept"'Kayit.Contac_Organizations(i).Department
        Corg.JobDescription=""'Kayit.Contac_Organizations(i).JobDescription
        Corg.OfficeLocation=""'Kayit.Contac_Organizations(i).OfficeLocation
        Corg.PhoneticName=""'Kayit.Contac_Organizations(i).PhoneticName
        Corg.PhoneticNameStyle=""'Kayit.Contac_Organizations(i).PhoneticNameStyle
        Corg.Symbol=""'Kayit.Contac_Organizations(i).Symbol
        Corg.Title="title"'Kayit.Contac_Organizations(i).Title
        Citem.AddOrganization(Corg)



Dim Cpno As ContactPhoneNumber
Cpno.Initialize
        Cpno.Number="1234567898555"'Kayit.Phone_Numbers(i).Number
        Cpno.Label=""'Kayit.Phone_Numbers(i).Label
        Cpno.Typ="saaal"'Kayit.Phone_Numbers(i).Typ
        If True Then
            Citem.SetPrimaryPhoneNumber(Cpno)
            Else
            Citem.AddPhoneNumber(Cpno)
        End If

Dim Csip As ContactSipAddress
Csip.Initialize
        Csip.SipAddress="sipa dres"'Kayit.Sip_Addresses(i).SipAddress
        Csip.Label=""'Kayit.Sip_Addresses(i).Label
        Csip.Typ=""'Kayit.Sip_Addresses(i).Typ
        Citem.AddSipAddress(Csip)

Dim Csname As ContactStructuredName
Csname.Initialize
        Csname.DisplayName="disp name"'Kayit.Structured_Name.DisplayName
        Csname.FamilyName=""'Kayit.Structured_Name.FamilyName
        Csname.FullNameStyle = ""'Kayit.Structured_Name.FullNameStyle
        Csname.GivenName =""' Kayit.Structured_Name.GivenName
        Csname.MiddleName =""' Kayit.Structured_Name.MiddleName
        Csname.PhoneticFamilyName = ""'Kayit.Structured_Name.PhoneticFamilyName
        Csname.PhoneticGivenName =""' Kayit.Structured_Name.PhoneticGivenName
        Csname.Prefix = ""'Kayit.Structured_Name.Prefix
        Csname.Suffix = ""'Kayit.Structured_Name.Suffix
        Csname.PhoneticMiddleName = ""'Kayit.Structured_Name.PhoneticMiddleName
        Citem.StructuredName=Csname

Dim Cweb As ContactWebsite
Cweb.Initialize
        Cweb.URL="www.gogle.com"'Kayit.Web_sites(i).Label
        Cweb.Label=""'Kayit.Web_sites(i).Label
        Cweb.Typ=""'Kayit.Web_sites(i).Typ
        Citem.AddWebsite(Cweb)


Dim Cevent As ContactEvent
Cevent.Initialize
        Cevent.StartDate=""'Kayit.Events(i).StartDate
        Cevent.Label=""'Kayit.Events(i).Label
        Cevent.Typ=""'Kayit.Events(i).Typ
        Citem.AddEvent(Cevent)


Citem.ThumbnailPhoto.Initialize(File.DirAssets,"attach-0.png")

Citem.Starred =True

Citem.Note="Nottess"

End Sub
 
Last edited:

Serjoscha Bald

Member
Licensed User
Longtime User
Hi Tayfur,

thank you for using my contacts library. I will do my best to explain how to use the library in the right way:

First create a ContactBook instance like this:
B4X:
    Dim cBook As ContactBook
    cBook.Initialize

Next get an instance of ContactItem by add or find the right one:
B4X:
Dim cItem As ContactBook
'Add one:
cItem = cBook.AddContact("Firstname Lastname")

'Or Get one:
Dim foundCItems As List
foundCItems = cBook.GetContactsByName("A name", False, True)
If foundCItems.Size > 0 Then
  cItem = foundCItems.Get(0)
End If
Now we can work with the ContactItem instance and it's underlaying contact.

Bevor we go on to add, get or remove some of the ContactItem's entries you should know how to work with the properties like TYP (TYPE) and LABEL. This properties can be found in the classes ContactAddress, ContactEmailAddress, ContactEvent, ContactIM, ContactNickname, ContactOrganization, ContactPhoneNumber, ContactRelation, ContactSipAddress and ContactWebsite. If you create one of this classes the properties TYP and LABEL will be set to a default value. For example the default TYP value of a ContactAddress class instance is "home" and the default value of LABEL is "" empty string. A list of valid values for the TYP property can be found in the ContactStatics module:
B4X:
Dim cAddress As ContactAddress
cAddress.Initialize
cAddress.Typ = ContactStatics.AddressTypes.Get(2) 'same as "work"

'Or you set it by your self, if you know the right exact values:
cAddress.Typ = "other" 'same as ContactStatics.AddressTypes.Get(3)


The LABEL can be used for customized type name, but the TYP property must be set to "custom".
B4X:
Dim cPhoneNumber As ContactPhoneNumber
cPhoneNumber.Initialize
cPhoneNumber.Typ = ContactStatics.PhoneNumberTypes.Get(0) 'same as "custom"
cPhoneNumber.Label = "Private"


In ContactIM class you will find the property Protocol and in ContactStructuredName class the FullNameStyles property. These properties need also a exactly defined value that can be found in ContactStatics module.


How to add a nickname and a primary phonenumber:
B4X:
Dim cNickname As ContactNickname
cNickname.Initialize
cNickname.Name = "Someone"
cNickname.Typ = ContactStatics.NicknameTypes.Get(4) ' "short_name"
cItem.AddNickname(cNickname)
   
Dim cPhoneNumber As ContactPhoneNumber
cPhoneNumber.Initialize
cPhoneNumber.Number = "0037383940"
cItem.AddPhoneNumber(cPhoneNumber) ' It's a number with the type "home"
cItem.SetPrimaryPhoneNumber(cPhoneNumber)
Only one phone number and one email address can be primary in a contact. When another phone number or email address was set as primary the last one will lost this status.


How to change data without to add twice entries:
B4X:
Dim cOrganizations() As ContactOrganization
cOrganizations = cItem.Organizations
If cOrganizations.Length > 0 Then
  Dim cOrganizationChanged As ContactOrganization
  cOrganizationChanged = cOrganizations(0)
  'The entry must be removed before it was changed
  cItem.RemoveOrganization(cOrganizationChanged)
  cOrganizationChanged.Company = "Renamed Company"
  cItem.AddOrganization(cOrganizationChanged)
End If


How to set a thumbnail:
B4X:
Dim thumbBitmap As Bitmap
thumbBitmap.Initialize(File.DirRootExternal, "FileName")
cItem.ThumbnailPhoto = thumbBitmap

'Or this way:
thumbBitmap = LoadBitmap(File.DirRootExternal, "FileName")
cItem.ThumbnailPhoto = thumbBitmap

I hope this will help to understand how it works.
 

Tayfur

Well-Known Member
Licensed User
Longtime User
Hi Tayfur,

thank you for using my contacts library. I will do my best to explain how to use the library in the right way:

First create a ContactBook instance like this:
B4X:
    Dim cBook As ContactBook
    cBook.Initialize

Next get an instance of ContactItem by add or find the right one:
B4X:
Dim cItem As ContactBook
'Add one:
cItem = cBook.AddContact("Firstname Lastname")

'Or Get one:
Dim foundCItems As List
foundCItems = cBook.GetContactsByName("A name", False, True)
If foundCItems.Size > 0 Then
  cItem = foundCItems.Get(0)
End If
Now we can work with the ContactItem instance and it's underlaying contact.

Bevor we go on to add, get or remove some of the ContactItem's entries you should know how to work with the properties like TYP (TYPE) and LABEL. This properties can be found in the classes ContactAddress, ContactEmailAddress, ContactEvent, ContactIM, ContactNickname, ContactOrganization, ContactPhoneNumber, ContactRelation, ContactSipAddress and ContactWebsite. If you create one of this classes the properties TYP and LABEL will be set to a default value. For example the default TYP value of a ContactAddress class instance is "home" and the default value of LABEL is "" empty string. A list of valid values for the TYP property can be found in the ContactStatics module:
B4X:
Dim cAddress As ContactAddress
cAddress.Initialize
cAddress.Typ = ContactStatics.AddressTypes.Get(2) 'same as "work"

'Or you set it by your self, if you know the right exact values:
cAddress.Typ = "other" 'same as ContactStatics.AddressTypes.Get(3)


The LABEL can be used for customized type name, but the TYP property must be set to "custom".
B4X:
Dim cPhoneNumber As ContactPhoneNumber
cPhoneNumber.Initialize
cPhoneNumber.Typ = ContactStatics.PhoneNumberTypes.Get(0) 'same as "custom"
cPhoneNumber.Label = "Private"


In ContactIM class you will find the property Protocol and in ContactStructuredName class the FullNameStyles property. These properties need also a exactly defined value that can be found in ContactStatics module.


How to add a nickname and a primary phonenumber:
B4X:
Dim cNickname As ContactNickname
cNickname.Initialize
cNickname.Name = "Someone"
cNickname.Typ = ContactStatics.NicknameTypes.Get(4) ' "short_name"
cItem.AddNickname(cNickname)
  
Dim cPhoneNumber As ContactPhoneNumber
cPhoneNumber.Initialize
cPhoneNumber.Number = "0037383940"
cItem.AddPhoneNumber(cPhoneNumber) ' It's a number with the type "home"
cItem.SetPrimaryPhoneNumber(cPhoneNumber)
Only one phone number and one email address can be primary in a contact. When another phone number or email address was set as primary the last one will lost this status.


How to change data without to add twice entries:
B4X:
Dim cOrganizations() As ContactOrganization
cOrganizations = cItem.Organizations
If cOrganizations.Length > 0 Then
  Dim cOrganizationChanged As ContactOrganization
  cOrganizationChanged = cOrganizations(0)
  'The entry must be removed before it was changed
  cItem.RemoveOrganization(cOrganizationChanged)
  cOrganizationChanged.Company = "Renamed Company"
  cItem.AddOrganization(cOrganizationChanged)
End If


How to set a thumbnail:
B4X:
Dim thumbBitmap As Bitmap
thumbBitmap.Initialize(File.DirRootExternal, "FileName")
cItem.ThumbnailPhoto = thumbBitmap

'Or this way:
thumbBitmap = LoadBitmap(File.DirRootExternal, "FileName")
cItem.ThumbnailPhoto = thumbBitmap

I hope this will help to understand how it works.

Thanks for sharing @Serjoscha Bald ; I will try it.
thanks
 

Tayfur

Well-Known Member
Licensed User
Longtime User
I recived an error load bitmap?

Note: this picture recorded from orginally phone thumbail picture. (size 96*96)

B4X:
Dim thumbBitmap As Bitmap
thumbBitmap.Initialize(File.DirAssets,"r1.png")
If thumbBitmap.IsInitialized Then Log("ok intliza")
Citem.ThumbnailPhoto = thumbBitmap 'line 2124

'-----------------EROROR--------------
** Activity (main) Pause, UserClosed = false **
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
ok intliza
Error occurred on line: 2124 (Main)
java.lang.ClassCastException: java.util.ArrayList cannot be cast to android.database.Cursor
    at b4a.example.contacthandler._setthumbnailphoto(contacthandler.java:747)
    at b4a.example.contactitem._setthumbnailphoto(contactitem.java:906)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:697)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:336)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
    at contact.backup.main._fake_record(main.java:1750)
    at contact.backup.main._btn_3_click(main.java:1696)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:697)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
    at android.view.View.performClick(View.java:4789)
    at android.view.View$PerformClick.run(View.java:19881)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5294)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
** Activity (main) Pause, UserClosed = true **
** Service (starter) Destroy **
** Activity (main) Resume **
 

Serjoscha Bald

Member
Licensed User
Longtime User
Hi Tayfur,

thank you, there was a bug in the thumbnail setting method. I will correct this in the next days and bring out a new lib version.

I have added a new class to the lib (ContactItemSnapshot) with binary load and save functions. But they don't working well. I working hard on it to get a bug free new lib out.
 
Last edited:

Tayfur

Well-Known Member
Licensed User
Longtime User
Hi Tayfur,

thank you, there was a bug in the thumbnail setting method. I will correct this in the next days and bring out a new lib version.

I have added a new class to the lib (ContactItemSnapshot) with binary load and save functions. But they don't working well. I working hard on it to get a bug free new lib out.
I am waiting Impatiently your good news.
Thanks.
 

Serjoscha Bald

Member
Licensed User
Longtime User
Hi Tayfur,

it's done you can download now the contacts lib 1.01. Please let me know if the thumbnail error was solved.
 

Tayfur

Well-Known Member
Licensed User
Longtime User

Serjoscha Bald

Member
Licensed User
Longtime User
Hi,

the base lib was removed from contacts lib. The new stream methods in the ContactItemSnapshot class are removed for now!
I think i will add them in one of the next updates again.
 

Serjoscha Bald

Member
Licensed User
Longtime User
A new Version is out now. All needed classes are transfered from my base lib to the contacts lib to get the stream methods working.
 

Tayfur

Well-Known Member
Licensed User
Longtime User
sorry ; Again me;

I have a contact with 4 phone numbers.

And I recived some error and Record cancelled when I was record.
Your error in log records=>" Error getting raw_contact_id"

This is record code. (Use own type. it is simple string array like you.)
B4X:
Dim Citem As ContactItem
Citem=cbook.AddContact (".XX."&Kayit.DisplayName)
Dim Cpno As ContactPhoneNumber
    For i=0 To Kayit.Phone_Numbers.Length-1
        Cpno.Initialize
        Cpno.Number=Kayit.Phone_Numbers(i).Number
        If  Kayit.Phone_Numbers(i).Typ.EqualsIgnoreCase("custom") Then
            Cpno.Label=Kayit.Phone_Numbers(i).Label
            Cpno.Typ=Kayit.Phone_Numbers(i).Typ
        Else
            Cpno.Typ=Kayit.Phone_Numbers(i).Typ
        End If
        If Kayit.Phone_Numbers(i).IsPrimary Then
            Citem.SetPrimaryPhoneNumber(Cpno)
            Else
            Log(Cpno)
            Citem.AddPhoneNumber(Cpno)
        End If
      
    Next


B4X:
Cames Brown
[DisplayName=Cames Brown, FamilyName=James FullNameStyle=western
, GivenName=James, IsInitialized=true, MiddleName=
, PhoneticFamilyName=, PhoneticGivenName=, PhoneticMiddleName=
, Prefix=, Suffix=]
[contactmimetypes=null, contactstatics=null, internalisprimary=false
, internallabel=, internalnumber=+901233812222, internaltyp=mobile
]
[contactmimetypes=null, contactstatics=null, internalisprimary=false
, internallabel=, internalnumber=+90123312327, internaltyp=mobile
]
Error getting raw_contact_id
[contactmimetypes=null, contactstatics=null, internalisprimary=false
, internallabel=, internalnumber=905-123-512347, internaltyp=mobile
]
Error getting raw_contact_id
[contactmimetypes=null, contactstatics=null, internalisprimary=false
, internallabel=, internalnumber=+901239512347, internaltyp=mobile
]
Error getting raw_contact_id
 

Serjoscha Bald

Member
Licensed User
Longtime User
Hi Tayfur,

can you tell me on which device you test your projects?

I test my projects on a real, not virtual, samsung s5 device. I have added some lines to my test project like this:
B4X:
Dim cItem As ContactItem
cItem = cb.AddContact("Check Checker")
Dim cPhoneNum As ContactPhoneNumber
For index = 0 To 3
   cPhoneNum.Initialize
   cPhoneNum.Typ = "mobile"
   cPhoneNum.Number = "+4955334455" & index
   cItem.AddPhoneNumber(cPhoneNum)
Next

And there wasn't any problem. I looked thru the forum and found that some other developers have problems with the standard SetData method of ContactUtils too, where my contact library based on. So i have changed some lines in my SetData method to get the raw_contact_id on another way. Please test the library ContactsLib1.02a and tell me here if your problem has gone.


I found another problem in your code:
B4X:
If Kayit.Phone_Numbers(i).IsPrimary Then
   Citem.SetPrimaryPhoneNumber(Cpno) '<!---- You can't set a phone number as primary before it was added to the contact
Else
   Log(Cpno)
   Citem.AddPhoneNumber(Cpno)
End If

Try it like this:
B4X:
Log(Cpno)
Citem.AddPhoneNumber(Cpno)
If Kayit.Phone_Numbers(i).IsPrimary Then
   Citem.SetPrimaryPhoneNumber(Cpno)
End If
 

Attachments

  • ContactsLib1.02a.zip
    71.1 KB · Views: 211

Tayfur

Well-Known Member
Licensed User
Longtime User
Hello @Serjoscha Bald ;

Thank you for helps.

I have 2 issuse;

1- I read an log from your lib."Error getting raw_contact_id".
I think; your methods has maybe return values for cheking.
for example:
B4X:
dim rtn as boolen
rtn =Citem.AddPhoneNumber(Cpno)
if rtn=false then .......
'////////////// or---------------
dim rtn as string
rtn =Citem.AddPhoneNumber(Cpno)
if rtn<>"" then log("Error is:" & rtn)

2- My contact list get backup. After I want restore it. But, Your lib cant add a new records. It change old records. I think, lib dont make a new record / or it delete old records and add a new record. (Therefore some my contacts lost :( )
How is fix it? this is big problem.
 

Tayfur

Well-Known Member
Licensed User
Longtime User
I read an error in log.

Error getting raw_contact_id

B4X:
Dim Citem As ContactItem

    Citem=cbook.AddContact (".Demo."&Kayit.DisplayName)
    Log(".demo."&Kayit.DisplayName)
  
    Log("---start phone records---------")
    '---------------------PhoneNumber s records ---------------------------
    Dim Cpno As ContactPhoneNumber
    For i=0 To Kayit.Phone_Numbers.Length-1
        Cpno.Initialize
        Cpno.Number=Kayit.Phone_Numbers(i).Number
        If  Kayit.Phone_Numbers(i).Typ.EqualsIgnoreCase("custom") Then
            Cpno.Label=Kayit.Phone_Numbers(i).Label
            Cpno.Typ=Kayit.Phone_Numbers(i).Typ
        Else
            Cpno.Typ=Kayit.Phone_Numbers(i).Typ
        End If
        LogColor("1>"&Cpno,Colors.Blue)
        LogColor("2>"&Citem,Colors.Red)
        Citem.AddPhoneNumber(Cpno)
        Log("Number recorded")
        If Kayit.Phone_Numbers(i).IsPrimary Then
            Citem.SetPrimaryPhoneNumber(Cpno)
        End If
      
    Next
    Log("8------end no record-------------")


B4X:
.demo.Mahir Egemen Kaderli

---start phone records---------

1> [contactmimetypes=null, contactstatics=null, internalisprimary=false
, internallabel=, internalnumber=+905322123401, internaltyp=mobile]
2>[contactmimetypes=null, contactstatics=null, internalcontacthandler=[contactmimetypes=null, contactstatics=null, internalcontactsuri=(StringUri) content://com.android.contacts/contacts
, internalcontentresolver=anywheresoftware.b4a.objects.ContentResolverWrapper@38f23576, internaldatauri=(StringUri) content://com.android.contacts/data, internaldisplayphotodimensions=720
, internalrawcontactsuri=(StringUri) content://com.android.contacts/raw_contacts, internalthumbnailphotodimensions=96]
, internalid=742]
Number recorded

1 >[contactmimetypes=null, contactstatics=null, internalisprimary=false
, internallabel=, internalnumber=053-014-12341, internaltyp=mobile]
2>[contactmimetypes=null, contactstatics=null, internalcontacthandler=[contactmimetypes=null, contactstatics=null, internalcontactsuri=(StringUri) content://com.android.contacts/contacts
, internalcontentresolver=anywheresoftware.b4a.objects.ContentResolverWrapper@38f23576, internaldatauri=(StringUri) content://com.android.contacts/data, internaldisplayphotodimensions=720
, internalrawcontactsuri=(StringUri) content://com.android.contacts/raw_contacts, internalthumbnailphotodimensions=96]
, internalid=742]
Error getting raw_contact_id
Number recorded

1>[contactmimetypes=null, contactstatics=null, internalisprimary=false
, internallabel=, internalnumber=0530112345, internaltyp=mobile]
2>[contactmimetypes=null, contactstatics=null, internalcontacthandler=[contactmimetypes=null, contactstatics=null, internalcontactsuri=(StringUri) content://com.android.contacts/contacts
, internalcontentresolver=anywheresoftware.b4a.objects.ContentResolverWrapper@38f23576, internaldatauri=(StringUri) content://com.android.contacts/data, internaldisplayphotodimensions=720
, internalrawcontactsuri=(StringUri) content://com.android.contacts/raw_contacts, internalthumbnailphotodimensions=96]
, internalid=742]
Error getting raw_contact_id
Number recorded

8------end no record-------------
10
1
2
3
4
5
7
9
11
12
resim
13
14
15*** finiiş
 

Serjoscha Bald

Member
Licensed User
Longtime User
Hi Tayfur,

i'm sorry that the contacts are get lost or damaged. Please backup your contacts next time before play around with it. The most standard contact apps have a export function in the menu. Select export to sdcard, and i think all your contacts will be backuped into a vcf file.

I didn't understand why it works on my Samsung Galaxy S5 device correctly and not on your ??? device. The contacts lib setdata makes the same then Erel's contactutils class setdata. The documentation of android tells also that this is the correct way to add the data. My device has android version 5.0 installed. When i run this code:
B4X:
Dim cItem As ContactItem
cItem = cb.AddContact("Check Checker")
Dim cPhoneNum As ContactPhoneNumber
For index = 0 To 3
cPhoneNum.Initialize
cPhoneNum.Typ = "mobile"
cPhoneNum.Number = "+4955334455" & index
cItem.AddPhoneNumber(cPhoneNum)
Next
on it 10 times, i get 10 entries in my real contact book of "Check Checker".

Which device and android version you use to test?

When you look into your real contact app, was there the added contact with the first number that was added ? With the right type "mobile"?

If not it can be that, i think, the contact was removed on adding the first phone number. So the raw id can't be found any more. Test it. Add a contact that wasn't there before with your app. Look in the contacts app if it is there. Then add with your app one phone number to it and look again into the contacts app. Was the contact removed or was there the correct phone number and type. If yes look into your app and get all contacts. Has the contact id changed ?

Please test if adding a address or something else has the same problem. Add two or more of it.

One question: On your first post, you had problems with adding thumbnail. But all other data was added correctly ?

I will add a Boolean return value to the adding methods next time, so you can check if something goes wrong.
 
Last edited:

Tayfur

Well-Known Member
Licensed User
Longtime User
Hi Tayfur,

i'm sorry that the contacts are get lost or damaged. Please backup your contacts next time before play around with it. The most standard contact apps have a export function in the menu. Select export to sdcard, and i think all your contacts will be backuped into a vcf file.

This is not important lost data is my foult. I must thinking this posibility.

Hi Tayfur,



I didn't understand why it works on my Samsung Galaxy S5 device correctly and not on your ??? device. The contacts lib setdata makes the same then Erel's contactutils class setdata. The documentation of android tells also that this is the correct way to add the data. My device has android version 5.0 installed. When i run this code:
B4X:
Dim cItem As ContactItem
cItem = cb.AddContact("Check Checker")
Dim cPhoneNum As ContactPhoneNumber
For index = 0 To 3
cPhoneNum.Initialize
cPhoneNum.Typ = "mobile"
cPhoneNum.Number = "+4955334455" & index
cItem.AddPhoneNumber(cPhoneNum)
Next
on it 10 times, i get 10 entries in my real contact book of "Check Checker".
Which device and android version you use to test?

I used Samsung S2 with cyanogenmod cm12 / android 5.1.1.

When you look into your real contact app, was there the added contact with the first number that was added ? With the right type "mobile"?

If not it can be that, i think, the contact was removed on adding the first phone number. So the raw id can't be found any more. Test it. Add a contact that wasn't there before with your app. Look in the contacts app if it is there. Then add with your app one phone number to it and look again into the contacts app. Was the contact removed or was there the correct phone number and type. If yes look into your app and get all contacts. Has the contact id changed ?

Please test if adding a address or something else has the same problem. Add two or more of it.

One question: On your first post, you had problems with adding thumbnail. But all other data was added correctly ?

I will add a Boolean return value to the adding methods next time, so you can check if something goes wrong.

I think your code is correct, so I read SOMETIMES error "Error raw id" for same resotore data. I mixed my brain, Because SOMETIMES read error in log.
I willmake a short my project. And I will send to you when i recive error. (if ı cant solve.)

Good news for "I will add a Boolean return value to the adding methods next time".
and can your methods (add/remove) use with Contact ID parameter. (first We read contact ID for firt record or old records. After all method can use with ID)
its for next version.

Today I will work this issue and check again all values. Mybe I can solve.




Thanks for helps.
 
Last edited:

Serjoscha Bald

Member
Licensed User
Longtime User
Hi Tayfur,

i have tested your project. But i can't, there are missing files. So i changed the btn1_click method to run your fake_record method. I run the project on a virtual machine with Android 4.4.2 and this was my log:

** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
(MyMap) {0=custom, 1=home, 2=mobile, 3=work, 4=fax_work, 5=fax_home, 6=pager, 7=other, 8=callback, 9=car, 10=company_main, 11=isdn, 12=main, 13=other_fax, 14=radio, 15=telex, 16=tty_tdd, 17=work_mobile, 18=work_pager, 19=assistant, 20=mms}(MyMap) {0=custom, 1=home, 2=work, 3=other}
content://com.android.contacts/contacts
content://com.android.contacts/data
(MyMap) {0=custom, 1=home, 2=work, 3=other, 4=mobile}
(MyMap) {0=custom, 1=anniversary, 2=other, 3=birthday}
(MyMap) {0=undefined, 1=western, 2=cjk, 3=chinese, 4=japanese, 5=korean}
content://com.android.contacts/groups
(MyMap) {0=custom, 1=home, 2=work, 3=other}
(MyMap) {-1=custom, 0=aim, 1=msn, 2=yahoo, 3=skype, 4=qq, 5=google_talk, 6=icq, 7=jabber, 8=netmeeting}
(MyMap) {0=custom, 1=default, 2=other_name, 3=maiden_name, 4=short_name, 5=initials}
(MyMap) {0=custom, 1=work, 2=other}
(MyMap) {0=custom, 1=home, 2=mobile, 3=work, 4=fax_work, 5=fax_home, 6=pager, 7=other, 8=callback, 9=car, 10=company_main, 11=isdn, 12=main, 13=other_fax, 14=radio, 15=telex, 16=tty_tdd, 17=work_mobile, 18=work_pager, 19=assistant, 20=mms}
content://com.android.contacts/raw_contacts
(MyMap) {0=custom, 1=assistant, 2=brother, 3=child, 4=domestic_partner, 5=father, 6=friend, 7=manager, 8=mother, 9=parent, 10=partner, 11=referred_by, 12=relative, 13=sister, 14=spouse}
(MyMap) {0=custom, 1=home, 2=work, 3=other}(MyMap) {0=custom, 1=homepage, 2=blog, 3=profile, 4=home, 5=work, 6=ftp, 7=other}
[contactmimetypes=null, contactstatics=null, internaltyp=mobile
, internallabel=, internalnumber=+49553 34 4550, internalisprimary=false
]
[contactmimetypes=null, contactstatics=null, internaltyp=mobile
, internallabel=, internalnumber=+49553 34 4551, internalisprimary=false
]
[contactmimetypes=null, contactstatics=null, internaltyp=mobile
, internallabel=, internalnumber=+49553 34 4552, internalisprimary=false
]
[contactmimetypes=null, contactstatics=null, internaltyp=mobile
, internallabel=, internalnumber=+49553 34 4553, internalisprimary=false
]
Error occurred on line: 864 (Main)
java.io.FileNotFoundException: /storage/sdcard/Android/data/contact.backup/files/r1.png: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:409)
at java.io.FileInputStream.<init>(FileInputStream.java:78)
at anywheresoftware.b4a.objects.streams.File.OpenInput(File.java:209)
at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize(CanvasWrapper.java:498)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:747)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
at libcore.io.IoBridge.open(IoBridge.java:393)
... 25 more

The exception is normal, there was no image file yet. At this time i can't run the project on my real device, because i'm in family holidays and the hotels wlan doesn't allow a direct connect between devices. Bluetooth is not supported by b4a above version 5.x . I will download and test it on a android 5.1.1 virtual device next time.

Next is i didn't understand what you mean with your wish " and can you (add/remove) use with contact ID ... ", can you please explain it.
 
Top