diff --git a/packages/DocumentsUI/src/com/android/documentsui/dirlist/Model.java b/packages/DocumentsUI/src/com/android/documentsui/dirlist/Model.java index 68b1bcc411fce..c5ee59237fb08 100644 --- a/packages/DocumentsUI/src/com/android/documentsui/dirlist/Model.java +++ b/packages/DocumentsUI/src/com/android/documentsui/dirlist/Model.java @@ -20,9 +20,10 @@ import static com.android.documentsui.Shared.DEBUG; import static com.android.documentsui.State.SORT_ORDER_DISPLAY_NAME; import static com.android.documentsui.State.SORT_ORDER_LAST_MODIFIED; import static com.android.documentsui.State.SORT_ORDER_SIZE; +import static com.android.documentsui.model.DocumentInfo.getCursorLong; +import static com.android.documentsui.model.DocumentInfo.getCursorString; import android.database.Cursor; -import android.database.MergeCursor; import android.os.Bundle; import android.provider.DocumentsContract; import android.provider.DocumentsContract.Document; @@ -61,17 +62,34 @@ public class Model { private String mIds[] = new String[0]; private int mSortOrder = SORT_ORDER_DISPLAY_NAME; - private int mAuthorityIndex = -1; - private int mDocIdIndex = -1; - private int mMimeTypeIndex = -1; - private int mDisplayNameIndex = -1; - private int mColumnSizeIndex = -1; - private int mLastModifiedIndex = -1; - @Nullable String info; @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. + */ + private static String createModelId(Cursor c) { + // TODO: Maybe more efficient to use just the document ID, in cases where there is only one + // authority (which should be the majority of cases). + return createModelId( + getCursorString(c, RootCursorWrapper.COLUMN_AUTHORITY), + getCursorString(c, Document.COLUMN_DOCUMENT_ID)); + } + + /** + * 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); @@ -109,13 +127,6 @@ public class Model { mCursor = result.cursor; mCursorCount = mCursor.getCount(); mSortOrder = result.sortOrder; - mAuthorityIndex = mCursor.getColumnIndex(RootCursorWrapper.COLUMN_AUTHORITY); - assert(mAuthorityIndex != -1); - mDocIdIndex = mCursor.getColumnIndex(Document.COLUMN_DOCUMENT_ID); - mMimeTypeIndex = mCursor.getColumnIndex(Document.COLUMN_MIME_TYPE); - mDisplayNameIndex = mCursor.getColumnIndex(Document.COLUMN_DISPLAY_NAME); - mColumnSizeIndex = mCursor.getColumnIndex(Document.COLUMN_SIZE); - doc = result.doc; updateModelData(); @@ -162,30 +173,22 @@ public class Model { for (int pos = 0; pos < mCursorCount; ++pos) { mCursor.moveToNext(); positions[pos] = pos; + mIds[pos] = createModelId(mCursor); - // 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. - // 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); + mimeType = getCursorString(mCursor, Document.COLUMN_MIME_TYPE); isDirs[pos] = Document.MIME_TYPE_DIR.equals(mimeType); - switch (mSortOrder) { + switch(mSortOrder) { case SORT_ORDER_DISPLAY_NAME: - displayNames[pos] = getString(mDisplayNameIndex); + final String displayName = getCursorString( + mCursor, Document.COLUMN_DISPLAY_NAME); + displayNames[pos] = displayName; break; case SORT_ORDER_LAST_MODIFIED: - longValues[pos] = getLastModified(); + longValues[pos] = getLastModified(mCursor); break; case SORT_ORDER_SIZE: - longValues[pos] = mColumnSizeIndex != -1 - ? mCursor.getLong(mColumnSizeIndex) : 0; + longValues[pos] = getCursorLong(mCursor, Document.COLUMN_SIZE); break; } } @@ -361,17 +364,13 @@ public class Model { } } - private String getString(int columnIndex) { - return columnIndex != -1 ? mCursor.getString(columnIndex) : null; - } - /** * @return Timestamp for the given document. Some docs (e.g. active downloads) have a null * timestamp - these will be replaced with MAX_LONG so that such files get sorted to the top * when sorting by date. */ - private long getLastModified() { - long l = mCursor.getLong(mLastModifiedIndex); + long getLastModified(Cursor cursor) { + long l = getCursorLong(mCursor, Document.COLUMN_LAST_MODIFIED); return (l == -1) ? Long.MAX_VALUE : l; } 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 3536593564afa..c6ad5116d93bf 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(id); + currentDownloads.add(Model.createModelId(AUTHORITY, 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 2d819ffcdf07d..d8c29db81a77f 100644 --- a/packages/DocumentsUI/tests/src/com/android/documentsui/dirlist/TestModel.java +++ b/packages/DocumentsUI/tests/src/com/android/documentsui/dirlist/TestModel.java @@ -62,7 +62,9 @@ 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 Integer.toString(p); + return createModelId(mAuthority, Integer.toString(p)); } }