diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml
index 85e8ad3147..582183f383 100644
--- a/OsmAnd/res/values/strings.xml
+++ b/OsmAnd/res/values/strings.xml
@@ -11,6 +11,7 @@
Thx - Hardy
-->
+ File is already imported in OsmAnd
Logout successful
Clear OpenStreetMap OAuth token
Log in via OAuth
diff --git a/OsmAnd/src/net/osmand/plus/dialogs/ImportGpxBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/dialogs/ImportGpxBottomSheetDialogFragment.java
index 4bc4e1589a..b0b9587781 100644
--- a/OsmAnd/src/net/osmand/plus/dialogs/ImportGpxBottomSheetDialogFragment.java
+++ b/OsmAnd/src/net/osmand/plus/dialogs/ImportGpxBottomSheetDialogFragment.java
@@ -23,6 +23,8 @@ public class ImportGpxBottomSheetDialogFragment extends MenuBottomSheetDialogFra
private GPXFile gpxFile;
private String fileName;
+ private long fileSize;
+
private boolean save;
private boolean useImportDir;
@@ -38,6 +40,10 @@ public class ImportGpxBottomSheetDialogFragment extends MenuBottomSheetDialogFra
this.fileName = fileName;
}
+ public void setFileSize(long fileSize) {
+ this.fileSize = fileSize;
+ }
+
public void setSave(boolean save) {
this.save = save;
}
@@ -84,7 +90,7 @@ public class ImportGpxBottomSheetDialogFragment extends MenuBottomSheetDialogFra
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
- importHelper.handleGpxImport(gpxFile, fileName, save, useImportDir);
+ importHelper.handleGpxImport(gpxFile, fileName, fileSize, save, useImportDir);
dismiss();
}
})
diff --git a/OsmAnd/src/net/osmand/plus/importfiles/GpxImportTask.java b/OsmAnd/src/net/osmand/plus/importfiles/GpxImportTask.java
index 4a410172a7..82b6e1d555 100644
--- a/OsmAnd/src/net/osmand/plus/importfiles/GpxImportTask.java
+++ b/OsmAnd/src/net/osmand/plus/importfiles/GpxImportTask.java
@@ -10,6 +10,7 @@ import net.osmand.GPXUtilities.GPXFile;
import net.osmand.util.Algorithms;
import java.io.FileNotFoundException;
+import java.io.IOException;
import java.io.InputStream;
class GpxImportTask extends BaseImportAsyncTask {
@@ -17,13 +18,15 @@ class GpxImportTask extends BaseImportAsyncTask {
private ImportHelper importHelper;
private Uri gpxFile;
private String fileName;
+ private long fileSize;
+
private boolean save;
private boolean useImportDir;
private boolean showInDetailsActivity;
public GpxImportTask(@NonNull ImportHelper importHelper, @NonNull FragmentActivity activity,
- @NonNull Uri gpxFile, @NonNull String fileName, boolean save, boolean useImportDir,
- boolean showInDetailsActivity) {
+ @NonNull Uri gpxFile, @NonNull String fileName, boolean save, boolean useImportDir,
+ boolean showInDetailsActivity) {
super(activity);
this.importHelper = importHelper;
this.gpxFile = gpxFile;
@@ -39,10 +42,13 @@ class GpxImportTask extends BaseImportAsyncTask {
try {
is = app.getContentResolver().openInputStream(gpxFile);
if (is != null) {
+ fileSize = is.available();
return GPXUtilities.loadGPXFile(is);
}
} catch (FileNotFoundException e) {
//
+ } catch (IOException e) {
+ ImportHelper.log.error(e.getMessage(), e);
} catch (SecurityException e) {
ImportHelper.log.error(e.getMessage(), e);
} finally {
@@ -54,6 +60,6 @@ class GpxImportTask extends BaseImportAsyncTask {
@Override
protected void onPostExecute(GPXFile result) {
hideProgress();
- importHelper.handleResult(result, fileName, save, useImportDir, false, showInDetailsActivity);
+ importHelper.handleResult(result, fileName, fileSize, save, useImportDir, false, showInDetailsActivity);
}
}
\ No newline at end of file
diff --git a/OsmAnd/src/net/osmand/plus/importfiles/GpxOrFavouritesImportTask.java b/OsmAnd/src/net/osmand/plus/importfiles/GpxOrFavouritesImportTask.java
index ed1a16c3b1..0d8f788e23 100644
--- a/OsmAnd/src/net/osmand/plus/importfiles/GpxOrFavouritesImportTask.java
+++ b/OsmAnd/src/net/osmand/plus/importfiles/GpxOrFavouritesImportTask.java
@@ -10,25 +10,28 @@ import net.osmand.GPXUtilities.GPXFile;
import net.osmand.util.Algorithms;
import java.io.FileNotFoundException;
+import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
-import static net.osmand.plus.importfiles.KmlImportTask.loadGpxFromKml;
+import static net.osmand.plus.importfiles.KmlImportTask.convertKmlToGpxStream;
class GpxOrFavouritesImportTask extends BaseImportAsyncTask {
private ImportHelper importHelper;
private Uri fileUri;
private String fileName;
+ private long fileSize;
+
private boolean save;
private boolean useImportDir;
- private boolean forceImportFavourites;
private boolean forceImportGpx;
+ private boolean forceImportFavourites;
public GpxOrFavouritesImportTask(@NonNull ImportHelper importHelper, @NonNull FragmentActivity activity,
- @NonNull Uri fileUri, String fileName, boolean save, boolean useImportDir,
- boolean forceImportFavourites, boolean forceImportGpx) {
+ @NonNull Uri fileUri, String fileName, boolean save, boolean useImportDir,
+ boolean forceImportFavourites, boolean forceImportGpx) {
super(activity);
this.importHelper = importHelper;
this.fileUri = fileUri;
@@ -46,15 +49,24 @@ class GpxOrFavouritesImportTask extends BaseImportAsyncTask
try {
is = app.getContentResolver().openInputStream(fileUri);
if (is != null) {
+ fileSize = is.available();
if (fileName != null && fileName.endsWith(ImportHelper.KML_SUFFIX)) {
- return loadGpxFromKml(is);
+ InputStream gpxStream = convertKmlToGpxStream(is);
+ if (gpxStream != null) {
+ fileSize = gpxStream.available();
+ return GPXUtilities.loadGPXFile(gpxStream);
+ }
} else if (fileName != null && fileName.endsWith(ImportHelper.KMZ_SUFFIX)) {
try {
zis = new ZipInputStream(is);
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
if (entry.getName().endsWith(ImportHelper.KML_SUFFIX)) {
- return loadGpxFromKml(zis);
+ InputStream gpxStream = convertKmlToGpxStream(is);
+ if (gpxStream != null) {
+ fileSize = gpxStream.available();
+ return GPXUtilities.loadGPXFile(gpxStream);
+ }
}
}
} catch (Exception e) {
@@ -66,6 +78,8 @@ class GpxOrFavouritesImportTask extends BaseImportAsyncTask
}
} catch (FileNotFoundException e) {
//
+ } catch (IOException e) {
+ ImportHelper.log.error(e.getMessage(), e);
} catch (SecurityException e) {
ImportHelper.log.error(e.getMessage(), e);
} finally {
@@ -78,6 +92,6 @@ class GpxOrFavouritesImportTask extends BaseImportAsyncTask
@Override
protected void onPostExecute(GPXFile result) {
hideProgress();
- importHelper.importGpxOrFavourites(result, fileName, save, useImportDir, forceImportFavourites, forceImportGpx);
+ importHelper.importGpxOrFavourites(result, fileName, fileSize, save, useImportDir, forceImportFavourites, forceImportGpx);
}
}
\ No newline at end of file
diff --git a/OsmAnd/src/net/osmand/plus/importfiles/ImportHelper.java b/OsmAnd/src/net/osmand/plus/importfiles/ImportHelper.java
index c67ae8834e..1059a46bb0 100644
--- a/OsmAnd/src/net/osmand/plus/importfiles/ImportHelper.java
+++ b/OsmAnd/src/net/osmand/plus/importfiles/ImportHelper.java
@@ -31,6 +31,8 @@ import net.osmand.plus.activities.ActivityResultListener;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.TrackActivity;
import net.osmand.plus.dialogs.ImportGpxBottomSheetDialogFragment;
+import net.osmand.plus.helpers.GpxUiHelper;
+import net.osmand.plus.helpers.GpxUiHelper.GPXInfo;
import net.osmand.plus.measurementtool.MeasurementToolFragment;
import net.osmand.plus.settings.backend.SettingsHelper;
import net.osmand.plus.settings.backend.SettingsHelper.SettingsItem;
@@ -128,8 +130,8 @@ public class ImportHelper {
importFavoritesImpl(gpxFile, fileName, false);
}
- public void handleGpxImport(GPXFile result, String name, boolean save, boolean useImportDir) {
- handleResult(result, name, save, useImportDir, false);
+ public void handleGpxImport(GPXFile result, String name, long fileSize, boolean save, boolean useImportDir) {
+ handleResult(result, name, fileSize, save, useImportDir, false);
}
public boolean handleGpxImport(final Uri contentUri, final boolean useImportDir) {
@@ -387,12 +389,12 @@ public class ImportHelper {
return intent;
}
- protected void handleResult(GPXFile result, String name, boolean save,
+ protected void handleResult(GPXFile result, String name, long fileSize, boolean save,
boolean useImportDir, boolean forceImportFavourites) {
- handleResult(result, name, save, useImportDir, forceImportFavourites, true);
+ handleResult(result, name, fileSize, save, useImportDir, forceImportFavourites, true);
}
- protected void handleResult(final GPXFile result, final String name, final boolean save,
+ protected void handleResult(final GPXFile result, final String name, long fileSize, final boolean save,
final boolean useImportDir, boolean forceImportFavourites, boolean showInDetailsActivity) {
if (result != null) {
if (result.error != null) {
@@ -402,9 +404,15 @@ public class ImportHelper {
}
} else {
if (save) {
- executeImportTask(new SaveAsyncTask(result, name, useImportDir, showInDetailsActivity));
+ String existingFilePath = getExistingFilePath(name, fileSize);
+ if (existingFilePath != null) {
+ app.showToastMessage(R.string.file_already_imported);
+ showGpxInDetailsActivity(existingFilePath);
+ } else {
+ executeImportTask(new SaveAsyncTask(result, name, useImportDir, showInDetailsActivity));
+ }
} else {
- showGpxInDetailsActivity(result);
+ showGpxInDetailsActivity(result.path);
}
if (gpxImportCompleteListener != null) {
gpxImportCompleteListener.onImportComplete(true);
@@ -445,6 +453,20 @@ public class ImportHelper {
}
}
+ @Nullable
+ private String getExistingFilePath(String name, long fileSize) {
+ File dir = app.getAppPath(GPX_INDEX_DIR);
+ List gpxInfoList = GpxUiHelper.getSortedGPXFilesInfoByDate(dir, true);
+ for (GPXInfo gpxInfo : gpxInfoList) {
+ String fileName = gpxInfo.getFileName();
+ String nameWithoutDirs = Algorithms.getFileWithoutDirs(fileName);
+ if (nameWithoutDirs.equals(name) && gpxInfo.getFileSize() == fileSize) {
+ return fileName;
+ }
+ }
+ return null;
+ }
+
private String saveImport(final GPXFile gpxFile, final String fileName, final boolean useImportDir) {
final String warning;
@@ -531,7 +553,7 @@ public class ImportHelper {
}
if (success) {
if (showInDetailsActivity) {
- showGpxInDetailsActivity(result);
+ showGpxInDetailsActivity(result.path);
} else {
showPlanRouteFragment();
}
@@ -553,10 +575,10 @@ public class ImportHelper {
}
}
- private void showGpxInDetailsActivity(final GPXFile gpxFile) {
- if (gpxFile.path != null) {
+ private void showGpxInDetailsActivity(String gpxFilePath) {
+ if (!Algorithms.isEmpty(gpxFilePath)) {
Intent newIntent = new Intent(activity, app.getAppCustomization().getTrackActivity());
- newIntent.putExtra(TrackActivity.TRACK_FILE_NAME, gpxFile.path);
+ newIntent.putExtra(TrackActivity.TRACK_FILE_NAME, gpxFilePath);
newIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
activity.startActivity(newIntent);
}
@@ -576,9 +598,9 @@ public class ImportHelper {
}
}
- protected void importGpxOrFavourites(final GPXFile gpxFile, final String fileName, final boolean save,
- final boolean useImportDir, final boolean forceImportFavourites,
- final boolean forceImportGpx) {
+ protected void importGpxOrFavourites(final GPXFile gpxFile, final String fileName, final long fileSize,
+ final boolean save, final boolean useImportDir,
+ final boolean forceImportFavourites, final boolean forceImportGpx) {
if (gpxFile == null || gpxFile.isPointsEmpty()) {
if (forceImportFavourites) {
final DialogInterface.OnClickListener importAsTrackListener = new DialogInterface.OnClickListener() {
@@ -586,7 +608,7 @@ public class ImportHelper {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
- handleResult(gpxFile, fileName, save, useImportDir, true);
+ handleResult(gpxFile, fileName, fileSize, save, useImportDir, true);
break;
case DialogInterface.BUTTON_NEGATIVE:
dialog.dismiss();
@@ -602,7 +624,7 @@ public class ImportHelper {
.setNegativeButton(R.string.shared_string_cancel, importAsTrackListener)
.show();
} else {
- handleResult(gpxFile, fileName, save, useImportDir, false);
+ handleResult(gpxFile, fileName, fileSize, save, useImportDir, false);
}
return;
}
@@ -611,13 +633,14 @@ public class ImportHelper {
importFavoritesImpl(gpxFile, fileName, true);
} else if (fileName != null) {
if (forceImportGpx) {
- handleResult(gpxFile, fileName, save, useImportDir, false);
+ handleResult(gpxFile, fileName, fileSize, save, useImportDir, false);
} else {
ImportGpxBottomSheetDialogFragment fragment = new ImportGpxBottomSheetDialogFragment();
fragment.setUsedOnMap(true);
fragment.setImportHelper(this);
fragment.setGpxFile(gpxFile);
fragment.setFileName(fileName);
+ fragment.setFileSize(fileSize);
fragment.setSave(save);
fragment.setUseImportDir(useImportDir);
activity.getSupportFragmentManager().beginTransaction()
diff --git a/OsmAnd/src/net/osmand/plus/importfiles/KmlImportTask.java b/OsmAnd/src/net/osmand/plus/importfiles/KmlImportTask.java
index 8d48485b33..bab8e0df78 100644
--- a/OsmAnd/src/net/osmand/plus/importfiles/KmlImportTask.java
+++ b/OsmAnd/src/net/osmand/plus/importfiles/KmlImportTask.java
@@ -12,6 +12,7 @@ import net.osmand.util.Algorithms;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
+import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
@@ -20,11 +21,13 @@ class KmlImportTask extends BaseImportAsyncTask {
private ImportHelper importHelper;
private Uri uri;
private String name;
+ private long fileSize;
+
private boolean save;
private boolean useImportDir;
public KmlImportTask(@NonNull ImportHelper importHelper, @NonNull FragmentActivity activity,
- @NonNull Uri uri, String name, boolean save, boolean useImportDir) {
+ @NonNull Uri uri, String name, boolean save, boolean useImportDir) {
super(activity);
this.importHelper = importHelper;
this.uri = uri;
@@ -39,12 +42,18 @@ class KmlImportTask extends BaseImportAsyncTask {
try {
is = app.getContentResolver().openInputStream(uri);
if (is != null) {
- return loadGpxFromKml(is);
+ InputStream gpxStream = convertKmlToGpxStream(is);
+ if (gpxStream != null) {
+ fileSize = gpxStream.available();
+ return GPXUtilities.loadGPXFile(gpxStream);
+ }
}
} catch (FileNotFoundException e) {
//
} catch (SecurityException e) {
ImportHelper.log.error(e.getMessage(), e);
+ } catch (IOException e) {
+ ImportHelper.log.error(e.getMessage(), e);
} finally {
Algorithms.closeStream(is);
}
@@ -54,16 +63,16 @@ class KmlImportTask extends BaseImportAsyncTask {
@Override
protected void onPostExecute(GPXFile result) {
hideProgress();
- importHelper.handleResult(result, name, save, useImportDir, false);
+ importHelper.handleResult(result, name, fileSize, save, useImportDir, false);
}
- protected static GPXFile loadGpxFromKml(@NonNull InputStream is) {
+ protected static InputStream convertKmlToGpxStream(@NonNull InputStream is) {
String result = Kml2Gpx.toGpx(is);
if (result != null) {
try {
- return GPXUtilities.loadGPXFile(new ByteArrayInputStream(result.getBytes("UTF-8")));
+ return new ByteArrayInputStream(result.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
- return null;
+ ImportHelper.log.error(e.getMessage(), e);
}
}
return null;
diff --git a/OsmAnd/src/net/osmand/plus/importfiles/KmzImportTask.java b/OsmAnd/src/net/osmand/plus/importfiles/KmzImportTask.java
index 41d01be264..65b1e80c09 100644
--- a/OsmAnd/src/net/osmand/plus/importfiles/KmzImportTask.java
+++ b/OsmAnd/src/net/osmand/plus/importfiles/KmzImportTask.java
@@ -5,6 +5,7 @@ import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
+import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.util.Algorithms;
@@ -13,18 +14,20 @@ import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import static net.osmand.plus.importfiles.ImportHelper.KML_SUFFIX;
-import static net.osmand.plus.importfiles.KmlImportTask.loadGpxFromKml;
+import static net.osmand.plus.importfiles.KmlImportTask.convertKmlToGpxStream;
class KmzImportTask extends BaseImportAsyncTask {
private ImportHelper importHelper;
private Uri uri;
private String name;
+ private long fileSize;
+
private boolean save;
private boolean useImportDir;
public KmzImportTask(@NonNull ImportHelper importHelper, @NonNull FragmentActivity activity,
- @NonNull Uri uri, @NonNull String name, boolean save, boolean useImportDir) {
+ @NonNull Uri uri, @NonNull String name, boolean save, boolean useImportDir) {
super(activity);
this.importHelper = importHelper;
this.uri = uri;
@@ -45,7 +48,11 @@ class KmzImportTask extends BaseImportAsyncTask {
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
if (entry.getName().endsWith(KML_SUFFIX)) {
- return loadGpxFromKml(is);
+ InputStream gpxStream = convertKmlToGpxStream(is);
+ if (gpxStream != null) {
+ fileSize = gpxStream.available();
+ return GPXUtilities.loadGPXFile(gpxStream);
+ }
}
}
}
@@ -61,6 +68,6 @@ class KmzImportTask extends BaseImportAsyncTask {
@Override
protected void onPostExecute(GPXFile result) {
hideProgress();
- importHelper.handleResult(result, name, save, useImportDir, false);
+ importHelper.handleResult(result, name, fileSize, save, useImportDir, false);
}
}
\ No newline at end of file