refactor LocalIndexesFragment

This commit is contained in:
veliymolfar 2020-05-28 11:43:03 +03:00
parent 2151028d2d
commit f69dc384dc
5 changed files with 161 additions and 137 deletions

View file

@ -0,0 +1,154 @@
package net.osmand;
import android.app.Activity;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.SQLiteTileSource;
import net.osmand.plus.download.ui.LocalIndexesFragment;
import net.osmand.util.Algorithms;
import java.io.File;
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) {
AlertDialog.Builder b = new AlertDialog.Builder(a);
if (f.exists()) {
int xt = f.getName().lastIndexOf('.');
final String ext = xt == -1 ? "" : f.getName().substring(xt);
final String originalName = xt == -1 ? f.getName() : f.getName().substring(0, xt);
final EditText editText = new EditText(a);
editText.setText(originalName);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
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));
}
}
}
});
b.setTitle(R.string.shared_string_rename);
int leftPadding = AndroidUtils.dpToPx(a, 24f);
int topPadding = AndroidUtils.dpToPx(a, 4f);
b.setView(editText, leftPadding, topPadding, leftPadding, topPadding);
// Behaviour will be overwritten later;
b.setPositiveButton(R.string.shared_string_save, null);
b.setNegativeButton(R.string.shared_string_cancel, null);
final AlertDialog alertDialog = b.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
OsmandApplication app = (OsmandApplication) a.getApplication();
if (ext.equals(SQLiteTileSource.EXT)) {
if (renameSQLiteFile(app, f, editText.getText().toString() + ext,
callback) != null) {
alertDialog.dismiss();
}
} else {
if (renameGpxFile(app, f, editText.getText().toString() + ext,
false, callback) != null) {
alertDialog.dismiss();
}
}
}
});
}
});
alertDialog.show();
}
}
public static File renameSQLiteFile(OsmandApplication ctx, File source, String newName,
LocalIndexesFragment.RenameCallback callback) {
File dest = checkRenamePossibility(ctx, source, newName, false);
if (dest == null) {
return null;
}
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
if (source.renameTo(dest)) {
final String[] suffixes = new String[]{"-journal", "-wal", "-shm"};
for (String s : suffixes) {
File file = new File(ctx.getDatabasePath(source + s).toString());
if (file.exists()) {
file.renameTo(ctx.getDatabasePath(dest + s));
}
}
if (callback != null) {
callback.renamedTo(dest);
}
return dest;
} else {
Toast.makeText(ctx, R.string.file_can_not_be_renamed, Toast.LENGTH_LONG).show();
}
return null;
}
public static File renameGpxFile(OsmandApplication ctx, File source, String newName, boolean dirAllowed,
LocalIndexesFragment.RenameCallback callback) {
File dest = checkRenamePossibility(ctx, source, newName, dirAllowed);
if (dest == null) {
return null;
}
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
if (source.renameTo(dest)) {
ctx.getGpxDbHelper().rename(source, dest);
if (callback != null) {
callback.renamedTo(dest);
}
return dest;
} else {
Toast.makeText(ctx, R.string.file_can_not_be_renamed, Toast.LENGTH_LONG).show();
}
return null;
}
public static File checkRenamePossibility(OsmandApplication ctx, File source, String newName, boolean dirAllowed) {
if (Algorithms.isEmpty(newName)) {
Toast.makeText(ctx, R.string.empty_filename, Toast.LENGTH_LONG).show();
return null;
}
Pattern illegalCharactersPattern = dirAllowed ? ILLEGAL_PATH_NAME_CHARACTERS : ILLEGAL_FILE_NAME_CHARACTERS;
if (illegalCharactersPattern.matcher(newName).find()) {
Toast.makeText(ctx, R.string.file_name_containes_illegal_char, Toast.LENGTH_LONG).show();
return null;
}
File dest = new File(source.getParentFile(), newName);
if (dest.exists()) {
Toast.makeText(ctx, R.string.file_with_name_already_exists, Toast.LENGTH_LONG).show();
return null;
}
return dest;
}
}

View file

