Fix potential NPE with null cursor

Detect and handle null cursors returned from CP2 queries.

Bug:12767708
Change-Id: I7bc4f97848a59140aba5a2c924c79fdc2749b150
diff --git a/src/com/android/contacts/common/model/ContactLoader.java b/src/com/android/contacts/common/model/ContactLoader.java
index ce177b0..926c09f 100644
--- a/src/com/android/contacts/common/model/ContactLoader.java
+++ b/src/com/android/contacts/common/model/ContactLoader.java
@@ -790,26 +790,28 @@
         final Cursor cursor = getContext().getContentResolver().query(Groups.CONTENT_URI,
                 GroupQuery.COLUMNS, selection.toString(), selectionArgs.toArray(new String[0]),
                 null);
-        try {
-            while (cursor.moveToNext()) {
-                final String accountName = cursor.getString(GroupQuery.ACCOUNT_NAME);
-                final String accountType = cursor.getString(GroupQuery.ACCOUNT_TYPE);
-                final String dataSet = cursor.getString(GroupQuery.DATA_SET);
-                final long groupId = cursor.getLong(GroupQuery.ID);
-                final String title = cursor.getString(GroupQuery.TITLE);
-                final boolean defaultGroup = cursor.isNull(GroupQuery.AUTO_ADD)
-                        ? false
-                        : cursor.getInt(GroupQuery.AUTO_ADD) != 0;
-                final boolean favorites = cursor.isNull(GroupQuery.FAVORITES)
-                        ? false
-                        : cursor.getInt(GroupQuery.FAVORITES) != 0;
+        if (cursor != null) {
+            try {
+                while (cursor.moveToNext()) {
+                    final String accountName = cursor.getString(GroupQuery.ACCOUNT_NAME);
+                    final String accountType = cursor.getString(GroupQuery.ACCOUNT_TYPE);
+                    final String dataSet = cursor.getString(GroupQuery.DATA_SET);
+                    final long groupId = cursor.getLong(GroupQuery.ID);
+                    final String title = cursor.getString(GroupQuery.TITLE);
+                    final boolean defaultGroup = cursor.isNull(GroupQuery.AUTO_ADD)
+                            ? false
+                            : cursor.getInt(GroupQuery.AUTO_ADD) != 0;
+                    final boolean favorites = cursor.isNull(GroupQuery.FAVORITES)
+                            ? false
+                            : cursor.getInt(GroupQuery.FAVORITES) != 0;
 
-                groupListBuilder.add(new GroupMetaData(
-                        accountName, accountType, dataSet, groupId, title, defaultGroup,
-                        favorites));
+                    groupListBuilder.add(new GroupMetaData(
+                                    accountName, accountType, dataSet, groupId, title, defaultGroup,
+                                    favorites));
+                }
+            } finally {
+                cursor.close();
             }
-        } finally {
-            cursor.close();
         }
         result.setGroupMetaData(groupListBuilder.build());
     }
diff --git a/src/com/android/contacts/common/model/RawContactModifier.java b/src/com/android/contacts/common/model/RawContactModifier.java
index ac586a2..954123b 100644
--- a/src/com/android/contacts/common/model/RawContactModifier.java
+++ b/src/com/android/contacts/common/model/RawContactModifier.java
@@ -635,16 +635,18 @@
                                 StructuredName.SUFFIX,
                         }, null, null, null);
 
-                try {
-                    if (cursor.moveToFirst()) {
-                        child.put(StructuredName.PREFIX, cursor.getString(0));
-                        child.put(StructuredName.GIVEN_NAME, cursor.getString(1));
-                        child.put(StructuredName.MIDDLE_NAME, cursor.getString(2));
-                        child.put(StructuredName.FAMILY_NAME, cursor.getString(3));
-                        child.put(StructuredName.SUFFIX, cursor.getString(4));
+                if (cursor != null) {
+                    try {
+                        if (cursor.moveToFirst()) {
+                            child.put(StructuredName.PREFIX, cursor.getString(0));
+                            child.put(StructuredName.GIVEN_NAME, cursor.getString(1));
+                            child.put(StructuredName.MIDDLE_NAME, cursor.getString(2));
+                            child.put(StructuredName.FAMILY_NAME, cursor.getString(3));
+                            child.put(StructuredName.SUFFIX, cursor.getString(4));
+                        }
+                    } finally {
+                        cursor.close();
                     }
-                } finally {
-                    cursor.close();
                 }
             }
         }
diff --git a/src/com/android/contacts/common/util/NameConverter.java b/src/com/android/contacts/common/util/NameConverter.java
index 56f3192..9706353 100644
--- a/src/com/android/contacts/common/util/NameConverter.java
+++ b/src/com/android/contacts/common/util/NameConverter.java
@@ -94,12 +94,14 @@
                 StructuredName.DISPLAY_NAME,
         }, null, null, null);
 
-        try {
-            if (cursor.moveToFirst()) {
-                displayName = cursor.getString(0);
+        if (cursor != null) {
+            try {
+                if (cursor.moveToFirst()) {
+                    displayName = cursor.getString(0);
+                }
+            } finally {
+                cursor.close();
             }
-        } finally {
-            cursor.close();
         }
         return displayName;
     }
@@ -123,14 +125,16 @@
         Cursor cursor = context.getContentResolver().query(builder.build(), STRUCTURED_NAME_FIELDS,
                 null, null, null);
 
-        try {
-            if (cursor.moveToFirst()) {
-                for (int i = 0; i < STRUCTURED_NAME_FIELDS.length; i++) {
-                    structuredName.put(STRUCTURED_NAME_FIELDS[i], cursor.getString(i));
+        if (cursor != null) {
+            try {
+                if (cursor.moveToFirst()) {
+                    for (int i = 0; i < STRUCTURED_NAME_FIELDS.length; i++) {
+                        structuredName.put(STRUCTURED_NAME_FIELDS[i], cursor.getString(i));
+                    }
                 }
+            } finally {
+                cursor.close();
             }
-        } finally {
-            cursor.close();
         }
         return structuredName;
     }