Android Question Add Firstname, LastName in ContactsUtils and add sort Column in FindAllContacts

Alain75

Member
I used ContactsUtils 1.20 to explore my contacts but I was surprised not to found FirstName and LastName. I had difficulties to found references about it except an EREL's post (of course :)) on android constants for Contacts

Finally I add the 2 attributes in the type definition of cuContact :
New fields:
Sub Class_Globals
    Type cuContact (Id As Long, DisplayName As String, FirstName As String, LastName As String)
    Type cuEmail (Email As String, EmailType As String)
    Type cuPhone (Number As String, PhoneType As String)
    ...

and modify the function which retrieves data :
FindContactsIdFromData:
Private Sub FindContactsIdFromData (Mime As String, DataColumn As String, Value As String, Operator As String, _
    Exact As Boolean, VisibleOnly As Boolean, SortColumn As String) As List
    If Not(Exact) Then
        Operator = "LIKE"
        Value = "%" & Value & "%"
    End If
    Dim selection As String = "mimetype = ? AND " & DataColumn & " " & Operator & " ? "
    If VisibleOnly Then selection = selection & " AND in_visible_group = 1"
    'Dim crsr As Cursor = cr.Query(dataUri, Array As String("contact_id", "display_name"), selection, _
    Dim crsr As Cursor = cr.Query(dataUri, Array As String("contact_id", "display_name","data2","data3"), selection, _
        Array As String(Mime, Value), SortColumn)
    Dim res As List
    res.Initialize
    Dim m As Map
    m.Initialize
    For i = 0 To crsr.RowCount - 1
        crsr.Position = i
        Dim cu As cuContact
        cu.Initialize
        cu.Id = crsr.GetLong("contact_id")
        cu.DisplayName    = crsr.GetString("display_name")
        cu.FirstName    = IIf(crsr.GetString("data2")<>Null,crsr.GetString("data2"),"")
        cu.LastName        = IIf(crsr.GetString("data3")<>Null,crsr.GetString("data3"),"")
        ...

And export the sort option in the public method FindAllContacts, other public ones not sorting :
Public methods:
...
'Returns the starred contacts.
Public Sub FindContactsByStarred(Starred As Boolean) As List
    Dim value As String
    If Starred Then value = "1" Else value = "0"
    Return FindContactsIdFromData("vnd.android.cursor.item/name", "starred", value,"=", True, True,"")
End Sub
'Returns all contacts.
Public Sub FindAllContacts(VisibleOnly As Boolean, Sort As String) As List
    Return FindContactsIdFromData("vnd.android.cursor.item/name", "data1", "null", "<>", True, VisibleOnly,Sort)
End Sub
'Returns all contacts with a photo.
Public Sub FindContactsWithPhotos As List
    Return FindContactsIdFromData("vnd.android.cursor.item/photo", "data15", "null", "<>", True, False,"")
End Sub
...

I hope it may help others developers
 

Attachments

  • ContactsUtils.zip
    3.5 KB · Views: 250

Alain75

Member
Hi @walt61 ,
thank you for your reply. I didn't have time for the moment to look at your job because I continued to enhance contactsutils. Moreover, I understood why the names of fields data1, data2, data3 are so weird. With these names, we can use the same cursor and the same code with the same fields to read different views. Personaly, I am not sure it's a good choice...

I added a new method, GetNames, to get DisplayName, FirstName and LastName with an ID in the same way as GetNote, GetPhones, ... And last but not least, I changed the private sub FindContactsIdFromData in order for public methods FindContactsByMail, FindContactsByNotes, FindContactsByPhone and FindContactsWithPhotos to return a list of cuEmail, cuNote, cuPhone and cuPhoto instead of cuContact :
FindContactsIdFromData:
Private Sub FindContactsIdFromData (Mime As String, DataColumn As String, Value As String, Operator As String, _
    Exact As Boolean, VisibleOnly As Boolean, SortColumn As String) As List
    If Not(Exact) Then
        Operator = "LIKE"
        Value = "%" & Value & "%"
    End If
    Dim selection As String = "mimetype = ? AND " & DataColumn & " " & Operator & " ? "
    If VisibleOnly Then selection = selection & " AND in_visible_group = 1"
    Dim crsr As Cursor = cr.Query(dataUri, Array As String("contact_id", "data1","data2","data3"), selection, _
        Array As String("vnd.android.cursor.item/"&Mime, Value), SortColumn)
    Dim res As List
    res.Initialize
    Dim m As Map
    m.Initialize
    For i = 0 To crsr.RowCount - 1
        crsr.Position = i
        Dim Id As Long = crsr.GetLong("contact_id")
        If m.ContainsKey(Id) Then Continue    ' It returns first mail or phone for a contact
        m.Put(Id, Null)
        Select Mime
            Case "email_v2"
                Dim oe As cuEmail
                oe.Initialize
                oe.Id             = Id
                oe.Email        = crsr.GetString2(1)
                oe.EmailType    = mailTypes.Get(crsr.GetString2(2))
                res.Add(oe)
            Case "note"
                Dim on As cuNote
                on.Initialize
                on.Id            = Id
                on.Note            = crsr.GetString2(1)
                res.Add(on)
            Case "phone_v2"
                Dim op As cuPhone
                op.Initialize
                op.Id            = Id
                op.Number        = crsr.GetString2(1)
                op.PhoneType    = phoneTypes.Get(crsr.GetString2(2))
                res.Add(op)
            Case "photo"
                Dim oo As cuPhoto
                oo.Initialize
                oo.Id            = Id
                oo.Photo        = crsr.GetString2(1)
                res.Add(on)
            Case Else
                Dim o As cuContact
                o.Initialize
                o.Id             = Id
                o.DisplayName    = crsr.GetString2(1)
                o.FirstName        = IIf(crsr.GetString2(2)<>Null,crsr.GetString2(2),"")
                o.LastName        = IIf(crsr.GetString2(3)<>Null,crsr.GetString2(3),"")
                res.Add(o)
        End Select
    Next
    crsr.Close
    Return res
