Skip to content

Commit

Permalink
#169: Fixed caching fo copy/move/delete
Browse files Browse the repository at this point in the history
  • Loading branch information
k3b committed Mar 28, 2021
1 parent fb68b02 commit ef95471
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 61 deletions.
4 changes: 3 additions & 1 deletion app/src/main/java/de/k3b/android/io/AndroidFileCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,9 @@ public int execRename(File srcDirFile, String newFolderName) {
public void onMoveOrCopyDirectoryPick(boolean move, SelectedFiles selectedFiles, IDirectory destFolder) {
if (destFolder != null) {
String copyToPath = destFolder.getAbsolute();
IFile destDirFolder = FileFacade.convert("AndroidFileCommands.onMoveOrCopyDirectoryPick", copyToPath);
IFile destDirFolder = FileFacade
.convert("AndroidFileCommands.onMoveOrCopyDirectoryPick", copyToPath)
.cacheStrategy(IFile.STRATEGY_OUTPUT);

setLastCopyToPath(copyToPath);

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

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

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

@Override
Expand All @@ -111,22 +112,6 @@ private DocumentFile getDocumentFileOrDirOrNull(@NonNull File file) {
return documentFileTranslator.getDocumentFileOrDirOrNull(file, null, this.strategyID);
}

@Override
public boolean renameTo(@NonNull IFile newName) {
if (exists() && !newName.exists()) {
if (getParentFile().equals(newName.getParentFile())) {
// same directory
return renameTo(newName.getName());
}

if (copyImpl((AndroidFileFacade) newName, true)) {
return true;
}
}
Log.e(LOG_TAG, "renameTo " + this + " -> " + newName + " failed");
return false;
}

private boolean copyImpl(@NonNull AndroidFileFacade targetFullPath, boolean deleteSourceWhenSuccess) {
final String dbgContext = "AndroidFileFacade.copyImpl " + this + " -> " + targetFullPath;
try {
Expand All @@ -144,6 +129,7 @@ private boolean copyImpl(@NonNull AndroidFileFacade targetFullPath, boolean dele
@Override
public boolean renameTo(@NonNull String newName) {
if (exists() && getAndroidFile(false).renameTo(newName)) {
invalidateParentDirCache();
return true;
}

Expand All @@ -156,6 +142,7 @@ public boolean delete() {
boolean result = exists() && getAndroidFile(false).delete();

if (result) {
invalidateParentDirCache();
// File (and reference to it) does not exist any more
androidFileMayExist = false;
androidFile = null;
Expand Down Expand Up @@ -212,7 +199,7 @@ public IFile getCanonicalFile() {
public IFile getParentFile() {
final DocumentFile androidFile = getAndroidFile(false);
if (androidFile != null) {
return new AndroidFileFacade(androidFile.getParentFile(), getFile().getParentFile());
return new AndroidFileFacade(androidFile.getParentFile(), getFile().getParentFile(), strategyID);
} else {
return super.getParentFile();
}
Expand Down Expand Up @@ -264,7 +251,7 @@ public boolean mkdirs() {
public IFile[] listFiles() {
final DocumentFile androidFile = getAndroidFile(false);
if (androidFile != null) {
return get(androidFile.listFiles());
return get(androidFile.listFiles(), strategyID);
}
return new IFile[0];
}
Expand All @@ -278,7 +265,7 @@ public IFile[] listDirs() {
if (file != null &&
(file.isDirectory() || accept(file.getName().toLowerCase()))) {
found.add(new AndroidFileFacade(
file, new File(parent, file.getName())));
file, new File(parent, file.getName()), strategyID));
}
}
}
Expand All @@ -302,11 +289,12 @@ public boolean copy(@NonNull IFile targetFullPath, boolean deleteSourceWhenSucce
@Override
public OutputStream openOutputStream() throws FileNotFoundException {
DocumentFile androidFile = getAndroidFile(false);
String context = "openOutputStream overwrite existing ";
String context = strategyID + "openOutputStream overwrite existing ";
if (androidFile == null) {
final DocumentFile documentFileParent = documentFileTranslator.getOrCreateDirectory(getFile().getParentFile(), strategyID);
androidFile = this.androidFile = documentFileParent.createFile(null, getFile().getName());
context = "openOutputStream create new ";
context = strategyID + "openOutputStream create new ";
invalidateParentDirCache();
}
if (FileFacade.debugLogFacade) {
Log.i(LOG_TAG, context + this);
Expand All @@ -316,22 +304,23 @@ public OutputStream openOutputStream() throws FileNotFoundException {

@Override
public InputStream openInputStream() throws FileNotFoundException {
String context = strategyID + "openInputStream ";
if ((readUri != null)) {
if (debugLogFacade) {
Log.i(LOG_TAG, "openInputStream " + this + " for uri " + readUri);
Log.i(LOG_TAG, context + this + " for uri " + readUri);
}
return documentFileTranslator.openInputStream(readUri);
}
final DocumentFile androidFile = getAndroidFile(true);
final InputStream resultInputStream = documentFileTranslator.openInputStream(androidFile);
if (resultInputStream == null) {
final String msg = "openInputStream " + this + " for uri "
final String msg = context + this + " for uri "
+ ((androidFile != null) ? androidFile.getUri() : "null") + " returns null";
Log.w(LOG_TAG, msg);
getAndroidFile(true); // allow debugger to step in
throw new FileNotFoundException(msg);
} else if (debugLogFacade) {
Log.i(LOG_TAG, "openInputStream " + this + " for uri " + androidFile.getUri());
Log.i(LOG_TAG, context + this + " for uri " + androidFile.getUri());
}
return resultInputStream;
}
Expand All @@ -344,12 +333,12 @@ public void invalidateParentDirCache() {
}


private IFile[] get(DocumentFile[] docs) {
private IFile[] get(DocumentFile[] docs, int strategyID) {
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()));
f[i] = new AndroidFileFacade(doc, new File(parent, doc.getName()), strategyID);
}

return f;
Expand Down
5 changes: 4 additions & 1 deletion fotolib2/src/main/java/de/k3b/io/FileCommandLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ public void openLogfile() {
if (mLogFilePath != null) {
OutputStream stream = null;
try {
IFile logFile = FileFacade.convert("FileCommandLogger.openLogfile", mLogFilePath);
IFile logFile = FileFacade
.convert("FileCommandLogger.openLogfile", mLogFilePath)
.cacheStrategy(IFile.STRATEGY_OUTPUT);

if (logFile.exists()) {
// open existing in append mode
long ageInHours = (new Date().getTime() - logFile.lastModified()) / (1000 * 60 * 60);
Expand Down
7 changes: 6 additions & 1 deletion fotolib2/src/main/java/de/k3b/io/FileCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ 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 @@ -297,6 +298,7 @@ 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 @@ -418,6 +420,9 @@ 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 Expand Up @@ -532,7 +537,7 @@ protected boolean osRenameTo(File dest, File source) {
}

protected boolean osRenameTo(IFile dest, IFile source) {
return source.renameTo(dest);
return source.renameTo(dest.getName());
}

/**
Expand Down
4 changes: 0 additions & 4 deletions fotolib2/src/main/java/de/k3b/media/ExifInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -2877,10 +2877,6 @@ protected OutputStream createOutputStream(IFile outFile) throws FileNotFoundExce
return outFile.openOutputStream();
}

protected boolean renameTo(IFile originalInFile, IFile renamedInFile) {
return originalInFile.renameTo(renamedInFile.getName());
}

protected String getAbsolutePath(IFile inFile) {
return inFile.getAbsolutePath();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,24 +135,13 @@ public static void setFileFacade(Converter<File, IFile> fileFacade) {
FileFacade.fileFacade = fileFacade;
}

@Deprecated
@Override
public boolean renameTo(IFile newName) {
return renameImpl(newName.getFile());
}

@Override
public boolean renameTo(String newName) {
File newFile = new File(this.file.getParentFile(), newName);
final boolean result = renameImpl(newFile);
final boolean result = this.file.renameTo(newFile);
return result;
}

private boolean renameImpl(File newFile) {
final boolean success = this.file.renameTo(newFile);
return success;
}

@Override
public boolean delete() {
return file.delete();
Expand Down Expand Up @@ -326,7 +315,7 @@ public long length() {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ public void set(IFile src) {
child.set(src);
}

@Override
public boolean renameTo(IFile newName) {
return child.renameTo(newName);
}

@Override
public boolean renameTo(String newName) {
return child.renameTo(newName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ public interface IFile {

void set(IFile src);

@Deprecated
boolean renameTo(IFile newName);

boolean renameTo(String newName);

boolean delete();
Expand Down Expand Up @@ -104,5 +101,6 @@ public interface IFile {
//------- 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 @@ -84,11 +84,6 @@ public void set(IFile src) {

}

@Override
public boolean renameTo(IFile newName) {
return false;
}

@Override
public boolean renameTo(String newName) {
return false;
Expand Down

0 comments on commit ef95471

Please sign in to comment.