luajluajava

How to get "android.provider.ContactsContract.Contacts" field in Luaj


I use Lua Interpreter to get information for my iGO navigator and I need to get some fields from android.provider.ContactsContract.Contacts

I got successfully

cntr = luajava.bindClass("android.provider.ContactsContract")

and I try to get Contacts

cntct = cntr.Contacts

or

cntct = luajava.bindClass("android.provider.ContactsContract.Contacts")

No success

Basically I need to get result as there

Intent pickIntent = new Intent(Intent.ACTION_PICK, 
android.provider.ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(pickIntent, PICK_RESULT);

Solution

  • This is an answer to myself. Ofcourse you need to have permission in the manifest.

        cr = activity:getContentResolver()
        contacts = luajava.bindClass("android.provider.ContactsContract")
        build = luajava.bindClass("android.os.Build")
    
        D_Name = build.VERSION.SDK_INT >= build.VERSION_CODES.HONEYCOMB and contacts.Contacts.DISPLAY_NAME_PRIMARY or contacts.Contacts.DISPLAY_NAME
        --str = luajava.bindClass("java.lang.String")
    
        cur = cr:query(contacts.Contacts.CONTENT_URI)
        local count = cur and cur:getCount() or -1
        print(count)
        if cur and count > 0 then
            --local name, id, pCur, phoneNo
            while cur:moveToNext() do
                id = cur:getString(cur:getColumnIndex(contacts.Contacts._ID))
                name = cur:getString(cur:getColumnIndex(D_Name))
                --print("id: " .. id, "name: " .. name)
    
                if cur:getInt(cur:getColumnIndex(contacts.Contacts.HAS_PHONE_NUMBER)) > 0 then
                    --str_str = luajava.newInstance( "java.lang.String", id )
                 -- str_array = luajava.newInstance( "java.lang.reflect.Array", ({str_str}) )
                    pCur = cr:query(contacts.CommonDataKinds.Phone.CONTENT_URI, nil, contacts.CommonDataKinds.Phone.CONTACT_ID .. " = ?", ({id}), nil)
           print(pCur:getCount())
                    while pCur:moveToNext() do
                        phoneNo = pCur:getString(pCur:getColumnIndex(contacts.CommonDataKinds.Phone.NUMBER))
                        print("Name: " .. name, "Phone Number: " .. phoneNo)
                    end
                    pCur:close()
                end
    
                --if cur:getInt(cur:getColumnIndex(contacts.Contacts.HAS_EMAIL_DATA)) > 0 then
                    --str_str = luajava.newInstance( "java.lang.String", id )
                 -- str_array = luajava.newInstance( "java.lang.reflect.Array", ({str_str}) )
                pCur = cr:query(contacts.CommonDataKinds.Email.CONTENT_URI, nil, contacts.CommonDataKinds.Email.CONTACT_ID .. " = ?", ({id}), nil)
           print(pCur:getCount())
                if pCur then
                    while pCur:moveToNext() do
                        Email_data = pCur:getString(pCur:getColumnIndex(contacts.CommonDataKinds.Email.DATA))
                        print("Name: " .. name, "Email_data: " .. Email_data)
                    end
                end
                pCur:close()
    
                pCur = cr:query(contacts.Data.CONTENT_URI, nil, contacts.Data.CONTACT_ID .. " = " .. id)
           print(pCur:getCount())
                if pCur then
                    while pCur:moveToNext() do
                        skype_type = pCur:getInt(pCur:getColumnIndex(contacts.CommonDataKinds.Im.PROTOCOL))
            print(skype_type, contacts.CommonDataKinds.Im.PROTOCOL_SKYPE)
                        if contacts.CommonDataKinds.Im.PROTOCOL_SKYPE == skype_type then
                            imName = pCur:getString(pCur:getColumnIndex(contacts.CommonDataKinds.Im.DATA))
                            print("Name: " .. name, "skype_type: " .. skype_type .. "imName: " .. imName)
                        end
                    end
                end
                pCur:close()
                --end
    
            end
        end
        if cur then
            cur:close()
        end