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?