End Sub

Of course, I changed the public methods associated :
Public methods:
Public Sub FindContactsByName(Name As String, Exact As Boolean, VisibleOnly As Boolean) As List
    Return FindContactsIdFromData("name", "data1", Name, "=", Exact, VisibleOnly,"")
End Sub
Public Sub FindContactsByMail(Mail As String, Exact As Boolean, VisibleOnly As Boolean) As List
    Return FindContactsIdFromData("email_v2", "data1", Mail, "=", Exact, VisibleOnly,"")
End Sub
Public Sub FindContactsByNotes(Note As String, Exact As Boolean, VisibleOnly As Boolean) As List
    Return FindContactsIdFromData("note", "data1", Note, "=", Exact, VisibleOnly,"")
End Sub
Public Sub FindContactsByPhone(PhoneNumber As String, Exact As Boolean, VisibleOnly As Boolean) As List
    Return FindContactsIdFromData("phone_v2", "data1", PhoneNumber, "=", Exact, VisibleOnly,"")
End Sub
Public Sub FindContactsByStarred(Starred As Boolean) As List
    Dim value As String = IIf(Starred,"1","0")
    Return FindContactsIdFromData("name", "starred", value,"=", True, True,"")
End Sub
Public Sub FindAllContacts(VisibleOnly As Boolean, Sort As String) As List
    Return FindContactsIdFromData("name", "data1", "null", "<>", True, VisibleOnly,Sort)
End Sub
Public Sub FindContactsWithPhotos As List
    Return FindContactsIdFromData("photo", "data15", "null", "<>", True, False,"")
End Sub

Hope it helps others :)
 

Attachments

  • ContactsUtils2.bas
    15.1 KB · Views: 219
Upvote 0

Alain75

Member
I have integrated some of the functions added by @walt61 and did modifications or enhancements :
  1. Integrate addressTypes and cuAddress and add structured fields : street, neighborhood, postcode and city
  2. Add GetAdresses function
  3. Integrate FindContactsByGroupRowId
  4. Integrate FindContactsByGroupName but the group name may now be part of a group name
  5. Integrate organization and events
  6. Add setName (for first or last name) and setNames (for all)
  7. Add setPhone to modify an old number in order to avoid deleting and adding number
  8. Add DeletePhoneByType, DeleteAddressByType and DeleteEmailByType
  9. Rename insertcontact with createcontact and :
    1. Suppress unusefull creation of dummy phone number
    2. Add accountName and accountType in order to be able to create Google contact (set accountName with your account and accountType with "com.google"). But default will still create contact in the smartphone address book
  10. Group functions by ADD, SET or DELETE purposes
 

Attachments

  • ContactsUtils3.bas
    25.9 KB · Views: 182
Upvote 0

Alain75

Member
In new attachment a few fixes and enhancements for ContactUtils integrating FirstName and LastName :
  1. Suppress Id from cuEmail, cuPhone (Id is useless because the call of the function needs it)
  2. Rename parameter Sort with SortColumn in «FindContacts» functions
  3. Modify FindContactsByGroupName : if parameter group is 1 car length, search first group starting with it else search first group containing it
  4. FindContactsByName, FindContactsByMail, FindContactsByNotes, FindContactsByPhone, FindContactsByStarred, FindAllContacts and FindContactsWithPhotos return cuContact (with FirstName and LastName)
  5. Suppress GetAccount2 which seems not working
  6. Fix GetStarred when no row is returned by query function
  7. Add function GetOwner returning google admin email (code from @Erel)
  8. Add function DeleteNote
 

Attachments

  • ContactsUtils4.bas
    25.7 KB · Views: 201
Upvote 0
Top