Reduce memory allocations and improve sorting in DocumentsUI by 8.42%.

Reland after a revert. No changes.

Bug: 27286016
Change-Id: I0988062106faf4086c44d9b344649ef2cb86d3e7
diff --git a/src/com/android/documentsui/dirlist/Model.java b/src/com/android/documentsui/dirlist/Model.java
index 179cb9b..3642b01 100644
--- a/src/com/android/documentsui/dirlist/Model.java
+++ b/src/com/android/documentsui/dirlist/Model.java
@@ -22,6 +22,7 @@
 import static com.android.documentsui.State.SORT_ORDER_SIZE;
 
 import android.database.Cursor;
+import android.database.MergeCursor;
 import android.os.Bundle;
 import android.provider.DocumentsContract;
 import android.provider.DocumentsContract.Document;
@@ -72,16 +73,6 @@
     @Nullable String error;
     @Nullable DocumentInfo doc;
 
-    /**
-     * Generates a Model ID for a cursor entry that refers to a document. The Model ID is a unique
-     * string that can be used to identify the document referred to by the cursor.
-     *
-     * @param c A cursor that refers to a document.
-     */
-    static String createModelId(String authority, String docId) {
-        return authority + "|" + docId;
-    }
-
     private void notifyUpdateListeners() {
         for (UpdateListener listener: mUpdateListeners) {
             listener.onModelUpdate(this);
@@ -176,8 +167,13 @@
 
             // Generates a Model ID for a cursor entry that refers to a document. The Model ID is a
             // unique string that can be used to identify the document referred to by the cursor.
-            mIds[pos] = createModelId(
-                    getStringOrEmpty(mAuthorityIndex), getStringOrEmpty(mDocIdIndex));
+            // If the cursor is a merged cursor over multiple authorities, then prefix the ids
+            // with the authority to avoid collisions.
+            if (mCursor instanceof MergeCursor) {
+                mIds[pos] = getStringOrEmpty(mAuthorityIndex) + "|" + getStringOrEmpty(mDocIdIndex);
+            } else {
+                mIds[pos] = getStringOrEmpty(mDocIdIndex);
+            }
 
             mimeType = getStringOrEmpty(mMimeTypeIndex);
             isDirs[pos] = Document.MIME_TYPE_DIR.equals(mimeType);