GPX track moving action fix

IMHO, there is no sense in checking possible destinations list for
emptiness as soon as we always hav an option to create new one just in
this dialog. In any case an item that in some obscure circumstances
simply does nothing looks quirky.
This commit is contained in:
Igor B. Poretsky 2017-05-24 00:31:06 +03:00
parent 9688ef9fb3
commit 254b5aad14

View file

@ -687,125 +687,123 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment {
final List<File> dirs = new ArrayList<>(); final List<File> dirs = new ArrayList<>();
collectDirs(app.getAppPath(IndexConstants.GPX_INDEX_DIR), dirs, info.file.getParentFile()); collectDirs(app.getAppPath(IndexConstants.GPX_INDEX_DIR), dirs, info.file.getParentFile());
if (!dirs.isEmpty()) { if (!info.file.getParentFile().equals(app.getAppPath(IndexConstants.GPX_INDEX_DIR))) {
if (!info.file.getParentFile().equals(app.getAppPath(IndexConstants.GPX_INDEX_DIR))) { dirs.add(0, app.getAppPath(IndexConstants.GPX_INDEX_DIR));
dirs.add(0, app.getAppPath(IndexConstants.GPX_INDEX_DIR)); }
} String gpxDir = app.getAppPath(IndexConstants.GPX_INDEX_DIR).getPath();
String gpxDir = app.getAppPath(IndexConstants.GPX_INDEX_DIR).getPath(); int i = 0;
int i = 0; for (File dir : dirs) {
for (File dir : dirs) { String dirName = dir.getPath();
String dirName = dir.getPath(); if (dirName.startsWith(gpxDir)) {
if (dirName.startsWith(gpxDir)) { if (dirName.length() == gpxDir.length()) {
if (dirName.length() == gpxDir.length()) { dirName = dir.getName();
dirName = dir.getName(); } else {
} else { dirName = dirName.substring(gpxDir.length() + 1);
dirName = dirName.substring(gpxDir.length() + 1);
}
} }
menuAdapter.addItem(itemBuilder.setTitle(Algorithms.capitalizeFirstLetter(dirName))
.setIcon(R.drawable.ic_action_folder_stroke).setTag(i).createItem());
i++;
} }
menuAdapter.addItem(itemBuilder.setTitleId(R.string.add_new_folder, app) menuAdapter.addItem(itemBuilder.setTitle(Algorithms.capitalizeFirstLetter(dirName))
.setIcon(R.drawable.map_zoom_in).setTag(-1).createItem()); .setIcon(R.drawable.ic_action_folder_stroke).setTag(i).createItem());
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); i++;
final ArrayAdapter<ContextMenuItem> listAdapter = }
menuAdapter.createListAdapter(getActivity(), app.getSettings().isLightContent()); menuAdapter.addItem(itemBuilder.setTitleId(R.string.add_new_folder, app)
builder.setTitle(R.string.select_gpx_folder); .setIcon(R.drawable.map_zoom_in).setTag(-1).createItem());
builder.setAdapter(listAdapter, new DialogInterface.OnClickListener() { final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final ArrayAdapter<ContextMenuItem> listAdapter =
menuAdapter.createListAdapter(getActivity(), app.getSettings().isLightContent());
builder.setTitle(R.string.select_gpx_folder);
builder.setAdapter(listAdapter, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
ContextMenuItem item = menuAdapter.getItem(which); ContextMenuItem item = menuAdapter.getItem(which);
int index = item.getTag(); int index = item.getTag();
if (index == -1) { if (index == -1) {
Activity a = getActivity(); Activity a = getActivity();
AlertDialog.Builder b = new AlertDialog.Builder(a); AlertDialog.Builder b = new AlertDialog.Builder(a);
b.setTitle(R.string.add_new_folder); b.setTitle(R.string.add_new_folder);
final EditText editText = new EditText(a); final EditText editText = new EditText(a);
editText.addTextChangedListener(new TextWatcher() { editText.addTextChangedListener(new TextWatcher() {
@Override @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { public void beforeTextChanged(CharSequence s, int start, int count, int after) {
} }
@Override @Override
public void onTextChanged(CharSequence s, int start, int before, int count) { public void onTextChanged(CharSequence s, int start, int before, int count) {
} }
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
Editable text = editText.getText(); Editable text = editText.getText();
if (text.length() >= 1) { if (text.length() >= 1) {
if (ILLEGAL_PATH_NAME_CHARACTERS.matcher(text).find()) { if (ILLEGAL_PATH_NAME_CHARACTERS.matcher(text).find()) {
editText.setError(app.getString(R.string.file_name_containes_illegal_char)); editText.setError(app.getString(R.string.file_name_containes_illegal_char));
}
} }
} }
}); }
int leftPadding = AndroidUtils.dpToPx(a, 24f); });
int topPadding = AndroidUtils.dpToPx(a, 4f); int leftPadding = AndroidUtils.dpToPx(a, 24f);
b.setView(editText, leftPadding, topPadding, leftPadding, topPadding); int topPadding = AndroidUtils.dpToPx(a, 4f);
// Behaviour will be overwritten later; b.setView(editText, leftPadding, topPadding, leftPadding, topPadding);
b.setPositiveButton(R.string.shared_string_ok, null); // Behaviour will be overwritten later;
b.setNegativeButton(R.string.shared_string_cancel, null); b.setPositiveButton(R.string.shared_string_ok, null);
final AlertDialog alertDialog = b.create(); b.setNegativeButton(R.string.shared_string_cancel, null);
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() { final AlertDialog alertDialog = b.create();
@Override alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
public void onShow(DialogInterface dialog) { @Override
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener( public void onShow(DialogInterface dialog) {
new View.OnClickListener() { alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(
@Override new View.OnClickListener() {
public void onClick(View v) { @Override
String newName = editText.getText().toString(); public void onClick(View v) {
if (ILLEGAL_PATH_NAME_CHARACTERS.matcher(newName).find()) { String newName = editText.getText().toString();
Toast.makeText(app, R.string.file_name_containes_illegal_char, if (ILLEGAL_PATH_NAME_CHARACTERS.matcher(newName).find()) {
Toast.LENGTH_LONG).show(); Toast.makeText(app, R.string.file_name_containes_illegal_char,
return; Toast.LENGTH_LONG).show();
} return;
File destFolder = new File(app.getAppPath(IndexConstants.GPX_INDEX_DIR), newName); }
if (destFolder.exists()) { File destFolder = new File(app.getAppPath(IndexConstants.GPX_INDEX_DIR), newName);
Toast.makeText(app, R.string.file_with_name_already_exists, if (destFolder.exists()) {
Toast.LENGTH_LONG).show(); Toast.makeText(app, R.string.file_with_name_already_exists,
return; Toast.LENGTH_LONG).show();
} else if (destFolder.mkdirs()) { return;
File dest = new File(destFolder, info.fileName); } else if (destFolder.mkdirs()) {
if (info.file.renameTo(dest)) { File dest = new File(destFolder, info.fileName);
app.getGpxDatabase().rename(info.file, dest); if (info.file.renameTo(dest)) {
asyncLoader = new LoadGpxTask(); app.getGpxDatabase().rename(info.file, dest);
asyncLoader.execute(getActivity()); asyncLoader = new LoadGpxTask();
} else { asyncLoader.execute(getActivity());
Toast.makeText(app, R.string.file_can_not_be_moved, Toast.LENGTH_LONG).show();
}
} else { } else {
Toast.makeText(app, R.string.file_can_not_be_moved, Toast.LENGTH_LONG).show(); Toast.makeText(app, R.string.file_can_not_be_moved, Toast.LENGTH_LONG).show();
} }
alertDialog.dismiss();
} else {
Toast.makeText(app, R.string.file_can_not_be_moved, Toast.LENGTH_LONG).show();
} }
}); alertDialog.dismiss();
} }
}); });
alertDialog.show(); }
});
alertDialog.show();
} else {
File dir = dirs.get(index);
File dest = new File(dir, info.file.getName());
if (dest.exists()) {
Toast.makeText(app, R.string.file_with_name_already_exists, Toast.LENGTH_LONG).show();
} else { } else {
File dir = dirs.get(index); if (info.file.renameTo(dest)) {
File dest = new File(dir, info.file.getName()); app.getGpxDatabase().rename(info.file, dest);
if (dest.exists()) { asyncLoader = new LoadGpxTask();
Toast.makeText(app, R.string.file_with_name_already_exists, Toast.LENGTH_LONG).show(); asyncLoader.execute(getActivity());
} else { } else {
if (info.file.renameTo(dest)) { Toast.makeText(app, R.string.file_can_not_be_moved, Toast.LENGTH_LONG).show();
app.getGpxDatabase().rename(info.file, dest);
asyncLoader = new LoadGpxTask();
asyncLoader.execute(getActivity());
} else {
Toast.makeText(app, R.string.file_can_not_be_moved, Toast.LENGTH_LONG).show();
}
} }
} }
} }
}); }
builder.setNegativeButton(R.string.shared_string_cancel, null); });
builder.create().show(); builder.setNegativeButton(R.string.shared_string_cancel, null);
} builder.create().show();
} }
public class LoadGpxTask extends AsyncTask<Activity, GpxInfo, List<GpxInfo>> { public class LoadGpxTask extends AsyncTask<Activity, GpxInfo, List<GpxInfo>> {