Skip to content

Instantly share code, notes, and snippets.

@bubbleguuum
Created April 10, 2019 10:57
Show Gist options
  • Save bubbleguuum/eedb38cd44e0e002e910d1cb08b61c4e to your computer and use it in GitHub Desktop.
Save bubbleguuum/eedb38cd44e0e002e910d1cb08b61c4e to your computer and use it in GitHub Desktop.
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 DocumentFile getWrappedDocumentFile() {
return mWrapped;
}
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;
}
@Override
public boolean isVirtual() {
return mWrapped.isVirtual();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment