Move SaveGpxAsyncTask to separate file and remove duplicates

This commit is contained in:
Vitaliy 2020-07-15 13:45:41 +03:00
parent 620b821512
commit 6773ef5031
7 changed files with 203 additions and 150 deletions

View file

@ -11,7 +11,6 @@ import androidx.core.content.ContextCompat;
import net.osmand.GPXUtilities; import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.GPXFile;
import net.osmand.plus.track.GpxSplitType;
import net.osmand.GPXUtilities.GPXTrackAnalysis; import net.osmand.GPXUtilities.GPXTrackAnalysis;
import net.osmand.GPXUtilities.Route; import net.osmand.GPXUtilities.Route;
import net.osmand.GPXUtilities.Track; import net.osmand.GPXUtilities.Track;
@ -28,6 +27,7 @@ import net.osmand.plus.helpers.GpxUiHelper;
import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetAxisType; import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetAxisType;
import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetType; import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetType;
import net.osmand.plus.settings.backend.OsmandSettings.MetricsConstants; import net.osmand.plus.settings.backend.OsmandSettings.MetricsConstants;
import net.osmand.plus.track.GpxSplitType;
import net.osmand.plus.track.GradientScaleType; import net.osmand.plus.track.GradientScaleType;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
@ -533,9 +533,7 @@ public class GpxSelectionHelper {
} }
if (obj.has(GRADIENT_SCALE_TYPE)) { if (obj.has(GRADIENT_SCALE_TYPE)) {
String gradientScaleTypeName = obj.optString(GRADIENT_SCALE_TYPE); String gradientScaleTypeName = obj.optString(GRADIENT_SCALE_TYPE);
if (!Algorithms.isEmpty(gradientScaleTypeName)) { gpx.setGradientScaleType(gradientScaleTypeName);
gpx.setGradientScaleType(GradientScaleType.valueOf(gradientScaleTypeName).getTypeName());
}
} }
if (obj.has(SHOW_START_FINISH)) { if (obj.has(SHOW_START_FINISH)) {
boolean showStartFinish = obj.optBoolean(SHOW_START_FINISH, false); boolean showStartFinish = obj.optBoolean(SHOW_START_FINISH, false);

View file

@ -148,6 +148,10 @@ public abstract class ContextMenuFragment extends BaseOsmAndFragment {
return getLandscapeWidth() - getResources().getDimensionPixelSize(R.dimen.dashboard_land_shadow_width); return getLandscapeWidth() - getResources().getDimensionPixelSize(R.dimen.dashboard_land_shadow_width);
} }
public float getMiddleStateKoef() {
return MIDDLE_STATE_KOEF;
}
public abstract int getToolbarHeight(); public abstract int getToolbarHeight();
public boolean isSingleFragment() { public boolean isSingleFragment() {
@ -605,7 +609,7 @@ public abstract class ContextMenuFragment extends BaseOsmAndFragment {
} }
private int getMinHalfY(MapActivity mapActivity) { private int getMinHalfY(MapActivity mapActivity) {
return viewHeight - (int) Math.min(viewHeight * MIDDLE_STATE_KOEF, return viewHeight - (int) Math.min(viewHeight * getMiddleStateKoef(),
MIDDLE_STATE_MIN_HEIGHT_DP * mapActivity.getMapView().getDensity() ); MIDDLE_STATE_MIN_HEIGHT_DP * mapActivity.getMapView().getDensity() );
} }

View file

@ -12,7 +12,6 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment; import androidx.fragment.app.DialogFragment;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.GPXFile;
import net.osmand.GPXUtilities.WptPt; import net.osmand.GPXUtilities.WptPt;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
@ -27,9 +26,10 @@ import net.osmand.plus.activities.SavingTrackHelper;
import net.osmand.plus.base.PointImageDrawable; import net.osmand.plus.base.PointImageDrawable;
import net.osmand.plus.mapcontextmenu.MapContextMenu; import net.osmand.plus.mapcontextmenu.MapContextMenu;
import net.osmand.plus.mapcontextmenu.editors.WptPtEditor.OnDismissListener; import net.osmand.plus.mapcontextmenu.editors.WptPtEditor.OnDismissListener;
import net.osmand.plus.myplaces.SaveGpxAsyncTask;
import net.osmand.plus.myplaces.SaveGpxAsyncTask.SaveGpxListener;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import java.io.File;
import java.util.Map; import java.util.Map;
public class WptPtEditorFragment extends PointEditorFragment { public class WptPtEditorFragment extends PointEditorFragment {
@ -251,7 +251,7 @@ public class WptPtEditorFragment extends PointEditorFragment {
} }
} else { } else {
addWpt(gpx, description, name, category, color); addWpt(gpx, description, name, category, color);
new SaveGpxAsyncTask(getMyApplication(), gpx, editor.isGpxSelected()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); saveGpx(getMyApplication(), gpx, editor.isGpxSelected());
} }
syncGpx(gpx); syncGpx(gpx);
} }
@ -284,7 +284,7 @@ public class WptPtEditorFragment extends PointEditorFragment {
} else { } else {
gpx.updateWptPt(wpt, wpt.getLatitude(), wpt.getLongitude(), gpx.updateWptPt(wpt, wpt.getLatitude(), wpt.getLongitude(),
System.currentTimeMillis(), description, name, category, color); System.currentTimeMillis(), description, name, category, color);
new SaveGpxAsyncTask(getMyApplication(), gpx, editor.isGpxSelected()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); saveGpx(getMyApplication(), gpx, editor.isGpxSelected());
} }
syncGpx(gpx); syncGpx(gpx);
} }
@ -309,7 +309,7 @@ public class WptPtEditorFragment extends PointEditorFragment {
savingTrackHelper.deletePointData(wpt); savingTrackHelper.deletePointData(wpt);
} else { } else {
gpx.deleteWptPt(wpt); gpx.deleteWptPt(wpt);
new SaveGpxAsyncTask(getMyApplication(), gpx, editor.isGpxSelected()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); saveGpx(getMyApplication(), gpx, editor.isGpxSelected());
} }
syncGpx(gpx); syncGpx(gpx);
} }
@ -378,28 +378,19 @@ public class WptPtEditorFragment extends PointEditorFragment {
return color == 0 ? defaultColor : color; return color == 0 ? defaultColor : color;
} }
private static class SaveGpxAsyncTask extends AsyncTask<Void, Void, Void> { private void saveGpx(final OsmandApplication app, final GPXFile gpxFile, final boolean gpxSelected) {
private final OsmandApplication app; new SaveGpxAsyncTask(gpxFile, new SaveGpxListener() {
private final GPXFile gpx; @Override
private final boolean gpxSelected; public void gpxSavingStarted() {
SaveGpxAsyncTask(OsmandApplication app, GPXFile gpx, boolean gpxSelected) {
this.app = app;
this.gpx = gpx;
this.gpxSelected = gpxSelected;
} }
@Override @Override
protected Void doInBackground(Void... params) { public void gpxSavingFinished() {
GPXUtilities.writeGpxFile(new File(gpx.path), gpx);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
if (!gpxSelected) { if (!gpxSelected) {
app.getSelectedGpxHelper().setGpxFileToDisplay(gpx); app.getSelectedGpxHelper().setGpxFileToDisplay(gpxFile);
} }
} }
}).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
} }

