Fix saving imported files to database

This commit is contained in:
Chumva 2019-05-03 14:15:44 +03:00
parent 1fa2a75ff9
commit 1504b99811
5 changed files with 82 additions and 41 deletions

View file

@ -3,8 +3,8 @@ package net.osmand.plus;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import net.osmand.IndexConstants;
import net.osmand.GPXUtilities.GPXTrackAnalysis;
import net.osmand.IndexConstants;
import net.osmand.plus.api.SQLiteAPI.SQLiteConnection;
import net.osmand.plus.api.SQLiteAPI.SQLiteCursor;
import net.osmand.util.Algorithms;
@ -114,6 +114,27 @@ public class GPXDatabase {
GPX_COL_SHOW_AS_MARKERS +
" FROM " + GPX_TABLE_NAME;
private static final String GPX_TABLE_UPDATE_ANALYSIS = "UPDATE " +
GPX_TABLE_NAME + " SET " +
GPX_COL_TOTAL_DISTANCE + " = ?, " +
GPX_COL_TOTAL_TRACKS + " = ?, " +
GPX_COL_START_TIME + " = ?, " +
GPX_COL_END_TIME + " = ?, " +
GPX_COL_TIME_SPAN + " = ?, " +
GPX_COL_TIME_MOVING + " = ?, " +
GPX_COL_TOTAL_DISTANCE_MOVING + " = ?, " +
GPX_COL_DIFF_ELEVATION_UP + " = ?, " +
GPX_COL_DIFF_ELEVATION_DOWN + " = ?, " +
GPX_COL_AVG_ELEVATION + " = ?, " +
GPX_COL_MIN_ELEVATION + " = ?, " +
GPX_COL_MAX_ELEVATION + " = ?, " +
GPX_COL_MAX_SPEED + " = ?, " +
GPX_COL_AVG_SPEED + " = ?, " +
GPX_COL_POINTS + " = ?, " +
GPX_COL_WPT_POINTS + " = ?, " +
GPX_COL_FILE_LAST_MODIFIED_TIME + " = ?, " +
GPX_COL_WPT_CATEGORY_NAMES + " = ? ";
private OsmandApplication context;
public static class GpxDataItem {
@ -140,6 +161,7 @@ public class GPXDatabase {
return file;
}
@Nullable
public GPXTrackAnalysis getAnalysis() {
return analysis;
}
@ -456,26 +478,7 @@ public class GPXDatabase {
try {
String fileName = getFileName(item.file);
String fileDir = getFileDir(item.file);
db.execSQL("UPDATE " + GPX_TABLE_NAME + " SET " +
GPX_COL_TOTAL_DISTANCE + " = ?, " +
GPX_COL_TOTAL_TRACKS + " = ?, " +
GPX_COL_START_TIME + " = ?, " +
GPX_COL_END_TIME + " = ?, " +
GPX_COL_TIME_SPAN + " = ?, " +
GPX_COL_TIME_MOVING + " = ?, " +
GPX_COL_TOTAL_DISTANCE_MOVING + " = ?, " +
GPX_COL_DIFF_ELEVATION_UP + " = ?, " +
GPX_COL_DIFF_ELEVATION_DOWN + " = ?, " +
GPX_COL_AVG_ELEVATION + " = ?, " +
GPX_COL_MIN_ELEVATION + " = ?, " +
GPX_COL_MAX_ELEVATION + " = ?, " +
GPX_COL_MAX_SPEED + " = ?, " +
GPX_COL_AVG_SPEED + " = ?, " +
GPX_COL_POINTS + " = ?, " +
GPX_COL_WPT_POINTS + " = ?, " +
GPX_COL_FILE_LAST_MODIFIED_TIME + " = ?, " +
GPX_COL_WPT_CATEGORY_NAMES + " = ? " +
" WHERE " + GPX_COL_NAME + " = ? AND " + GPX_COL_DIR + " = ?",
db.execSQL(GPX_TABLE_UPDATE_ANALYSIS + " WHERE " + GPX_COL_NAME + " = ? AND " + GPX_COL_DIR + " = ?",
new Object[]{ a.totalDistance, a.totalTracks, a.startTime, a.endTime,
a.timeSpan, a.timeMoving, a.totalDistanceMoving, a.diffElevationUp,
a.diffElevationDown, a.avgElevation, a.minElevation, a.maxElevation,
@ -489,6 +492,23 @@ public class GPXDatabase {
return false;
}
public boolean clearAnalysis(GpxDataItem item) {
SQLiteConnection db = openConnection(false);
if (db != null) {
try {
Object[] bindArgs = new Object[20];
bindArgs[16] = 0;
bindArgs[18] = getFileName(item.file);
bindArgs[19] = getFileDir(item.file);
db.execSQL(GPX_TABLE_UPDATE_ANALYSIS + " WHERE " + GPX_COL_NAME + " = ? AND " + GPX_COL_DIR + " = ?", bindArgs);
} finally {
db.close();
}
return true;
}
return false;
}
private GpxDataItem readItem(SQLiteCursor query) {
String fileName = query.getString(0);
String fileDir = query.getString(1);

View file

@ -23,6 +23,8 @@ import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Toast;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.GPXUtilities.WptPt;
import net.osmand.IProgress;
import net.osmand.IndexConstants;
@ -32,8 +34,7 @@ import net.osmand.plus.AppInitializer;
import net.osmand.plus.AppInitializer.AppInitializeListener;
import net.osmand.plus.AppInitializer.InitEvents;
import net.osmand.plus.FavouritesDbHelper;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.plus.GPXDatabase;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.R;
@ -50,6 +51,8 @@ import net.osmand.plus.rastermaps.OsmandRasterMapsPlugin;
import net.osmand.plus.views.OsmandMapTileView;
import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
@ -65,7 +68,6 @@ import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.zip.ZipInputStream;
import org.apache.commons.logging.Log;
/**
* @author Koen Rabaey
@ -616,10 +618,21 @@ public class ImportHelper {
if (importDir.exists() && importDir.isDirectory() && importDir.canWrite()) {
final WptPt pt = gpxFile.findPointToShow();
final File toWrite = getFileToSave(fileName, importDir, pt);
boolean destinationExists = toWrite.exists();
Exception e = GPXUtilities.writeGpxFile(toWrite, gpxFile);
if(e == null) {
if (e == null) {
gpxFile.path = toWrite.getAbsolutePath();
app.getGpxDatabase().remove(new File(gpxFile.path));
File file = new File(gpxFile.path);
if (!destinationExists) {
GPXDatabase.GpxDataItem item = new GPXDatabase.GpxDataItem(file, gpxFile.getColor(0));
app.getGpxDatabase().add(item);
} else {
GPXDatabase.GpxDataItem item = app.getGpxDatabase().getItem(file);
if (item != null) {
app.getGpxDatabase().clearAnalysis(item);
}
}
warning = null;
} else {
warning = app.getString(R.string.error_reading_gpx);

View file

@ -9,12 +9,12 @@ import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import net.osmand.IndexConstants;
import net.osmand.plus.GPXDatabase;
import net.osmand.plus.GPXDatabase.GpxDataItem;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.GPXUtilities.GPXTrackAnalysis;
import net.osmand.IndexConstants;
import net.osmand.plus.GPXDatabase;
import net.osmand.plus.GPXDatabase.GpxDataItem;
import net.osmand.plus.GpxSelectionHelper;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
@ -58,7 +58,8 @@ public class AddTracksGroupBottomSheetDialogFragment extends AddGroupBottomSheet
@Override
protected void onItemClick(int position) {
GpxDataItem dataItem = gpxList.get(position - 1);
if (dataItem.getAnalysis().wptCategoryNames != null && dataItem.getAnalysis().wptCategoryNames.size() > 1) {
GPXTrackAnalysis analysis = dataItem.getAnalysis();
if (analysis != null && analysis.wptCategoryNames != null && analysis.wptCategoryNames.size() > 1) {
Bundle args = new Bundle();
args.putString(SelectWptCategoriesBottomSheetDialogFragment.GPX_FILE_PATH_KEY, dataItem.getFile().getAbsolutePath());
@ -131,9 +132,11 @@ public class AddTracksGroupBottomSheetDialogFragment extends AddGroupBottomSheet
processGPXFolder(gpxFile, sub);
} else if (gpxFile.isFile() && gpxFile.getName().toLowerCase().endsWith(".gpx")) {
GpxDataItem item = processedDataFiles.get(gpxFile);
GPXTrackAnalysis itemAnalysis = item != null ? item.getAnalysis() : null;
if (item == null
|| item.getFileLastModifiedTime() != gpxFile.lastModified()
|| item.getAnalysis().wptCategoryNames == null) {
|| itemAnalysis == null
|| itemAnalysis.wptCategoryNames == null) {
GPXFile f = GPXUtilities.loadGPXFile(gpxFile);
GPXTrackAnalysis analysis = f.getAnalysis(gpxFile.lastModified());
if (item == null) {
@ -144,7 +147,7 @@ public class AddTracksGroupBottomSheetDialogFragment extends AddGroupBottomSheet
}
}
processedDataFiles.put(gpxFile, item);
if (item.getAnalysis().wptPoints > 0) {
if (itemAnalysis != null && itemAnalysis.wptPoints > 0) {
gpxList.add(item);
}
}

View file

@ -5,6 +5,7 @@ import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import net.osmand.GPXUtilities.GPXTrackAnalysis;
import net.osmand.plus.GPXDatabase.GpxDataItem;
import net.osmand.plus.R;
@ -32,7 +33,8 @@ public class TracksGroupsAdapter extends GroupsAdapter {
MapMarkersGroupViewHolder markersGroupViewHolder = (MapMarkersGroupViewHolder) holder;
markersGroupViewHolder.icon.setImageDrawable(iconsCache.getThemedIcon(R.drawable.ic_action_polygom_dark));
markersGroupViewHolder.name.setText(gpx.getFile().getName().replace(".gpx", "").replace("/", " ").replace("_", " "));
markersGroupViewHolder.numberCount.setText(String.valueOf(gpx.getAnalysis().wptPoints));
GPXTrackAnalysis analysis = gpx.getAnalysis();
markersGroupViewHolder.numberCount.setText(analysis != null ? String.valueOf(analysis.wptPoints) : "");
String description = getDescription(gpx);
markersGroupViewHolder.description.setVisibility(description == null ? View.GONE : View.VISIBLE);
markersGroupViewHolder.description.setText(description);
@ -46,7 +48,8 @@ public class TracksGroupsAdapter extends GroupsAdapter {
@Nullable
private String getDescription(GpxDataItem item) {
Set<String> categories = item.getAnalysis().wptCategoryNames;
GPXTrackAnalysis analysis = item.getAnalysis();
Set<String> categories = analysis != null ? analysis.wptCategoryNames : null;
if (categories != null && !categories.isEmpty() && !(categories.size() == 1 && categories.contains(""))) {
StringBuilder sb = new StringBuilder();
Iterator<String> it = categories.iterator();

View file

@ -44,6 +44,11 @@ import android.widget.TextView;
import android.widget.Toast;
import net.osmand.AndroidUtils;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.GPXUtilities.GPXTrackAnalysis;
import net.osmand.GPXUtilities.Track;
import net.osmand.GPXUtilities.WptPt;
import net.osmand.IndexConstants;
import net.osmand.data.PointDescription;
import net.osmand.plus.ContextMenuAdapter;
@ -51,21 +56,16 @@ import net.osmand.plus.ContextMenuAdapter.ItemClickListener;
import net.osmand.plus.ContextMenuItem;
import net.osmand.plus.GPXDatabase;
import net.osmand.plus.GPXDatabase.GpxDataItem;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.GPXUtilities.GPXTrackAnalysis;
import net.osmand.GPXUtilities.Track;
import net.osmand.GPXUtilities.WptPt;
import net.osmand.plus.GpxSelectionHelper;
import net.osmand.plus.GpxSelectionHelper.GpxDisplayGroup;
import net.osmand.plus.GpxSelectionHelper.GpxDisplayItem;
import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.OsmandActionBarActivity;
import net.osmand.plus.activities.OsmandBaseExpandableListAdapter;
@ -1614,9 +1614,11 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment {
processGPXFolder(gpxFile, sub);
} else if (gpxFile.isFile() && gpxFile.getName().toLowerCase().endsWith(".gpx")) {
GpxDataItem item = processedDataFiles.get(gpxFile);
GPXTrackAnalysis itemAnalysis = item != null ? item.getAnalysis() : null;
if (item == null
|| item.getFileLastModifiedTime() != gpxFile.lastModified()
|| item.getAnalysis().wptCategoryNames == null) {
|| itemAnalysis == null
|| itemAnalysis.wptCategoryNames == null) {
GPXFile f = GPXUtilities.loadGPXFile(gpxFile);
GPXTrackAnalysis analysis = f.getAnalysis(gpxFile.lastModified());
if (item == null) {