Skip to content

Commit

Permalink
#169: FileCache automatically Disabled, if there are saf-Write operat…
Browse files Browse the repository at this point in the history
…ions; Enabled on listFiles or listDirs
  • Loading branch information
k3b committed Apr 26, 2021
1 parent b5f9c65 commit 87a2f9f
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 11 deletions.
6 changes: 4 additions & 2 deletions app/src/main/java/de/k3b/android/androFotoFinder/Global.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,10 @@ public static class Media {
// Build.VERSION_CODES.??ANDROID10?? = 29
public static boolean useAo10MediaImageDbReplacement = isAndroid10OrAbove();

// #169 Android SAF DocumentFile performance improvements fast but disabled because it is still error-prone
public final static boolean android_DocumentFile_find_cache = true;
// #169 Android SAF DocumentFile performance improvements fast find.
// automatically Disabled, if there are saf-Write operations
// automatically Enabled, if there are listFiles or listDirs
public static boolean android_DocumentFile_find_cache = true;

/**
* map with blue selection markers: how much to area to increase
Expand Down
23 changes: 19 additions & 4 deletions app/src/main/java/de/k3b/android/io/AndroidFileFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.ArrayList;
import java.util.List;

import de.k3b.android.androFotoFinder.Global;
import de.k3b.android.widget.FilePermissionActivity;
import de.k3b.io.Converter;
import de.k3b.io.FileUtils;
Expand Down Expand Up @@ -258,14 +259,23 @@ public boolean mkdirs() {

@Override
public IFile[] listFiles() {
reEnableCache();
final DocumentFile androidFile = getAndroidFile(false);
if (androidFile != null) {
return get(androidFile.listFiles());
}
return new IFile[0];
}

private void reEnableCache() {
if (!Global.android_DocumentFile_find_cache) {
Global.android_DocumentFile_find_cache = true;
invalidateParentDirCache();
}
}

public IFile[] listDirs() {
reEnableCache();
List<IFile> found = new ArrayList<>();
final DocumentFile androidFile = getAndroidFile(false);
if (androidFile != null) {
Expand Down Expand Up @@ -303,12 +313,15 @@ public OutputStream openOutputStream() throws FileNotFoundException {
final DocumentFile documentFileParent = documentFileTranslator.getOrCreateDirectory(getFile().getParentFile());
androidFile = this.androidFile = documentFileParent.createFile(null, getFile().getName());
context = "openOutputStream create new ";
invalidateParentDirCache();

}
if (FileFacade.debugLogFacade) {
Log.i(LOG_TAG, context + this);
}
return documentFileTranslator.createOutputStream(androidFile);
OutputStream result = documentFileTranslator.createOutputStream(androidFile);
Global.android_DocumentFile_find_cache = false;
invalidateParentDirCache();
return result;
}

@Override
Expand Down Expand Up @@ -336,9 +349,11 @@ public InputStream openInputStream() throws FileNotFoundException {

//------- file cache support for android
@Override
public void invalidateParentDirCache() {
if (documentFileTranslator != null)
public IFile invalidateParentDirCache() {
if (documentFileTranslator != null) {
documentFileTranslator.documentFileCache.invalidateParentDirCache();
}
return this;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.HashMap;
import java.util.Map;

import de.k3b.android.androFotoFinder.Global;
import de.k3b.io.filefacade.FileFacade;

/**
Expand Down Expand Up @@ -231,6 +232,7 @@ public DocumentFile getOrCreateDirectory(File directory) {
result = findFile(parent, directory, true);

if (result == null) {
Global.android_DocumentFile_find_cache = false;
result = parent.createDirectory(directory.getName());
add(directory, result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ protected void setFile(File file) {


@Override
public void invalidateParentDirCache() {
public IFile invalidateParentDirCache() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,9 @@ public long length() {
}

@Override
public void invalidateParentDirCache() {
public IFile invalidateParentDirCache() {
child.invalidateParentDirCache();
return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,5 @@ public interface IFile {

//------- file cache support
// may be called after delete, renameTo, openOutputStream, mkdirs
void invalidateParentDirCache();
IFile invalidateParentDirCache();
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public long length() {
}

@Override
public void invalidateParentDirCache() {

public IFile invalidateParentDirCache() {
return this;
}
}

0 comments on commit 87a2f9f

Please sign in to comment.