Fix wpt and tre

This commit is contained in:
PavelRatushny 2017-08-04 16:29:07 +03:00
parent 89146d4bfe
commit 26e31fb9bf
6 changed files with 96 additions and 358 deletions

View file

@ -1,28 +0,0 @@
package net.osmand.plus.mapcontextmenu.editors;
import net.osmand.plus.GPXUtilities.GPXFile;
import net.osmand.plus.GPXUtilities.WptPt;
import net.osmand.plus.activities.MapActivity;
public abstract class GpxPointEditor extends PointEditor {
protected GPXFile gpxFile;
protected WptPt wpt;
protected boolean gpxSelected;
public GpxPointEditor(MapActivity mapActivity) {
super(mapActivity);
}
public GPXFile getGpxFile() {
return gpxFile;
}
public boolean isGpxSelected() {
return gpxSelected;
}
public WptPt getWptPt() {
return wpt;
}
}

View file

@ -1,13 +1,8 @@
package net.osmand.plus.mapcontextmenu.editors;
import net.osmand.data.LatLon;
import net.osmand.plus.FavouritesDbHelper;
import net.osmand.plus.GPXUtilities.GPXFile;
import net.osmand.plus.GPXUtilities.WptPt;
import net.osmand.plus.GpxSelectionHelper;
import net.osmand.plus.activities.MapActivity;
public class RtePtEditor extends GpxPointEditor {
public class RtePtEditor extends WptPtEditor {
public static final String TAG = "RtePtEditorFragment";
@ -20,71 +15,13 @@ public class RtePtEditor extends GpxPointEditor {
return TAG;
}
public void add(GPXFile gpxFile, LatLon latLon, String title) {
if (latLon == null) {
return;
}
isNew = true;
this.gpxFile = gpxFile;
GpxSelectionHelper.SelectedGpxFile selectedGpxFile =
mapActivity.getMyApplication().getSelectedGpxHelper().getSelectedFileByPath(gpxFile.path);
gpxSelected = selectedGpxFile != null;
wpt = new WptPt(latLon.getLatitude(), latLon.getLongitude(),
System.currentTimeMillis(), Double.NaN, 0, Double.NaN);
wpt.name = title;
@Override
public void showEditorFragment() {
RtePtEditorFragment.showInstance(mapActivity);
}
public void add(GPXFile gpxFile, LatLon latLon, String title, String categoryName, int categoryColor, boolean skipDialog) {
if (latLon == null) {
return;
}
isNew = true;
this.gpxFile = gpxFile;
GpxSelectionHelper.SelectedGpxFile selectedGpxFile =
mapActivity.getMyApplication().getSelectedGpxHelper().getSelectedFileByPath(gpxFile.path);
gpxSelected = selectedGpxFile != null;
wpt = new WptPt(latLon.getLatitude(), latLon.getLongitude(),
System.currentTimeMillis(), Double.NaN, 0, Double.NaN);
wpt.name = title;
if (categoryName != null && !categoryName.isEmpty()) {
FavouritesDbHelper.FavoriteGroup category = mapActivity.getMyApplication()
.getFavorites()
.getGroup(categoryName);
if (category == null) {
mapActivity.getMyApplication()
.getFavorites()
.addEmptyCategory(categoryName, categoryColor);
}
} else categoryName = "";
wpt.category = categoryName;
@Override
public void showEditorFragment(boolean skipDialog) {
RtePtEditorFragment.showInstance(mapActivity, skipDialog);
}
public void edit(WptPt wpt) {
if (wpt == null) {
return;
}
isNew = false;
GpxSelectionHelper.SelectedGpxFile selectedGpxFile =
mapActivity.getMyApplication().getSelectedGpxHelper().getSelectedGPXFile(wpt);
if (selectedGpxFile != null) {
gpxSelected = true;
gpxFile = selectedGpxFile.getGpxFile();
}
this.wpt = wpt;
RtePtEditorFragment.showInstance(mapActivity);
}
}

View file

@ -1,77 +1,13 @@
package net.osmand.plus.mapcontextmenu.editors;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import net.osmand.data.LatLon;
import net.osmand.plus.FavouritesDbHelper;
import net.osmand.plus.GPXUtilities;
import net.osmand.plus.GPXUtilities.GPXFile;
import net.osmand.plus.GPXUtilities.WptPt;
import net.osmand.plus.GpxSelectionHelper;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.SavingTrackHelper;
import net.osmand.plus.base.FavoriteImageDrawable;
import net.osmand.plus.mapcontextmenu.MapContextMenu;
import net.osmand.util.Algorithms;
import java.io.File;
public class RtePtEditorFragment extends PointEditorFragment{
private WptPtEditor editor;
private WptPt wpt;
private SavingTrackHelper savingTrackHelper;
private GpxSelectionHelper selectedGpxHelper;
private boolean saved;
private int color;
private int defaultColor;
private boolean skipDialog;
public class RtePtEditorFragment extends WptPtEditorFragment {
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
savingTrackHelper = getMapActivity().getMyApplication().getSavingTrackHelper();
selectedGpxHelper = getMapActivity().getMyApplication().getSelectedGpxHelper();
editor = getMapActivity().getContextMenu().getWptPtPointEditor();
defaultColor = getResources().getColor(R.color.gpx_color_point);
}
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wpt = editor.getWptPt();
FavouritesDbHelper.FavoriteGroup group = getMyApplication().getFavorites().getGroup(wpt.category);
if (group == null) {
color = wpt.getColor(0);
} else {
color = group.color;
}
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (skipDialog) {
save(true);
}
}
@Override
public PointEditor getEditor() {
return editor;
public void assignEditor() {
editor = getMapActivity().getContextMenu().getRtePtPointEditor();
}
@Override
@ -104,178 +40,4 @@ public class RtePtEditorFragment extends PointEditorFragment{
.add(R.id.fragmentContainer, fragment, editor.getFragmentTag())
.addToBackStack(null).commit();
}
@Override
protected boolean wasSaved() {
return saved;
}
@Override
protected void save(boolean needDismiss) {
String name = Algorithms.isEmpty(getNameTextValue()) ? null : getNameTextValue();
String category = Algorithms.isEmpty(getCategoryTextValue()) ? null : getCategoryTextValue();
String description = Algorithms.isEmpty(getDescriptionTextValue()) ? null : getDescriptionTextValue();
if (editor.isNew()) {
doAddWpt(name, category, description);
} else {
doUpdateWpt(name, category, description);
}
getMapActivity().refreshMap();
if (needDismiss) {
dismiss(false);
}
MapContextMenu menu = getMapActivity().getContextMenu();
if (menu.getLatLon() != null) {
LatLon latLon = new LatLon(wpt.getLatitude(), wpt.getLongitude());
if (menu.getLatLon().equals(latLon)) {
menu.update(latLon, wpt.getPointDescription(getMapActivity()), wpt);
}
}
saved = true;
}
private void doAddWpt(String name, String category, String description) {
wpt.name = name;
wpt.category = category;
wpt.desc = description;
if (color != 0) {
wpt.setColor(color);
}
GPXFile gpx = editor.getGpxFile();
if (gpx != null) {
if (gpx.showCurrentTrack) {
wpt = savingTrackHelper.insertPointData(wpt.getLatitude(), wpt.getLongitude(),
System.currentTimeMillis(), description, name, category, color);
if (!editor.isGpxSelected()) {
selectedGpxHelper.setGpxFileToDisplay(gpx);
}
} else {
wpt = gpx.addRtePt(wpt.getLatitude(), wpt.getLongitude(),
System.currentTimeMillis(), description, name, category, color);
new RtePtEditorFragment.SaveGpxAsyncTask(getMyApplication(), gpx, editor.isGpxSelected()).execute();
}
}
}
private void doUpdateWpt(String name, String category, String description) {
GPXFile gpx = editor.getGpxFile();
if (gpx != null) {
if (gpx.showCurrentTrack) {
savingTrackHelper.updatePointData(wpt, wpt.getLatitude(), wpt.getLongitude(),
System.currentTimeMillis(), description, name, category, color);
if (!editor.isGpxSelected()) {
selectedGpxHelper.setGpxFileToDisplay(gpx);
}
} else {
gpx.updateWptPt(wpt, wpt.getLatitude(), wpt.getLongitude(),
System.currentTimeMillis(), description, name, category, color);
new RtePtEditorFragment.SaveGpxAsyncTask(getMyApplication(), gpx, editor.isGpxSelected()).execute();
}
}
}
@Override
protected void delete(final boolean needDismiss) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(getString(R.string.context_menu_item_delete_waypoint));
builder.setNegativeButton(R.string.shared_string_no, null);
builder.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
GPXFile gpx = editor.getGpxFile();
if (gpx != null) {
if (gpx.showCurrentTrack) {
savingTrackHelper.deletePointData(wpt);
} else {
gpx.deleteWptPt(wpt);
new RtePtEditorFragment.SaveGpxAsyncTask(getMyApplication(), gpx, editor.isGpxSelected()).execute();
}
}
saved = true;
if (needDismiss) {
dismiss(true);
} else {
getMapActivity().refreshMap();
}
}
});
builder.create().show();
}
@Override
public void setCategory(String name) {
FavouritesDbHelper.FavoriteGroup group = getMyApplication().getFavorites().getGroup(name);
if (group != null) {
color = group.color;
}
super.setCategory(name);
}
@Override
protected String getDefaultCategoryName() {
return getString(R.string.shared_string_favorites);
}
@Override
public String getHeaderCaption() {
return getMapActivity().getResources().getString(R.string.gpx_wpt);
}
@Override
public String getNameInitValue() {
return wpt.name;
}
@Override
public String getCategoryInitValue() {
return Algorithms.isEmpty(wpt.category) ? "" : wpt.category;
}
@Override
public String getDescriptionInitValue() {
return wpt.desc;
}
@Override
public Drawable getNameIcon() {
return FavoriteImageDrawable.getOrCreate(getMapActivity(), color == 0 ? defaultColor : color, false);
}
@Override
public Drawable getCategoryIcon() {
return getPaintedIcon(R.drawable.ic_action_folder_stroke, color == 0 ? defaultColor : color);
}
private static class SaveGpxAsyncTask extends AsyncTask<Void, Void, Void> {
private final OsmandApplication app;
private final GPXFile gpx;
private final boolean gpxSelected;
public SaveGpxAsyncTask(OsmandApplication app, GPXFile gpx, boolean gpxSelected) {
this.app = app;
this.gpx = gpx;
this.gpxSelected = gpxSelected;
}
@Override
protected Void doInBackground(Void... params) {
GPXUtilities.writeGpxFile(new File(gpx.path), gpx, app);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
if (!gpxSelected) {
app.getSelectedGpxHelper().setGpxFileToDisplay(gpx);
}
}
}
}

