refactor renameFile

This commit is contained in:
veliymolfar 2020-05-28 13:14:05 +03:00
parent 88d3dc12ee
commit 0e93d40666
5 changed files with 15 additions and 20 deletions

View file

@ -17,14 +17,16 @@ import net.osmand.plus.download.ui.LocalIndexesFragment;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import java.io.File; import java.io.File;
import java.lang.ref.WeakReference;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static net.osmand.plus.download.ui.LocalIndexesFragment.ILLEGAL_FILE_NAME_CHARACTERS;
import static net.osmand.plus.download.ui.LocalIndexesFragment.ILLEGAL_PATH_NAME_CHARACTERS;
public class FileUtils { public class FileUtils {
public static void renameFile(final Activity a, final File f, final LocalIndexesFragment.RenameCallback callback) { public static final Pattern ILLEGAL_FILE_NAME_CHARACTERS = Pattern.compile("[?:\"*|/<>]");
public static final Pattern ILLEGAL_PATH_NAME_CHARACTERS = Pattern.compile("[?:\"*|<>]");
public static void renameFile(Activity a, final File f, final LocalIndexesFragment.RenameCallback callback) {
final WeakReference<Activity> weakActivity = new WeakReference<>(a);
AlertDialog.Builder b = new AlertDialog.Builder(a); AlertDialog.Builder b = new AlertDialog.Builder(a);
if (f.exists()) { if (f.exists()) {
int xt = f.getName().lastIndexOf('.'); int xt = f.getName().lastIndexOf('.');
@ -46,7 +48,7 @@ public class FileUtils {
Editable text = editText.getText(); Editable text = editText.getText();
if (text.length() >= 1) { if (text.length() >= 1) {
if (ILLEGAL_FILE_NAME_CHARACTERS.matcher(text).find()) { if (ILLEGAL_FILE_NAME_CHARACTERS.matcher(text).find()) {
editText.setError(a.getString(R.string.file_name_containes_illegal_char)); editText.setError(weakActivity.get().getString(R.string.file_name_containes_illegal_char));
} }
} }
} }
@ -66,7 +68,7 @@ public class FileUtils {
new View.OnClickListener() { new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
OsmandApplication app = (OsmandApplication) a.getApplication(); OsmandApplication app = (OsmandApplication) weakActivity.get().getApplication();
if (ext.equals(SQLiteTileSource.EXT)) { if (ext.equals(SQLiteTileSource.EXT)) {
if (renameSQLiteFile(app, f, editText.getText().toString() + ext, if (renameSQLiteFile(app, f, editText.getText().toString() + ext,
callback) != null) { callback) != null) {

View file

@ -35,6 +35,7 @@ import androidx.core.view.MenuItemCompat;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.Collator; import net.osmand.Collator;
import net.osmand.FileUtils;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.OsmAndCollator; import net.osmand.OsmAndCollator;
import net.osmand.map.ITileSource; import net.osmand.map.ITileSource;
@ -74,16 +75,9 @@ import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.regex.Pattern;
import static net.osmand.FileUtils.renameFile;
public class LocalIndexesFragment extends OsmandExpandableListFragment implements DownloadEvents, public class LocalIndexesFragment extends OsmandExpandableListFragment implements DownloadEvents,
OnMapSourceUpdateListener { OnMapSourceUpdateListener {
public static final Pattern ILLEGAL_FILE_NAME_CHARACTERS = Pattern.compile("[?:\"*|/<>]");
public static final Pattern ILLEGAL_PATH_NAME_CHARACTERS = Pattern.compile("[?:\"*|<>]");
private LoadLocalIndexTask asyncLoader; private LoadLocalIndexTask asyncLoader;
private Map<String, IndexItem> filesToUpdate = new HashMap<>(); private Map<String, IndexItem> filesToUpdate = new HashMap<>();
private LocalIndexesAdapter listAdapter; private LocalIndexesAdapter listAdapter;
@ -219,7 +213,7 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement
private boolean performBasicOperation(int resId, final LocalIndexInfo info) { private boolean performBasicOperation(int resId, final LocalIndexInfo info) {
if (resId == R.string.shared_string_rename) { if (resId == R.string.shared_string_rename) {
renameFile(getActivity(), new File(info.getPathToData()), new RenameCallback() { FileUtils.renameFile(getActivity(), new File(info.getPathToData()), new RenameCallback() {
@Override @Override
public void renamedTo(File file) { public void renamedTo(File file) {

View file

@ -34,6 +34,7 @@ import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout; import com.google.android.material.textfield.TextInputLayout;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.FileUtils;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.map.TileSourceManager; import net.osmand.map.TileSourceManager;
@ -56,8 +57,6 @@ import org.apache.commons.logging.Log;
import java.io.File; import java.io.File;
import java.util.List; import java.util.List;
import static net.osmand.FileUtils.renameSQLiteFile;
public class EditMapSourceDialogFragment extends BaseOsmAndDialogFragment public class EditMapSourceDialogFragment extends BaseOsmAndDialogFragment
implements OnZoomSetListener, OnExpireValueSetListener, OnMercatorSelectedListener, implements OnZoomSetListener, OnExpireValueSetListener, OnMercatorSelectedListener,
OnTileStorageFormatSelectedListener { OnTileStorageFormatSelectedListener {
@ -301,7 +300,7 @@ public class EditMapSourceDialogFragment extends BaseOsmAndDialogFragment
String originalName = extIndex == -1 ? f.getName() : f.getName().substring(0, extIndex); String originalName = extIndex == -1 ? f.getName() : f.getName().substring(0, extIndex);
if (!Algorithms.objectEquals(newName, originalName)) { if (!Algorithms.objectEquals(newName, originalName)) {
if (IndexConstants.SQLITE_EXT.equals(ext) && sqliteDB) { if (IndexConstants.SQLITE_EXT.equals(ext) && sqliteDB) {
renameSQLiteFile(app, f, newName, null); FileUtils.renameSQLiteFile(app, f, newName, null);
} else if (!sqliteDB) { } else if (!sqliteDB) {
f.renameTo(app.getAppPath(IndexConstants.TILES_INDEX_DIR + newName)); f.renameTo(app.getAppPath(IndexConstants.TILES_INDEX_DIR + newName));
} }

View file

@ -43,7 +43,7 @@ import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static net.osmand.plus.download.ui.LocalIndexesFragment.ILLEGAL_FILE_NAME_CHARACTERS; import static net.osmand.FileUtils.ILLEGAL_FILE_NAME_CHARACTERS;
public class OnSaveCurrentTrackFragment extends BottomSheetDialogFragment { public class OnSaveCurrentTrackFragment extends BottomSheetDialogFragment {

View file

@ -46,6 +46,7 @@ import androidx.core.content.ContextCompat;
import androidx.core.view.MenuItemCompat; import androidx.core.view.MenuItemCompat;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.FileUtils;
import net.osmand.GPXUtilities; import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.GPXFile;
import net.osmand.GPXUtilities.GPXTrackAnalysis; import net.osmand.GPXUtilities.GPXTrackAnalysis;
@ -100,7 +101,6 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static net.osmand.FileUtils.renameFile;
import static net.osmand.plus.GpxSelectionHelper.CURRENT_TRACK; import static net.osmand.plus.GpxSelectionHelper.CURRENT_TRACK;
import static net.osmand.plus.myplaces.FavoritesActivity.GPX_TAB; import static net.osmand.plus.myplaces.FavoritesActivity.GPX_TAB;
import static net.osmand.plus.myplaces.FavoritesActivity.TAB_ID; import static net.osmand.plus.myplaces.FavoritesActivity.TAB_ID;
@ -1482,7 +1482,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override @Override
public boolean onMenuItemClick(MenuItem item) { public boolean onMenuItemClick(MenuItem item) {
renameFile(getActivity(), gpxInfo.file, new RenameCallback() { FileUtils.renameFile(getActivity(), gpxInfo.file, new RenameCallback() {
@Override @Override
public void renamedTo(File file) { public void renamedTo(File file) {
asyncLoader = new LoadGpxTask(); asyncLoader = new LoadGpxTask();