Handle removal of local raw contact in profiles.

Bug 5284316

Change-Id: I40160543af3cb717791ee8109f0cbf5d06327111
diff --git a/src/com/android/contacts/ContactSaveService.java b/src/com/android/contacts/ContactSaveService.java
index 2df2e47..e465772 100644
--- a/src/com/android/contacts/ContactSaveService.java
+++ b/src/com/android/contacts/ContactSaveService.java
@@ -323,10 +323,26 @@
                 if (rawContactId == -1) {
                     throw new IllegalStateException("Could not determine RawContact ID after save");
                 }
-                final Uri rawContactUri = ContentUris.withAppendedId(
-                        isProfile ? Profile.CONTENT_RAW_CONTACTS_URI : RawContacts.CONTENT_URI,
-                                rawContactId);
-                lookupUri = RawContacts.getContactLookupUri(resolver, rawContactUri);
+                if (isProfile) {
+                    // Since the profile supports local raw contacts, which may have been completely
+                    // removed if all information was removed, we need to do a special query to
+                    // get the lookup URI for the profile contact (if it still exists).
+                    Cursor c = resolver.query(Profile.CONTENT_URI,
+                            new String[] {Contacts._ID, Contacts.LOOKUP_KEY},
+                            null, null, null);
+                    try {
+                        c.moveToFirst();
+                        final long contactId = c.getLong(0);
+                        final String lookupKey = c.getString(1);
+                        lookupUri = Contacts.getLookupUri(contactId, lookupKey);
+                    } finally {
+                        c.close();
+                    }
+                } else {
+                    final Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
+                                    rawContactId);
+                    lookupUri = RawContacts.getContactLookupUri(resolver, rawContactUri);
+                }
                 Log.v(TAG, "Saved contact. New URI: " + lookupUri);
                 break;