The naive approach is to iterate over all the records, copy them one by one and delete all old things ...
I am really afraid that this method may fail with real-world users.
private void naiveRename(ContentResolver resolver) { ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>(); Cursor cur = resolver.query(RawContacts.CONTENT_URI, null, RawContacts.ACCOUNT_NAME + "='" + "OLD NAME" + "'", null, null); if (cur != null) { // copy all data while (cur.moveToNext()) { Uri curUri = RawContacts.CONTENT_URI.buildUpon() .appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true") .build(); ContentProviderOperation.Builder builder = ContentProviderOperation .newInsert(curUri); for (int i = 0; i < cur.getColumnCount(); i++) { String colName = cur.getColumnName(i); if (RawContacts._ID.equals(colName) || RawContacts.VERSION.equals(colName) || RawContacts.CONTACT_ID.equals(colName)) { // Skip - read only } else if (RawContacts.ACCOUNT_NAME.equals(colName)) { builder.withValue(RawContacts.ACCOUNT_NAME, "NEW NAME"); } else { builder.withValue(colName, cur.getString(i)); } } operationList.add(builder.build()); } // delete all old data ContentProviderOperation.Builder builder = ContentProviderOperation .newDelete(RawContacts.CONTENT_URI); builder.withSelection(RawContacts.ACCOUNT_NAME + "='" + "OLD NAME" + "'", null); try { resolver.applyBatch(ContactsContract.AUTHORITY, operationList); } catch (RemoteException e) { // PANIC! } catch (OperationApplicationException e) { // OMG! WHAT TO DO?! } } else { // LORDI! } }
source share