fix saving gpx with the same name and refactor code

This commit is contained in:
Chumva 2018-06-25 14:29:35 +03:00
parent c8a76c3de2
commit 80741dc7e2
5 changed files with 45 additions and 38 deletions

View file

@ -849,6 +849,11 @@ public class GPXUtilities {
modifiedTime = System.currentTimeMillis();
}
public void addPoint(int position, WptPt point) {
points.add(position, point);
modifiedTime = System.currentTimeMillis();
}
void addPoints(Collection<? extends WptPt> collection) {
points.addAll(collection);
modifiedTime = System.currentTimeMillis();
@ -1231,11 +1236,6 @@ public class GPXUtilities {
}
return categories;
}
public void addPoint(int position, WptPt point) {
points.add(position, point);
modifiedTime = System.currentTimeMillis();
}
}
public static String asString(GPXFile file, OsmandApplication ctx) {

View file

@ -408,11 +408,8 @@ public class TrackActivity extends TabActivity {
trackBitmapDrawer.setDrawEnabled(trackPointFragment.isUpdateEnable());
}
trackPointFragment.setContent();
} else if (frag instanceof CoordinateInputDialogFragment) {
CoordinateInputDialogFragment coordinateInputDialogFragment = (CoordinateInputDialogFragment) frag;
if (gpxFile != null) {
coordinateInputDialogFragment.setGpx(gpxFile);
}
} else if (gpxFile != null && frag instanceof CoordinateInputDialogFragment) {
((CoordinateInputDialogFragment) frag).setGpx(gpxFile);
}
}
OsmandFragmentPagerAdapter pagerAdapter = (OsmandFragmentPagerAdapter) viewPager.getAdapter();

View file

