Search code examples
androidsqliteormcontactsaccountmanager

Android contact manipulation


I'm developing an IM app on Android. The aim is to have multi accounts and protocols compatibility. I'm looking for a way to get the best status of a contact on Android. I've got a list of account types supported. I have a function that takes a long (contact_id) and must return the best Account + best way to contact (e.g. [email protected]).

I already looked the documentation, and I know SQL already. But the documentation is a little messy on that point and I don't know how to get the best way to send a message to a contact. Here is some code of what I'm trying to do:

public static Account best(Context context, long contact)
{
    final ContentResolver resolver = context.getContentResolver();
    final Cursor rawContacts = resolver.query(RawContacts.CONTENT_URI,
              new String[]{RawContacts._ID},
              RawContacts.CONTACT_ID + "=? AND " + registeredProtocolsSelection(),
              new String[]{String.valueOf(contact)}, null);
    while(rawContacts.moveToNext())
    {
        final long rawContactId = rawContacts.getLong(0);
        Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
        Uri entityUri = Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);
        Cursor rawContact = resolver.query(entityUri,
                  new String[]{RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1},
                  null, null, null);
    }
    return null;
}

I can select all RawContacts that matter for my app but then I don't know how to join and I can't get a clear schema of the database. I don't know how to access StatusUpdate from there. The aim is like ORDER BY presence.

Is there an easy way to do that? Is there a way to manipulate Contacts like normal Object via something like an ORM? Or do we have a mechanism to create and manage our own IM protocols easily on Android?


Solution

  • With time I found it. We can use the last parameter of ContentProvider.query(..., sort); to have the best RawContact Presence first for a given Contact. For those who still search here is the code

    final Cursor cursor = contentResolver.query
    (
        StatusUpdates.CONTENT_URI, 
        new String[]
        {
            StatusUpdates.PRESENCE,
            StatusUpdates.STATUS_LABEL,
            StatusUpdates.STATUS_RES_PACKAGE
        }, 
        Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "=?", 
        new String[]
        { 
            Long.toString(id), 
            CommonDataKinds.Im.CONTENT_ITEM_TYPE 
        },
        StatusUpdates.STATUS_TIMESTAMP + " DESC" // For last Or
        StatusUpdates.PRESENCE + " DESC" // For presence
    );
    if(cursor!=null && cursor.moveToFirst())
    {
        presence = Presence.get(cursor.getLong(0));
        status = Utils.Contact.status(cursor.getInt(1), cursor.getString(2));
    }
    if(cursor!=null)
    {
        cursor.close();
    }
    

    You can also use the predifined field of the contact table with automatic best status for a given contact.