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.GPXFile;
import net.osmand.plus.track.GpxSplitType;
import net.osmand.GPXUtilities.GPXTrackAnalysis;
import net.osmand.GPXUtilities.Route;
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.GPXDataSetType;
import net.osmand.plus.settings.backend.OsmandSettings.MetricsConstants;
import net.osmand.plus.track.GpxSplitType;
import net.osmand.plus.track.GradientScaleType;
import net.osmand.util.Algorithms;
@ -533,9 +533,7 @@ public class GpxSelectionHelper {
}
if (obj.has(GRADIENT_SCALE_TYPE)) {
String gradientScaleTypeName = obj.optString(GRADIENT_SCALE_TYPE);
if (!Algorithms.isEmpty(gradientScaleTypeName)) {
gpx.setGradientScaleType(GradientScaleType.valueOf(gradientScaleTypeName).getTypeName());
}
gpx.setGradientScaleType(gradientScaleTypeName);
}
if (obj.has(SHOW_START_FINISH)) {
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);
}
public float getMiddleStateKoef() {
return MIDDLE_STATE_KOEF;
}
public abstract int getToolbarHeight();
public boolean isSingleFragment() {
@ -605,7 +609,7 @@ public abstract class ContextMenuFragment extends BaseOsmAndFragment {
}
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() );
}

View file