View file

@ -7,7 +7,12 @@ import net.osmand.plus.GPXUtilities.WptPt;
import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
import net.osmand.plus.activities.MapActivity;
public class WptPtEditor extends GpxPointEditor {
public class WptPtEditor extends PointEditor {
private OnDismissListener onDismissListener;
private GPXFile gpxFile;
private WptPt wpt;
private boolean gpxSelected;
public static final String TAG = "WptPtEditorFragment";
@ -15,6 +20,30 @@ public class WptPtEditor extends GpxPointEditor {
super(mapActivity);
}
public interface OnDismissListener {
void openTrackActivity();
}
public void setOnDismissListener(OnDismissListener listener) {
onDismissListener = listener;
}
public OnDismissListener getOnDismissListener() {
return onDismissListener;
}
public GPXFile getGpxFile() {
return gpxFile;
}
public boolean isGpxSelected() {
return gpxSelected;
}
public WptPt getWptPt() {
return wpt;
}
@Override
public String getFragmentTag() {
return TAG;
@ -34,7 +63,7 @@ public class WptPtEditor extends GpxPointEditor {
wpt = new WptPt(latLon.getLatitude(), latLon.getLongitude(),
System.currentTimeMillis(), Double.NaN, 0, Double.NaN);
wpt.name = title;
WptPtEditorFragment.showInstance(mapActivity);
showEditorFragment();
}
public void add(GPXFile gpxFile, LatLon latLon, String title, String categoryName, int categoryColor, boolean skipDialog) {
@ -70,7 +99,7 @@ public class WptPtEditor extends GpxPointEditor {
wpt.category = categoryName;
WptPtEditorFragment.showInstance(mapActivity, skipDialog);
showEditorFragment(skipDialog);
}
public void edit(WptPt wpt) {
@ -85,6 +114,14 @@ public class WptPtEditor extends GpxPointEditor {
gpxFile = selectedGpxFile.getGpxFile();
}
this.wpt = wpt;
showEditorFragment();
}
public void showEditorFragment() {
WptPtEditorFragment.showInstance(mapActivity);
}
public void showEditorFragment(boolean skipDialog) {
WptPtEditorFragment.showInstance(mapActivity, skipDialog);
}
}

View file

@ -26,7 +26,8 @@ import net.osmand.util.Algorithms;
import java.io.File;
public class WptPtEditorFragment extends PointEditorFragment {
private WptPtEditor editor;
protected WptPtEditor editor;
private WptPt wpt;
private SavingTrackHelper savingTrackHelper;
private GpxSelectionHelper selectedGpxHelper;
@ -34,17 +35,21 @@ public class WptPtEditorFragment extends PointEditorFragment {
private boolean saved;
private int color;
private int defaultColor;
private boolean skipDialog;
protected boolean skipDialog;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
savingTrackHelper = getMapActivity().getMyApplication().getSavingTrackHelper();
selectedGpxHelper = getMapActivity().getMyApplication().getSelectedGpxHelper();
editor = getMapActivity().getContextMenu().getWptPtPointEditor();
assignEditor();
defaultColor = getResources().getColor(R.color.gpx_color_point);
}
protected void assignEditor() {
editor = getMapActivity().getContextMenu().getWptPtPointEditor();
}
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -69,6 +74,16 @@ public class WptPtEditorFragment extends PointEditorFragment {
}
}
@Override
public void dismiss() {
super.dismiss();
WptPtEditor.OnDismissListener listener = editor.getOnDismissListener();
if (listener != null) {
listener.openTrackActivity();
}
editor.setOnDismissListener(null);
}
@Override
public PointEditor getEditor() {
return editor;

View file

@ -1,8 +1,7 @@
package net.osmand.plus.views;
import android.content.Context;
import android.content.Intent;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
@ -14,26 +13,30 @@ import net.osmand.plus.GPXUtilities.GPXFile;
import net.osmand.plus.IconsCache;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.TrackActivity;
import net.osmand.plus.mapcontextmenu.MapContextMenu;
import net.osmand.plus.mapcontextmenu.editors.RtePtEditor;
import net.osmand.plus.mapcontextmenu.editors.WptPtEditor;
import net.osmand.plus.mapcontextmenu.editors.WptPtEditor.OnDismissListener;
public class AddGpxPointBottomSheetHelper {
public class AddGpxPointBottomSheetHelper implements OnDismissListener {
private final View view;
private final TextView title;
private final TextView description;
private final ImageView icon;
private final Context context;
private final MapActivity mapActivity;
private final MapContextMenu contextMenu;
private final ContextMenuLayer contextMenuLayer;
private final IconsCache iconsCache;
private String titleText;
private boolean applyingPositionMode;
private NewGpxPoint newPoint;
private NewGpxPoint newGpxPoint;
private PointDescription pointDescription;
public AddGpxPointBottomSheetHelper(final MapActivity activity, ContextMenuLayer ctxMenuLayer) {
this.contextMenuLayer = ctxMenuLayer;
iconsCache = activity.getMyApplication().getIconsCache();
context = activity;
mapActivity = activity;
contextMenu = activity.getContextMenu();
view = activity.findViewById(R.id.add_gpx_point_bottom_sheet);
title = (TextView) view.findViewById(R.id.add_gpx_point_bottom_sheet_title);
@ -44,12 +47,16 @@ public class AddGpxPointBottomSheetHelper {
@Override
public void onClick(View v) {
contextMenuLayer.createGpxPoint();
GPXFile gpx = newPoint.getGpx();
GPXFile gpx = newGpxPoint.getGpx();
LatLon latLon = contextMenu.getLatLon();
if (pointDescription.isWpt()) {
activity.getContextMenu().getWptPtPointEditor().add(gpx, latLon, titleText);
WptPtEditor editor = activity.getContextMenu().getWptPtPointEditor();
editor.setOnDismissListener(AddGpxPointBottomSheetHelper.this);
editor.add(gpx, latLon, titleText);
} else if (pointDescription.isRte()) {
activity.getContextMenu().getRtePtPointEditor().add(gpx, latLon, titleText);
RtePtEditor editor = activity.getContextMenu().getRtePtPointEditor();
editor.setOnDismissListener(AddGpxPointBottomSheetHelper.this);
editor.add(gpx, latLon, titleText);
}
}
});
@ -66,15 +73,15 @@ public class AddGpxPointBottomSheetHelper {
PointF point = contextMenuLayer.getMovableCenterPoint(rt);
double lat = rt.getLatFromPixel(point.x, point.y);
double lon = rt.getLonFromPixel(point.x, point.y);
description.setText(PointDescription.getLocationName(context, lat, lon, true));
description.setText(PointDescription.getLocationName(mapActivity, lat, lon, true));
}
public void setTitle(String title) {
if (title.equals("")) {
if (pointDescription.isWpt()) {
title = context.getString(R.string.waypoint_one);
title = mapActivity.getString(R.string.waypoint_one);
} else if (pointDescription.isRte()) {
title = context.getString(R.string.route_point_one);
title = mapActivity.getString(R.string.route_point_one);
}
}
titleText = title;
@ -86,13 +93,13 @@ public class AddGpxPointBottomSheetHelper {
}
public void show(NewGpxPoint newPoint) {
this.newPoint = newPoint;
this.newGpxPoint = newPoint;
pointDescription = newPoint.getPointDescription();
if (pointDescription.isWpt()) {
setTitle(context.getString(R.string.waypoint_one));
setTitle(mapActivity.getString(R.string.waypoint_one));
icon.setImageDrawable(iconsCache.getThemedIcon(R.drawable.ic_action_marker_dark));
} else if (pointDescription.isRte()) {
setTitle(context.getString(R.string.route_point_one));
setTitle(mapActivity.getString(R.string.route_point_one));
icon.setImageDrawable(iconsCache.getThemedIcon(R.drawable.ic_action_markers_dark));
}
exitApplyPositionMode();
@ -118,6 +125,14 @@ public class AddGpxPointBottomSheetHelper {
}
}
@Override
public void openTrackActivity() {
Intent newIntent = new Intent(mapActivity, mapActivity.getMyApplication().getAppCustomization().getTrackActivity());
newIntent.putExtra(TrackActivity.TRACK_FILE_NAME, newGpxPoint.getGpx().path);
newIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mapActivity.startActivity(newIntent);
}
public static class NewGpxPoint {
private PointDescription pointDescription;
private GPXFile gpx;