diff --git a/OsmAnd/src/net/osmand/FileUtils.java b/OsmAnd/src/net/osmand/FileUtils.java index 3fd823d22b..412955fa5f 100644 --- a/OsmAnd/src/net/osmand/FileUtils.java +++ b/OsmAnd/src/net/osmand/FileUtils.java @@ -17,14 +17,16 @@ import net.osmand.plus.download.ui.LocalIndexesFragment; import net.osmand.util.Algorithms; import java.io.File; +import java.lang.ref.WeakReference; 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 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 weakActivity = new WeakReference<>(a); AlertDialog.Builder b = new AlertDialog.Builder(a); if (f.exists()) { int xt = f.getName().lastIndexOf('.'); @@ -46,7 +48,7 @@ public class FileUtils { Editable text = editText.getText(); if (text.length() >= 1) { 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() { @Override public void onClick(View v) { - OsmandApplication app = (OsmandApplication) a.getApplication(); + OsmandApplication app = (OsmandApplication) weakActivity.get().getApplication(); if (ext.equals(SQLiteTileSource.EXT)) { if (renameSQLiteFile(app, f, editText.getText().toString() + ext, callback) != null) { diff --git a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java index 6843e99a7c..08a7fb3724 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java @@ -35,6 +35,7 @@ import androidx.core.view.MenuItemCompat; import net.osmand.AndroidUtils; import net.osmand.Collator; +import net.osmand.FileUtils; import net.osmand.IndexConstants; import net.osmand.OsmAndCollator; import net.osmand.map.ITileSource; @@ -74,16 +75,9 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.regex.Pattern; - -import static net.osmand.FileUtils.renameFile; - public class LocalIndexesFragment extends OsmandExpandableListFragment implements DownloadEvents, 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 Map filesToUpdate = new HashMap<>(); private LocalIndexesAdapter listAdapter; @@ -219,7 +213,7 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement private boolean performBasicOperation(int resId, final LocalIndexInfo info) { 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 public void renamedTo(File file) { diff --git a/OsmAnd/src/net/osmand/plus/mapsource/EditMapSourceDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapsource/EditMapSourceDialogFragment.java index e3e2afd0a7..8b80435fdd 100644 --- a/OsmAnd/src/net/osmand/plus/mapsource/EditMapSourceDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapsource/EditMapSourceDialogFragment.java @@ -34,6 +34,7 @@ import com.google.android.material.textfield.TextInputEditText; import com.google.android.material.textfield.TextInputLayout; import net.osmand.AndroidUtils; +import net.osmand.FileUtils; import net.osmand.IndexConstants; import net.osmand.PlatformUtil; import net.osmand.map.TileSourceManager; @@ -56,8 +57,6 @@ import org.apache.commons.logging.Log; import java.io.File; import java.util.List; -import static net.osmand.FileUtils.renameSQLiteFile; - public class EditMapSourceDialogFragment extends BaseOsmAndDialogFragment implements OnZoomSetListener, OnExpireValueSetListener, OnMercatorSelectedListener, OnTileStorageFormatSelectedListener { @@ -301,7 +300,7 @@ public class EditMapSourceDialogFragment extends BaseOsmAndDialogFragment String originalName = extIndex == -1 ? f.getName() : f.getName().substring(0, extIndex); if (!Algorithms.objectEquals(newName, originalName)) { if (IndexConstants.SQLITE_EXT.equals(ext) && sqliteDB) { - renameSQLiteFile(app, f, newName, null); + FileUtils.renameSQLiteFile(app, f, newName, null); } else if (!sqliteDB) { f.renameTo(app.getAppPath(IndexConstants.TILES_INDEX_DIR + newName)); } diff --git a/OsmAnd/src/net/osmand/plus/monitoring/OnSaveCurrentTrackFragment.java b/OsmAnd/src/net/osmand/plus/monitoring/OnSaveCurrentTrackFragment.java index 4dc1e39f5c..77be5ca614 100644 --- a/OsmAnd/src/net/osmand/plus/monitoring/OnSaveCurrentTrackFragment.java +++ b/OsmAnd/src/net/osmand/plus/monitoring/OnSaveCurrentTrackFragment.java @@ -43,7 +43,7 @@ import java.io.File; import java.util.ArrayList; 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 { diff --git a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java index fe2fd9a71f..eab365418d 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java @@ -46,6 +46,7 @@ import androidx.core.content.ContextCompat; import androidx.core.view.MenuItemCompat; import net.osmand.AndroidUtils; +import net.osmand.FileUtils; import net.osmand.GPXUtilities; import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.GPXTrackAnalysis; @@ -100,7 +101,6 @@ import java.util.Map; import java.util.Set; import java.util.regex.Pattern; -import static net.osmand.FileUtils.renameFile; import static net.osmand.plus.GpxSelectionHelper.CURRENT_TRACK; import static net.osmand.plus.myplaces.FavoritesActivity.GPX_TAB; import static net.osmand.plus.myplaces.FavoritesActivity.TAB_ID; @@ -1482,7 +1482,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { - renameFile(getActivity(), gpxInfo.file, new RenameCallback() { + FileUtils.renameFile(getActivity(), gpxInfo.file, new RenameCallback() { @Override public void renamedTo(File file) { asyncLoader = new LoadGpxTask();