Use system collator for display names.
Continue sorting directory names first, but use system collator in case-insensitive ordering. Bug: 13545569 Change-Id: I24b314c9ef42f397bc96f474d9593bdefc0061d1
This commit is contained in:
committed by
Jeff Sharkey
parent
8811f3457e
commit
fa5ec770ec
@@ -27,6 +27,8 @@ import android.database.Cursor;
|
||||
import android.os.Bundle;
|
||||
import android.provider.DocumentsContract.Document;
|
||||
|
||||
import com.android.documentsui.model.DocumentInfo;
|
||||
|
||||
/**
|
||||
* Cursor wrapper that presents a sorted view of the underlying cursor. Handles
|
||||
* common {@link Document} sorting modes, such as ordering directories first.
|
||||
@@ -68,7 +70,7 @@ public class SortingCursorWrapper extends AbstractCursor {
|
||||
final String displayName = getCursorString(
|
||||
cursor, Document.COLUMN_DISPLAY_NAME);
|
||||
if (Document.MIME_TYPE_DIR.equals(mimeType)) {
|
||||
mValueString[i] = '\001' + displayName;
|
||||
mValueString[i] = DocumentInfo.DIR_PREFIX + displayName;
|
||||
} else {
|
||||
mValueString[i] = displayName;
|
||||
}
|
||||
@@ -180,14 +182,7 @@ public class SortingCursorWrapper extends AbstractCursor {
|
||||
|
||||
final String lhs = pivotValue;
|
||||
final String rhs = value[mid];
|
||||
final int compare;
|
||||
if (lhs == null) {
|
||||
compare = -1;
|
||||
} else if (rhs == null) {
|
||||
compare = 1;
|
||||
} else {
|
||||
compare = lhs.compareToIgnoreCase(rhs);
|
||||
}
|
||||
final int compare = DocumentInfo.compareToIgnoreCaseNullable(lhs, rhs);
|
||||
|
||||
if (compare < 0) {
|
||||
right = mid;
|
||||
|
||||
@@ -25,6 +25,7 @@ import android.os.Parcelable;
|
||||
import android.provider.DocumentsContract;
|
||||
import android.provider.DocumentsContract.Document;
|
||||
import android.provider.DocumentsProvider;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.android.documentsui.DocumentsApplication;
|
||||
import com.android.documentsui.RootCursorWrapper;
|
||||
@@ -36,6 +37,7 @@ import java.io.DataOutputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.net.ProtocolException;
|
||||
import java.text.Collator;
|
||||
|
||||
/**
|
||||
* Representation of a {@link Document}.
|
||||
@@ -44,6 +46,13 @@ public class DocumentInfo implements Durable, Parcelable {
|
||||
private static final int VERSION_INIT = 1;
|
||||
private static final int VERSION_SPLIT_URI = 2;
|
||||
|
||||
private static final Collator sCollator;
|
||||
|
||||
static {
|
||||
sCollator = Collator.getInstance();
|
||||
sCollator.setStrength(Collator.SECONDARY);
|
||||
}
|
||||
|
||||
public String authority;
|
||||
public String documentId;
|
||||
public String mimeType;
|
||||
@@ -268,9 +277,30 @@ public class DocumentInfo implements Durable, Parcelable {
|
||||
throw fnfe;
|
||||
}
|
||||
|
||||
/**
|
||||
* String prefix used to indicate the document is a directory.
|
||||
*/
|
||||
public static final char DIR_PREFIX = '\001';
|
||||
|
||||
/**
|
||||
* Compare two strings against each other using system default collator in a
|
||||
* case-insensitive mode. Clusters strings prefixed with {@link #DIR_PREFIX}
|
||||
* before other items.
|
||||
*/
|
||||
public static int compareToIgnoreCaseNullable(String lhs, String rhs) {
|
||||
if (lhs == null) return -1;
|
||||
if (rhs == null) return 1;
|
||||
return lhs.compareToIgnoreCase(rhs);
|
||||
final boolean leftEmpty = TextUtils.isEmpty(lhs);
|
||||
final boolean rightEmpty = TextUtils.isEmpty(rhs);
|
||||
|
||||
if (leftEmpty && rightEmpty) return 0;
|
||||
if (leftEmpty) return -1;
|
||||
if (rightEmpty) return 1;
|
||||
|
||||
final boolean leftDir = (lhs.charAt(0) == DIR_PREFIX);
|
||||
final boolean rightDir = (rhs.charAt(0) == DIR_PREFIX);
|
||||
|
||||
if (leftDir && !rightDir) return -1;
|
||||
if (rightDir && !leftDir) return 1;
|
||||
|
||||
return sCollator.compare(lhs, rhs);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user