Fix potential NPE with null cursor

Detect and handle null cursors returned from CP2 queries.

Bug:12767708
Change-Id: I1571463b80e5b85656fe119c6bc83de36be30c7f
diff --git a/src/com/android/contacts/SplitAggregateView.java b/src/com/android/contacts/SplitAggregateView.java
index 6e38549..2281ec6 100644
--- a/src/com/android/contacts/SplitAggregateView.java
+++ b/src/com/android/contacts/SplitAggregateView.java
@@ -157,6 +157,9 @@
         Uri dataUri = Uri.withAppendedPath(mAggregateUri, Data.CONTENT_DIRECTORY);
         Cursor cursor = getContext().getContentResolver().query(dataUri,
                 SplitQuery.COLUMNS, null, null, null);
+        if (cursor == null) {
+            return Collections.emptyList();
+        }
         try {
             while (cursor.moveToNext()) {
                 long rawContactId = cursor.getLong(SplitQuery.RAW_CONTACT_ID);
diff --git a/src/com/android/contacts/editor/AggregationSuggestionEngine.java b/src/com/android/contacts/editor/AggregationSuggestionEngine.java
index 2f77858..f121605 100644
--- a/src/com/android/contacts/editor/AggregationSuggestionEngine.java
+++ b/src/com/android/contacts/editor/AggregationSuggestionEngine.java
@@ -300,6 +300,9 @@
     private void loadAggregationSuggestions(Uri uri) {
         ContentResolver contentResolver = mContext.getContentResolver();
         Cursor cursor = contentResolver.query(uri, new String[]{Contacts._ID}, null, null, null);
+        if (cursor == null) {
+            return;
+        }
         try {
             // If a new request is pending, chuck the result of the previous request
             if (getHandler().hasMessages(MESSAGE_NAME_CHANGE)) {
@@ -324,7 +327,9 @@
 
             Cursor dataCursor = contentResolver.query(Data.CONTENT_URI,
                     DataQuery.COLUMNS, sb.toString(), null, Data.CONTACT_ID);
-            mMainHandler.sendMessage(mMainHandler.obtainMessage(MESSAGE_DATA_CURSOR, dataCursor));
+            if (dataCursor != null) {
+                mMainHandler.sendMessage(mMainHandler.obtainMessage(MESSAGE_DATA_CURSOR, dataCursor));
+            }
         } finally {
             cursor.close();
         }
diff --git a/src/com/android/contacts/group/SuggestedMemberListAdapter.java b/src/com/android/contacts/group/SuggestedMemberListAdapter.java
index 067c052..6d60a3e 100644
--- a/src/com/android/contacts/group/SuggestedMemberListAdapter.java
+++ b/src/com/android/contacts/group/SuggestedMemberListAdapter.java
@@ -263,31 +263,33 @@
                     "=?) AND " + rawContactIdSelectionBuilder.toString(),
                     selectionArgs.toArray(new String[0]), null);
 
-            try {
-                memberDataCursor.moveToPosition(-1);
-                while (memberDataCursor.moveToNext()) {
-                    long rawContactId = memberDataCursor.getLong(RAW_CONTACT_ID_COLUMN_INDEX);
-                    SuggestedMember member = suggestionsMap.get(rawContactId);
-                    if (member == null) {
-                        continue;
-                    }
-                    String mimetype = memberDataCursor.getString(MIMETYPE_COLUMN_INDEX);
-                    if (Photo.CONTENT_ITEM_TYPE.equals(mimetype)) {
-                        // Set photo
-                        byte[] bitmapArray = memberDataCursor.getBlob(PHOTO_COLUMN_INDEX);
-                        member.setPhotoByteArray(bitmapArray);
-                    } else if (Email.CONTENT_ITEM_TYPE.equals(mimetype) ||
-                            Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
-                        // Set at most 1 extra piece of contact info that can be a phone number or
-                        // email
-                        if (!member.hasExtraInfo()) {
-                            String info = memberDataCursor.getString(DATA_COLUMN_INDEX);
-                            member.setExtraInfo(info);
+            if (memberDataCursor != null) {
+                try {
+                    memberDataCursor.moveToPosition(-1);
+                    while (memberDataCursor.moveToNext()) {
+                        long rawContactId = memberDataCursor.getLong(RAW_CONTACT_ID_COLUMN_INDEX);
+                        SuggestedMember member = suggestionsMap.get(rawContactId);
+                        if (member == null) {
+                            continue;
+                        }
+                        String mimetype = memberDataCursor.getString(MIMETYPE_COLUMN_INDEX);
+                        if (Photo.CONTENT_ITEM_TYPE.equals(mimetype)) {
+                            // Set photo
+                            byte[] bitmapArray = memberDataCursor.getBlob(PHOTO_COLUMN_INDEX);
+                            member.setPhotoByteArray(bitmapArray);
+                        } else if (Email.CONTENT_ITEM_TYPE.equals(mimetype) ||
+                                Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
+                            // Set at most 1 extra piece of contact info that can be a phone number or
+                            // email
+                            if (!member.hasExtraInfo()) {
+                                String info = memberDataCursor.getString(DATA_COLUMN_INDEX);
+                                member.setExtraInfo(info);
+                            }
                         }
                     }
+                } finally {
+                    memberDataCursor.close();
                 }
-            } finally {
-                memberDataCursor.close();
             }
             results.values = suggestionsList;
             return results;