Last active
October 23, 2023 22:26
-
-
Save bubbleguuum/55a6f27d97096583e79248e2e367a032 to your computer and use it in GitHub Desktop.
Improve DocumentFile performance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
But this is which v4 support version