Protect against CP2 crash in Dialer

Bug: 21755796
Change-Id: Icd6ca156b799457bd527fd4e2a83c37b9faf4b9b
diff --git a/src/com/android/contacts/common/list/ContactEntryListFragment.java b/src/com/android/contacts/common/list/ContactEntryListFragment.java
index 8689874..8d1cede 100644
--- a/src/com/android/contacts/common/list/ContactEntryListFragment.java
+++ b/src/com/android/contacts/common/list/ContactEntryListFragment.java
@@ -31,6 +31,7 @@
 import android.os.Parcelable;
 import android.provider.ContactsContract.Directory;
 import android.text.TextUtils;
+import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.MotionEvent;
 import android.view.View;
@@ -346,7 +347,19 @@
     }
 
     public CursorLoader createCursorLoader(Context context) {
-        return new CursorLoader(context, null, null, null, null, null);
+        return new CursorLoader(context, null, null, null, null, null) {
+            @Override
+            protected Cursor onLoadInBackground() {
+                try {
+                    return super.onLoadInBackground();
+                } catch (RuntimeException e) {
+                    // We don't even know what the projection should be, so no point trying to
+                    // return an empty MatrixCursor with the correct projection here.
+                    Log.w(TAG, "RuntimeException while trying to query ContactsProvider.");
+                    return null;
+                }
+            }
+        };
     }
 
     private void startLoadingDirectoryPartition(int partitionIndex) {
diff --git a/src/com/android/contacts/common/list/DirectoryListLoader.java b/src/com/android/contacts/common/list/DirectoryListLoader.java
index dedd62e..b327361 100644
--- a/src/com/android/contacts/common/list/DirectoryListLoader.java
+++ b/src/com/android/contacts/common/list/DirectoryListLoader.java
@@ -144,13 +144,15 @@
                 throw new RuntimeException(
                         "Unsupported directory search mode: " + mDirectorySearchMode);
         }
-
-        Cursor cursor = context.getContentResolver().query(DirectoryQuery.URI,
-                DirectoryQuery.PROJECTION, selection, null, DirectoryQuery.ORDER_BY);
-        if (cursor == null) {
-            return result;
-        }
+        Cursor cursor = null;
         try {
+            cursor = context.getContentResolver().query(DirectoryQuery.URI,
+                    DirectoryQuery.PROJECTION, selection, null, DirectoryQuery.ORDER_BY);
+
+            if (cursor == null) {
+                return result;
+            }
+
             while(cursor.moveToNext()) {
                 long directoryId = cursor.getLong(DirectoryQuery.ID);
                 String directoryType = null;
@@ -169,8 +171,12 @@
                 int photoSupport = cursor.getInt(DirectoryQuery.PHOTO_SUPPORT);
                 result.addRow(new Object[]{directoryId, directoryType, displayName, photoSupport});
             }
+        } catch (RuntimeException e) {
+            Log.w(TAG, "Runtime Exception when querying directory");
         } finally {
-            cursor.close();
+            if (cursor != null) {
+                cursor.close();
+            }
         }
 
         return result;