@ -12,7 +12,6 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.GPXUtilities.WptPt;
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.mapcontextmenu.MapContextMenu;
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 java.io.File;
import java.util.Map;
public class WptPtEditorFragment extends PointEditorFragment {
@ -251,7 +251,7 @@ public class WptPtEditorFragment extends PointEditorFragment {
}
} else {
addWpt(gpx, description, name, category, color);
new SaveGpxAsyncTask(getMyApplication(), gpx, editor.isGpxSelected()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
saveGpx(getMyApplication(), gpx, editor.isGpxSelected());
}
syncGpx(gpx);
}
@ -284,7 +284,7 @@ public class WptPtEditorFragment extends PointEditorFragment {
} else {
gpx.updateWptPt(wpt, wpt.getLatitude(), wpt.getLongitude(),
System.currentTimeMillis(), description, name, category, color);
new SaveGpxAsyncTask(getMyApplication(), gpx, editor.isGpxSelected()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
saveGpx(getMyApplication(), gpx, editor.isGpxSelected());
}
syncGpx(gpx);
}
@ -309,7 +309,7 @@ public class WptPtEditorFragment extends PointEditorFragment {
savingTrackHelper.deletePointData(wpt);
} else {
gpx.deleteWptPt(wpt);
new SaveGpxAsyncTask(getMyApplication(), gpx, editor.isGpxSelected()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
saveGpx(getMyApplication(), gpx, editor.isGpxSelected());
}
syncGpx(gpx);
}
@ -378,28 +378,19 @@ public class WptPtEditorFragment extends PointEditorFragment {
return 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;
private void saveGpx(final OsmandApplication app, final GPXFile gpxFile, final boolean gpxSelected) {
new SaveGpxAsyncTask(gpxFile, new SaveGpxListener() {
@Override
public void gpxSavingStarted() {
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);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
if (!gpxSelected) {
app.getSelectedGpxHelper().setGpxFileToDisplay(gpx);
}
}
@Override
public void gpxSavingFinished() {
if (!gpxSelected) {
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.FragmentActivity;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.GPXUtilities.WptPt;
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.mapcontextmenu.MapContextMenu;
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 java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -269,7 +269,7 @@ public class WptPtEditorFragmentNew extends PointEditorFragmentNew {
}
} else {
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);
}
@ -303,7 +303,7 @@ public class WptPtEditorFragmentNew extends PointEditorFragmentNew {
} else {
gpx.updateWptPt(wpt, wpt.getLatitude(), wpt.getLongitude(),
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);
}
@ -332,7 +332,7 @@ public class WptPtEditorFragmentNew extends PointEditorFragmentNew {
savingTrackHelper.deletePointData(wpt);
} else {
gpx.deleteWptPt(wpt);
new SaveGpxAsyncTask(getMyApplication(), gpx, editor.isGpxSelected()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
saveGpx(getMyApplication(), gpx, editor.isGpxSelected());
}
syncGpx(gpx);
}
@ -478,28 +478,19 @@ public class WptPtEditorFragmentNew extends PointEditorFragmentNew {
return 0;
}
private static class SaveGpxAsyncTask extends AsyncTask<Void, Void, Void> {
private final OsmandApplication app;
private final GPXFile gpx;
private final boolean gpxSelected;
private void saveGpx(final OsmandApplication app, final GPXFile gpxFile, final boolean gpxSelected) {
new SaveGpxAsyncTask(gpxFile, new SaveGpxListener() {
@Override
public void gpxSavingStarted() {
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);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
if (!gpxSelected) {
app.getSelectedGpxHelper().setGpxFileToDisplay(gpx);
}
}
@Override
public void gpxSavingFinished() {
if (!gpxSelected) {
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.OrderedLineDataSet;
import net.osmand.plus.measurementtool.NewGpxData;
import net.osmand.plus.myplaces.SaveGpxAsyncTask.SaveGpxListener;
import net.osmand.plus.myplaces.TrackBitmapDrawer.TrackBitmapDrawerListener;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.views.controls.PagerSlidingTabStrip;
@ -79,7 +80,6 @@ import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils;
import java.io.File;
import java.lang.ref.WeakReference;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@ -788,10 +788,7 @@ public class TrackSegmentFragment extends OsmAndListFragment implements TrackBit
public boolean onMenuItemClick(MenuItem item) {
int i = item.getItemId();
if (i == R.id.action_edit) {
TrkSegment segment = getTrkSegment();
if (segment != null && fragmentAdapter != null) {
fragmentAdapter.addNewGpxData(NewGpxData.ActionType.EDIT_SEGMENT, segment);
}
editSegment();
return true;
} else if (i == R.id.action_delete) {
TrackActivity activity = getTrackActivity();
@ -801,16 +798,7 @@ public class TrackSegmentFragment extends OsmAndListFragment implements TrackBit
builder.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
TrackActivity trackActivity = getTrackActivity();
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);
}
}
deleteAndSaveSegment();
}
});
builder.setNegativeButton(R.string.shared_string_cancel, null);
@ -914,22 +902,10 @@ public class TrackSegmentFragment extends OsmAndListFragment implements TrackBit
public boolean onMenuItemClick(MenuItem item) {
int i = item.getItemId();
if (i == R.id.action_edit) {
TrkSegment segment = getTrkSegment();
if (segment != null && fragmentAdapter != null) {
fragmentAdapter.addNewGpxData(NewGpxData.ActionType.EDIT_SEGMENT, segment);
}
editSegment();
return true;
} else if (i == R.id.action_delete) {
TrackActivity trackActivity = getTrackActivity();
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);
}
}
deleteAndSaveSegment();
return true;
}
return false;
@ -1023,22 +999,10 @@ public class TrackSegmentFragment extends OsmAndListFragment implements TrackBit
public boolean onMenuItemClick(MenuItem item) {
int i = item.getItemId();
if (i == R.id.action_edit) {
TrkSegment segment = getTrkSegment();
if (segment != null && fragmentAdapter != null) {
fragmentAdapter.addNewGpxData(NewGpxData.ActionType.EDIT_SEGMENT, segment);
}
editSegment();
return true;
} else if (i == R.id.action_delete) {
TrackActivity trackActivity = getTrackActivity();
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);
}
}
deleteAndSaveSegment();
return true;
}
return false;
@ -1060,6 +1024,25 @@ public class TrackSegmentFragment extends OsmAndListFragment implements TrackBit
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() {
TrkSegment segment = getTrkSegment();
if (segment != null) {
@ -1269,55 +1252,33 @@ public class TrackSegmentFragment extends OsmAndListFragment implements TrackBit
}
}
private static class SaveGpxAsyncTask extends AsyncTask<Void, Void, Void> {
private final GPXFile gpx;
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
protected void onPreExecute() {
TrackActivity activity = activityRef.get();
if (activity != null) {
activity.setSupportProgressBarIndeterminateVisibility(true);
private void saveGpx(final SelectedGpxFile selectedGpxFile, GPXFile gpxFile) {
new SaveGpxAsyncTask(gpxFile, new SaveGpxListener() {
@Override
public void gpxSavingStarted() {
TrackActivity activity = getTrackActivity();
if (activity != null && AndroidUtils.isActivityNotDestroyed(activity)) {
activity.setSupportProgressBarIndeterminateVisibility(true);
}
}
}
@Override
protected Void doInBackground(Void... params) {
GPXUtilities.writeGpxFile(new File(gpx.path), gpx);
return null;
}
@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) {
selectedGpx.setDisplayGroups(groups, app);
selectedGpx.processPoints(app);
@Override
public void gpxSavingFinished() {
TrackActivity activity = getTrackActivity();
if (activity != null) {
if (selectedGpxFile != null) {
List<GpxDisplayGroup> groups = getDisplayGroups();
if (groups != null) {
selectedGpxFile.setDisplayGroups(groups, app);
selectedGpxFile.processPoints(app);
}
}
updateContent();
if (AndroidUtils.isActivityNotDestroyed(activity)) {
activity.setSupportProgressBarIndeterminateVisibility(false);
}
}
fragment.updateContent();
if (!activity.isFinishing()) {
activity.setSupportProgressBarIndeterminateVisibility(false);
}
}
}
}).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
}

View file

@ -1,5 +1,6 @@
package net.osmand.plus.track;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
@ -11,6 +12,8 @@ import android.widget.LinearLayout;
import androidx.annotation.NonNull;
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.OsmandApplication;
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.helpers.AndroidUiHelper;
import net.osmand.plus.myplaces.DirectionArrowsCard;
import net.osmand.plus.myplaces.SaveGpxAsyncTask;
import net.osmand.plus.routepreparationmenu.cards.BaseCard;
import java.io.File;
public class TrackAppearanceFragment extends ContextMenuFragment {
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 int menuTitleHeight;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OsmandApplication app = requireMyApplication();
app = requireMyApplication();
String gpxFilePath = null;
Bundle arguments = getArguments();
@ -43,6 +54,10 @@ public class TrackAppearanceFragment extends ContextMenuFragment {
gpxFilePath = arguments.getString(SELECTED_TRACK_FILE_PATH);
}
selectedGpxFile = app.getSelectedGpxHelper().getSelectedFileByPath(gpxFilePath);
File file = new File(selectedGpxFile.getGpxFile().path);
gpxDataItem = app.getGpxDbHelper().getItem(file);
trackDrawInfo = new TrackDrawInfo(gpxDataItem);
}
@Override
@ -96,7 +111,8 @@ public class TrackAppearanceFragment extends ContextMenuFragment {
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
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);
}
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() {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
@ -142,7 +188,15 @@ public class TrackAppearanceFragment extends ContextMenuFragment {
@Override
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
@ -155,6 +209,10 @@ public class TrackAppearanceFragment extends ContextMenuFragment {
return 0;
}
public float getMiddleStateKoef() {
return 0.5f;
}
public static boolean showInstance(@NonNull MapActivity mapActivity, TrackAppearanceFragment fragment) {
try {
mapActivity.getSupportFragmentManager()