View file

@ -14,7 +14,6 @@ import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment; import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentActivity;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.GPXFile;
import net.osmand.GPXUtilities.WptPt; import net.osmand.GPXUtilities.WptPt;
import net.osmand.data.FavouritePoint.BackgroundType; import net.osmand.data.FavouritePoint.BackgroundType;
@ -31,9 +30,10 @@ import net.osmand.plus.activities.SavingTrackHelper;
import net.osmand.plus.base.PointImageDrawable; import net.osmand.plus.base.PointImageDrawable;
import net.osmand.plus.mapcontextmenu.MapContextMenu; import net.osmand.plus.mapcontextmenu.MapContextMenu;
import net.osmand.plus.mapcontextmenu.editors.WptPtEditor.OnDismissListener; import net.osmand.plus.mapcontextmenu.editors.WptPtEditor.OnDismissListener;
import net.osmand.plus.myplaces.SaveGpxAsyncTask;
import net.osmand.plus.myplaces.SaveGpxAsyncTask.SaveGpxListener;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import java.io.File;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -269,7 +269,7 @@ public class WptPtEditorFragmentNew extends PointEditorFragmentNew {
} }
} else { } else {
addWpt(gpx, description, name, category, color, iconName, backgroundTypeName); addWpt(gpx, description, name, category, color, iconName, backgroundTypeName);
new SaveGpxAsyncTask(getMyApplication(), gpx, editor.isGpxSelected()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); saveGpx(getMyApplication(), gpx, editor.isGpxSelected());
} }
syncGpx(gpx); syncGpx(gpx);
} }
@ -303,7 +303,7 @@ public class WptPtEditorFragmentNew extends PointEditorFragmentNew {
} else { } else {
gpx.updateWptPt(wpt, wpt.getLatitude(), wpt.getLongitude(), gpx.updateWptPt(wpt, wpt.getLatitude(), wpt.getLongitude(),
System.currentTimeMillis(), description, name, category, color, iconName, backgroundTypeName); System.currentTimeMillis(), description, name, category, color, iconName, backgroundTypeName);
new SaveGpxAsyncTask(getMyApplication(), gpx, editor.isGpxSelected()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); saveGpx(getMyApplication(), gpx, editor.isGpxSelected());
} }
syncGpx(gpx); syncGpx(gpx);
} }
@ -332,7 +332,7 @@ public class WptPtEditorFragmentNew extends PointEditorFragmentNew {
savingTrackHelper.deletePointData(wpt); savingTrackHelper.deletePointData(wpt);
} else { } else {
gpx.deleteWptPt(wpt); gpx.deleteWptPt(wpt);
new SaveGpxAsyncTask(getMyApplication(), gpx, editor.isGpxSelected()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); saveGpx(getMyApplication(), gpx, editor.isGpxSelected());
} }
syncGpx(gpx); syncGpx(gpx);
} }
@ -478,28 +478,19 @@ public class WptPtEditorFragmentNew extends PointEditorFragmentNew {
return 0; return 0;
} }
private static class SaveGpxAsyncTask extends AsyncTask<Void, Void, Void> { private void saveGpx(final OsmandApplication app, final GPXFile gpxFile, final boolean gpxSelected) {
private final OsmandApplication app; new SaveGpxAsyncTask(gpxFile, new SaveGpxListener() {
private final GPXFile gpx; @Override
private final boolean gpxSelected; public void gpxSavingStarted() {
SaveGpxAsyncTask(OsmandApplication app, GPXFile gpx, boolean gpxSelected) {
this.app = app;
this.gpx = gpx;
this.gpxSelected = gpxSelected;
} }
@Override @Override
protected Void doInBackground(Void... params) { public void gpxSavingFinished() {
GPXUtilities.writeGpxFile(new File(gpx.path), gpx);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
if (!gpxSelected) { if (!gpxSelected) {
app.getSelectedGpxHelper().setGpxFileToDisplay(gpx); app.getSelectedGpxHelper().setGpxFileToDisplay(gpxFile);
} }
} }
}).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
} }