@ -1,14 +1,11 @@
package net.osmand.plus.download.ui;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.TypedValue;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
@ -21,7 +18,6 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
import android.widget.ImageButton;
@ -80,6 +76,8 @@ 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 {
@ -266,134 +264,6 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement
return true;
}
public static void renameFile(final Activity a, final File f, final RenameCallback callback) {
AlertDialog.Builder b = new AlertDialog.Builder(a);
if (f.exists()) {
int xt = f.getName().lastIndexOf('.');
final String ext = xt == -1 ? "" : f.getName().substring(xt);
final String originalName = xt == -1 ? f.getName() : f.getName().substring(0, xt);
final EditText editText = new EditText(a);
editText.setText(originalName);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
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));
}
}
}
});
b.setTitle(R.string.shared_string_rename);
int leftPadding = AndroidUtils.dpToPx(a, 24f);
int topPadding = AndroidUtils.dpToPx(a, 4f);
b.setView(editText, leftPadding, topPadding, leftPadding, topPadding);
// Behaviour will be overwritten later;
b.setPositiveButton(R.string.shared_string_save, null);
b.setNegativeButton(R.string.shared_string_cancel, null);
final AlertDialog alertDialog = b.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
OsmandApplication app = (OsmandApplication) a.getApplication();
if (ext.equals(SQLiteTileSource.EXT)) {
if (renameSQLiteFile(app, f, editText.getText().toString() + ext,
callback) != null) {
alertDialog.dismiss();
}
} else {
if (renameGpxFile(app, f, editText.getText().toString() + ext,
false, callback) != null) {
alertDialog.dismiss();
}
}
}
});
}
});
alertDialog.show();
}
}
public static File renameSQLiteFile(OsmandApplication ctx, File source, String newName,
RenameCallback callback) {
File dest = checkRenamePossibility(ctx, source, newName, false);
if (dest == null) {
return null;
}
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
if (source.renameTo(dest)) {
final String[] suffixes = new String[]{"-journal", "-wal", "-shm"};
for (String s : suffixes) {
File file = new File(ctx.getDatabasePath(source + s).toString());
if (file.exists()) {
file.renameTo(ctx.getDatabasePath(dest + s));
}
}
if (callback != null) {
callback.renamedTo(dest);
}
return dest;
} else {
Toast.makeText(ctx, R.string.file_can_not_be_renamed, Toast.LENGTH_LONG).show();
}
return null;
}
public static File renameGpxFile(OsmandApplication ctx, File source, String newName, boolean dirAllowed,
RenameCallback callback) {
File dest = checkRenamePossibility(ctx, source, newName, dirAllowed);
if (dest == null) {
return null;
}
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
if (source.renameTo(dest)) {
ctx.getGpxDbHelper().rename(source, dest);
if (callback != null) {
callback.renamedTo(dest);
}
return dest;
} else {
Toast.makeText(ctx, R.string.file_can_not_be_renamed, Toast.LENGTH_LONG).show();
}
return null;
}
public static File checkRenamePossibility(OsmandApplication ctx, File source, String newName, boolean dirAllowed) {
if (Algorithms.isEmpty(newName)) {
Toast.makeText(ctx, R.string.empty_filename, Toast.LENGTH_LONG).show();
return null;
}
Pattern illegalCharactersPattern = dirAllowed ? ILLEGAL_PATH_NAME_CHARACTERS : ILLEGAL_FILE_NAME_CHARACTERS;
if (illegalCharactersPattern.matcher(newName).find()) {
Toast.makeText(ctx, R.string.file_name_containes_illegal_char, Toast.LENGTH_LONG).show();
return null;
}
File dest = new File(source.getParentFile(), newName);
if (dest.exists()) {
Toast.makeText(ctx, R.string.file_with_name_already_exists, Toast.LENGTH_LONG).show();
return null;
}
return dest;
}
@Override
public void onMapSourceUpdated() {
getDownloadActivity().reloadLocalIndexes();

View file

@ -54,7 +54,7 @@ import org.apache.commons.logging.Log;
import java.io.File;
import java.util.List;
import static net.osmand.plus.download.ui.LocalIndexesFragment.renameSQLiteFile;
import static net.osmand.FileUtils.renameSQLiteFile;
public class EditMapSourceDialogFragment extends BaseOsmAndDialogFragment
implements OnZoomSetListener, OnExpireValueSetListener, OnMercatorSelectedListener,

View file

@ -23,6 +23,7 @@ import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import net.osmand.FileUtils;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.WptPt;
import net.osmand.IndexConstants;
@ -32,7 +33,6 @@ import net.osmand.plus.UiUtilities;
import net.osmand.plus.UiUtilities.DialogButtonType;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.base.BottomSheetDialogFragment;
import net.osmand.plus.download.ui.LocalIndexesFragment;
import net.osmand.plus.myplaces.AvailableGPXFragment;
import net.osmand.plus.myplaces.AvailableGPXFragment.GpxInfo;
import net.osmand.plus.views.OsmandMapTileView;
@ -185,7 +185,7 @@ public class OnSaveCurrentTrackFragment extends BottomSheetDialogFragment {
Toast.makeText(app, R.string.empty_filename, Toast.LENGTH_LONG).show();
return null;
}
return LocalIndexesFragment.renameGpxFile(app, savedFile, newGpxName + IndexConstants.GPX_FILE_EXT, true, null);
return FileUtils.renameGpxFile(app, savedFile, newGpxName + IndexConstants.GPX_FILE_EXT, true, null);
}
private void showOnMap(File f, boolean animated) {

View file

@ -75,7 +75,6 @@ import net.osmand.plus.activities.SavingTrackHelper;
import net.osmand.plus.activities.TrackActivity;
import net.osmand.plus.base.OsmandExpandableListFragment;
import net.osmand.plus.dialogs.DirectionsDialogs;
import net.osmand.plus.download.ui.LocalIndexesFragment;
import net.osmand.plus.download.ui.LocalIndexesFragment.RenameCallback;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetType;
@ -101,6 +100,7 @@ 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) {
LocalIndexesFragment.renameFile(getActivity(), gpxInfo.file, new RenameCallback() {
renameFile(getActivity(), gpxInfo.file, new RenameCallback() {
@Override
public void renamedTo(File file) {
asyncLoader = new LoadGpxTask();