Merge pull request #9776 from osmandapp/fix_plan_route_save_as_new_track

Fix plan route save as new track
This commit is contained in:
max-klaus 2020-09-10 18:58:06 +03:00 committed by GitHub
commit b5df0d9271
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 184 additions and 171 deletions

View file

@ -180,7 +180,10 @@
android:ellipsize="end"
android:gravity="center_vertical"
android:maxLines="1"
android:padding="@dimen/measurement_tool_text_button_padding_small"
android:paddingStart="@dimen/measurement_tool_text_button_padding_small"
android:paddingLeft="@dimen/measurement_tool_text_button_padding_small"
android:paddingEnd="@dimen/measurement_tool_text_button_padding_small"
android:paddingRight="@dimen/measurement_tool_text_button_padding_small"
android:text="@string/shared_string_options"
android:textColor="?attr/color_dialog_buttons"
osmand:typeface="@string/font_roboto_medium"/>

View file

@ -53,6 +53,7 @@ public class BottomSheetItemWithDescription extends SimpleBottomSheetItem {
public void setDescription(CharSequence description) {
this.description = description;
descriptionTv.setText(description);
changeDescriptionVisibility();
}
public void setDescriptionMaxLines(int maxLines) {
@ -76,11 +77,7 @@ public class BottomSheetItemWithDescription extends SimpleBottomSheetItem {
super.inflate(context, container, nightMode);
descriptionTv = view.findViewById(R.id.description);
if (descriptionTv != null) {
if (Algorithms.isEmpty(description)) {
descriptionTv.setVisibility(View.GONE);
} else {
descriptionTv.setVisibility(View.VISIBLE);
}
changeDescriptionVisibility();
descriptionTv.setText(description);
if (descriptionColorId != INVALID_ID) {
descriptionTv.setTextColor(ContextCompat.getColor(context, descriptionColorId));
@ -94,6 +91,14 @@ public class BottomSheetItemWithDescription extends SimpleBottomSheetItem {
}
}
private void changeDescriptionVisibility() {
if (Algorithms.isEmpty(description)) {
descriptionTv.setVisibility(View.GONE);
} else {
descriptionTv.setVisibility(View.VISIBLE);
}
}
public static class Builder extends SimpleBottomSheetItem.Builder {
protected CharSequence description;

View file

@ -4,20 +4,16 @@ import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
@ -27,8 +23,6 @@ import android.widget.Toast;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.SwitchCompat;
import androidx.core.content.ContextCompat;
import androidx.core.widget.TextViewCompat;
import androidx.fragment.app.Fragment;
@ -40,13 +34,11 @@ import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.snackbar.Snackbar;
import net.osmand.AndroidUtils;
import net.osmand.FileUtils;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.GPXUtilities.Track;
import net.osmand.GPXUtilities.TrkSegment;
import net.osmand.GPXUtilities.WptPt;
import net.osmand.IndexConstants;
import net.osmand.LocationsHolder;
import net.osmand.data.LatLon;
import net.osmand.data.QuadRect;
@ -100,6 +92,7 @@ import java.util.List;
import java.util.Locale;
import static net.osmand.IndexConstants.GPX_FILE_EXT;
import static net.osmand.IndexConstants.GPX_INDEX_DIR;
import static net.osmand.plus.measurementtool.MeasurementEditingContext.CalculationMode;
import static net.osmand.plus.measurementtool.MeasurementEditingContext.SnapToRoadProgressListener;
import static net.osmand.plus.measurementtool.SaveAsNewTrackBottomSheetDialogFragment.SaveAsNewTrackFragmentListener;
@ -156,7 +149,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
LINE
}
private enum SaveAction {
private enum FinalSaveAction {
SHOW_SNACK_BAR_AND_CLOSE,
SHOW_TOAST,
SHOW_IS_SAVED_FRAGMENT
@ -419,7 +412,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
toolBarController.setOnSaveViewClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
saveChanges(SaveAction.SHOW_SNACK_BAR_AND_CLOSE);
saveChanges(FinalSaveAction.SHOW_SNACK_BAR_AND_CLOSE, false);
}
});
updateToolbar();
@ -623,17 +616,19 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
}
}
public void saveChanges(SaveAction saveAction) {
public void saveChanges(FinalSaveAction finalSaveAction, boolean showDialog) {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
if (editingCtx.getPointsCount() > 0) {
GpxData gpxData = editingCtx.getGpxData();
if (editingCtx.isNewData()) {
saveAsGpx(SaveType.ROUTE_POINT, saveAction);
} else if (isInEditMode() && gpxData.getActionType() == ActionType.EDIT_SEGMENT) {
openSaveAsNewTrackMenu(mapActivity);
if (editingCtx.isNewData() || (isInEditMode() && gpxData.getActionType() == ActionType.EDIT_SEGMENT)) {
if (showDialog) {
openSaveAsNewTrackMenu(mapActivity);
} else {
saveNewGpx(null, getSuggestedFileName(), true, false, finalSaveAction);
}
} else {
addToGpx(mapActivity, saveAction);
addToGpx(mapActivity, finalSaveAction);
}
} else {
Toast.makeText(mapActivity, getString(R.string.none_point_error), Toast.LENGTH_SHORT).show();
@ -748,7 +743,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
@Override
public void saveChangesOnClick() {
saveChanges(SaveAction.SHOW_TOAST);
saveChanges(FinalSaveAction.SHOW_TOAST, true);
}
@Override
@ -912,7 +907,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
@Override
public void onChangeApplicationMode(ApplicationMode mode, RouteBetweenPointsDialogType dialogType,
RouteBetweenPointsDialogMode dialogMode) {
RouteBetweenPointsDialogMode dialogMode) {
MeasurementToolLayer measurementLayer = getMeasurementLayer();
if (measurementLayer != null) {
ChangeRouteType changeRouteType = ChangeRouteType.NEXT_SEGMENT;
@ -988,8 +983,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
if (selectedGpxFile != null) {
gpxFile = selectedGpxFile.getGpxFile();
} else {
gpxFile = GPXUtilities.loadGPXFile(new File(
getMyApplication().getAppPath(IndexConstants.GPX_INDEX_DIR), gpxFileName));
gpxFile = GPXUtilities.loadGPXFile(new File(app.getAppPath(GPX_INDEX_DIR), gpxFileName));
}
}
@ -1012,7 +1006,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
.getSelectedFileByPath(gpxFile.path);
boolean showOnMap = selectedGpxFile != null;
saveExistingGpx(gpxFile, showOnMap, ActionType.ADD_SEGMENT,
editingCtx.hasRoute() ? SaveType.ROUTE_POINT : SaveType.LINE, SaveAction.SHOW_TOAST);
editingCtx.hasRoute() ? SaveType.ROUTE_POINT : SaveType.LINE, FinalSaveAction.SHOW_TOAST);
}
}
@ -1050,13 +1044,21 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
@Override
public void onSaveAsNewTrack(String folderName, String fileName, boolean showOnMap, boolean simplifiedTrack) {
File dir = getMyApplication().getAppPath(IndexConstants.GPX_INDEX_DIR);
if (folderName != null) {
dir = new File(dir, folderName);
saveNewGpx(folderName, fileName, showOnMap, simplifiedTrack, FinalSaveAction.SHOW_IS_SAVED_FRAGMENT);
}
private void saveNewGpx(String folderName, String fileName, boolean showOnMap, boolean simplifiedTrack,
FinalSaveAction finalSaveAction) {
OsmandApplication app = getMyApplication();
if (app != null) {
File dir = getMyApplication().getAppPath(GPX_INDEX_DIR);
if (folderName != null) {
dir = new File(dir, folderName);
}
fileName += GPX_FILE_EXT;
SaveType saveType = simplifiedTrack ? SaveType.LINE : SaveType.ROUTE_POINT;
saveNewGpx(dir, fileName, showOnMap, saveType, finalSaveAction);
}
fileName = fileName + GPX_FILE_EXT;
SaveType saveType = simplifiedTrack ? SaveType.LINE : SaveType.ROUTE_POINT;
saveNewGpx(dir, fileName, showOnMap, saveType, SaveAction.SHOW_IS_SAVED_FRAGMENT);
}
private MeasurementAdapterListener createMeasurementAdapterListener(final ItemTouchHelper touchHelper) {
@ -1183,7 +1185,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
if (mapActivity != null) {
if (editingCtx.getPointsCount() > 0) {
SaveAsNewTrackBottomSheetDialogFragment.showInstance(mapActivity.getSupportFragmentManager(),
this, getSuggestedFileName());
this, "", getSuggestedFileName(), true, true);
} else {
Toast.makeText(mapActivity, getString(R.string.none_point_error), Toast.LENGTH_SHORT).show();
}
@ -1450,7 +1452,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
}
}
private void addToGpx(MapActivity mapActivity, SaveAction saveAction) {
private void addToGpx(MapActivity mapActivity, FinalSaveAction finalSaveAction) {
GpxData gpxData = editingCtx.getGpxData();
GPXFile gpx = gpxData != null ? gpxData.getGpxFile() : null;
if (gpx != null) {
@ -1458,84 +1460,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
mapActivity.getMyApplication().getSelectedGpxHelper().getSelectedFileByPath(gpx.path);
boolean showOnMap = selectedGpxFile != null;
saveExistingGpx(gpx, showOnMap, gpxData.getActionType(),
editingCtx.hasRoute() ? SaveType.ROUTE_POINT : SaveType.LINE, saveAction);
}
}
private void saveAsGpx(final SaveType saveType, final SaveAction saveAction) {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
final File dir = mapActivity.getMyApplication().getAppPath(IndexConstants.GPX_INDEX_DIR);
final View view = UiUtilities.getInflater(mapActivity, nightMode).inflate(R.layout.save_gpx_dialog, null);
final EditText nameEt = (EditText) view.findViewById(R.id.gpx_name_et);
final TextView warningTextView = (TextView) view.findViewById(R.id.file_exists_text_view);
final View buttonView = view.findViewById(R.id.button_view);
final SwitchCompat showOnMapToggle = (SwitchCompat) view.findViewById(R.id.toggle_show_on_map);
UiUtilities.setupCompoundButton(showOnMapToggle, nightMode, UiUtilities.CompoundButtonType.GLOBAL);
buttonView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showOnMapToggle.setChecked(!showOnMapToggle.isChecked());
}
});
showOnMapToggle.setChecked(true);
String displayedName = getSuggestedFileName();
nameEt.setText(displayedName);
nameEt.setSelection(displayedName.length());
final boolean[] textChanged = new boolean[1];
AlertDialog.Builder builder = new AlertDialog.Builder(UiUtilities.getThemedContext(mapActivity, nightMode))
.setTitle(R.string.enter_gpx_name)
.setView(view)
.setPositiveButton(R.string.shared_string_save, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final String name = nameEt.getText().toString();
String fileName = name + GPX_FILE_EXT;
if (textChanged[0]) {
File fout = new File(dir, fileName);
int ind = 1;
while (fout.exists()) {
fileName = name + "_" + (++ind) + GPX_FILE_EXT;
fout = new File(dir, fileName);
}
}
saveNewGpx(dir, fileName, showOnMapToggle.isChecked(), saveType, saveAction);
}
})
.setNegativeButton(R.string.shared_string_cancel, null);
final AlertDialog dialog = builder.create();
dialog.show();
nameEt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (new File(dir, editable.toString() + GPX_FILE_EXT).exists()) {
warningTextView.setVisibility(View.VISIBLE);
warningTextView.setText(R.string.file_with_name_already_exists);
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
} else if (editable.toString().trim().isEmpty()) {
warningTextView.setVisibility(View.VISIBLE);
warningTextView.setText(R.string.enter_the_file_name);
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
} else {
warningTextView.setVisibility(View.INVISIBLE);
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
}
textChanged[0] = true;
}
});
editingCtx.hasRoute() ? SaveType.ROUTE_POINT : SaveType.LINE, finalSaveAction);
}
}
@ -1547,7 +1472,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
displayedName = suggestedName;
OsmandApplication app = getMyApplication();
if (app != null) {
File dir = app.getAppPath(IndexConstants.GPX_INDEX_DIR);
File dir = app.getAppPath(GPX_INDEX_DIR);
File fout = new File(dir, suggestedName + GPX_FILE_EXT);
int ind = 0;
while (fout.exists()) {
@ -1561,13 +1486,13 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
return displayedName;
}
private void saveNewGpx(File dir, String fileName, boolean showOnMap, SaveType saveType, SaveAction saveAction) {
saveGpx(dir, fileName, showOnMap, null, null, saveType, saveAction);
private void saveNewGpx(File dir, String fileName, boolean showOnMap, SaveType saveType, FinalSaveAction finalSaveAction) {
saveGpx(dir, fileName, showOnMap, null, null, saveType, finalSaveAction);
}
private void saveExistingGpx(GPXFile gpx, boolean showOnMap, ActionType actionType, SaveType saveType,
SaveAction saveAction) {
saveGpx(null, null, showOnMap, gpx, actionType, saveType, saveAction);
FinalSaveAction finalSaveAction) {
saveGpx(null, null, showOnMap, gpx, actionType, saveType, finalSaveAction);
}
@SuppressLint("StaticFieldLeak")
@ -1577,7 +1502,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
final GPXFile gpxFile,
final ActionType actionType,
final SaveType saveType,
final SaveAction saveAction) {
final FinalSaveAction finalSaveAction) {
new AsyncTask<Void, Void, Exception>() {
@ -1748,25 +1673,13 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
if (isInEditMode()) {
dismiss(mapActivity);
} else {
switch (saveAction) {
switch (finalSaveAction) {
case SHOW_SNACK_BAR_AND_CLOSE:
final WeakReference<MapActivity> mapActivityRef = new WeakReference<>(mapActivity);
snackbar = Snackbar.make(mapActivity.getLayout(),
MessageFormat.format(getString(R.string.gpx_saved_sucessfully), toSave.getName()),
Snackbar.LENGTH_LONG)
.setAction(R.string.shared_string_rename, new OnClickListener() {
@Override
public void onClick(View view) {
MapActivity mapActivity = mapActivityRef.get();
if (AndroidUtils.isActivityNotDestroyed(mapActivity)) {
FileUtils.renameFile(mapActivity, toSave, new FileUtils.RenameCallback() {
@Override
public void renamedTo(File file) {
}
});
}
}
});
.setAction(R.string.shared_string_rename, getRenameListener(mapActivityRef));
snackbar.getView().<TextView>findViewById(com.google.android.material.R.id.snackbar_action)
.setAllCaps(false);
UiUtilities.setupSnackbar(snackbar, nightMode);
@ -1790,6 +1703,22 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
Toast.makeText(mapActivity, warning.getMessage(), Toast.LENGTH_LONG).show();
}
}
private OnClickListener getRenameListener(final WeakReference<MapActivity> mapActivityRef) {
return new OnClickListener() {
@Override
public void onClick(View view) {
MapActivity mapActivity = mapActivityRef.get();
String parentFolder = toSave.getParentFile().getName();
if (GPX_INDEX_DIR.equals(parentFolder + File.separator)) {
parentFolder = null;
}
SaveAsNewTrackBottomSheetDialogFragment.showInstance(
mapActivity.getSupportFragmentManager(), null, parentFolder,
AndroidUtils.trimExtension(toSave.getName()), false, showOnMap);
}
};
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@ -2050,8 +1979,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
layoutParams.leftMargin = ctx.getResources().getDimensionPixelSize(R.dimen.context_menu_padding_margin_large);
layoutParams.rightMargin = ctx.getResources().getDimensionPixelSize(R.dimen.context_menu_padding_margin_large);
int paddingH = ctx.getResources().getDimensionPixelSize(R.dimen.context_menu_padding_margin_large);
int paddingV = ctx.getResources().getDimensionPixelSize(R.dimen.context_menu_padding_margin_small);
done.setPadding(paddingH, paddingV, paddingH, paddingV);
done.setPadding(paddingH, done.getPaddingTop(), paddingH, done.getPaddingBottom());
AndroidUtils.setBackground(ctx, done, nightMode, R.drawable.purchase_dialog_outline_btn_bg_light,
R.drawable.purchase_dialog_outline_btn_bg_dark);
}

View file

@ -2,11 +2,13 @@ package net.osmand.plus.measurementtool;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -19,6 +21,8 @@ import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import net.osmand.AndroidUtils;
import net.osmand.GPXUtilities;
import net.osmand.IndexConstants;
import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
@ -33,6 +37,8 @@ import net.osmand.plus.measurementtool.adapter.FolderListAdapter;
import org.apache.commons.logging.Log;
import java.io.File;
public class SaveAsNewTrackBottomSheetDialogFragment extends MenuBottomSheetDialogFragment {
public static final String TAG = SaveAsNewTrackBottomSheetDialogFragment.class.getSimpleName();
@ -41,12 +47,18 @@ public class SaveAsNewTrackBottomSheetDialogFragment extends MenuBottomSheetDial
public static final String SIMPLIFIED_TRACK_KEY = "simplified_track_key";
public static final String FOLDER_NAME_KEY = "folder_name_key";
public static final String FILE_NAME_KEY = "file_name_key";
public static final String SOURCE_FILE_NAME_KEY = "source_file_name_key";
public static final String SOURCE_FOLDER_NAME_KEY = "source_folder_name_key";
public static final String SHOW_SIMPLIFIED_BUTTON_KEY = "show_simplified_button_key";
private boolean showOnMap;
private boolean simplifiedTrack;
private String fileName;
private String sourceFileName;
private String sourceFolderName;
private String folderName;
private boolean rightButtonEnabled = true;
private boolean showSimplifiedButton = true;
@Override
public void createMenuItems(Bundle savedInstanceState) {
@ -59,9 +71,12 @@ public class SaveAsNewTrackBottomSheetDialogFragment extends MenuBottomSheetDial
simplifiedTrack = savedInstanceState.getBoolean(SIMPLIFIED_TRACK_KEY);
folderName = savedInstanceState.getString(FOLDER_NAME_KEY);
fileName = savedInstanceState.getString(FILE_NAME_KEY);
sourceFileName = savedInstanceState.getString(SOURCE_FILE_NAME_KEY);
sourceFolderName = savedInstanceState.getString(SOURCE_FOLDER_NAME_KEY);
showSimplifiedButton = savedInstanceState.getBoolean(SHOW_SIMPLIFIED_BUTTON_KEY);
}
items.add(new TitleItem(getString(R.string.shared_string_save_as_gpx)));
items.add(new TitleItem(getString(R.string.save_as_new_track)));
View editNameView = View.inflate(UiUtilities.getThemedContext(app, nightMode),
R.layout.track_name_edit_text, null);
@ -113,46 +128,38 @@ public class SaveAsNewTrackBottomSheetDialogFragment extends MenuBottomSheetDial
items.add(new DividerSpaceItem(app, app.getResources().getDimensionPixelSize(R.dimen.dialog_content_margin)));
}
int activeColorRes = nightMode ? R.color.active_color_primary_dark : R.color.active_color_primary_light;
int backgroundColor = AndroidUtils.getColorFromAttr(UiUtilities.getThemedContext(app, nightMode),
R.attr.activity_background_color);
GradientDrawable background = (GradientDrawable) AppCompatResources.getDrawable(app,
R.drawable.bg_select_group_button_outline);
if (background != null) {
background = (GradientDrawable) background.mutate();
background.setStroke(0, Color.TRANSPARENT);
background.setColor(backgroundColor);
}
final BottomSheetItemWithCompoundButton[] simplifiedTrackItem = new BottomSheetItemWithCompoundButton[1];
simplifiedTrackItem[0] = (BottomSheetItemWithCompoundButton) new BottomSheetItemWithCompoundButton.Builder()
.setChecked(simplifiedTrack)
.setCompoundButtonColorId(activeColorRes)
.setDescription(getString(R.string.simplified_track_description))
.setBackground(background)
.setTitle(getString(R.string.simplified_track))
.setLayoutId(R.layout.bottom_sheet_item_with_switch_and_descr)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
simplifiedTrack = !simplifiedTrack;
simplifiedTrackItem[0].setChecked(simplifiedTrack);
}
})
.create();
items.add(simplifiedTrackItem[0]);
items.add(new DividerSpaceItem(app, app.getResources().getDimensionPixelSize(R.dimen.content_padding)));
if (showSimplifiedButton) {
final BottomSheetItemWithCompoundButton[] simplifiedTrackItem = new BottomSheetItemWithCompoundButton[1];
simplifiedTrackItem[0] = (BottomSheetItemWithCompoundButton) new BottomSheetItemWithCompoundButton.Builder()
.setChecked(simplifiedTrack)
.setCompoundButtonColorId(activeColorRes)
.setDescription(getSimplifiedTrackDescription())
.setBackground(getBackground(simplifiedTrack))
.setTitle(getString(R.string.simplified_track))
.setLayoutId(R.layout.bottom_sheet_item_with_switch_and_descr)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
simplifiedTrack = !simplifiedTrack;
simplifiedTrackItem[0].setChecked(simplifiedTrack);
AndroidUtils.setBackground(simplifiedTrackItem[0].getView(), getBackground(simplifiedTrack));
simplifiedTrackItem[0].setDescription(getSimplifiedTrackDescription());
}
})
.create();
items.add(simplifiedTrackItem[0]);
background = (GradientDrawable) AppCompatResources.getDrawable(app, R.drawable.bg_select_group_button_outline);
if (background != null) {
background = (GradientDrawable) background.mutate();
background.setStroke(app.getResources().getDimensionPixelSize(R.dimen.map_button_stroke), backgroundColor);
items.add(new DividerSpaceItem(app, app.getResources().getDimensionPixelSize(R.dimen.content_padding)));
}
final BottomSheetItemWithCompoundButton[] showOnMapItem = new BottomSheetItemWithCompoundButton[1];
showOnMapItem[0] = (BottomSheetItemWithCompoundButton) new BottomSheetItemWithCompoundButton.Builder()
.setCompoundButtonColorId(activeColorRes)
.setChecked(showOnMap)
.setBackground(background)
.setBackground(getBackground(showOnMap))
.setTitle(getString(R.string.shared_string_show_on_map))
.setLayoutId(R.layout.bottom_sheet_item_with_switch_and_descr)
.setOnClickListener(new View.OnClickListener() {
@ -160,6 +167,7 @@ public class SaveAsNewTrackBottomSheetDialogFragment extends MenuBottomSheetDial
public void onClick(View v) {
showOnMap = !showOnMap;
showOnMapItem[0].setChecked(showOnMap);
AndroidUtils.setBackground(showOnMapItem[0].getView(), getBackground(showOnMap));
}
})
.create();
@ -168,6 +176,31 @@ public class SaveAsNewTrackBottomSheetDialogFragment extends MenuBottomSheetDial
items.add(new DividerSpaceItem(app, contentPaddingSmall));
}
private String getSimplifiedTrackDescription() {
return simplifiedTrack ? getString(R.string.simplified_track_description) : "";
}
private Drawable getBackground(boolean checked) {
OsmandApplication app = getMyApplication();
if (app != null) {
GradientDrawable background = (GradientDrawable) AppCompatResources.getDrawable(app,
R.drawable.bg_select_group_button_outline);
if (background != null) {
int backgroundColor = AndroidUtils.getColorFromAttr(UiUtilities.getThemedContext(app, nightMode),
R.attr.activity_background_color);
background = (GradientDrawable) background.mutate();
if (checked) {
background.setStroke(0, Color.TRANSPARENT);
background.setColor(backgroundColor);
} else {
background.setStroke(app.getResources().getDimensionPixelSize(R.dimen.map_button_stroke), backgroundColor);
}
}
return background;
}
return null;
}
private FolderListAdapter.FolderListAdapterListener createFolderSelectListener() {
return new FolderListAdapter.FolderListAdapterListener() {
@Override
@ -183,15 +216,23 @@ public class SaveAsNewTrackBottomSheetDialogFragment extends MenuBottomSheetDial
outState.putBoolean(SIMPLIFIED_TRACK_KEY, simplifiedTrack);
outState.putString(FOLDER_NAME_KEY, folderName);
outState.putString(FILE_NAME_KEY, fileName);
outState.putString(SOURCE_FILE_NAME_KEY, sourceFileName);
outState.putString(SOURCE_FOLDER_NAME_KEY, sourceFolderName);
outState.putBoolean(SHOW_SIMPLIFIED_BUTTON_KEY, showSimplifiedButton);
super.onSaveInstanceState(outState);
}
public static void showInstance(@NonNull FragmentManager fm, @Nullable Fragment targetFragment, String fileName) {
public static void showInstance(@NonNull FragmentManager fm, @Nullable Fragment targetFragment, String folderName,
String fileName, boolean showSimplifiedButton, boolean showOnMap) {
try {
if (!fm.isStateSaved()) {
SaveAsNewTrackBottomSheetDialogFragment fragment = new SaveAsNewTrackBottomSheetDialogFragment();
fragment.setTargetFragment(targetFragment, 0);
fragment.fileName = fileName;
fragment.sourceFileName = fileName;
fragment.sourceFolderName = folderName;
fragment.showSimplifiedButton = showSimplifiedButton;
fragment.showOnMap = showOnMap;
fragment.show(fm, TAG);
}
} catch (RuntimeException e) {
@ -210,10 +251,45 @@ public class SaveAsNewTrackBottomSheetDialogFragment extends MenuBottomSheetDial
if (targetFragment instanceof SaveAsNewTrackFragmentListener) {
((SaveAsNewTrackFragmentListener) targetFragment).onSaveAsNewTrack(folderName, fileName, showOnMap,
simplifiedTrack);
} else {
renameFile();
}
dismiss();
}
private void renameFile() {
OsmandApplication app = getMyApplication();
if (app != null) {
File dir = getMyApplication().getAppPath(IndexConstants.GPX_INDEX_DIR);
File source = dir;
if (sourceFolderName != null) {
source = new File(dir, sourceFolderName);
}
source = new File(source, sourceFileName + IndexConstants.GPX_FILE_EXT);
File dest = dir;
if (folderName != null) {
dest = new File(dir, folderName);
}
dest = new File(dest, fileName + IndexConstants.GPX_FILE_EXT);
if (!source.equals(dest)) {
if (dest.exists()) {
Toast.makeText(app, R.string.file_with_name_already_exists, Toast.LENGTH_LONG).show();
} else {
if (source.renameTo(dest)) {
app.getGpxDbHelper().rename(source, dest);
} else {
Toast.makeText(app, R.string.file_can_not_be_moved, Toast.LENGTH_LONG).show();
}
}
}
GPXUtilities.GPXFile gpxFile = GPXUtilities.loadGPXFile(dest);
if (gpxFile.error != null) {
return;
}
app.getSelectedGpxHelper().selectGpxFile(gpxFile, showOnMap, false);
}
}
@Override
protected boolean isRightBottomButtonEnabled() {
return rightButtonEnabled;

View file

@ -120,12 +120,13 @@ public class SelectFileBottomSheet extends MenuBottomSheetDialogFragment {
adapter.setAdapterListener(new OnItemClickListener() {
@Override
public void onItemClick(int position) {
if (position != RecyclerView.NO_POSITION && position < allGpxList.size()) {
List<GPXInfo> gpxList = adapter.getGpxInfoList();
if (position != RecyclerView.NO_POSITION && position < gpxList.size()) {
String fileName;
if (isShowCurrentGpx() && position == 0) {
fileName = null;
} else {
fileName = allGpxList.get(position).getFileName();
fileName = gpxList.get(position).getFileName();
}
if (listener != null) {
listener.selectFileOnCLick(fileName);