Android: Adding ringtone to contact doesn't work on a contact I just added, but works on a contact I added on previous sync
Asked Answered
C

1

4

So I am doing Account Sync, and included in that process is a step where a custom ringtone is added. Here is my method for adding a ringtone:

private static void ringtoneSync(ContentResolver resolver, String username, Context context) {
    ContentValues values = new ContentValues();
    Log.e("SYNC", "setting ringtone for " + username);

    long rawContactId = lookupRawContact(resolver, username);
    long contactId = getContactId(resolver, rawContactId);

    File root = Environment.getExternalStorageDirectory();
    TagDBAdapter adapter = new TagDBAdapter(context);
    adapter.open();
    String ringtone = adapter.getContactRingtonePath(username);
    adapter.close();

    Log.e("test", "ringtone checkpoint name here: " + ringtone);

    File file = new File(root, "tag/ringtones/"+ ringtone + ".mp3");
    if(file.exists()) {

        Log.e("test", "ringtone checkpoint if file exists");

        Uri oldUri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());
        resolver.delete(oldUri, MediaStore.MediaColumns.DATA + "=\"" + file.getAbsolutePath() + "\"", null);

        values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
        values.put(MediaStore.MediaColumns.TITLE, ringtone);
        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
        values.put(MediaStore.Audio.Media.IS_RINGTONE, true);

        Uri uri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());
        Uri newUri = resolver.insert(uri, values);
        String uriString = newUri.toString();
        values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, uriString);
        Log.e("Uri String for " + username, uriString);
        resolver.update(ContactsContract.Contacts.CONTENT_URI, values, Contacts._ID + "=" + contactId, null);
    }
}

This method works great, except when I am adding a contact to the account for the first time beforehand. My call to adding contacts is structured like this:

    for(Contact contact : friends) {
        Log.e("SYNCING CONTACTS", "Start for loop");
        username = contact.getUsername();
        rawContactId = lookupRawContact(resolver, username);
        if(rawContactId != 0) {
            if(!contact.isDeleted()) {
                Log.e("SYNCING CONTACTS", "Updating " + username);
                updateContact(context, resolver, account, contact, rawContactId, batchOperation);
                ringtoneSync(resolver, username, context);

            }
            else {
                Log.e("SYNCING CONTACTS", "Deleting " + username);
                deleteContact(context, rawContactId, batchOperation);
            }
        }
        else {
            if(!contact.isDeleted()) {
                Log.e("SYNCING CONTACTS", "Adding " + username);
                addContact(context, account, contact, batchOperation);
                ringtoneSync(resolver, username, context);
            }
        }

So as you can see it is called very similarly regardless if it is a new or existing contact, but it only actually works for an existing contact. What's more, all those log lines I entered in as checkpoints are displayed accurately in logcat even when the ringtone is not successfully added.

I can't figure out for the life of me what is going on here, any thoughts?

Coinsurance answered 21/12, 2011 at 16:55 Comment(0)
C
2

Found an answer to my question. I should ask SO questions sooner, it seems as soon as I ask it the answer comes to me, even if I was working on the issue for days.

Anyways, here is what is going on: the ringtoneSync method is looking for a rawContactId, which is created when you do the addContact() method. Problem is, the rawContactId is not committed until you call batchOperation.execute().

So by changing my contact adding loop from this:

        if(rawContactId != 0) {
            if(!contact.isDeleted()) {
                Log.e("SYNCING CONTACTS", "Updating " + username);
                updateContact(context, resolver, account, contact, rawContactId, batchOperation);
                ringtoneSync(resolver, username, context);

            }
            else {
                Log.e("SYNCING CONTACTS", "Deleting " + username);
                deleteContact(context, rawContactId, batchOperation);
            }
        }
        else {
            if(!contact.isDeleted()) {
                Log.e("SYNCING CONTACTS", "Adding " + username);
                addContact(context, account, contact, batchOperation);
                ringtoneSync(resolver, username, context);
            }
        }

To this:

        if(rawContactId != 0) {
            if(!contact.isDeleted()) {
                Log.e("SYNCING CONTACTS", "Updating " + username);
                updateContact(context, resolver, account, contact, rawContactId, batchOperation);
                ringtoneSync(resolver, username, context);

            }
            else {
                Log.e("SYNCING CONTACTS", "Deleting " + username);
                deleteContact(context, rawContactId, batchOperation);
            }
        }
        else {
            if(!contact.isDeleted()) {
                Log.e("SYNCING CONTACTS", "Adding " + username);
                addContact(context, account, contact, batchOperation);
/* -------> */  batchOperation.execute(); //EXECUTE BATCH OPERATION BEFORE SYNCING RINGTONE
                ringtoneSync(resolver, username, context);
            }
        }

The process works fine.

Hope this can help someone else out in the future.

Coinsurance answered 21/12, 2011 at 16:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.