Improved aidl import gpx

This commit is contained in:
Alexey Kulish 2017-07-07 19:31:25 +03:00
parent e3c6545bd4
commit 8a671f8572
6 changed files with 268 additions and 56 deletions

View file

@ -39,18 +39,10 @@ import net.osmand.aidl.maplayer.AddMapLayerParams;
import net.osmand.aidl.maplayer.RemoveMapLayerParams;
import net.osmand.aidl.maplayer.UpdateMapLayerParams;
// NOTE: Add new methods at the end of file!!!
interface IOsmAndAidlInterface {
boolean refreshMap();
boolean addFavoriteGroup(in AddFavoriteGroupParams params);
boolean removeFavoriteGroup(in RemoveFavoriteGroupParams params);
boolean updateFavoriteGroup(in UpdateFavoriteGroupParams params);
boolean addFavorite(in AddFavoriteParams params);
boolean removeFavorite(in RemoveFavoriteParams params);
boolean updateFavorite(in UpdateFavoriteParams params);
boolean addMapMarker(in AddMapMarkerParams params);
boolean removeMapMarker(in RemoveMapMarkerParams params);
boolean updateMapMarker(in UpdateMapMarkerParams params);
@ -74,4 +66,15 @@ interface IOsmAndAidlInterface {
boolean setMapLocation(in SetMapLocationParams params);
boolean calculateRoute(in CalculateRouteParams params);
boolean refreshMap();
boolean addFavoriteGroup(in AddFavoriteGroupParams params);
boolean removeFavoriteGroup(in RemoveFavoriteGroupParams params);
boolean updateFavoriteGroup(in UpdateFavoriteGroupParams params);
boolean addFavorite(in AddFavoriteParams params);
boolean removeFavorite(in RemoveFavoriteParams params);
boolean updateFavorite(in UpdateFavoriteParams params);
}

View file

@ -5,6 +5,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.ParcelFileDescriptor;
import android.view.View;
@ -20,13 +21,16 @@ import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon;
import net.osmand.data.PointDescription;
import net.osmand.plus.FavouritesDbHelper;
import net.osmand.plus.GPXDatabase.GpxDataItem;
import net.osmand.plus.GPXUtilities;
import net.osmand.plus.GPXUtilities.GPXFile;
import net.osmand.plus.GpxSelectionHelper;
import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
import net.osmand.plus.MapMarkersHelper;
import net.osmand.plus.MapMarkersHelper.MapMarker;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.dialogs.ConfigureMapMenu;
import net.osmand.plus.helpers.ColorDialogs;
import net.osmand.plus.views.AidlMapLayer;
import net.osmand.plus.views.MapInfoLayer;
@ -631,13 +635,71 @@ public class OsmandAidlApi {
return false;
}
boolean importGpxFromFile(File source, String destinationPath) {
private void finishGpxImport(boolean destinationExists, File destination, String color, boolean show) {
int col = ConfigureMapMenu.GpxAppearanceAdapter.parseTrackColor(
app.getRendererRegistry().getCurrentSelectedRenderer(), color);
if (!destinationExists) {
GpxDataItem gpxDataItem = new GpxDataItem(destination, col);
app.getGpxDatabase().add(gpxDataItem);
} else {
GpxDataItem item = app.getGpxDatabase().getItem(destination);
if (item != null) {
app.getGpxDatabase().updateColor(item, col);
}
}
final GpxSelectionHelper helper = app.getSelectedGpxHelper();
final SelectedGpxFile selectedGpx = helper.getSelectedFileByName(destination.getName());
if (selectedGpx != null) {
if (show) {
new AsyncTask<File, Void, GPXFile>() {
@Override
protected GPXFile doInBackground(File... files) {
return GPXUtilities.loadGPXFile(app, files[0]);
}
@Override
protected void onPostExecute(GPXFile gpx) {
if (gpx.warning == null) {
selectedGpx.setGpxFile(gpx);
refreshMap();
}
}
}.execute(destination);
} else {
helper.selectGpxFile(selectedGpx.getGpxFile(), false, false);
refreshMap();
}
} else if (show) {
new AsyncTask<File, Void, GPXFile>() {
@Override
protected GPXFile doInBackground(File... files) {
return GPXUtilities.loadGPXFile(app, files[0]);
}
@Override
protected void onPostExecute(GPXFile gpx) {
if (gpx.warning == null) {
helper.selectGpxFile(gpx, true, false);
refreshMap();
}
}
}.execute(destination);
}
}
boolean importGpxFromFile(File source, String destinationPath, String color, boolean show) {
if (source != null && !Algorithms.isEmpty(destinationPath)) {
if (source.exists() && source.canRead()) {
File destination = app.getAppPath(IndexConstants.GPX_INDEX_DIR + destinationPath);
if (destination.getParentFile().canWrite()) {
boolean destinationExists = destination.exists();
try {
Algorithms.fileCopy(source, destination);
finishGpxImport(destinationExists, destination, color, show);
return true;
} catch (IOException e) {
e.printStackTrace();
@ -648,18 +710,20 @@ public class OsmandAidlApi {
return false;
}
boolean importGpxFromUri(Uri gpxUri, String destinationPath) {
boolean importGpxFromUri(Uri gpxUri, String destinationPath, String color, boolean show) {
if (gpxUri != null && !Algorithms.isEmpty(destinationPath)) {
File destination = app.getAppPath(IndexConstants.GPX_INDEX_DIR + destinationPath);
ParcelFileDescriptor gpxParcelDescriptor = null;
ParcelFileDescriptor gpxParcelDescriptor;
try {
gpxParcelDescriptor = app.getContentResolver().openFileDescriptor(gpxUri, "r");
if (gpxParcelDescriptor != null) {
boolean destinationExists = destination.exists();
FileDescriptor fileDescriptor = gpxParcelDescriptor.getFileDescriptor();
InputStream is = new FileInputStream(fileDescriptor);
FileOutputStream fout = new FileOutputStream(destination);
try {
Algorithms.streamCopy(is, fout);
finishGpxImport(destinationExists, destination, color, show);
} finally {
try {
is.close();
@ -681,14 +745,16 @@ public class OsmandAidlApi {
return false;
}
boolean importGpxFromData(String sourceRawData, String destinationPath) {
boolean importGpxFromData(String sourceRawData, String destinationPath, String color, boolean show) {
if (!Algorithms.isEmpty(sourceRawData) && !Algorithms.isEmpty(destinationPath)) {
File destination = app.getAppPath(IndexConstants.GPX_INDEX_DIR + destinationPath);
try {
InputStream is = new ByteArrayInputStream(sourceRawData.getBytes());
FileOutputStream fout = new FileOutputStream(destination);
boolean destinationExists = destination.exists();
try {
Algorithms.streamCopy(is, fout);
finishGpxImport(destinationExists, destination, color, show);
} finally {
try {
is.close();
@ -713,9 +779,23 @@ public class OsmandAidlApi {
if (!Algorithms.isEmpty(fileName)) {
File f = app.getAppPath(IndexConstants.GPX_INDEX_DIR + fileName);
if (f.exists()) {
GPXFile gpx = GPXUtilities.loadGPXFile(app, f);
app.getSelectedGpxHelper().selectGpxFile(gpx, true, false);
refreshMap();
new AsyncTask<File, Void, GPXFile>() {
@Override
protected GPXFile doInBackground(File... files) {
return GPXUtilities.loadGPXFile(app, files[0]);
}
@Override
protected void onPostExecute(GPXFile gpx) {
if (gpx.warning == null) {
app.getSelectedGpxHelper().selectGpxFile(gpx, true, false);
refreshMap();
}
}
}.execute(f);
return true;
}
}

View file

@ -227,11 +227,14 @@ public class OsmandAidlService extends Service {
public boolean importGpx(ImportGpxParams params) throws RemoteException {
if (params != null && !Algorithms.isEmpty(params.getDestinationPath())) {
if (params.getGpxFile() != null) {
return getApi().importGpxFromFile(params.getGpxFile(), params.getDestinationPath());
return getApi().importGpxFromFile(params.getGpxFile(), params.getDestinationPath(),
params.getColor(), params.isShow());
} else if (params.getGpxUri() != null) {
return getApi().importGpxFromUri(params.getGpxUri(), params.getDestinationPath());
return getApi().importGpxFromUri(params.getGpxUri(), params.getDestinationPath(),
params.getColor(), params.isShow());
} else if (params.getSourceRawData() != null) {
return getApi().importGpxFromData(params.getSourceRawData(), params.getDestinationPath());
return getApi().importGpxFromData(params.getSourceRawData(), params.getDestinationPath(),
params.getColor(), params.isShow());
}
}
return false;

View file

@ -12,20 +12,28 @@ public class ImportGpxParams implements Parcelable {
private Uri gpxUri;
private String sourceRawData;
private String destinationPath;
private String color;
private boolean show;
public ImportGpxParams(File gpxFile, String destinationPath) {
public ImportGpxParams(File gpxFile, String destinationPath, String color, boolean show) {
this.gpxFile = gpxFile;
this.destinationPath = destinationPath;
this.color = color;
this.show = show;
}
public ImportGpxParams(Uri gpxUri, String destinationPath) {
public ImportGpxParams(Uri gpxUri, String destinationPath, String color, boolean show) {
this.gpxUri = gpxUri;
this.destinationPath = destinationPath;
this.color = color;
this.show = show;
}
public ImportGpxParams(String sourceRawData, String destinationPath) {
public ImportGpxParams(String sourceRawData, String destinationPath, String color, boolean show) {
this.sourceRawData = sourceRawData;
this.destinationPath = destinationPath;
this.color = color;
this.show = show;
}
public ImportGpxParams(Parcel in) {
@ -59,6 +67,14 @@ public class ImportGpxParams implements Parcelable {
return destinationPath;
}
public String getColor() {
return color;
}
public boolean isShow() {
return show;
}
public void writeToParcel(Parcel out, int flags) {
if (gpxFile != null) {
out.writeString(gpxFile.getAbsolutePath());
@ -68,6 +84,8 @@ public class ImportGpxParams implements Parcelable {
out.writeParcelable(gpxUri, flags);
out.writeString(sourceRawData);
out.writeString(destinationPath);
out.writeString(color);
out.writeByte((byte) (show ? 1 : 0));
}
private void readFromParcel(Parcel in) {
@ -78,9 +96,11 @@ public class ImportGpxParams implements Parcelable {
gpxUri = in.readParcelable(Uri.class.getClassLoader());
sourceRawData = in.readString();
destinationPath = in.readString();
color = in.readString();
show = in.readByte() == 1;
}
public int describeContents() {
return 0;
}
}
}

View file

@ -13,7 +13,7 @@ import java.util.List;
public class GPXDatabase {
private static final String DB_NAME = "gpx_database";
private static final int DB_VERSION = 2;
private static final int DB_VERSION = 3;
private static final String GPX_TABLE_NAME = "gpxTable";
private static final String GPX_COL_NAME = "fileName";
private static final String GPX_COL_DIR = "fileDir";
@ -38,6 +38,7 @@ public class GPXDatabase {
private static final String GPX_COL_WPT_POINTS = "wptPoints";
private static final String GPX_COL_COLOR = "color";
private static final String GPX_COL_FILE_LAST_MODIFIED_TIME = "fileLastModifiedTime";
private static final String GPX_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS " + GPX_TABLE_NAME + " (" +
GPX_COL_NAME + " TEXT, " +
@ -61,14 +62,30 @@ public class GPXDatabase {
GPX_COL_POINTS + " int, " +
GPX_COL_WPT_POINTS + " int, " +
GPX_COL_COLOR + " TEXT);";
GPX_COL_COLOR + " TEXT, " +
GPX_COL_FILE_LAST_MODIFIED_TIME + " long);";
private static final String GPX_TABLE_SELECT = "SELECT " + GPX_COL_NAME + ", " + GPX_COL_DIR + "," + 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_COLOR +
private static final String GPX_TABLE_SELECT = "SELECT " +
GPX_COL_NAME + ", " +
GPX_COL_DIR + "," +
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_COLOR + ", " +
GPX_COL_FILE_LAST_MODIFIED_TIME +
" FROM " + GPX_TABLE_NAME;
private OsmandApplication context;
@ -77,12 +94,18 @@ public class GPXDatabase {
private File file;
private GPXTrackAnalysis analysis;
private int color;
private long fileLastModifiedTime;
public GpxDataItem(File file, GPXTrackAnalysis analysis) {
this.file = file;
this.analysis = analysis;
}
public GpxDataItem(File file, int color) {
this.file = file;
this.color = color;
}
public File getFile() {
return file;
}
@ -95,8 +118,8 @@ public class GPXDatabase {
return color;
}
public void setColor(int color) {
this.color = color;
public long getFileLastModifiedTime() {
return fileLastModifiedTime;
}
}
@ -117,7 +140,6 @@ public class GPXDatabase {
onUpgrade(conn, conn.getVersion(), DB_VERSION);
}
conn.setVersion(DB_VERSION);
}
return conn;
}
@ -130,6 +152,33 @@ public class GPXDatabase {
if (oldVersion < 2) {
db.execSQL("ALTER TABLE " + GPX_TABLE_NAME + " ADD " + GPX_COL_COLOR + " TEXT");
}
if (oldVersion < 3) {
db.execSQL("ALTER TABLE " + GPX_TABLE_NAME + " ADD " + GPX_COL_FILE_LAST_MODIFIED_TIME + " long");
List<GpxDataItem> items = getItems();
for (GpxDataItem item : items) {
updateLastModifiedTime(item);
}
}
}
private boolean updateLastModifiedTime(GpxDataItem item) {
SQLiteConnection db = openConnection(false);
if (db != null) {
try {
String fileName = getFileName(item.file);
String fileDir = getFileDir(item.file);
long fileLastModifiedTime = item.file.lastModified();
db.execSQL("UPDATE " + GPX_TABLE_NAME + " SET " +
GPX_COL_FILE_LAST_MODIFIED_TIME + " = ? " +
" WHERE " + GPX_COL_NAME + " = ? AND " + GPX_COL_DIR + " = ?",
new Object[] { fileLastModifiedTime, fileName, fileDir });
item.fileLastModifiedTime = fileLastModifiedTime;
} finally {
db.close();
}
return true;
}
return false;
}
public boolean rename(File currentFile, File newFile) {
@ -163,7 +212,7 @@ public class GPXDatabase {
GPX_COL_COLOR + " = ? " +
" WHERE " + GPX_COL_NAME + " = ? AND " + GPX_COL_DIR + " = ?",
new Object[] { (color == 0 ? "" : Algorithms.colorToString(color)), fileName, fileDir });
item.setColor(color);
item.color = color;
} finally {
db.close();
}
@ -225,11 +274,57 @@ public class GPXDatabase {
} else {
color = Algorithms.colorToString(item.color);
}
db.execSQL(
"INSERT INTO " + GPX_TABLE_NAME + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
new Object[] { fileName, fileDir, a.totalDistance, a.totalTracks, a.startTime, a.endTime,
a.timeSpan, a.timeMoving, a.totalDistanceMoving, a.diffElevationUp, a.diffElevationDown,
a.avgElevation, a.minElevation, a.maxElevation, a.maxSpeed, a.avgSpeed, a.points, a.wptPoints, color });
if (a != null) {
db.execSQL(
"INSERT INTO " + GPX_TABLE_NAME + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
new Object[]{ fileName, fileDir, a.totalDistance, a.totalTracks, a.startTime, a.endTime,
a.timeSpan, a.timeMoving, a.totalDistanceMoving, a.diffElevationUp, a.diffElevationDown,
a.avgElevation, a.minElevation, a.maxElevation, a.maxSpeed, a.avgSpeed, a.points, a.wptPoints,
color, item.file.lastModified() });
} else {
db.execSQL(
"INSERT INTO " + GPX_TABLE_NAME + "(" + GPX_COL_NAME + ", " + GPX_COL_DIR + ", " +
GPX_COL_COLOR + ", " + GPX_COL_FILE_LAST_MODIFIED_TIME + ") VALUES (?, ?, ?, ?)",
new Object[]{ fileName, fileDir, color, 0 });
}
}
public boolean updateAnalysis(GpxDataItem item, GPXTrackAnalysis a) {
SQLiteConnection db = openConnection(false);
if (db != null && a != null) {
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 + " = ? " +
" 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,
a.maxSpeed, a.avgSpeed, a.points, a.wptPoints, item.file.lastModified(),
fileName, fileDir });
} finally {
db.close();
}
return true;
}
return false;
}
private GpxDataItem readItem(SQLiteCursor query) {
@ -252,6 +347,7 @@ public class GPXDatabase {
int points = (int)query.getInt(16);
int wptPoints = (int)query.getInt(17);
String color = query.getString(18);
long fileLastModifiedTime = query.getLong(19);
GPXTrackAnalysis a = new GPXTrackAnalysis();
a.totalDistance = totalDistance;
@ -280,10 +376,11 @@ public class GPXDatabase {
}
GpxDataItem item = new GpxDataItem(new File(dir, fileName), a);
try {
item.setColor(Algorithms.isEmpty(color) ? 0 : Algorithms.parseColor(color));
item.color = Algorithms.isEmpty(color) ? 0 : Algorithms.parseColor(color);
} catch (IllegalArgumentException e) {
item.setColor(0);
item.color = 0;
}
item.fileLastModifiedTime = fileLastModifiedTime;
return item;
}

View file

@ -46,6 +46,7 @@ import net.osmand.data.PointDescription;
import net.osmand.plus.ContextMenuAdapter;
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.plus.GPXUtilities;
import net.osmand.plus.GPXUtilities.GPXFile;
@ -86,6 +87,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
@ -1511,12 +1513,13 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment {
public class ProcessGpxTask extends AsyncTask<Void, GpxDataItem, Void> {
private List<File> processedDataFiles = new ArrayList<>();
private Map<File, GpxDataItem> processedDataFiles = new HashMap<>();
private GPXDatabase db = app.getGpxDatabase();
ProcessGpxTask() {
List<GpxDataItem> dataItems = app.getGpxDatabase().getItems();
List<GpxDataItem> dataItems = db.getItems();
for (GpxDataItem item : dataItems) {
processedDataFiles.add(item.getFile());
processedDataFiles.put(item.getFile(), item);
}
}
@ -1545,17 +1548,23 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment {
String sub = gpxSubfolder.length() == 0 ?
gpxFile.getName() : gpxSubfolder + "/" + gpxFile.getName();
processGPXFolder(gpxFile, sub);
} else if (gpxFile.isFile() && gpxFile.getName().toLowerCase().endsWith(".gpx") && !processedDataFiles.contains(gpxFile)) {
GPXFile f = GPXUtilities.loadGPXFile(app, gpxFile);
GPXTrackAnalysis analysis = f.getAnalysis(gpxFile.lastModified());
GpxDataItem newItem = new GpxDataItem(gpxFile, analysis);
app.getGpxDatabase().add(newItem);
if (isCancelled()) {
break;
} else if (gpxFile.isFile() && gpxFile.getName().toLowerCase().endsWith(".gpx")) {
GpxDataItem item = processedDataFiles.get(gpxFile);
if (item == null || item.getFileLastModifiedTime() != gpxFile.lastModified()) {
GPXFile f = GPXUtilities.loadGPXFile(app, gpxFile);
GPXTrackAnalysis analysis = f.getAnalysis(gpxFile.lastModified());
if (item == null) {
item = new GpxDataItem(gpxFile, analysis);
db.add(item);
} else {
db.updateAnalysis(item, analysis);
}
}
processedDataFiles.add(gpxFile);
publishProgress(newItem);
processedDataFiles.put(gpxFile, item);
publishProgress(item);
}
if (isCancelled()) {
break;
}
}
}