1

I'm getting a weird behavior when testing my app on Huawei P8. Everything works nice on emulators (android version 4, 5, 6, 7) and on ASUS (android 6) and Samsung Galaxy S2 (Android 4.2).

The following steps work both on physical device and emulators. After calling contentResolver.query() I get a Cursor with all the contacts of my device.

  1. I open my app
  2. I call contentResolver.query(ContactsContract.Contacts.CONTENT_URI...);

The following steps work everywhere except on my Huawei P8; with my Huawei P8, and only with it, I get a 0 count Cursor

  1. I open my app
  2. From my app, I open an Intent to add a new contact on the device
  3. I add a new contact or I close the activity without adding contact
  4. I call contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

This is how I get the contacts on the device:

ContentResolver contentResolver = getBaseContext().getContentResolver();    
Cursor cursor =  contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

This is how I open, from my app, the activity to add a new contact on the device

Intent intent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
intent.putExtra("finishActivityOnSaveCompleted", true);
startActivityForResult(intent, 101);

It's seems that after calling new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI) the contact table is "locked".

Do I have to "close" the Intent someway to be able to query ContactsContract.Contacts.CONTENT_URI?

IMPORTANT: if, for example, instead of Intent.ACTION_INSERT I call Intent.ACTION_CALL I don't get any problem and I'm able to query successfully ContactsContract.Contacts.CONTENT_URI. So, the problem is "calling" Intent.ACTION_INSERT and after query ContactsContract.Contacts.CONTENT_URI on Huawei P8.

Thank you in adavance.

MDP
  • 4,177
  • 21
  • 63
  • 119
  • are you able to constantly recreate this behavior? sounds very strange... what if you delay the query for a couple of seconds after getting back from the insert intent? – marmor Jul 24 '17 at 08:19
  • Yes, I'm able to constantly recreate this behavior! I also tried to wait almost a minute before make a query on ContactsContract.Contacts.CONTENT_URI, but I keep on getting a 0 count Cursor – MDP Jul 24 '17 at 08:26
  • test on some other device, maybe it's a bug in your phone – marmor Jul 24 '17 at 08:34
  • I tried on an Asus, and it works. It's a bug of Huawei P8!!! What the hell! – MDP Jul 24 '17 at 08:45
  • try `intent.setType(Contacts.CONTENT_TYPE);` – Ashutosh Sagar Jul 24 '17 at 09:55
  • @Ashutosh Sagar I already tried it man. It seems a Huawei P8 bug :( – MDP Jul 24 '17 at 10:12
  • You can add contacts programmatically by `ContentValues values = new ContentValues(); values.put(Data.RAW_CONTACT_ID, 001); values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); values.put(Phone.NUMBER, "1-800-GOOG-411"); values.put(Phone.TYPE, Phone.TYPE_CUSTOM); values.put(Phone.LABEL, "Nirav"); Uri dataUri = getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);` and then pass an Intent with this URI – Ashutosh Sagar Jul 24 '17 at 10:18
  • @MDP yeah, this stuff happens... – marmor Jul 24 '17 at 10:51
  • @marmor I also tried my code on samsung galaxy S2 and it works! It's officially a Huawei P8 bug!! – MDP Jul 26 '17 at 06:25
  • @AshutoshSagar I'll try your solution to see if I can "bypass" the problem with your code! Thank you – MDP Jul 26 '17 at 06:26

1 Answers1

0

(For completeness, I'll make this an answer)

This might be a device bug, try the same code on some other device.

marmor
  • 27,641
  • 11
  • 107
  • 150
  • I tried on Samsung S2 (android 4.2) and ASUS (android 6) and my code works. It seems to really be a Huawei P8 bug – MDP Jul 26 '17 at 06:34