Skip to content

Instantly share code, notes, and snippets.

@draekko
Forked from bubbleguuum/CachedDocumentFile.java
Created April 26, 2018 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save draekko/4b71f113feb0c8bc487d49c86ba9f810 to your computer and use it in GitHub Desktop.
Save draekko/4b71f113feb0c8bc487d49c86ba9f810 to your computer and use it in GitHub Desktop.
Improve DocumentFile performance
package android.support.v4.provider;
import android.net.Uri;
import android.support.v4.provider.DocumentFile;
public class CachedDocumentFile extends DocumentFile {
final DocumentFile mWrapped;
final boolean mCacheChildren;
Boolean mCanRead;
Boolean mCanWrite;
Boolean mExists;
String mName;
String mType;
Boolean mIsDirectory;
// Boolean mIsFile;
Long mLastModified;
Long mLength;
DocumentFile[] mListFiles;
public void invalidate() {
mCanRead = null;
mCanWrite = null;
mExists = null;
mName = null;
mType = null;
mIsDirectory = null;
//mIsFile = null;
mLastModified = null;
mLength = null;
mListFiles = null;
}
public CachedDocumentFile(DocumentFile wrapped) {
this(wrapped, true);
}
public CachedDocumentFile(DocumentFile wrapped, boolean cacheChildren) {
super(wrapped.getParentFile());
mWrapped = wrapped;
mCacheChildren = cacheChildren;
}
@Override
public boolean canRead() {
if(mCanRead == null) {
mCanRead = mWrapped.canRead();
}
return mCanRead;
}
@Override
public boolean canWrite() {
if(mCanWrite == null) {
mCanWrite = mWrapped.canWrite();
}
return mCanWrite;
}
@Override
public DocumentFile createDirectory(String arg0) {
return mWrapped.createDirectory(arg0);
}
@Override
public DocumentFile createFile(String arg0, String arg1) {
return mWrapped.createFile(arg0, arg1);
}
@Override
public boolean delete() {
if(mWrapped.delete()) {
invalidate();
return true;
}
return false;
}
@Override
public boolean exists() {
if(mExists == null) {
mExists = mWrapped.exists();
}
return mExists;
}
@Override
public String getName() {
if(mName == null) {
mName = mWrapped.getName();
}
return mName;
}
@Override
public String getType() {
if(mType == null) {
mType = mWrapped.getType();
}
return mType;
}
@Override
public Uri getUri() {
return mWrapped.getUri();
}
@Override
public boolean isDirectory() {
if(mIsDirectory == null) {
mIsDirectory = mWrapped.isDirectory();
}
return mIsDirectory;
}
@Override
public boolean isFile() {
return !isDirectory();
/*
if(mIsFile == null) {
mIsFile = mWrapped.isFile();
}
return mIsFile;
*/
}
@Override
public long lastModified() {
if(mLastModified == null) {
mLastModified = mWrapped.lastModified();
}
return mLastModified;
}
@Override
public long length() {
if(mLength == null) {
mLength = mWrapped.length();
}
return mLength;
}
@Override
public DocumentFile[] listFiles() {
if(mListFiles == null) {
mListFiles = mWrapped.listFiles();
if(mCacheChildren) {
for(int i = 0 ; i < mListFiles.length ; i++) {
mListFiles[i] = new CachedDocumentFile(mListFiles[i]);
}
}
}
return mListFiles;
}
@Override
public boolean renameTo(String arg0) {
if(mWrapped.renameTo(arg0)) {
invalidate();
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment