Guard against missing document columns.

When filtering and sorting, guard against missing columns to avoid
crashing entire app.

Bug: 11377065
Change-Id: I04f035c918d743bcc84592f05e3ef9ad0ebfadce
diff --git a/packages/DocumentsUI/src/com/android/documentsui/FilteringCursorWrapper.java b/packages/DocumentsUI/src/com/android/documentsui/FilteringCursorWrapper.java
index 52d816f..55d73f2 100644
--- a/packages/DocumentsUI/src/com/android/documentsui/FilteringCursorWrapper.java
+++ b/packages/DocumentsUI/src/com/android/documentsui/FilteringCursorWrapper.java
@@ -17,6 +17,8 @@
 package com.android.documentsui;
 
 import static com.android.documentsui.DocumentsActivity.TAG;
+import static com.android.documentsui.model.DocumentInfo.getCursorLong;
+import static com.android.documentsui.model.DocumentInfo.getCursorString;
 
 import android.database.AbstractCursor;
 import android.database.Cursor;
@@ -50,10 +52,8 @@
 
         cursor.moveToPosition(-1);
         while (cursor.moveToNext()) {
-            final String mimeType = cursor.getString(
-                    cursor.getColumnIndex(Document.COLUMN_MIME_TYPE));
-            final long lastModified = cursor.getLong(
-                    cursor.getColumnIndex(Document.COLUMN_LAST_MODIFIED));
+            final String mimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
+            final long lastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
             if (rejectMimes != null && MimePredicate.mimeMatches(rejectMimes, mimeType)) {
                 continue;
             }
diff --git a/packages/DocumentsUI/src/com/android/documentsui/SortingCursorWrapper.java b/packages/DocumentsUI/src/com/android/documentsui/SortingCursorWrapper.java
index 19ad2e2..a23dd15 100644
--- a/packages/DocumentsUI/src/com/android/documentsui/SortingCursorWrapper.java
+++ b/packages/DocumentsUI/src/com/android/documentsui/SortingCursorWrapper.java
@@ -19,6 +19,8 @@
 import static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_DISPLAY_NAME;
 import static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_LAST_MODIFIED;
 import static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_SIZE;
+import static com.android.documentsui.model.DocumentInfo.getCursorLong;
+import static com.android.documentsui.model.DocumentInfo.getCursorString;
 
 import android.database.AbstractCursor;
 import android.database.Cursor;
@@ -62,10 +64,9 @@
 
             switch (sortOrder) {
                 case SORT_ORDER_DISPLAY_NAME:
-                    final String mimeType = cursor.getString(
-                            cursor.getColumnIndex(Document.COLUMN_MIME_TYPE));
-                    final String displayName = cursor.getString(
-                            cursor.getColumnIndex(Document.COLUMN_DISPLAY_NAME));
+                    final String mimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
+                    final String displayName = getCursorString(
+                            cursor, Document.COLUMN_DISPLAY_NAME);
                     if (Document.MIME_TYPE_DIR.equals(mimeType)) {
                         mValueString[i] = '\001' + displayName;
                     } else {
@@ -73,11 +74,10 @@
                     }
                     break;
                 case SORT_ORDER_LAST_MODIFIED:
-                    mValueLong[i] = cursor.getLong(
-                            cursor.getColumnIndex(Document.COLUMN_LAST_MODIFIED));
+                    mValueLong[i] = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
                     break;
                 case SORT_ORDER_SIZE:
-                    mValueLong[i] = cursor.getLong(cursor.getColumnIndex(Document.COLUMN_SIZE));
+                    mValueLong[i] = getCursorLong(cursor, Document.COLUMN_SIZE);
                     break;
             }
         }
diff --git a/packages/ExternalStorageProvider/src/com/android/externalstorage/TestDocumentsProvider.java b/packages/ExternalStorageProvider/src/com/android/externalstorage/TestDocumentsProvider.java
index af6ff01..8eb70e9 100644
--- a/packages/ExternalStorageProvider/src/com/android/externalstorage/TestDocumentsProvider.java
+++ b/packages/ExternalStorageProvider/src/com/android/externalstorage/TestDocumentsProvider.java
@@ -57,6 +57,9 @@
 
     private static final boolean LAG = false;
 
+    private static final boolean ROOT_LAME_PROJECTION = false;
+    private static final boolean DOCUMENT_LAME_PROJECTION = false;
+
     private static final boolean ROOTS_WEDGE = false;
     private static final boolean ROOTS_CRASH = false;
     private static final boolean ROOTS_REFRESH = false;
@@ -88,10 +91,12 @@
     };
 
     private static String[] resolveRootProjection(String[] projection) {
+        if (ROOT_LAME_PROJECTION) return new String[0];
         return projection != null ? projection : DEFAULT_ROOT_PROJECTION;
     }
 
     private static String[] resolveDocumentProjection(String[] projection) {
+        if (DOCUMENT_LAME_PROJECTION) return new String[0];
         return projection != null ? projection : DEFAULT_DOCUMENT_PROJECTION;
     }