Add separate method for renaming files

This commit is contained in:
Vitaliy 2021-01-11 07:57:16 +02:00
parent 643eaf3293
commit 34837cb3d9
2 changed files with 28 additions and 2 deletions

View file

@ -8,6 +8,8 @@ import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.GPXUtilities.Metadata;
import net.osmand.plus.GpxSelectionHelper; import net.osmand.plus.GpxSelectionHelper;
import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
@ -42,7 +44,7 @@ public class FileUtils {
dest.getParentFile().mkdirs(); dest.getParentFile().mkdirs();
} }
if (source.renameTo(dest)) { if (source.renameTo(dest)) {
final String[] suffixes = new String[]{"-journal", "-wal", "-shm"}; final String[] suffixes = new String[] {"-journal", "-wal", "-shm"};
for (String s : suffixes) { for (String s : suffixes) {
File file = new File(ctx.getDatabasePath(source + s).toString()); File file = new File(ctx.getDatabasePath(source + s).toString());
if (file.exists()) { if (file.exists()) {
@ -76,6 +78,26 @@ public class FileUtils {
return res; return res;
} }
public static File renameFile(@NonNull OsmandApplication app, @NonNull File source,
@NonNull String newName, boolean dirAllowed, RenameCallback callback) {
File dest = checkRenamePossibility(app, source, newName, dirAllowed);
if (dest == null) {
return null;
}
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
File res = source.renameTo(dest) ? dest : null;
if (res != null) {
if (callback != null) {
callback.renamedTo(res);
}
} else {
Toast.makeText(app, R.string.file_can_not_be_renamed, Toast.LENGTH_LONG).show();
}
return res;
}
public static File renameGpxFile(@NonNull OsmandApplication app, @NonNull File src, @NonNull File dest) { public static File renameGpxFile(@NonNull OsmandApplication app, @NonNull File src, @NonNull File dest) {
if (!dest.getParentFile().exists()) { if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs(); dest.getParentFile().mkdirs();

View file

@ -18,6 +18,7 @@ import com.google.android.material.textfield.TextInputLayout;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.FileUtils.RenameCallback; import net.osmand.FileUtils.RenameCallback;
import net.osmand.IndexConstants;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
@ -33,6 +34,7 @@ import org.apache.commons.logging.Log;
import java.io.File; import java.io.File;
import static net.osmand.FileUtils.ILLEGAL_FILE_NAME_CHARACTERS; import static net.osmand.FileUtils.ILLEGAL_FILE_NAME_CHARACTERS;
import static net.osmand.FileUtils.renameFile;
import static net.osmand.FileUtils.renameGpxFile; import static net.osmand.FileUtils.renameGpxFile;
import static net.osmand.FileUtils.renameSQLiteFile; import static net.osmand.FileUtils.renameSQLiteFile;
@ -127,8 +129,10 @@ public class RenameFileBottomSheet extends MenuBottomSheetDialogFragment {
String ext = index == -1 ? "" : file.getName().substring(index); String ext = index == -1 ? "" : file.getName().substring(index);
if (SQLiteTileSource.EXT.equals(ext)) { if (SQLiteTileSource.EXT.equals(ext)) {
dest = renameSQLiteFile(app, file, selectedFileName + ext, null); dest = renameSQLiteFile(app, file, selectedFileName + ext, null);
} else { } else if (IndexConstants.GPX_FILE_EXT.equals(ext)) {
dest = renameGpxFile(app, file, selectedFileName + ext, false, null); dest = renameGpxFile(app, file, selectedFileName + ext, false, null);
} else {
dest = renameFile(app, file, selectedFileName + ext, false, null);
} }
if (dest != null) { if (dest != null) {
Fragment fragment = getTargetFragment(); Fragment fragment = getTargetFragment();