View file

@ -0,0 +1,50 @@
package net.osmand.plus.myplaces;
import android.os.AsyncTask;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile;
import java.io.File;
public class SaveGpxAsyncTask extends AsyncTask<Void, Void, Void> {
private final GPXFile gpx;
private final SaveGpxListener saveGpxListener;
public SaveGpxAsyncTask(@NonNull GPXFile gpx,
@Nullable SaveGpxListener saveGpxListener) {
this.gpx = gpx;
this.saveGpxListener = saveGpxListener;
}
@Override
protected void onPreExecute() {
if (saveGpxListener != null) {
saveGpxListener.gpxSavingStarted();
}
}
@Override
protected Void doInBackground(Void... params) {
GPXUtilities.writeGpxFile(new File(gpx.path), gpx);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
if (saveGpxListener != null) {
saveGpxListener.gpxSavingFinished();
}
}
public interface SaveGpxListener {
void gpxSavingStarted();
void gpxSavingFinished();
}
}

View file

@ -68,6 +68,7 @@ import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetAxisType;
import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetType; import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetType;
import net.osmand.plus.helpers.GpxUiHelper.OrderedLineDataSet; import net.osmand.plus.helpers.GpxUiHelper.OrderedLineDataSet;
import net.osmand.plus.measurementtool.NewGpxData; import net.osmand.plus.measurementtool.NewGpxData;
import net.osmand.plus.myplaces.SaveGpxAsyncTask.SaveGpxListener;
import net.osmand.plus.myplaces.TrackBitmapDrawer.TrackBitmapDrawerListener; import net.osmand.plus.myplaces.TrackBitmapDrawer.TrackBitmapDrawerListener;
import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.views.controls.PagerSlidingTabStrip; import net.osmand.plus.views.controls.PagerSlidingTabStrip;
@ -79,7 +80,6 @@ import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils; import net.osmand.util.MapUtils;
import java.io.File; import java.io.File;
import java.lang.ref.WeakReference;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
@ -788,10 +788,7 @@ public class TrackSegmentFragment extends OsmAndListFragment implements TrackBit
public boolean onMenuItemClick(MenuItem item) { public boolean onMenuItemClick(MenuItem item) {
int i = item.getItemId(); int i = item.getItemId();
if (i == R.id.action_edit) { if (i == R.id.action_edit) {
TrkSegment segment = getTrkSegment(); editSegment();
if (segment != null && fragmentAdapter != null) {
fragmentAdapter.addNewGpxData(NewGpxData.ActionType.EDIT_SEGMENT, segment);
}
return true; return true;
} else if (i == R.id.action_delete) { } else if (i == R.id.action_delete) {
TrackActivity activity = getTrackActivity(); TrackActivity activity = getTrackActivity();
@ -801,16 +798,7 @@ public class TrackSegmentFragment extends OsmAndListFragment implements TrackBit
builder.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { builder.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
TrackActivity trackActivity = getTrackActivity(); deleteAndSaveSegment();
if (trackActivity != null && deleteSegment()) {
GPXFile gpx = getGpx();
if (gpx != null && fragmentAdapter != null) {
boolean showOnMap = fragmentAdapter.isShowOnMap();
SelectedGpxFile sf = app.getSelectedGpxHelper().selectGpxFile(gpx, showOnMap, false);
new SaveGpxAsyncTask(trackActivity, TrackSegmentFragment.this, gpx, showOnMap ? sf : null)
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
} }
}); });
builder.setNegativeButton(R.string.shared_string_cancel, null); builder.setNegativeButton(R.string.shared_string_cancel, null);
@ -914,22 +902,10 @@ public class TrackSegmentFragment extends OsmAndListFragment implements TrackBit
public boolean onMenuItemClick(MenuItem item) { public boolean onMenuItemClick(MenuItem item) {
int i = item.getItemId(); int i = item.getItemId();
if (i == R.id.action_edit) { if (i == R.id.action_edit) {
TrkSegment segment = getTrkSegment(); editSegment();
if (segment != null && fragmentAdapter != null) {
fragmentAdapter.addNewGpxData(NewGpxData.ActionType.EDIT_SEGMENT, segment);
}
return true; return true;
} else if (i == R.id.action_delete) { } else if (i == R.id.action_delete) {
TrackActivity trackActivity = getTrackActivity(); deleteAndSaveSegment();
if (trackActivity != null && deleteSegment()) {
GPXFile gpx = getGpx();
if (gpx != null && fragmentAdapter != null) {
boolean showOnMap = fragmentAdapter.isShowOnMap();
SelectedGpxFile sf = app.getSelectedGpxHelper().selectGpxFile(gpx, showOnMap, false);
new SaveGpxAsyncTask(trackActivity, TrackSegmentFragment.this, gpx, showOnMap ? sf : null)
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
return true; return true;
} }
return false; return false;
@ -1023,22 +999,10 @@ public class TrackSegmentFragment extends OsmAndListFragment implements TrackBit
public boolean onMenuItemClick(MenuItem item) { public boolean onMenuItemClick(MenuItem item) {
int i = item.getItemId(); int i = item.getItemId();
if (i == R.id.action_edit) { if (i == R.id.action_edit) {
TrkSegment segment = getTrkSegment(); editSegment();
if (segment != null && fragmentAdapter != null) {
fragmentAdapter.addNewGpxData(NewGpxData.ActionType.EDIT_SEGMENT, segment);
}
return true; return true;
} else if (i == R.id.action_delete) { } else if (i == R.id.action_delete) {
TrackActivity trackActivity = getTrackActivity(); deleteAndSaveSegment();
if (trackActivity != null && deleteSegment()) {
GPXFile gpx = getGpx();
if (gpx != null && fragmentAdapter != null) {
boolean showOnMap = fragmentAdapter.isShowOnMap();
SelectedGpxFile sf = app.getSelectedGpxHelper().selectGpxFile(gpx, showOnMap, false);
new SaveGpxAsyncTask(trackActivity, TrackSegmentFragment.this, gpx, showOnMap ? sf : null)
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
return true; return true;
} }
return false; return false;
@ -1060,6 +1024,25 @@ public class TrackSegmentFragment extends OsmAndListFragment implements TrackBit
return view; return view;
} }
private void editSegment() {
TrkSegment segment = getTrkSegment();
if (segment != null && fragmentAdapter != null) {
fragmentAdapter.addNewGpxData(NewGpxData.ActionType.EDIT_SEGMENT, segment);
}
}
private void deleteAndSaveSegment() {
TrackActivity trackActivity = getTrackActivity();
if (trackActivity != null && deleteSegment()) {
GPXFile gpx = getGpx();
if (gpx != null && fragmentAdapter != null) {
boolean showOnMap = fragmentAdapter.isShowOnMap();
SelectedGpxFile selectedGpxFile = app.getSelectedGpxHelper().selectGpxFile(gpx, showOnMap, false);
saveGpx(showOnMap ? selectedGpxFile : null, gpx);
}
}
}
private boolean deleteSegment() { private boolean deleteSegment() {
TrkSegment segment = getTrkSegment(); TrkSegment segment = getTrkSegment();
if (segment != null) { if (segment != null) {
@ -1269,55 +1252,33 @@ public class TrackSegmentFragment extends OsmAndListFragment implements TrackBit
} }
} }
private static class SaveGpxAsyncTask extends AsyncTask<Void, Void, Void> { private void saveGpx(final SelectedGpxFile selectedGpxFile, GPXFile gpxFile) {
private final GPXFile gpx; new SaveGpxAsyncTask(gpxFile, new SaveGpxListener() {
private final SelectedGpxFile selectedGpx;
private OsmandApplication app;
private final WeakReference<TrackActivity> activityRef;
private final WeakReference<TrackSegmentFragment> fragmentRef;
SaveGpxAsyncTask(@NonNull TrackActivity activity,
@NonNull TrackSegmentFragment fragment,
@NonNull GPXFile gpx,
@Nullable SelectedGpxFile selectedGpx) {
this.gpx = gpx;
activityRef = new WeakReference<>(activity);
fragmentRef = new WeakReference<>(fragment);
app = activity.getMyApplication();
this.selectedGpx = selectedGpx;
}
@Override @Override
protected void onPreExecute() { public void gpxSavingStarted() {
TrackActivity activity = activityRef.get(); TrackActivity activity = getTrackActivity();
if (activity != null) { if (activity != null && AndroidUtils.isActivityNotDestroyed(activity)) {
activity.setSupportProgressBarIndeterminateVisibility(true); activity.setSupportProgressBarIndeterminateVisibility(true);
} }
} }
@Override @Override
protected Void doInBackground(Void... params) { public void gpxSavingFinished() {
GPXUtilities.writeGpxFile(new File(gpx.path), gpx); TrackActivity activity = getTrackActivity();
return null; if (activity != null) {
} if (selectedGpxFile != null) {
List<GpxDisplayGroup> groups = getDisplayGroups();
@Override
protected void onPostExecute(Void aVoid) {
TrackActivity activity = activityRef.get();
TrackSegmentFragment fragment = fragmentRef.get();
if (activity != null && fragment != null) {
if (selectedGpx != null) {
List<GpxDisplayGroup> groups = fragment.getDisplayGroups();
if (groups != null) { if (groups != null) {
selectedGpx.setDisplayGroups(groups, app); selectedGpxFile.setDisplayGroups(groups, app);
selectedGpx.processPoints(app); selectedGpxFile.processPoints(app);
} }
} }
fragment.updateContent(); updateContent();
if (!activity.isFinishing()) { if (AndroidUtils.isActivityNotDestroyed(activity)) {
activity.setSupportProgressBarIndeterminateVisibility(false); activity.setSupportProgressBarIndeterminateVisibility(false);
} }
} }
} }
}).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
} }

View file

@ -1,5 +1,6 @@
package net.osmand.plus.track; package net.osmand.plus.track;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.view.Gravity; import android.view.Gravity;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -11,6 +12,8 @@ import android.widget.LinearLayout;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.plus.GPXDatabase.GpxDataItem;
import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
@ -20,19 +23,27 @@ import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.base.ContextMenuFragment; import net.osmand.plus.base.ContextMenuFragment;
import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.myplaces.DirectionArrowsCard; import net.osmand.plus.myplaces.DirectionArrowsCard;
import net.osmand.plus.myplaces.SaveGpxAsyncTask;
import net.osmand.plus.routepreparationmenu.cards.BaseCard; import net.osmand.plus.routepreparationmenu.cards.BaseCard;
import java.io.File;
public class TrackAppearanceFragment extends ContextMenuFragment { public class TrackAppearanceFragment extends ContextMenuFragment {
public static final String SELECTED_TRACK_FILE_PATH = "selected_track_file_path"; public static final String SELECTED_TRACK_FILE_PATH = "selected_track_file_path";
private OsmandApplication app;
private GpxDataItem gpxDataItem;
private TrackDrawInfo trackDrawInfo;
private SelectedGpxFile selectedGpxFile; private SelectedGpxFile selectedGpxFile;
private int menuTitleHeight;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
OsmandApplication app = requireMyApplication(); app = requireMyApplication();
String gpxFilePath = null; String gpxFilePath = null;
Bundle arguments = getArguments(); Bundle arguments = getArguments();
@ -43,6 +54,10 @@ public class TrackAppearanceFragment extends ContextMenuFragment {
gpxFilePath = arguments.getString(SELECTED_TRACK_FILE_PATH); gpxFilePath = arguments.getString(SELECTED_TRACK_FILE_PATH);
} }
selectedGpxFile = app.getSelectedGpxHelper().getSelectedFileByPath(gpxFilePath); selectedGpxFile = app.getSelectedGpxHelper().getSelectedFileByPath(gpxFilePath);
File file = new File(selectedGpxFile.getGpxFile().path);
gpxDataItem = app.getGpxDbHelper().getItem(file);
trackDrawInfo = new TrackDrawInfo(gpxDataItem);
} }
@Override @Override
@ -96,7 +111,8 @@ public class TrackAppearanceFragment extends ContextMenuFragment {
saveButton.setOnClickListener(new View.OnClickListener() { saveButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
saveTrackInfo();
dismiss();
} }
}); });
@ -115,6 +131,36 @@ public class TrackAppearanceFragment extends ContextMenuFragment {
AndroidUiHelper.updateVisibility(view.findViewById(R.id.buttons_divider), true); AndroidUiHelper.updateVisibility(view.findViewById(R.id.buttons_divider), true);
} }
private void saveTrackInfo() {
GPXFile gpxFile = selectedGpxFile.getGpxFile();
gpxFile.setWidth(trackDrawInfo.getWidth());
gpxFile.setGradientScaleType(trackDrawInfo.getGradientScaleType().name());
gpxFile.setColor(trackDrawInfo.getColor());
gpxFile.setGradientScaleColor(GradientScaleType.SPEED.getTypeName(), trackDrawInfo.getGradientSpeedColor());
gpxFile.setGradientScaleColor(GradientScaleType.ALTITUDE.getTypeName(), trackDrawInfo.getGradientAltitudeColor());
gpxFile.setGradientScaleColor(GradientScaleType.SLOPE.getTypeName(), trackDrawInfo.getGradientSlopeColor());
for (GpxSplitType gpxSplitType : GpxSplitType.values()) {
if (gpxSplitType.getType() == trackDrawInfo.getSplitType()) {
gpxFile.setSplitType(gpxSplitType.name());
break;
}
}
gpxFile.setSplitInterval(trackDrawInfo.getSplitInterval());
gpxFile.setShowArrows(trackDrawInfo.isShowArrows());
gpxFile.setShowStartFinish(trackDrawInfo.isShowStartFinish());
app.getSelectedGpxHelper().updateSelectedGpxFile(selectedGpxFile);
saveGpx(gpxFile);
}
private void saveGpx(GPXFile gpxFile) {
new SaveGpxAsyncTask(gpxFile, null).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
private void updateCards() { private void updateCards() {
MapActivity mapActivity = getMapActivity(); MapActivity mapActivity = getMapActivity();
if (mapActivity != null) { if (mapActivity != null) {
@ -142,7 +188,15 @@ public class TrackAppearanceFragment extends ContextMenuFragment {
@Override @Override
public int getHeaderViewHeight() { public int getHeaderViewHeight() {
return 0; return menuTitleHeight;
}
@Override
protected void calculateLayout(View view, boolean initLayout) {
menuTitleHeight = view.findViewById(R.id.route_menu_top_shadow_all).getHeight()
+ view.findViewById(R.id.control_buttons).getHeight()
- view.findViewById(R.id.buttons_shadow).getHeight();
super.calculateLayout(view, initLayout);
} }
@Override @Override
@ -155,6 +209,10 @@ public class TrackAppearanceFragment extends ContextMenuFragment {
return 0; return 0;
} }
public float getMiddleStateKoef() {
return 0.5f;
}
public static boolean showInstance(@NonNull MapActivity mapActivity, TrackAppearanceFragment fragment) { public static boolean showInstance(@NonNull MapActivity mapActivity, TrackAppearanceFragment fragment) {
try { try {
mapActivity.getSupportFragmentManager() mapActivity.getSupportFragmentManager()