Android: access and proper handling using contact site id

So, my application is trying to integrate the sync adapter into Android’s native contact manager. All this runs smoothly, except when the contact is synchronized, I can’t update it. Details on this particular problem can be found here: Android: a content recognition request returns 0 lines if it shouldn't But I can summarize simply by simply saying that my content resolver request returns 0 values ​​because I'm requesting the wrong URI , I suppose.

When I write the id of a raw contact to the phone, I do this with the following code:

public ContactSyncOperations(Context context, String username, String accountName, BatchOperationForSync batchOperation) { this(context, batchOperation); mBackReference = mBatchOperation.size(); mIsNewContact = true; mValues.put(RawContacts.SOURCE_ID, username); mValues.put(RawContacts.ACCOUNT_TYPE, "com.tagapp.android"); mValues.put(RawContacts.ACCOUNT_NAME, accountName); mBuilder = newInsertCpo(RawContacts.CONTENT_URI, true).withValues(mValues); mBatchOperation.add(mBuilder.build()); } 

This constructor method is called when a new contact is added to the synchronization list:

 private static void addContact(Context context, String account, Contact contact, BatchOperationForSync batchOperation) { ContactSyncOperations contactOp = ContactSyncOperations.createNewContact(context, contact.getUsername(), account, batchOperation);//constructor called @ this line contactOp.addName(contact.getFirstName(), contact.getLastName()); contactOp.addEmail(contact.getEmail()); contactOp.addPhone(contact.getPhoneNumber(), Phone.TYPE_MOBILE); contactOp.addPhone(contact.getHomePhone(), Phone.TYPE_HOME); contactOp.addPhone(contact.getWorkPhone(), Phone.TYPE_WORK); contactOp.addProfileAction(contact.getUsername()); Log.e("Adding contact", "Via sync"); } 

As you can see in the constructor, I call the newInsertCpo method, which can be viewed here:

 private void addInsertOp() { if(!mIsNewContact) { mValues.put(Phone.RAW_CONTACT_ID, mRawContactId); } mBuilder = newInsertCpo(addCallerIsSyncAdapterParameter(Data.CONTENT_URI), mYield); mBuilder.withValues(mValues); if(mIsNewContact) { mBuilder.withValueBackReference(Data.RAW_CONTACT_ID, mBackReference); } mYield = false; mBatchOperation.add(mBuilder.build()); } 

Now that you see the code, let me explain the problem. When I create and write the identifier of the original contact, I do this with RawContacts.CONTENT_URI:

 mBuilder = newInsertCpo(RawContacts.CONTENT_URI, true).withValues(mValues); 

However, when I request uri, I request like this:

 Cursor cursor = resolver.query(Data.CONTENT_URI, DataQuery.PROJECTION, DataQuery.SELECTION, new String[] {String.valueOf(rawContactId)}, null); 

From Data.CONTENT_URI. I think this is my problem. I used the sample code from the official Android developer site and adapted it for my own purposes, but this part is pretty true for the example. But it does not work. My guide is what I said, I write to one of Uri and ask another. But I tried changing the request to RawContacts.CONTENT_URI (which caused the exception), and also changing the Uri that I write to Data.CONTENT_URI, which also throws the exception. Even more confusing is that I get the raw contact IDs from Data.CONTENT_URI when I use the lookupRawContactId method:

 private static long lookupRawContact(ContentResolver resolver, String username) { Log.e("Looking up Raw Contact", username); long authorId = 0; Cursor cursor = resolver.query(Data.CONTENT_URI, UserIdQuery.PROJECTION, UserIdQuery.SELECTION, new String[] {username}, null); try { if(cursor != null && cursor.moveToFirst()) { authorId = cursor.getLong(UserIdQuery.COLUMN_ID); } } finally { if(cursor != null) { cursor.close(); } } return authorId; } 

So, if I get a cursor returning rawcontactId, why should I get 0 values ​​requesting the same Uri with the same rawcontactId that was returned? That makes no sense! Does anyone have an understanding? Thanks to everyone.

0
source share
1 answer

The lookupRawContactId method should look like this:

 private static long lookupRawContact(ContentResolver resolver, String username) { Log.e("Looking up Raw Contact", username); long authorId = 0; Cursor cursor = resolver.query(RawContacts.CONTENT_URI, UserIdQuery.PROJECTION, UserIdQuery.SELECTION, new String[] {username}, null); try { if(cursor != null && cursor.moveToFirst()) { authorId = cursor.getLong(UserIdQuery.COLUMN_ID); } } finally { if(cursor != null) { cursor.close(); } } return authorId; } 

Note that the query searches RawContacts.CONTENT_URI.

0
source

Source: https://habr.com/ru/post/915598/


All Articles