Skip to content

Commit

Permalink
Massively improve findFile performance (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
raxod502 authored May 4, 2024
1 parent 7c257e1 commit e0def6b
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 50 deletions.
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;

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

0 comments on commit e0def6b

Please sign in to comment.