Skip to content

Commit

Permalink
#169: ExifInterface via IFile/FileFacade
Browse files Browse the repository at this point in the history
  • Loading branch information
k3b committed Apr 19, 2020
1 parent 4d59fba commit bb08d55
Show file tree
Hide file tree
Showing 7 changed files with 398 additions and 54 deletions.
42 changes: 40 additions & 2 deletions app/src/main/java/de/k3b/android/io/ExifInterfaceExAndroid.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.OutputStream;

import de.k3b.android.widget.FilePermissionActivity;
import de.k3b.io.IFile;
import de.k3b.media.ExifInterfaceEx;
import de.k3b.media.IPhotoProperties;

Expand Down Expand Up @@ -59,10 +60,47 @@ public static void setContext(FilePermissionActivity activity) {
documentFileTranslator = activity.getDocumentFileTranslator();
}

/**
* @deprecated use {@link #saveAttributes(IFile, IFile, boolean)} instead
*/
@Deprecated
@Override
public void saveAttributes(File inFile, File outFile, boolean deleteInFileOnFinish) throws IOException {
super.saveAttributes(inFile, outFile, deleteInFileOnFinish);
}

public void saveAttributes(IFile inFile, IFile outFile, boolean deleteInFileOnFinish) throws IOException {
throw new RuntimeException("not implemented yet");
// super.saveAttributes(inFile, outFile, deleteInFileOnFinish);
}

/**
* @deprecated use {@link #fixDateTakenIfNeccessary(IFile)} instead
*/
@Deprecated
@Override
protected void fixDateTakenIfNeccessary(File inFile) {
super.fixDateTakenIfNeccessary(inFile);
}

protected void fixDateTakenIfNeccessary(IFile inFile) {
throw new RuntimeException("not implemented yet");
}

/**
* @deprecated use {@link #setFilelastModified(IFile)} instead
*/
@Deprecated
@Override
public void setFilelastModified(File file) {
super.setFilelastModified(file);

}

public void setFilelastModified(IFile file) {
throw new RuntimeException("not implemented yet");
}

//------------- File api to be overwritten for android specific DocumentFile implementation
protected InputStream createInputStream(File exifFile) throws FileNotFoundException {
return documentFileTranslator.openInputStream(exifFile);
Expand All @@ -80,8 +118,8 @@ protected String getAbsolutePath(File inFile) {
return inFile.getAbsolutePath();
}

protected boolean deleteFile(File renamedInFile) {
return getDocumentFileOrDir(renamedInFile, false).delete();
protected boolean deleteFile(File file) {
return getDocumentFileOrDir(file, false).delete();
}

// -----
Expand Down
30 changes: 30 additions & 0 deletions fotolib2/src/main/java/de/k3b/io/Converter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2020 by k3b.
*
* This file is part of #APhotoManager (https://github.com/k3b/APhotoManager/)
* and #toGoZip (https://github.com/k3b/ToGoZip/).
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>
*/
package de.k3b.io;

/**
* converts from type SOURCE to RESULT so that RESULT can be used as a facade for SOURCE
*
* @param <TO>
* @param <FROM>
*/
public interface Converter<FROM, TO> {
public TO convert(FROM from);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package de.k3b.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
Expand All @@ -32,112 +33,152 @@
replacement (aka man-in-the-middle-attack to
add support for Android DocumentFile
*/
public class File {
public final java.io.File file;
public class FileFacade implements IFile {
private final java.io.File file;

public File(java.io.File file) {
public FileFacade(java.io.File file) {
this.file = file;
}

public File(String absolutPath) {
public FileFacade(String absolutPath) {
this(new java.io.File(absolutPath));
}

public File(File parent, String newFolderName) {
public FileFacade(FileFacade parent, String newFolderName) {
this(new java.io.File(parent.file, newFolderName));
}

public File(String parent, String newFolderName) {
public FileFacade(String parent, String newFolderName) {
this(new java.io.File(parent, newFolderName));
}

public static File[] get(java.io.File[] files) {
File f[] = new File[files.length];
public static FileFacade[] get(java.io.File[] files) {
FileFacade f[] = new FileFacade[files.length];
for (int i = 0; i < files.length; i++) {
f[i] = new File(files[i]);
f[i] = new FileFacade(files[i]);
}

return f;
}

public boolean renameTo(File newName) {
return file.renameTo(newName.file);
@Deprecated
@Override
public boolean renameTo(IFile newName) {
return file.renameTo(((FileFacade) newName).file);
}

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

@Override
public boolean delete() {
return file.delete();
}

@Override
public boolean exists() {
return file.exists();
}

@Override
public IFile findExisting(String name) {
final File candidate = new File(this.file, name);
if (candidate.exists()) {
return new FileFacade(candidate);
}
return null;
}

@Override
public boolean canWrite() {
return file.canWrite();
}

@Override
public boolean canRead() {
return canRead();
}

@Override
public boolean isFile() {
return file.isFile();
}

@Override
public boolean isDirectory() {
return file.isDirectory();
}

@Override
public boolean isHidden() {
return file.isHidden();
}

@Override
public boolean isAbsolute() {
return file.isAbsolute();
}

@Override
public String getAbsolutePath() {
return file.getAbsolutePath();
}

public File getCanonicalFile() {
return new File(FileUtils.tryGetCanonicalFile(file));
@Override
public IFile getCanonicalFile() {
return new FileFacade(FileUtils.tryGetCanonicalFile(file));
}

@Override
public String getCanonicalPath() {
return FileUtils.tryGetCanonicalPath(file, null);
}

public File getParentFile() {
return new File(file.getParentFile());
@Override
public IFile getParentFile() {
return new FileFacade(file.getParentFile());
}

@Override
public String getParent() {
return file.getParent();
}

@Override
public String getName() {
return file.getName();
}

@Override
public long lastModified() {
return file.lastModified();
}

@Override
public boolean mkdirs() {
return file.mkdirs();
}

public File[] listFiles() {
@Override
public IFile[] listFiles() {
return get(file.listFiles());
}

public void copy(File targetFullPath, boolean deleteSourceWhenSuccess) throws IOException {
@Override
public void copy(IFile targetFullPath, boolean deleteSourceWhenSuccess) throws IOException {
copyImpl((FileFacade) targetFullPath, deleteSourceWhenSuccess);
}

private void copyImpl(FileFacade targetFullPath, boolean deleteSourceWhenSuccess) throws IOException {
FileChannel in = null;
FileChannel out = null;
try {
in = new FileInputStream(file).getChannel();
out = new FileOutputStream(targetFullPath.file).getChannel();
out = new FileOutputStream((targetFullPath).file).getChannel();
long size = in.size();
MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
out.write(buf);
Expand All @@ -150,12 +191,41 @@ public void copy(File targetFullPath, boolean deleteSourceWhenSuccess) throws IO
}
}

public OutputStream openOutputStream(boolean var2) throws FileNotFoundException {
return new FileOutputStream(file, var2);
@Override
public OutputStream openOutputStream() throws FileNotFoundException {
// create parent dirs if not exist
file.getParentFile().mkdirs();

// replace existing
file.delete();

return new FileOutputStream(file);
}

@Override
public InputStream openInputStream() throws FileNotFoundException {
return new FileInputStream(file);
}

/**
* android DocumentFile specific, not supported in non-android
*/
@Override
public String getMime() {
return null;
}

@Override
public IFile create(String name, String mime) {
return new FileFacade(new File(file, name));
}

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

public File getFile() {
return file;
}
}
Loading

0 comments on commit bb08d55

Please sign in to comment.