@ -56,6 +56,8 @@ import net.osmand.AndroidUtils;
import net.osmand.IndexConstants;
import net.osmand.Location;
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.MapMarkersHelper;
import net.osmand.plus.OsmAndLocationProvider.OsmAndCompassListener;
@ -91,9 +93,9 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
public static final String TAG = "CoordinateInputDialogFragment";
public static final String ADDED_POINTS_NUMBER_KEY = "added_points_number_key";
private GPXUtilities.GPXFile newGpxFile;
private GPXFile newGpxFile;
private OnPointsSavedListener listener;
protected GPXUtilities.WptPt selectedWpt;
private WptPt selectedWpt;
private SavingTrackHelper savingTrackHelper;
private GpxSelectionHelper selectedGpxHelper;
@ -111,7 +113,6 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
private boolean east = true;
private Location location;
private String wptCategory = null;
private Float heading;
private boolean locationUpdateStarted;
private boolean compassUpdateAllowed = true;
@ -124,20 +125,24 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lightTheme = getMyApplication().getSettings().isLightContent();
OsmandApplication app = getMyApplication();
lightTheme = app.getSettings().isLightContent();
setStyle(STYLE_NO_FRAME, lightTheme ? R.style.OsmandLightTheme : R.style.OsmandDarkTheme);
newGpxFile = new GPXUtilities.GPXFile();
savingTrackHelper = getMyApplication().getSavingTrackHelper();
selectedGpxHelper = getMyApplication().getSelectedGpxHelper();
newGpxFile = new GPXFile();
savingTrackHelper = app.getSavingTrackHelper();
selectedGpxHelper = app.getSelectedGpxHelper();
}
@Nullable
private GPXUtilities.GPXFile getGpx() {
private GPXFile getGpx() {
TrackActivity activity = getTrackActivity();
if (activity != null) {
GPXUtilities.GPXFile gpx = activity.getGpx();
GPXFile gpx = activity.getGpx();
return gpx != null ? gpx : newGpxFile;
} else return newGpxFile;
} else {
return newGpxFile;
}
}
@Nullable
@ -150,7 +155,7 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
}
}
private void syncGpx(GPXUtilities.GPXFile gpxFile) {
private void syncGpx(GPXFile gpxFile) {
MapMarkersHelper helper = getMyApplication().getMapMarkersHelper();
MapMarkersHelper.MapMarkersGroup group = helper.getMarkersGroup(gpxFile);
if (group != null) {
@ -158,7 +163,7 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
}
}
protected void addWpt(GPXUtilities.GPXFile gpx, String description, String name, String category, int color, double lat, double lon) {
protected void addWpt(GPXFile gpx, String description, String name, String category, int color, double lat, double lon) {
if (gpx != null) {
if (gpx.showCurrentTrack) {
savingTrackHelper.insertPointData(lat, lon, System.currentTimeMillis(), description, name, category, color);
@ -169,7 +174,7 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
}
}
protected void updateWpt(GPXUtilities.GPXFile gpx, String description, String name, String category, int color, double lat, double lon) {
protected void updateWpt(GPXFile gpx, String description, String name, String category, int color, double lat, double lon) {
if (gpx != null) {
if (gpx.showCurrentTrack) {
savingTrackHelper.updatePointData(selectedWpt, lat, lon, System.currentTimeMillis(), description, name, category, color);
@ -185,7 +190,7 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
if (Algorithms.isEmpty(getGpx().path)) {
showSaveDialog();
} else {
GPXUtilities.GPXFile gpx = getGpx();
GPXFile gpx = getGpx();
new SaveGpxAsyncTask(getMyApplication(), gpx,null, false).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
syncGpx(gpx);
if (listener != null) {
@ -233,10 +238,9 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
return dialog;
}
public void setGpx(GPXUtilities.GPXFile gpx) {
public void setGpx(GPXFile gpx) {
this.newGpxFile = gpx;
adapter.setGpx(gpx);
adapter.notifyDataSetChanged();
}
@Nullable
@ -1030,7 +1034,7 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
@Override
public void removeItem(final int position) {
final GPXUtilities.WptPt wpt = adapter.getItem(position);
final WptPt wpt = adapter.getItem(position);
if (selectedWpt == wpt) {
dismissEditingMode();
clearInputs();
@ -1125,7 +1129,7 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
((TextView) mainView.findViewById(R.id.toolbar_text)).setText(R.string.coord_input_add_point);
}
private void enterEditingMode(GPXUtilities.WptPt wptPt) {
private void enterEditingMode(WptPt wptPt) {
selectedWpt = wptPt;
Format format = getMyApplication().getSettings().COORDS_INPUT_FORMAT.get();
double lat = Math.abs(wptPt.lat);
@ -1234,10 +1238,10 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
double lon = parseCoordinate(longitude);
String name = ((EditText) mainView.findViewById(R.id.point_name_et)).getText().toString();
if (selectedWpt != null) {
updateWpt(getGpx(), "", name, wptCategory, 0, lat, lon);
updateWpt(getGpx(), null, name, null, 0, lat, lon);
dismissEditingMode();
} else {
addWpt(getGpx(), "", name, wptCategory, 0, lat, lon);
addWpt(getGpx(), null, name, null, 0, lat, lon);
}
}
adapter.notifyDataSetChanged();
@ -1386,11 +1390,11 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
private static class SaveGpxAsyncTask extends AsyncTask<Void, Void, Void> {
private final OsmandApplication app;
private final GPXUtilities.GPXFile gpx;
private final GPXFile gpx;
private final boolean gpxSelected;
private final String fileName;
SaveGpxAsyncTask(OsmandApplication app, GPXUtilities.GPXFile gpx, String fileName, boolean gpxSelected) {
SaveGpxAsyncTask(OsmandApplication app, GPXFile gpx, String fileName, boolean gpxSelected) {
this.app = app;
this.gpx = gpx;
this.fileName = fileName;
@ -1406,6 +1410,10 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
dir.mkdirs();
}
File fout = new File(dir, fileName + ".gpx");
int ind = 1;
while (fout.exists()) {
fout = new File(dir, fileName + "_" + (++ind) + ".gpx");
}
GPXUtilities.writeGpxFile(fout, gpx, app);
}
} else {

View file

@ -49,12 +49,12 @@ public class SaveAsTrackBottomSheetDialogFragment extends BottomSheetDialogFragm
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final OsmandApplication app = getMyApplication();
boolean isCoordInput = false;
boolean openFromCoordinateInput = false;
int number = 0;
Bundle args = getArguments();
if (args != null) {
isCoordInput = args.getBoolean(COORDINATE_INPUT_MODE_KEY);
if (isCoordInput) {
openFromCoordinateInput = args.getBoolean(COORDINATE_INPUT_MODE_KEY);
if (openFromCoordinateInput) {
number = args.getInt(ADDED_POINTS_NUMBER_KEY);
}
}
@ -65,9 +65,9 @@ public class SaveAsTrackBottomSheetDialogFragment extends BottomSheetDialogFragm
final View mainView = View.inflate(new ContextThemeWrapper(getContext(), themeRes), R.layout.fragment_marker_save_as_track_bottom_sheet_dialog, container);
LinearLayout contentLayout = (LinearLayout) mainView.findViewById(R.id.content_linear_layout);
TextView titleTv = (TextView) mainView.findViewById(R.id.save_as_track_title);
titleTv.setText(isCoordInput ? R.string.coord_input_save_as_track : R.string.marker_save_as_track);
titleTv.setText(openFromCoordinateInput ? R.string.coord_input_save_as_track : R.string.marker_save_as_track);
TextView descriptionTv = (TextView) mainView.findViewById(R.id.save_as_track_description);
descriptionTv.setText(isCoordInput ? getString(R.string.coord_input_save_as_track_descr, number) : getString(R.string.marker_save_as_track_descr));
descriptionTv.setText(openFromCoordinateInput ? getString(R.string.coord_input_save_as_track_descr, number) : getString(R.string.marker_save_as_track_descr));
int layoutRes;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
layoutRes = R.layout.markers_track_name_text_field_box;

View file

@ -16,6 +16,7 @@ import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.UiUtilities.UpdateLocationViewCache;
import net.osmand.plus.base.FavoriteImageDrawable;
public class CoordinateInputAdapter extends RecyclerView.Adapter<MapMarkerItemViewHolder> {
@ -63,7 +64,7 @@ public class CoordinateInputAdapter extends RecyclerView.Adapter<MapMarkerItemVi
GPXUtilities.WptPt wpt = getItem(position);
holder.iconDirection.setVisibility(View.VISIBLE);
holder.icon.setVisibility(View.GONE);
holder.icon.setImageDrawable(FavoriteImageDrawable.getOrCreate(app, wpt.getColor(), false));
holder.mainLayout.setBackgroundColor(getResolvedColor(nightTheme ? R.color.ctx_menu_bg_dark : R.color.bg_color_light));
holder.title.setTextColor(getResolvedColor(nightTheme ? R.color.ctx_menu_title_color_dark : R.color.color_black));
holder.divider.setBackgroundColor(getResolvedColor(nightTheme ? R.color.route_info_divider_dark : R.color.dashboard_divider_light));
@ -110,6 +111,7 @@ public class CoordinateInputAdapter extends RecyclerView.Adapter<MapMarkerItemVi
public void setGpx(GPXUtilities.GPXFile gpx) {
this.gpx = gpx;
notifyDataSetChanged();
}
private Drawable getColoredIcon(@DrawableRes int resId, @ColorRes int colorResId) {