From fea6b424ca3b4ede41e432fe45848c13d3659699 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Wed, 7 Oct 2020 04:06:52 +0300 Subject: [PATCH] Add xml files import after check for file signature --- .../osmand/plus/importfiles/ImportHelper.java | 8 ++++--- .../plus/importfiles/UriImportTask.java | 10 +++++++- .../plus/importfiles/XmlImportTask.java | 23 ++++++++++++------- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/importfiles/ImportHelper.java b/OsmAnd/src/net/osmand/plus/importfiles/ImportHelper.java index d8bca201b4..62e0f1dbd0 100644 --- a/OsmAnd/src/net/osmand/plus/importfiles/ImportHelper.java +++ b/OsmAnd/src/net/osmand/plus/importfiles/ImportHelper.java @@ -87,7 +87,9 @@ public class ImportHelper { public enum ImportType { SETTINGS(OSMAND_SETTINGS_FILE_EXT), ROUTING(ROUTING_FILE_EXT), - RENDERING(RENDERER_INDEX_EXT); + RENDERING(RENDERER_INDEX_EXT), + GPX(GPX_FILE_EXT), + KML(KML_SUFFIX); ImportType(String extension) { this.extension = extension; @@ -226,8 +228,8 @@ public class ImportHelper { executeImportTask(new GpxImportTask(this, activity, gpxFile, fileName, save, useImportDir, showInDetailsActivity)); } - private void handleGpxOrFavouritesImport(Uri fileUri, String fileName, boolean save, boolean useImportDir, - boolean forceImportFavourites, boolean forceImportGpx) { + protected void handleGpxOrFavouritesImport(Uri fileUri, String fileName, boolean save, boolean useImportDir, + boolean forceImportFavourites, boolean forceImportGpx) { executeImportTask(new GpxOrFavouritesImportTask(this, activity, fileUri, fileName, save, useImportDir, forceImportFavourites, forceImportGpx)); } diff --git a/OsmAnd/src/net/osmand/plus/importfiles/UriImportTask.java b/OsmAnd/src/net/osmand/plus/importfiles/UriImportTask.java index 0bc9909643..27b4a0fa73 100644 --- a/OsmAnd/src/net/osmand/plus/importfiles/UriImportTask.java +++ b/OsmAnd/src/net/osmand/plus/importfiles/UriImportTask.java @@ -7,6 +7,7 @@ import androidx.fragment.app.FragmentActivity; import net.osmand.AndroidUtils; import net.osmand.plus.R; +import net.osmand.plus.importfiles.ImportHelper.ImportType; import net.osmand.util.Algorithms; import java.io.File; @@ -19,6 +20,7 @@ import java.io.OutputStream; import static net.osmand.FileUtils.createUniqueFileName; import static net.osmand.IndexConstants.BINARY_MAP_INDEX_EXT; import static net.osmand.IndexConstants.MAPS_PATH; +import static net.osmand.IndexConstants.RENDERER_INDEX_EXT; import static net.osmand.IndexConstants.ROUTING_FILE_EXT; import static net.osmand.IndexConstants.SQLITE_EXT; import static net.osmand.IndexConstants.TEMP_DIR; @@ -99,7 +101,13 @@ class UriImportTask extends BaseImportAsyncTask { if (error == null && file.exists()) { Uri tempUri = AndroidUtils.getUriForFile(app, file); if (XML_FILE_SIGNATURE == fileSignature) { - importHelper.handleXmlFileImport(tempUri, null, null); + ImportType importType = XmlImportTask.checkImportType(app, tempUri); + if (importType == ImportType.RENDERING || importType == ImportType.ROUTING) { + String name = importType == ImportType.RENDERING ? "renderer" + RENDERER_INDEX_EXT : "router" + ROUTING_FILE_EXT; + importHelper.handleXmlFileImport(tempUri, name, null); + } else if (importType == ImportType.GPX || importType == ImportType.KML) { + importHelper.handleGpxOrFavouritesImport(tempUri, tempFileName, save, useImportDir, false, false); + } } else if (OBF_FILE_SIGNATURE == fileSignature) { String name = createUniqueFileName(app, "map", MAPS_PATH, BINARY_MAP_INDEX_EXT); importHelper.handleObfImport(tempUri, name); diff --git a/OsmAnd/src/net/osmand/plus/importfiles/XmlImportTask.java b/OsmAnd/src/net/osmand/plus/importfiles/XmlImportTask.java index bc66d77a76..6d45466bab 100644 --- a/OsmAnd/src/net/osmand/plus/importfiles/XmlImportTask.java +++ b/OsmAnd/src/net/osmand/plus/importfiles/XmlImportTask.java @@ -11,6 +11,7 @@ import net.osmand.CallbackWithObject; import net.osmand.IndexConstants; import net.osmand.PlatformUtil; import net.osmand.plus.AppInitializer.LoadRoutingFilesCallback; +import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.importfiles.ImportHelper.ImportType; import net.osmand.router.RoutingConfiguration.Builder; @@ -29,26 +30,26 @@ import static net.osmand.plus.AppInitializer.loadRoutingFiles; class XmlImportTask extends BaseImportAsyncTask { - private Uri intentUri; + private Uri uri; private String destFileName; private ImportType importType; private CallbackWithObject routingCallback; - public XmlImportTask(@NonNull FragmentActivity activity, @NonNull Uri intentUri, - @NonNull String fileName, @Nullable CallbackWithObject routingCallback) { + public XmlImportTask(@NonNull FragmentActivity activity, @NonNull Uri uri, + @NonNull String fileName, @Nullable CallbackWithObject routingCallback) { super(activity); - this.intentUri = intentUri; + this.uri = uri; this.destFileName = fileName; this.routingCallback = routingCallback; } @Override protected String doInBackground(Void... voids) { - checkImportType(); + importType = checkImportType(app, uri); if (importType != null) { File dest = getDestinationFile(); if (dest != null) { - return ImportHelper.copyFile(app, dest, intentUri, true); + return ImportHelper.copyFile(app, dest, uri, true); } } return app.getString(R.string.file_import_error, destFileName, app.getString(R.string.unsupported_type_error)); @@ -115,10 +116,11 @@ class XmlImportTask extends BaseImportAsyncTask { return null; } - private void checkImportType() { + protected static ImportType checkImportType(OsmandApplication app, Uri uri) { + ImportType importType = null; InputStream is = null; try { - is = app.getContentResolver().openInputStream(intentUri); + is = app.getContentResolver().openInputStream(uri); if (is != null) { XmlPullParser parser = PlatformUtil.newXMLPullParser(); parser.setInput(is, "UTF-8"); @@ -130,6 +132,10 @@ class XmlImportTask extends BaseImportAsyncTask { importType = ImportType.ROUTING; } else if ("renderingStyle".equals(name)) { importType = ImportType.RENDERING; + } else if ("gpx".equals(name)) { + importType = ImportType.GPX; + } else if ("kml".equals(name)) { + importType = ImportType.KML; } break; } @@ -145,5 +151,6 @@ class XmlImportTask extends BaseImportAsyncTask { } finally { Algorithms.closeStream(is); } + return importType; } } \ No newline at end of file