Skip to content

Commit

Permalink
#169: get document uri from file infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
k3b committed Apr 3, 2020
1 parent 48075d7 commit dc235ef
Show file tree
Hide file tree
Showing 16 changed files with 592 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import de.k3b.android.widget.ActivityWithCallContext;
import de.k3b.android.widget.LocalizedActivity;
import de.k3b.database.QueryParameter;
import de.k3b.io.FileApi;
import de.k3b.io.PhotoAutoprocessingDto;
import de.k3b.media.ExifInterface;
import de.k3b.media.PhotoPropertiesImageReader;
Expand Down Expand Up @@ -169,7 +170,7 @@ public static RefWatcher getRefWatcher(Context context) {
LibGlobal.LOG_TAG, ThumbNailUtils.LOG_TAG, IMapView.LOGTAG,
ExifInterface.LOG_TAG, PhotoPropertiesImageReader.LOG_TAG,
FotoSql.LOG_TAG,
MediaDBRepository.LOG_TAG,
MediaDBRepository.LOG_TAG, FileApi.TAG,
MediaContentproviderRepositoryImpl.LOG_TAG) {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1457,7 +1457,7 @@ public void onLoadFinished(Loader<Cursor> _loader, Cursor data) {
title = context.getString(R.string.global_err_system);
context.finish();
}
Dialogs.messagebox(context, title, message);
Dialogs.messagebox(context, title, message, null);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import de.k3b.android.androFotoFinder.tagDB.TagSql;
import de.k3b.android.androFotoFinder.tagDB.TagTask;
import de.k3b.android.androFotoFinder.tagDB.TagsPickerFragment;
import de.k3b.android.util.AndroidFileApi;
import de.k3b.android.util.AndroidFileCommands;
import de.k3b.android.util.FileManagerUtil;
import de.k3b.android.util.IntentUtil;
Expand Down Expand Up @@ -663,43 +664,79 @@ private boolean checkForIncompleteMediaDatabase(String jpgFullFilePath, String w
return false;
}

private void onRenameAnswer(SelectedFiles currentFoto, final long fotoId, final String fotoSourcePath, String newFileName) {
private void onRenameFileAnswer(final SelectedFiles currentFoto, final long fotoId,
final String fotoSourcePath, final String newFileName) {
File src = new File(fotoSourcePath);
File dest = new File(src.getParentFile(), newFileName);
try {
File dest = new File(src.getParentFile(), newFileName);

File srcXmpShort = FileProcessor.getSidecar(src, false);
boolean hasSideCarShort = ((srcXmpShort != null) && (mFileCommands.osFileExists(srcXmpShort)));
File srcXmpLong = FileProcessor.getSidecar(src, true);
boolean hasSideCarLong = ((srcXmpLong != null) && (mFileCommands.osFileExists(srcXmpLong)));
File srcXmpShort = FileProcessor.getSidecar(src, false);
boolean hasSideCarShort = ((srcXmpShort != null) && (mFileCommands.osFileExists(srcXmpShort)));
File srcXmpLong = FileProcessor.getSidecar(src, true);
boolean hasSideCarLong = ((srcXmpLong != null) && (mFileCommands.osFileExists(srcXmpLong)));

File destXmpShort = FileProcessor.getSidecar(dest, false);
File destXmpLong = FileProcessor.getSidecar(dest, true);
File destXmpShort = FileProcessor.getSidecar(dest, false);
File destXmpLong = FileProcessor.getSidecar(dest, true);

if (src.equals(dest)) return; // new name == old name ==> nothing to do
if (src.equals(dest)) return; // new name == old name ==> nothing to do

String errorMessage = null;
if (hasSideCarShort && mFileCommands.osFileExists(destXmpShort)) {
errorMessage = getString(R.string.image_err_file_exists_format, destXmpShort.getAbsoluteFile());
}
if (hasSideCarLong && mFileCommands.osFileExists(destXmpLong)) {
errorMessage = getString(R.string.image_err_file_exists_format, destXmpLong.getAbsoluteFile());
}
if (mFileCommands.osFileExists(dest)) {
errorMessage = getString(R.string.image_err_file_exists_format, dest.getAbsoluteFile());
String errorMessage = null;
if (hasSideCarShort && mFileCommands.osFileExists(destXmpShort)) {
errorMessage = getString(R.string.image_err_file_exists_format, destXmpShort.getAbsoluteFile());
}
if (hasSideCarLong && mFileCommands.osFileExists(destXmpLong)) {
errorMessage = getString(R.string.image_err_file_exists_format, destXmpLong.getAbsoluteFile());
}
if (mFileCommands.osFileExists(dest)) {
errorMessage = getString(R.string.image_err_file_exists_format, dest.getAbsoluteFile());
}

PhotoChangeNotifyer.setPhotoChangedListener(this);
if (errorMessage != null) {
// dest-file already exists
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
onRenameFileQueston(currentFoto, fotoId, fotoSourcePath, newFileName);
} else if (osRenameTo(dest, currentFoto)) {
mModifyCount++;
} else {
// rename failed
errorMessage = getString(R.string.image_err_file_rename_format, src.getAbsoluteFile());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
} catch (AndroidFileApi.PermissionException ex) {
//!!! TODO
/*
requestWritePermission(src, new IOnDirectoryPermissionGrantedHandler() {
@Override
public void afterGrant(FilePermissionActivity activity) {
((ImageDetailActivityViewPager) activity).onRenameFileAnswer(currentFoto, fotoId, fotoSourcePath, newFileName);
}
});
*/
}
}

PhotoChangeNotifyer.setPhotoChangedListener(this);
if (errorMessage != null) {
// dest-file already exists
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
onRenameQueston(currentFoto, fotoId, fotoSourcePath, newFileName);
} else if (mFileCommands.rename(currentFoto, dest, null)) {
mModifyCount++;
} else {
// rename failed
errorMessage = getString(R.string.image_err_file_rename_format, src.getAbsoluteFile());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
private boolean osRenameTo(final File dest, final SelectedFiles currentFoto) {
boolean done = mFileCommands.rename(currentFoto, dest, null);
/*
if (!done) {
File oldFile = currentFoto.getFile(0);
DocumentFile[] dirs = requestDocumentDirWithPermissionsOrNull(
new IOnDirectoryPermissionGrantedHandler() {
@Override
public void afterGrant(FilePermissionActivity activity) {
((ImageDetailActivityViewPager) activity).osRenameTo(dest, currentFoto);
}
}, oldFile);
if (dirs != null) {
done = dirs[0].renameTo(dest.getName());
return true;
!!!
}
}
*/
return done;
}

@Override
Expand Down Expand Up @@ -763,7 +800,7 @@ public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}

private boolean onRenameQueston(final SelectedFiles currentFoto, final long fotoId, final String fotoPath, final String _newName) {
private boolean onRenameFileQueston(final SelectedFiles currentFoto, final long fotoId, final String fotoPath, final String _newName) {
if (AndroidFileCommands.canProcessFile(this, false)) {
final String newName = (_newName == null)
? new File(getCurrentFilePath()).getName()
Expand All @@ -774,7 +811,7 @@ private boolean onRenameQueston(final SelectedFiles currentFoto, final long foto
@Override
protected void onDialogResult(String newFileName, Object... parameters) {
if (newFileName != null) {
onRenameAnswer(currentFoto, (Long) parameters[0], (String) parameters[1], newFileName);
onRenameFileAnswer(currentFoto, (Long) parameters[0], (String) parameters[1], newFileName);
}
}
};
Expand Down Expand Up @@ -903,7 +940,7 @@ public boolean onOptionsItemSelected(MenuItem menuItem) {
break;
case R.id.menu_item_rename:
PhotoChangeNotifyer.setPhotoChangedListener(this);
result = onRenameQueston(getCurrentFoto(), getCurrentImageId(), getCurrentFilePath(), null);
result = onRenameFileQueston(getCurrentFoto(), getCurrentImageId(), getCurrentFilePath(), null);
break;
case R.id.menu_exif:
result = onEditExif(menuItem, getCurrentFoto(), getCurrentImageId(), getCurrentFilePath());
Expand Down Expand Up @@ -1036,6 +1073,7 @@ private void reloadIfDataHasChanged() {
}
}

//TODO implementation not completed yet
private boolean onRenameDirQueston(final SelectedFiles currentFoto, final long fotoId, final String fotoPath, final String _newName) {
if (AndroidFileCommands.canProcessFile(this, false)) {
final String newName = (_newName == null)
Expand Down Expand Up @@ -1085,7 +1123,7 @@ private void onRenameSubDirAnswer(SelectedFiles currentFoto, final long fotoId,
// dest-file already exists
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
onRenameDirQueston(currentFoto, fotoId, fotoSourcePath, newFileName);
} else if (mFileCommands.rename(currentFoto, dest, null)) {
} else if (osRenameTo(dest, currentFoto)) {
mModifyCount++;
} else {
// rename failed
Expand Down
70 changes: 70 additions & 0 deletions app/src/main/java/de/k3b/android/util/AndroidFileApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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.android.util;

import android.support.v4.provider.DocumentFile;
import android.util.Log;

import java.io.File;

import de.k3b.android.androFotoFinder.Global;
import de.k3b.io.FileApi;

/**
* File api abstraction based on android dependand {@link android.support.v4.provider.DocumentFile} API
*/
public class AndroidFileApi extends FileApi {
protected boolean osRenameTo(File dest, File source) throws PermissionException {
final String context = this.getClass().getSimpleName() +
".osRenameTo(" + dest + " <== " + source + ")";
if ((source != null) && (dest != null)) {
if (dest.getParentFile().equals(source.getParentFile())) {
Boolean result = null;
try {
final DocumentFile documentFile = DocumentFile.fromFile(source);
if (!documentFile.canWrite()) throw new PermissionException(source, context);
result = documentFile.renameTo(dest.getName());
return result;
} finally {
if (Global.debugEnabled) {
Log.d(TAG, context + " ==> " + result);
}
}
}
}

Log.w(TAG, context + " move between different directories is not implemented yet");
return super.osRenameTo(dest, source);
}

public static class PermissionException extends RuntimeException {
private final File sourceFile;

public PermissionException(File source, String errorMessage) {
super(errorMessage);
this.sourceFile = source;
}

public File getSourceFile() {
return sourceFile;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public class AndroidFileCommands extends FileCommands {

public AndroidFileCommands() {
// setLogFilePath(getDefaultLogFile());
super(new AndroidFileApi());
setContext(null);
}

Expand Down Expand Up @@ -218,14 +219,14 @@ public int execRename(File srcDirFile, String newFolderName) {
if (destDirFile != null) {
destDirFile.getParentFile().mkdirs();
boolean isDir = srcDirFile.isDirectory();
if (srcDirFile.renameTo(destDirFile)) {
if (osRenameTo(srcDirFile, destDirFile)) {
if (isDir) {
modifyCount = FotoSql.execRenameFolder(srcDirFile.getAbsolutePath() + "/", destDirFile.getAbsolutePath() + "/");
} else {
modifyCount = FotoSql.execRename(srcDirFile.getAbsolutePath(), destDirFile.getAbsolutePath());
}
if (modifyCount < 0) {
destDirFile.renameTo(srcDirFile); // error: undo change
osRenameTo(destDirFile, srcDirFile); // error: undo change
return -1;
} else {
long now = new Date().getTime();
Expand Down
Loading

0 comments on commit dc235ef

Please sign in to comment.