Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Massively improve findFile performance #2

Merged
merged 1 commit into from
May 4, 2024
Merged
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
Empty file modified gradlew
100644 → 100755
Empty file.
10 changes: 2 additions & 8 deletions library/src/main/java/com/hippo/unifile/AssetFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public UniFile createFile(String displayName) {

@Override
public UniFile createDirectory(String displayName) {
UniFile file = findFile(displayName, true);
UniFile file = findFile(displayName);
if (file != null && file.isDirectory()) {
return file;
} else {
Expand Down Expand Up @@ -208,12 +208,6 @@ public UniFile[] listFiles(FilenameFilter filter) {
@Nullable
@Override
public UniFile findFile(String displayName) {
return findFile(displayName, false);
}

@Nullable
@Override
public UniFile findFile(String displayName, boolean ignoreCase) {
if (TextUtils.isEmpty(displayName)) {
return null;
}
Expand All @@ -225,7 +219,7 @@ public UniFile findFile(String displayName, boolean ignoreCase) {
}

for (String f : files) {
if (Utils.equals(displayName, f, ignoreCase)) {
if (displayName.equals(f)) {
return new AssetFile(this, mAssetManager, Utils.resolve(mPath, displayName));
}
}
Expand Down
5 changes: 0 additions & 5 deletions library/src/main/java/com/hippo/unifile/MediaFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,6 @@ public UniFile findFile(String displayName) {
return null;
}

@Override
public UniFile findFile(String displayName, boolean ignoreCase) {
return null;
}

@Override
public boolean renameTo(String displayName) {
return false;
Expand Down
5 changes: 0 additions & 5 deletions library/src/main/java/com/hippo/unifile/RawFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,6 @@ public UniFile[] listFiles(FilenameFilter filter) {

@Override
public UniFile findFile(String displayName) {
return findFile(displayName, false);
}

@Override
public UniFile findFile(String displayName, boolean ignoreCase) {
if (TextUtils.isEmpty(displayName)) {
return null;
}
Expand Down
6 changes: 0 additions & 6 deletions library/src/main/java/com/hippo/unifile/ResourceFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,6 @@ public UniFile findFile(String displayName) {
return null;
}

@Nullable
@Override
public UniFile findFile(String displayName, boolean ignoreCase) {
return null;
}

@Override
public boolean renameTo(String displayName) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,6 @@ public UniFile findFile(String displayName) {
return null;
}

@Override
public UniFile findFile(String displayName, boolean ignoreCase) {
return null;
}

@Override
public boolean renameTo(String displayName) {
return false;
Expand Down
24 changes: 12 additions & 12 deletions library/src/main/java/com/hippo/unifile/TreeDocumentFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.content.Context;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.provider.DocumentsContract;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.MimeTypeMap;
Expand Down Expand Up @@ -93,7 +94,7 @@ public UniFile createDirectory(String displayName) {
return null;
}

UniFile child = findFile(displayName, true);
UniFile child = findFile(displayName);

if (child != null) {
if (child.isDirectory()) {
Expand Down Expand Up @@ -220,11 +221,6 @@ public UniFile[] listFiles(FilenameFilter filter) {

@Override
public UniFile findFile(String displayName) {
return findFile(displayName, false);
}

@Override
public UniFile findFile(String displayName, boolean ignoreCase) {
if (TextUtils.isEmpty(displayName)) {
return null;
}
Expand All @@ -233,13 +229,17 @@ 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);
}
String documentId = DocumentsContract.getDocumentId(mUri);
documentId += "/" + displayName;
Copy link

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

I was rather wondering about that. I will adjust the code so that it checks the provider that is being used, and if it is not ExternalStorageProvider then it can fall back to the slower implementation that is more generic.

Copy link

Choose a reason for hiding this comment

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

After reconsidering, I think maybe we just leave a comment and assume it will work on most providers, in case some OEMs use different package names for ExternalStorageProvider.

Copy link
Author

Choose a reason for hiding this comment

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

I've added #3 to document this, per your suggestion.


Uri documentUri = DocumentsContract.buildDocumentUriUsingTree(mUri, documentId);
UniFile child = new TreeDocumentFile(this, mContext, documentUri, displayName);

if (child.exists()) {
return child;
} else {
return null;
}
return null;
}

@Override
Expand Down
9 changes: 0 additions & 9 deletions library/src/main/java/com/hippo/unifile/UniFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -402,15 +402,6 @@ public UniFile getParentFile() {
@Nullable
public abstract UniFile findFile(String displayName);

/**
* Test there is a file with the display name in the directory.
*
* @param ignoreCase Whether to do a case insensitive check.
* @return the file if found it, or {@code null}.
*/
@Nullable
public abstract UniFile findFile(String displayName, boolean ignoreCase);

/**
* Renames this file to {@code displayName}.
* <p>
Expand Down