Skip to content

Commit

Permalink
removed cacheStrategyID
Browse files Browse the repository at this point in the history
  • Loading branch information
k3b committed Apr 26, 2021
1 parent ce82604 commit b5f9c65
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 91 deletions.
3 changes: 1 addition & 2 deletions app/src/main/java/de/k3b/android/io/AndroidFileCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,7 @@ public void onMoveOrCopyDirectoryPick(boolean move, SelectedFiles selectedFiles,
if (destFolder != null) {
String copyToPath = destFolder.getAbsolute();
IFile destDirFolder = FileFacade
.convert("AndroidFileCommands.onMoveOrCopyDirectoryPick", copyToPath)
.cacheStrategy(IFile.STRATEGY_OUTPUT);
.convert("AndroidFileCommands.onMoveOrCopyDirectoryPick", copyToPath);

setLastCopyToPath(copyToPath);

Expand Down
29 changes: 14 additions & 15 deletions app/src/main/java/de/k3b/android/io/AndroidFileFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,13 @@ public static void initFactory(Context context) {
}
}

private AndroidFileFacade(@Nullable DocumentFile parentFile, @NonNull File parentFile1, int strategyID) {
private AndroidFileFacade(@Nullable DocumentFile parentFile, @NonNull File parentFile1) {
super(parentFile1);
androidFile = parentFile;
this.strategyID = strategyID;
}

public AndroidFileFacade(@NonNull File file) {
this(null, file, IFile.STRATEGY_INPUT);
this(null, file);
}

@Override
Expand All @@ -119,7 +118,7 @@ public void set(IFile src) {
}

private DocumentFile getDocumentFileOrDirOrNull(@NonNull File file) {
return documentFileTranslator.getDocumentFileOrDirOrNull(file, null, this.strategyID);
return documentFileTranslator.getDocumentFileOrDirOrNull(file, null);
}

private boolean copyImpl(@NonNull AndroidFileFacade targetFullPath, boolean deleteSourceWhenSuccess) {
Expand Down Expand Up @@ -209,7 +208,7 @@ public IFile getCanonicalFile() {
public IFile getParentFile() {
final DocumentFile androidFile = getAndroidFile(false);
if (androidFile != null) {
return new AndroidFileFacade(androidFile.getParentFile(), getFile().getParentFile(), strategyID);
return new AndroidFileFacade(androidFile.getParentFile(), getFile().getParentFile());
} else {
return super.getParentFile();
}
Expand Down Expand Up @@ -252,7 +251,7 @@ public long lastModified() {

@Override
public boolean mkdirs() {
this.androidFile = documentFileTranslator.getOrCreateDirectory(getFile(), strategyID);
this.androidFile = documentFileTranslator.getOrCreateDirectory(getFile());
invalidateParentDirCache();
return null != this.androidFile;
}
Expand All @@ -261,7 +260,7 @@ public boolean mkdirs() {
public IFile[] listFiles() {
final DocumentFile androidFile = getAndroidFile(false);
if (androidFile != null) {
return get(androidFile.listFiles(), strategyID);
return get(androidFile.listFiles());
}
return new IFile[0];
}
Expand All @@ -275,7 +274,7 @@ public IFile[] listDirs() {
if (file != null &&
(file.isDirectory() || accept(file.getName().toLowerCase()))) {
found.add(new AndroidFileFacade(
file, new File(parent, file.getName()), strategyID));
file, new File(parent, file.getName())));
}
}
}
Expand All @@ -299,11 +298,11 @@ public boolean copy(@NonNull IFile targetFullPath, boolean deleteSourceWhenSucce
@Override
public OutputStream openOutputStream() throws FileNotFoundException {
DocumentFile androidFile = getAndroidFile(false);
String context = strategyID + "openOutputStream overwrite existing ";
String context = "openOutputStream overwrite existing ";
if (androidFile == null) {
final DocumentFile documentFileParent = documentFileTranslator.getOrCreateDirectory(getFile().getParentFile(), strategyID);
final DocumentFile documentFileParent = documentFileTranslator.getOrCreateDirectory(getFile().getParentFile());
androidFile = this.androidFile = documentFileParent.createFile(null, getFile().getName());
context = strategyID + "openOutputStream create new ";
context = "openOutputStream create new ";
invalidateParentDirCache();
}
if (FileFacade.debugLogFacade) {
Expand All @@ -314,7 +313,7 @@ public OutputStream openOutputStream() throws FileNotFoundException {

@Override
public InputStream openInputStream() throws FileNotFoundException {
String context = strategyID + "openInputStream ";
String context = "openInputStream ";
if ((readUri != null)) {
if (debugLogFacade) {
Log.i(LOG_TAG, context + this + " for uri " + readUri);
Expand All @@ -339,16 +338,16 @@ public InputStream openInputStream() throws FileNotFoundException {
@Override
public void invalidateParentDirCache() {
if (documentFileTranslator != null)
documentFileTranslator.documentFileCache.invalidateParentDirCache(strategyID);
documentFileTranslator.documentFileCache.invalidateParentDirCache();
}


private IFile[] get(DocumentFile[] docs, int strategyID) {
private IFile[] get(DocumentFile[] docs) {
AndroidFileFacade[] f = new AndroidFileFacade[docs.length];
final File parent = getFile();
for (int i = 0; i < docs.length; i++) {
final DocumentFile doc = docs[i];
f[i] = new AndroidFileFacade(doc, new File(parent, doc.getName()), strategyID);
f[i] = new AndroidFileFacade(doc, new File(parent, doc.getName()));
}

return f;
Expand Down
32 changes: 9 additions & 23 deletions app/src/main/java/de/k3b/android/io/DocumentFileCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,29 @@
*/
package de.k3b.android.io;

import android.util.Log;

import androidx.documentfile.provider.DocumentFile;

import java.io.File;
import java.util.HashMap;

import de.k3b.android.androFotoFinder.Global;
import de.k3b.io.filefacade.IFile;
import de.k3b.media.PhotoPropertiesUtil;

public class DocumentFileCache {
private static final String TAG = DocumentFileTranslator.TAG;

private CurrentFileCache[] currentFileCaches = null;
private CurrentFileCache currentFileCache = null;

/**
* strategyID : IFile.STRATEGY_XXX.
*/
public CurrentFileCache getCacheStrategy(int strategyID) {
if (currentFileCaches == null) {
currentFileCaches = new CurrentFileCache[IFile.STRATEGY_MAX + 1];
currentFileCaches[IFile.STRATEGY_INPUT] = new CurrentFileCache();
currentFileCaches[IFile.STRATEGY_OUTPUT] = new CurrentFileCache();
currentFileCaches[IFile.STRATEGY_NONE] = null;
}
if (strategyID >= 0 && strategyID < currentFileCaches.length) {
return currentFileCaches[strategyID];
public CurrentFileCache getCacheStrategy() {
if (currentFileCache == null) {
currentFileCache = new CurrentFileCache();
}
Log.w(TAG, "DocumentFileCache.setCacheStrategy(id=" + strategyID +
") unknow Strategy");
return currentFileCaches[IFile.STRATEGY_NONE];
return currentFileCache;
}

public DocumentFile findFile(DocumentFile parentDoc, File parentFile, String displayName, int strategyID) {
public DocumentFile findFile(DocumentFile parentDoc, File parentFile, String displayName) {
if (Global.android_DocumentFile_find_cache) {
CurrentFileCache currentFileCache = getCacheStrategy(strategyID);
CurrentFileCache currentFileCache = getCacheStrategy();

if (currentFileCache != null) {
if (!parentFile.equals(currentFileCache.lastParentFile)) {
Expand All @@ -80,8 +66,8 @@ public DocumentFile findFile(DocumentFile parentDoc, File parentFile, String dis
return parentDoc.findFile(displayName);
}

public void invalidateParentDirCache(int strategyID) {
CurrentFileCache currentFileCache = getCacheStrategy(strategyID);
public void invalidateParentDirCache() {
CurrentFileCache currentFileCache = getCacheStrategy();
if (currentFileCache != null) currentFileCache.lastParentFile = null;
}

Expand Down
25 changes: 12 additions & 13 deletions app/src/main/java/de/k3b/android/io/DocumentFileTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,21 +179,21 @@ public DocumentFileTranslator add(File directory, DocumentFile documentFileDir)
return this;
}

private DocumentFile getDocumentFileOrDirImpl(File fileOrDir, boolean isDir, int strategyID) {
private DocumentFile getDocumentFileOrDirImpl(File fileOrDir, boolean isDir) {
DocumentFile result = null;
if (fileOrDir != null) {
result = getFromCache(fileOrDir);
if (result == null) {
DocumentFile parent = getDocumentFileOrDirImpl(fileOrDir.getParentFile(), true, strategyID);
DocumentFile parent = getDocumentFileOrDirImpl(fileOrDir.getParentFile(), true);
if (parent != null) {
result = findFile(parent, fileOrDir, isDir, strategyID);
result = findFile(parent, fileOrDir, isDir);
}
}
}
return result;
}

private DocumentFile findFile(DocumentFile parentDoc, File fileOrDir, boolean isDir, int strategyID) {
private DocumentFile findFile(DocumentFile parentDoc, File fileOrDir, boolean isDir) {
String displayName = fileOrDir.getName();
File parentFile = fileOrDir.getParentFile();
if (isDir) {
Expand All @@ -212,7 +212,7 @@ private DocumentFile findFile(DocumentFile parentDoc, File fileOrDir, boolean is
}
return foundDoc;
} else {
return documentFileCache.findFile(parentDoc, parentFile, displayName, strategyID);
return documentFileCache.findFile(parentDoc, parentFile, displayName);
}
}

Expand All @@ -221,14 +221,14 @@ private DocumentFile findFile(DocumentFile parentDoc, File fileOrDir, boolean is
*
* @return the found or created directory
*/
public DocumentFile getOrCreateDirectory(File directory, int strategyID) {
public DocumentFile getOrCreateDirectory(File directory) {
DocumentFile result = null;
if (directory != null) {
result = getFromCache(directory);
if (result == null) {
DocumentFile parent = getOrCreateDirectory(directory.getParentFile(), strategyID);
DocumentFile parent = getOrCreateDirectory(directory.getParentFile());
if ((parent != null) && parent.isDirectory()) {
result = findFile(parent, directory, true, strategyID);
result = findFile(parent, directory, true);

if (result == null) {
result = parent.createDirectory(directory.getName());
Expand All @@ -244,18 +244,17 @@ public DocumentFile getOrCreateDirectory(File directory, int strategyID) {
* gets existing DocumentFile that correspondws to fileOrDir
* or null if not exists or no write permissions
*
* @param fileOrDir where DocumentFile is searched for
* @param isDir if null: return null if isDir is matchning
* @param strategyID
* @param fileOrDir where DocumentFile is searched for
* @param isDir if null: return null if isDir is matchning
* @return DocumentFile or null
*/
public DocumentFile getDocumentFileOrDirOrNull(File fileOrDir, Boolean isDir, int strategyID) {
public DocumentFile getDocumentFileOrDirOrNull(File fileOrDir, Boolean isDir) {
DocumentFile result = null;
String path = fileOrDir != null ? fileOrDir.getAbsolutePath() : "";
final String context = FileFacade.debugLogFacade ? (mDebugPrefix + "getDocumentFile('"
+ path + "') ") : null;
try {
result = getDocumentFileOrDirImpl(fileOrDir, isDir == Boolean.TRUE, strategyID);
result = getDocumentFileOrDirImpl(fileOrDir, isDir == Boolean.TRUE);
if ((context != null) && (result == null)) {
Log.i(TAG, context + "not found");
}
Expand Down
3 changes: 1 addition & 2 deletions fotolib2/src/main/java/de/k3b/io/FileCommandLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public void openLogfile() {
OutputStream stream = null;
try {
IFile logFile = FileFacade
.convert("FileCommandLogger.openLogfile", mLogFilePath)
.cacheStrategy(IFile.STRATEGY_OUTPUT);
.convert("FileCommandLogger.openLogfile", mLogFilePath);

if (logFile.exists()) {
// open existing in append mode
Expand Down
5 changes: 0 additions & 5 deletions fotolib2/src/main/java/de/k3b/io/FileCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ int moveOrCopyFilesTo(boolean move,
IFile destDirFolder, IProgessListener progessListener) {
int result = 0;
if (canProcessFile(move ? OP_MOVE : OP_COPY)) {
if (destDirFolder != null) destDirFolder.cacheStrategy(IFile.STRATEGY_OUTPUT);
if (osCreateDirIfNeccessary(destDirFolder)) {
IFile[] destFiles = createDestFiles(renameProcessor, destDirFolder, selectedFiles.getDatesPhotoTaken(), selectedFiles.getIFiles());

Expand Down Expand Up @@ -298,7 +297,6 @@ private IFile[] createDestFiles(IFileNameProcessor renameProcessor, IFile destDi
} else {
destFile = destDirFolder.create(srcFile.getName());
}
destFile.cacheStrategy(IFile.STRATEGY_OUTPUT);
result[pos++] = destFile;
}

Expand Down Expand Up @@ -420,9 +418,6 @@ protected int moveOrCopyFiles(final boolean move, String what, PhotoPropertiesDi
// destFile might have renamed it-s extension for private images
destFile = FileFacade.convert(mDebugPrefix + "moveOrCopyFiles dest", modifiedOutPath);
sameFile = (sourceFile != null) && sourceFile.equals(destFile);
if (!sameFile && destFile != null) {
destFile.cacheStrategy(IFile.STRATEGY_OUTPUT);
}
}

addProcessedFiles(move, destFile, sourceFile);
Expand Down
6 changes: 2 additions & 4 deletions fotolib2/src/main/java/de/k3b/media/ExifInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -1416,10 +1416,8 @@ public void saveAttributes(IFile inFile, IFile outFile,
if (overwriteOriginal) {
final String name = inFile.getName();
final String tempName = name + TMP_FILE_SUFFIX;
inFile = renameSouraceFileBeforeReplaceOrThrow(inFile.cacheStrategy(IFile.STRATEGY_NONE), tempName).cacheStrategy(IFile.STRATEGY_NONE);
currentOutFile = outFile.getParentFile().create(name).cacheStrategy(IFile.STRATEGY_NONE);
} else {
currentOutFile.cacheStrategy(IFile.STRATEGY_OUTPUT);
inFile = renameSouraceFileBeforeReplaceOrThrow(inFile, tempName);
currentOutFile = outFile.getParentFile().create(name);
}

saveJpegAttributes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ public IFile convert(String dbgContext, File file) {

private File file;

protected int strategyID = 0;

private static final List<String> allowedFileSuffixesLowercase = new ArrayList<>();

static {
Expand Down Expand Up @@ -315,7 +313,7 @@ public long length() {

@Override
public String toString() {
return String.format("%s: %d-%s", this.getClass().getSimpleName(), strategyID, file.getAbsoluteFile());
return String.format("%s: %s", this.getClass().getSimpleName(), file.getAbsoluteFile());
}

@Override
Expand All @@ -331,12 +329,6 @@ protected void setFile(File file) {
}


@Override
public IFile cacheStrategy(int strategyID) {
this.strategyID = strategyID;
return this;
}

@Override
public void invalidateParentDirCache() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,6 @@ public long length() {
return child.length();
}

@Override
public IFile cacheStrategy(int strategyID) {
child.cacheStrategy(strategyID);
return this;
}

@Override
public void invalidateParentDirCache() {
child.invalidateParentDirCache();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@
* This interface has the similar method names/sinatures as de.k3b.io.File
*/
public interface IFile {
int STRATEGY_INPUT = 0;
int STRATEGY_OUTPUT = 1;
int STRATEGY_NONE = 2;
int STRATEGY_MAX = STRATEGY_NONE;

void set(IFile src);

boolean renameTo(String newName);
Expand Down Expand Up @@ -99,8 +94,6 @@ public interface IFile {
long length();

//------- file cache support
IFile cacheStrategy(int strategyID);

// may be called after delete, renameTo, openOutputStream, mkdirs
void invalidateParentDirCache();
}
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,6 @@ public long length() {
return 0;
}

@Override
public IFile cacheStrategy(int strategyID) {
return this;
}

@Override
public void invalidateParentDirCache() {

Expand Down

0 comments on commit b5f9c65

Please sign in to comment.