fix no gpx after change orientation and add asyncTask for saving gpx

This commit is contained in:
Chumva 2018-06-22 20:02:45 +03:00
parent 9ca2add2f5
commit 2cf1f35581
5 changed files with 81 additions and 69 deletions

View file

@ -31,6 +31,7 @@ import net.osmand.plus.OsmAndAppCustomization;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.mapmarkers.CoordinateInputDialogFragment;
import net.osmand.plus.measurementtool.NewGpxData;
import net.osmand.plus.myplaces.FavoritesActivity;
import net.osmand.plus.myplaces.SplitSegmentDialogFragment;
@ -407,6 +408,11 @@ 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);
}
}
}
OsmandFragmentPagerAdapter pagerAdapter = (OsmandFragmentPagerAdapter) viewPager.getAdapter();

View file

@ -66,7 +66,6 @@ import net.osmand.plus.R;
import net.osmand.plus.activities.SavingTrackHelper;
import net.osmand.plus.activities.TrackActivity;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.mapcontextmenu.editors.WptPtEditor;
import net.osmand.plus.mapmarkers.CoordinateInputBottomSheetDialogFragment.CoordinateInputFormatChangeListener;
import net.osmand.plus.mapmarkers.CoordinateInputFormats.DDM;
import net.osmand.plus.mapmarkers.CoordinateInputFormats.DMS;
@ -86,15 +85,12 @@ import java.util.Locale;
import static android.content.ClipDescription.MIMETYPE_TEXT_PLAIN;
import static android.content.Context.CLIPBOARD_SERVICE;
import static net.osmand.plus.mapmarkers.SaveAsTrackBottomSheetDialogFragment.COORDINATE_INPUT_MODE_KEY;
public class CoordinateInputDialogFragment extends DialogFragment implements OsmAndCompassListener, OsmAndLocationListener {
public static final String TAG = "CoordinateInputDialogFragment";
public static final String ADDED_MARKERS_NUMBER_KEY = "added_markers_number_key";
public static final String WAYPOINTS_MODE_KEY = "waypoints_mode_key";
public static final String ADDED_POINTS_NUMBER_KEY = "added_points_number_key";
protected WptPtEditor editor;
private GPXUtilities.GPXFile newGpxFile;
private OnMapMarkersSavedListener listener;
protected GPXUtilities.WptPt selectedWpt;
@ -111,11 +107,11 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
private boolean hasUnsavedChanges;
private boolean isSoftKeyboardShown;
private String wptCategory;
private boolean north = true;
private boolean east = true;
private Location location;
private String wptCategory = null;
private Float heading;
private boolean locationUpdateStarted;
private boolean compassUpdateAllowed = true;
@ -133,17 +129,15 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
newGpxFile = new GPXUtilities.GPXFile();
savingTrackHelper = getMyApplication().getSavingTrackHelper();
selectedGpxHelper = getMyApplication().getSelectedGpxHelper();
Bundle args = getArguments();
if (args != null) {
wptCategory = args.getString("wptCategory");
}
}
@Nullable
private GPXUtilities.GPXFile getGpx() {
TrackActivity activity = getTrackActivity();
return activity != null ? activity.getGpx() : newGpxFile;
if (activity != null) {
GPXUtilities.GPXFile gpx = activity.getGpx();
return gpx != null ? gpx : newGpxFile;
} else return newGpxFile;
}
@Nullable
@ -171,48 +165,18 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
selectedGpxHelper.setGpxFileToDisplay(gpx);
} else {
gpx.addWptPt(lat, lon, System.currentTimeMillis(), description, name, category, color);
new SaveGpxAsyncTask(getMyApplication(), gpx, false).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
syncGpx(gpx);
}
}
private static class SaveGpxAsyncTask extends AsyncTask<Void, Void, Void> {
private final OsmandApplication app;
private final GPXUtilities.GPXFile gpx;
private final boolean gpxSelected;
SaveGpxAsyncTask(OsmandApplication app, GPXUtilities.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);
}
}
}
protected void updateWpt(GPXUtilities.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);
selectedGpxHelper.setGpxFileToDisplay(gpx);
} else {
gpx.updateWptPt(selectedWpt, lat, lon, System.currentTimeMillis(), description, name, category, color);
new SaveGpxAsyncTask(getMyApplication(), gpx, false).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
syncGpx(gpx);
}
}
@ -222,7 +186,7 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
showSaveDialog();
} else {
GPXUtilities.GPXFile gpx = getGpx();
GPXUtilities.writeGpxFile(new File(gpx.path), gpx, getMyApplication());
new SaveGpxAsyncTask(getMyApplication(), gpx,null, false).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
syncGpx(gpx);
if (listener != null) {
listener.onMapMarkersSaved();
@ -238,13 +202,13 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
hasUnsavedChanges = false;
SaveAsTrackBottomSheetDialogFragment fragment = new SaveAsTrackBottomSheetDialogFragment();
Bundle args = new Bundle();
args.putInt(ADDED_MARKERS_NUMBER_KEY, getGpx().getPointsSize());
args.putBoolean(COORDINATE_INPUT_MODE_KEY, true);
args.putInt(ADDED_POINTS_NUMBER_KEY, getGpx().getPointsSize());
args.putBoolean(SaveAsTrackBottomSheetDialogFragment.COORDINATE_INPUT_MODE_KEY, true);
fragment.setArguments(args);
fragment.setListener(createSaveAsTrackFragmentListener());
fragment.show(getChildFragmentManager(), SaveAsTrackBottomSheetDialogFragment.TAG);
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
@ -269,6 +233,12 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
return dialog;
}
public void setGpx(GPXUtilities.GPXFile gpx) {
this.newGpxFile = gpx;
adapter.setGpx(gpx);
adapter.notifyDataSetChanged();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
@ -452,7 +422,7 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
hasUnsavedChanges = true;
}
});
TextView cancelButton = (TextView) mainView.findViewById(R.id.cancel_button);
cancelButton.setText(R.string.shared_string_cancel);
cancelButton.setOnClickListener(new View.OnClickListener() {
@ -554,7 +524,7 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
for (@IdRes int id : itemsIds) {
View itemView = keyboardView.findViewById(id);
Object item = getItemObjectById(id);
final boolean controlItem = id == R.id.keyboard_item_next_field
final boolean controlItem = id == R.id.keyboard_item_next_field
|| id == R.id.keyboard_item_backspace
|| id == R.id.keyboard_item_hide;
@ -1032,14 +1002,9 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
@Override
public void saveGpx(final String fileName) {
final File dir = getMyApplication().getAppPath(IndexConstants.GPX_INDEX_DIR + IndexConstants.MAP_MARKERS_INDEX_DIR);
if (!dir.exists()) {
dir.mkdirs();
}
File fout = new File(dir, fileName + ".gpx");
GPXUtilities.writeGpxFile(fout, getGpx(), getMyApplication());
new SaveGpxAsyncTask(app, getGpx(),fileName, false).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
hasUnsavedChanges = false;
getMyApplication().getMapMarkersHelper().addOrEnableGroup(getGpx());
app.getMapMarkersHelper().addOrEnableGroup(getGpx());
if (listener != null) {
listener.onMapMarkersSaved();
}
@ -1047,7 +1012,7 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
.setAction(R.string.shared_string_show, new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(app, getMyApplication().getAppCustomization().getTrackActivity());
Intent intent = new Intent(app, app.getAppCustomization().getTrackActivity());
intent.putExtra(TrackActivity.OPEN_POINTS_TAB, true);
intent.putExtra(TrackActivity.TRACK_FILE_NAME, getGpx().path);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
@ -1419,6 +1384,44 @@ 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 boolean gpxSelected;
private final String fileName;
SaveGpxAsyncTask(OsmandApplication app, GPXUtilities.GPXFile gpx, String fileName, boolean gpxSelected) {
this.app = app;
this.gpx = gpx;
this.fileName = fileName;
this.gpxSelected = gpxSelected;
}
@Override
protected Void doInBackground(Void... params) {
if (Algorithms.isEmpty(gpx.path)) {
if (!Algorithms.isEmpty(fileName)) {
final File dir = app.getAppPath(IndexConstants.GPX_INDEX_DIR + IndexConstants.MAP_MARKERS_INDEX_DIR);
if (!dir.exists()) {
dir.mkdirs();
}
File fout = new File(dir, fileName + ".gpx");
GPXUtilities.writeGpxFile(fout, gpx, app);
}
} else {
GPXUtilities.writeGpxFile(new File(gpx.path), gpx, app);
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
if (!gpxSelected) {
app.getSelectedGpxHelper().selectGpxFile(gpx, true, false);
}
}
}
interface OnMapMarkersSavedListener {
void onMapMarkersSaved();
}

View file

@ -31,7 +31,7 @@ import java.io.File;
import java.util.Date;
import static net.osmand.plus.helpers.ImportHelper.GPX_SUFFIX;
import static net.osmand.plus.mapmarkers.CoordinateInputDialogFragment.ADDED_MARKERS_NUMBER_KEY;
import static net.osmand.plus.mapmarkers.CoordinateInputDialogFragment.ADDED_POINTS_NUMBER_KEY;
public class SaveAsTrackBottomSheetDialogFragment extends BottomSheetDialogFragment {
@ -55,7 +55,7 @@ public class SaveAsTrackBottomSheetDialogFragment extends BottomSheetDialogFragm
if (args != null) {
isCoordInput = args.getBoolean(COORDINATE_INPUT_MODE_KEY);
if (isCoordInput) {
number = args.getInt(ADDED_MARKERS_NUMBER_KEY);
number = args.getInt(ADDED_POINTS_NUMBER_KEY);
}
}
portrait = AndroidUiHelper.isOrientationPortrait(getActivity());

View file

@ -21,7 +21,7 @@ import net.osmand.plus.UiUtilities.UpdateLocationViewCache;
public class CoordinateInputAdapter extends RecyclerView.Adapter<MapMarkerItemViewHolder> {
public static final String ADAPTER_POSITION_KEY = "adapter_position_key";
private final GPXUtilities.GPXFile gpx;
private GPXUtilities.GPXFile gpx;
private OsmandApplication app;
@ -63,7 +63,7 @@ public class CoordinateInputAdapter extends RecyclerView.Adapter<MapMarkerItemVi
GPXUtilities.WptPt wpt = getItem(position);
holder.iconDirection.setVisibility(View.VISIBLE);
holder.icon.setImageDrawable(getColoredIcon(R.drawable.ic_action_flag_dark, wpt.colourARGB));
holder.icon.setVisibility(View.GONE);
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));
@ -107,7 +107,11 @@ public class CoordinateInputAdapter extends RecyclerView.Adapter<MapMarkerItemVi
notifyDataSetChanged();
}
}
public void setGpx(GPXUtilities.GPXFile gpx) {
this.gpx = gpx;
}
private Drawable getColoredIcon(@DrawableRes int resId, @ColorRes int colorResId) {
return uiUtilities.getIcon(resId, colorResId);
}

View file

@ -12,6 +12,7 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AlertDialog;
@ -84,7 +85,6 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import static net.osmand.plus.mapmarkers.CoordinateInputDialogFragment.WAYPOINTS_MODE_KEY;
public class TrackPointFragment extends OsmandExpandableListFragment implements TrackBitmapDrawerListener {
@ -309,13 +309,12 @@ public class TrackPointFragment extends OsmandExpandableListFragment implements
}
private void openCoordinatesInput() {
Bundle args = new Bundle();
args.putBoolean(WAYPOINTS_MODE_KEY, true);
args.putString("wptCategory", "Waypoints");
CoordinateInputDialogFragment fragment = new CoordinateInputDialogFragment();
fragment.setRetainInstance(true);
fragment.setArguments(args);
fragment.show(getChildFragmentManager(), CoordinateInputDialogFragment.TAG);
FragmentManager fm = getFragmentManager();
if (fm != null) {
CoordinateInputDialogFragment fragment = new CoordinateInputDialogFragment();
fragment.setRetainInstance(true);
fragment.show(fm, CoordinateInputDialogFragment.TAG);
}
}
@Override