From 49ca224623cb49342fec7e61d9f11d05d13d4a2f Mon Sep 17 00:00:00 2001 From: Tomasz Mikolajewski Date: Tue, 15 Mar 2016 15:55:46 +0900 Subject: [PATCH] Reduce memory allocations and improve sorting in DocumentsUI by 8.42%. Bug: 27286016 Change-Id: Icc84aaaedd396d82f4ca7de63a682deebe1e1b93 --- .../android/documentsui/dirlist/Model.java | 19 ++++++++----------- .../documentsui/dirlist/ModelTest.java | 2 +- .../documentsui/dirlist/TestModel.java | 4 +--- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/packages/DocumentsUI/src/com/android/documentsui/dirlist/Model.java b/packages/DocumentsUI/src/com/android/documentsui/dirlist/Model.java index bae18ab06317c..68b1bcc411fce 100644 --- a/packages/DocumentsUI/src/com/android/documentsui/dirlist/Model.java +++ b/packages/DocumentsUI/src/com/android/documentsui/dirlist/Model.java @@ -22,6 +22,7 @@ import static com.android.documentsui.State.SORT_ORDER_LAST_MODIFIED; 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; @@ -71,16 +72,6 @@ public class Model { @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); @@ -174,7 +165,13 @@ public class Model { // 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(getString(mAuthorityIndex), getString(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] = getString(mAuthorityIndex) + "|" + mCursor.getString(mDocIdIndex); + } else { + mIds[pos] = mCursor.getString(mDocIdIndex); + } mimeType = getString(mMimeTypeIndex); isDirs[pos] = Document.MIME_TYPE_DIR.equals(mimeType); diff --git a/packages/DocumentsUI/tests/src/com/android/documentsui/dirlist/ModelTest.java b/packages/DocumentsUI/tests/src/com/android/documentsui/dirlist/ModelTest.java index c6ad5116d93bf..3536593564afa 100644 --- a/packages/DocumentsUI/tests/src/com/android/documentsui/dirlist/ModelTest.java +++ b/packages/DocumentsUI/tests/src/com/android/documentsui/dirlist/ModelTest.java @@ -284,7 +284,7 @@ public class ModelTest extends AndroidTestCase { String id = Integer.toString(i); row.add(RootCursorWrapper.COLUMN_AUTHORITY, AUTHORITY); row.add(Document.COLUMN_DOCUMENT_ID, id); - currentDownloads.add(Model.createModelId(AUTHORITY, id)); + currentDownloads.add(id); } DirectoryResult r = new DirectoryResult(); diff --git a/packages/DocumentsUI/tests/src/com/android/documentsui/dirlist/TestModel.java b/packages/DocumentsUI/tests/src/com/android/documentsui/dirlist/TestModel.java index d8c29db81a77f..2d819ffcdf07d 100644 --- a/packages/DocumentsUI/tests/src/com/android/documentsui/dirlist/TestModel.java +++ b/packages/DocumentsUI/tests/src/com/android/documentsui/dirlist/TestModel.java @@ -62,9 +62,7 @@ public class TestModel extends Model { update(r); } - // Note that model id includes authority qualifier and is distinct - // WRT documentId because of this. String idForPosition(int p) { - return createModelId(mAuthority, Integer.toString(p)); + return Integer.toString(p); } }