Loading src/com/android/contacts/ContactSaveService.java +49 −39 Original line number Diff line number Diff line Loading @@ -179,6 +179,7 @@ public class ContactSaveService extends IntentService { private final int MAX_EMAIL_LENGTH = 40; private final int MAX_EN_LENGTH = 14; private final int MAX_CH_LENGTH = 6; private static final int BUFFER_LENGTH = 500; // Only for request accessing SIM card // when device is in the "AirPlane" mode. Loading Loading @@ -1016,21 +1017,31 @@ public class ContactSaveService extends IntentService { if (rawContactsToAdd == null) { return; } for (long rawContactId : rawContactsToAdd) { try { final ArrayList<ContentProviderOperation> rawContactOperations = new ArrayList<ContentProviderOperation>(); // Build an assert operation to ensure the contact is not already in the group final ContentProviderOperation.Builder assertBuilder = ContentProviderOperation .newAssertQuery(Data.CONTENT_URI); assertBuilder.withSelection(Data.RAW_CONTACT_ID + "=? AND " + ArrayList<Long> rawContactIdInDb = Lists.newArrayList(); final Cursor c = resolver.query(Data.CONTENT_URI, new String[] {Data.RAW_CONTACT_ID}, Data.MIMETYPE + "=? AND " + GroupMembership.GROUP_ROW_ID + "=?", new String[] { String.valueOf(rawContactId), GroupMembership.CONTENT_ITEM_TYPE, String.valueOf(groupId)}); assertBuilder.withExpectedCount(0); rawContactOperations.add(assertBuilder.build()); new String[] {GroupMembership.CONTENT_ITEM_TYPE, String.valueOf(groupId)}, Data.RAW_CONTACT_ID); try { while (c != null && c.moveToNext()) { final long id = c.getLong(0); rawContactIdInDb.add(id); } } finally { c.close(); } ArrayList<Long> rawContactIdToAdd = Lists.newArrayList(); for (long rawContactId : rawContactsToAdd) { if (!rawContactIdInDb.contains(rawContactId)) { rawContactIdToAdd.add(rawContactId); } } final ArrayList<ContentProviderOperation> rawContactOperations = new ArrayList<ContentProviderOperation>(); for (long rawContactId : rawContactIdToAdd) { // Build an insert operation to add the contact to the group final ContentProviderOperation.Builder insertBuilder = ContentProviderOperation .newInsert(Data.CONTENT_URI); Loading @@ -1039,26 +1050,25 @@ public class ContactSaveService extends IntentService { insertBuilder.withValue(GroupMembership.GROUP_ROW_ID, groupId); rawContactOperations.add(insertBuilder.build()); if (DEBUG) { for (ContentProviderOperation operation : rawContactOperations) { Log.v(TAG, operation.toString()); int size = rawContactOperations.size(); if (size > 0 && BUFFER_LENGTH - size < 10) { try { resolver.applyBatch(ContactsContract.AUTHORITY, rawContactOperations); } catch (Exception e) { Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); } finally { rawContactOperations.clear(); } } // Apply batch } // There maybe some sim operations left after the while loop if (!rawContactOperations.isEmpty()) { try { resolver.applyBatch(ContactsContract.AUTHORITY, rawContactOperations); } } catch (RemoteException e) { // Something went wrong, bail without success Log.e(TAG, "Problem persisting user edits for raw contact ID " + String.valueOf(rawContactId), e); } catch (OperationApplicationException e) { // The assert could have failed because the contact is already in the group, // just continue to the next contact Log.w(TAG, "Assert failed in adding raw contact ID " + String.valueOf(rawContactId) + ". Already exists in group " + String.valueOf(groupId), e); } catch (Exception e) { Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); } finally { rawContactOperations.clear(); } } } Loading Loading
src/com/android/contacts/ContactSaveService.java +49 −39 Original line number Diff line number Diff line Loading @@ -179,6 +179,7 @@ public class ContactSaveService extends IntentService { private final int MAX_EMAIL_LENGTH = 40; private final int MAX_EN_LENGTH = 14; private final int MAX_CH_LENGTH = 6; private static final int BUFFER_LENGTH = 500; // Only for request accessing SIM card // when device is in the "AirPlane" mode. Loading Loading @@ -1016,21 +1017,31 @@ public class ContactSaveService extends IntentService { if (rawContactsToAdd == null) { return; } for (long rawContactId : rawContactsToAdd) { try { final ArrayList<ContentProviderOperation> rawContactOperations = new ArrayList<ContentProviderOperation>(); // Build an assert operation to ensure the contact is not already in the group final ContentProviderOperation.Builder assertBuilder = ContentProviderOperation .newAssertQuery(Data.CONTENT_URI); assertBuilder.withSelection(Data.RAW_CONTACT_ID + "=? AND " + ArrayList<Long> rawContactIdInDb = Lists.newArrayList(); final Cursor c = resolver.query(Data.CONTENT_URI, new String[] {Data.RAW_CONTACT_ID}, Data.MIMETYPE + "=? AND " + GroupMembership.GROUP_ROW_ID + "=?", new String[] { String.valueOf(rawContactId), GroupMembership.CONTENT_ITEM_TYPE, String.valueOf(groupId)}); assertBuilder.withExpectedCount(0); rawContactOperations.add(assertBuilder.build()); new String[] {GroupMembership.CONTENT_ITEM_TYPE, String.valueOf(groupId)}, Data.RAW_CONTACT_ID); try { while (c != null && c.moveToNext()) { final long id = c.getLong(0); rawContactIdInDb.add(id); } } finally { c.close(); } ArrayList<Long> rawContactIdToAdd = Lists.newArrayList(); for (long rawContactId : rawContactsToAdd) { if (!rawContactIdInDb.contains(rawContactId)) { rawContactIdToAdd.add(rawContactId); } } final ArrayList<ContentProviderOperation> rawContactOperations = new ArrayList<ContentProviderOperation>(); for (long rawContactId : rawContactIdToAdd) { // Build an insert operation to add the contact to the group final ContentProviderOperation.Builder insertBuilder = ContentProviderOperation .newInsert(Data.CONTENT_URI); Loading @@ -1039,26 +1050,25 @@ public class ContactSaveService extends IntentService { insertBuilder.withValue(GroupMembership.GROUP_ROW_ID, groupId); rawContactOperations.add(insertBuilder.build()); if (DEBUG) { for (ContentProviderOperation operation : rawContactOperations) { Log.v(TAG, operation.toString()); int size = rawContactOperations.size(); if (size > 0 && BUFFER_LENGTH - size < 10) { try { resolver.applyBatch(ContactsContract.AUTHORITY, rawContactOperations); } catch (Exception e) { Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); } finally { rawContactOperations.clear(); } } // Apply batch } // There maybe some sim operations left after the while loop if (!rawContactOperations.isEmpty()) { try { resolver.applyBatch(ContactsContract.AUTHORITY, rawContactOperations); } } catch (RemoteException e) { // Something went wrong, bail without success Log.e(TAG, "Problem persisting user edits for raw contact ID " + String.valueOf(rawContactId), e); } catch (OperationApplicationException e) { // The assert could have failed because the contact is already in the group, // just continue to the next contact Log.w(TAG, "Assert failed in adding raw contact ID " + String.valueOf(rawContactId) + ". Already exists in group " + String.valueOf(groupId), e); } catch (Exception e) { Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); } finally { rawContactOperations.clear(); } } } Loading