FileDocCategorySizeDatePackage
SortingCursorWrapper.javaAPI DocAndroid 5.1 API8026Thu Mar 12 22:22:40 GMT 2015com.android.documentsui

SortingCursorWrapper

public class SortingCursorWrapper extends android.database.AbstractCursor
Cursor wrapper that presents a sorted view of the underlying cursor. Handles common {@link Document} sorting modes, such as ordering directories first.

Fields Summary
private final android.database.Cursor
mCursor
private final int[]
mPosition
private final String[]
mValueString
private final long[]
mValueLong
Constructors Summary
public SortingCursorWrapper(android.database.Cursor cursor, int sortOrder)

        mCursor = cursor;

        final int count = cursor.getCount();
        mPosition = new int[count];
        switch (sortOrder) {
            case SORT_ORDER_DISPLAY_NAME:
                mValueString = new String[count];
                mValueLong = null;
                break;
            case SORT_ORDER_LAST_MODIFIED:
            case SORT_ORDER_SIZE:
                mValueString = null;
                mValueLong = new long[count];
                break;
            default:
                throw new IllegalArgumentException();
        }

        cursor.moveToPosition(-1);
        for (int i = 0; i < count; i++) {
            cursor.moveToNext();
            mPosition[i] = i;

            switch (sortOrder) {
                case SORT_ORDER_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] = DocumentInfo.DIR_PREFIX + displayName;
                    } else {
                        mValueString[i] = displayName;
                    }
                    break;
                case SORT_ORDER_LAST_MODIFIED:
                    mValueLong[i] = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
                    break;
                case SORT_ORDER_SIZE:
                    mValueLong[i] = getCursorLong(cursor, Document.COLUMN_SIZE);
                    break;
            }
        }

        switch (sortOrder) {
            case SORT_ORDER_DISPLAY_NAME:
                synchronized (SortingCursorWrapper.class) {

                    binarySort(mPosition, mValueString);
                }
                break;
            case SORT_ORDER_LAST_MODIFIED:
            case SORT_ORDER_SIZE:
                binarySort(mPosition, mValueLong);
                break;
        }
    
Methods Summary
private static voidbinarySort(int[] position, java.lang.String[] value)
Borrowed from TimSort.binarySort(), but modified to sort two column dataset.

        final int count = position.length;
        for (int start = 1; start < count; start++) {
            final int pivotPosition = position[start];
            final String pivotValue = value[start];

            int left = 0;
            int right = start;

            while (left < right) {
                int mid = (left + right) >>> 1;

                final String lhs = pivotValue;
                final String rhs = value[mid];
                final int compare = DocumentInfo.compareToIgnoreCaseNullable(lhs, rhs);

                if (compare < 0) {
                    right = mid;
                } else {
                    left = mid + 1;
                }
            }

            int n = start - left;
            switch (n) {
                case 2:
                    position[left + 2] = position[left + 1];
                    value[left + 2] = value[left + 1];
                case 1:
                    position[left + 1] = position[left];
                    value[left + 1] = value[left];
                    break;
                default:
                    System.arraycopy(position, left, position, left + 1, n);
                    System.arraycopy(value, left, value, left + 1, n);
            }

            position[left] = pivotPosition;
            value[left] = pivotValue;
        }
    
private static voidbinarySort(int[] position, long[] value)
Borrowed from TimSort.binarySort(), but modified to sort two column dataset.

        final int count = position.length;
        for (int start = 1; start < count; start++) {
            final int pivotPosition = position[start];
            final long pivotValue = value[start];

            int left = 0;
            int right = start;

            while (left < right) {
                int mid = (left + right) >>> 1;

                final long lhs = pivotValue;
                final long rhs = value[mid];
                final int compare = Long.compare(lhs, rhs);
                if (compare > 0) {
                    right = mid;
                } else {
                    left = mid + 1;
                }
            }

            int n = start - left;
            switch (n) {
                case 2:
                    position[left + 2] = position[left + 1];
                    value[left + 2] = value[left + 1];
                case 1:
                    position[left + 1] = position[left];
                    value[left + 1] = value[left];
                    break;
                default:
                    System.arraycopy(position, left, position, left + 1, n);
                    System.arraycopy(value, left, value, left + 1, n);
            }

            position[left] = pivotPosition;
            value[left] = pivotValue;
        }
    
public voidclose()

        super.close();
        mCursor.close();
    
public java.lang.String[]getColumnNames()

        return mCursor.getColumnNames();
    
public intgetCount()

        return mCursor.getCount();
    
public doublegetDouble(int column)

        return mCursor.getDouble(column);
    
public android.os.BundlegetExtras()

        return mCursor.getExtras();
    
public floatgetFloat(int column)

        return mCursor.getFloat(column);
    
public intgetInt(int column)

        return mCursor.getInt(column);
    
public longgetLong(int column)

        return mCursor.getLong(column);
    
public shortgetShort(int column)

        return mCursor.getShort(column);
    
public java.lang.StringgetString(int column)

        return mCursor.getString(column);
    
public intgetType(int column)

        return mCursor.getType(column);
    
public booleanisNull(int column)

        return mCursor.isNull(column);
    
public booleanonMove(int oldPosition, int newPosition)

        return mCursor.moveToPosition(mPosition[newPosition]);