Skip to content
This repository has been archived by the owner on Jan 29, 2025. It is now read-only.

Improve performance when using SAF #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,36 @@ public static NamedUri[] listFilesNamed(Context context, Uri self) {
return results.toArray(new NamedUri[results.size()]);
}

public static Uri findFile(Context context, Uri self, String displayName, boolean ignoreCase) {
final ContentResolver resolver = context.getContentResolver();
final Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(self,
DocumentsContract.getDocumentId(self));

Cursor c = null;
try {
c = resolver.query(childrenUri, new String[] {
DocumentsContract.Document.COLUMN_DOCUMENT_ID,
DocumentsContract.Document.COLUMN_DISPLAY_NAME }, null, null, null);
if (null != c) {
while (c.moveToNext()) {
final String documentName = c.getString(1);
if (Utils.equals(displayName, documentName, ignoreCase)) {
final String documentId = c.getString(0);
final Uri documentUri = DocumentsContract.buildDocumentUriUsingTree(self,
documentId);
return documentUri;
}
}
}
} catch (Exception e) {
// Log.w(TAG, "Failed query: " + e);
} finally {
closeQuietly(c);
}

return null;
}

public static Uri renameTo(Context context, Uri self, String displayName) {
try {
return DocumentsContract.renameDocument(context.getContentResolver(), self, displayName);
Expand Down
17 changes: 7 additions & 10 deletions library/src/main/java/com/hippo/unifile/TreeDocumentFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ public Uri getUri() {

@Override
public String getName() {
if (mName != null) {
return mName;
if (mName == null) {
mName = DocumentsContractApi19.getName(mContext, mUri);
}
return DocumentsContractApi19.getName(mContext, mUri);
return mName;
}

@Override
Expand Down Expand Up @@ -210,8 +210,7 @@ public UniFile[] listFiles(FilenameFilter filter) {
final NamedUri[] uris = DocumentsContractApi21.listFilesNamed(mContext, mUri);
final ArrayList<UniFile> results = new ArrayList<>();
for (NamedUri uri : uris) {
String name = DocumentsContractApi19.getName(mContext, uri.uri);
if (name != null && filter.accept(this, name)) {
if (name != null && filter.accept(this, uri.name)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be this? Or should we still have the getName above? I tried checking out the branch locally and it failed to compile because name isn't declared.

Suggested change
if (name != null && filter.accept(this, uri.name)) {
if (uri.name != null && filter.accept(this, uri.name)) {

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops! Yes, it should be uri.name.
Anyway, Imma close this in favor of #2, those optimization can be implemented there.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok cool, I will do my best to merge in your changes to that branch.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oop, nevermind, since #2 got merged before I went back and updated things, it might make sense to re-open this PR so it can be merged separately.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #4 for that.

results.add(new TreeDocumentFile(this, mContext, uri.uri, uri.name));
}
}
Expand All @@ -233,11 +232,9 @@ public UniFile findFile(String displayName, boolean ignoreCase) {
return null;
}

final NamedUri[] result = DocumentsContractApi21.listFilesNamed(mContext, mUri);
for (NamedUri uri : result) {
if (Utils.equals(displayName, uri.name, ignoreCase)) {
return new TreeDocumentFile(this, mContext, uri.uri, displayName);
}
final Uri uri = DocumentsContractApi21.findFile(mContext, mUri, displayName, ignoreCase);
if (uri != null) {
return new TreeDocumentFile(this, mContext, uri, displayName);
}
return null;
}
Expand Down