Add xml files import after check for file signature

This commit is contained in:
Vitaliy 2020-10-07 04:06:52 +03:00
parent 27b235cb39
commit fea6b424ca
3 changed files with 29 additions and 12 deletions

View file

@ -87,7 +87,9 @@ public class ImportHelper {
public enum ImportType { public enum ImportType {
SETTINGS(OSMAND_SETTINGS_FILE_EXT), SETTINGS(OSMAND_SETTINGS_FILE_EXT),
ROUTING(ROUTING_FILE_EXT), ROUTING(ROUTING_FILE_EXT),
RENDERING(RENDERER_INDEX_EXT); RENDERING(RENDERER_INDEX_EXT),
GPX(GPX_FILE_EXT),
KML(KML_SUFFIX);
ImportType(String extension) { ImportType(String extension) {
this.extension = extension; this.extension = extension;
@ -226,8 +228,8 @@ public class ImportHelper {
executeImportTask(new GpxImportTask(this, activity, gpxFile, fileName, save, useImportDir, showInDetailsActivity)); executeImportTask(new GpxImportTask(this, activity, gpxFile, fileName, save, useImportDir, showInDetailsActivity));
} }
private void handleGpxOrFavouritesImport(Uri fileUri, String fileName, boolean save, boolean useImportDir, protected void handleGpxOrFavouritesImport(Uri fileUri, String fileName, boolean save, boolean useImportDir,
boolean forceImportFavourites, boolean forceImportGpx) { boolean forceImportFavourites, boolean forceImportGpx) {
executeImportTask(new GpxOrFavouritesImportTask(this, activity, fileUri, fileName, save, useImportDir, forceImportFavourites, forceImportGpx)); executeImportTask(new GpxOrFavouritesImportTask(this, activity, fileUri, fileName, save, useImportDir, forceImportFavourites, forceImportGpx));
} }

View file

@ -7,6 +7,7 @@ import androidx.fragment.app.FragmentActivity;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.importfiles.ImportHelper.ImportType;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import java.io.File; import java.io.File;
@ -19,6 +20,7 @@ import java.io.OutputStream;
import static net.osmand.FileUtils.createUniqueFileName; import static net.osmand.FileUtils.createUniqueFileName;
import static net.osmand.IndexConstants.BINARY_MAP_INDEX_EXT; import static net.osmand.IndexConstants.BINARY_MAP_INDEX_EXT;
import static net.osmand.IndexConstants.MAPS_PATH; 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.ROUTING_FILE_EXT;
import static net.osmand.IndexConstants.SQLITE_EXT; import static net.osmand.IndexConstants.SQLITE_EXT;
import static net.osmand.IndexConstants.TEMP_DIR; import static net.osmand.IndexConstants.TEMP_DIR;
@ -99,7 +101,13 @@ class UriImportTask extends BaseImportAsyncTask<Void, Void, String> {
if (error == null && file.exists()) { if (error == null && file.exists()) {
Uri tempUri = AndroidUtils.getUriForFile(app, file); Uri tempUri = AndroidUtils.getUriForFile(app, file);
if (XML_FILE_SIGNATURE == fileSignature) { 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) { } else if (OBF_FILE_SIGNATURE == fileSignature) {
String name = createUniqueFileName(app, "map", MAPS_PATH, BINARY_MAP_INDEX_EXT); String name = createUniqueFileName(app, "map", MAPS_PATH, BINARY_MAP_INDEX_EXT);
importHelper.handleObfImport(tempUri, name); importHelper.handleObfImport(tempUri, name);

View file

@ -11,6 +11,7 @@ import net.osmand.CallbackWithObject;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.plus.AppInitializer.LoadRoutingFilesCallback; import net.osmand.plus.AppInitializer.LoadRoutingFilesCallback;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.importfiles.ImportHelper.ImportType; import net.osmand.plus.importfiles.ImportHelper.ImportType;
import net.osmand.router.RoutingConfiguration.Builder; import net.osmand.router.RoutingConfiguration.Builder;
@ -29,26 +30,26 @@ import static net.osmand.plus.AppInitializer.loadRoutingFiles;
class XmlImportTask extends BaseImportAsyncTask<Void, Void, String> { class XmlImportTask extends BaseImportAsyncTask<Void, Void, String> {
private Uri intentUri; private Uri uri;
private String destFileName; private String destFileName;
private ImportType importType; private ImportType importType;
private CallbackWithObject routingCallback; private CallbackWithObject routingCallback;
public XmlImportTask(@NonNull FragmentActivity activity, @NonNull Uri intentUri, public XmlImportTask(@NonNull FragmentActivity activity, @NonNull Uri uri,
@NonNull String fileName, @Nullable CallbackWithObject routingCallback) { @NonNull String fileName, @Nullable CallbackWithObject routingCallback) {
super(activity); super(activity);
this.intentUri = intentUri; this.uri = uri;
this.destFileName = fileName; this.destFileName = fileName;
this.routingCallback = routingCallback; this.routingCallback = routingCallback;
} }
@Override @Override
protected String doInBackground(Void... voids) { protected String doInBackground(Void... voids) {
checkImportType(); importType = checkImportType(app, uri);
if (importType != null) { if (importType != null) {
File dest = getDestinationFile(); File dest = getDestinationFile();
if (dest != null) { 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)); return app.getString(R.string.file_import_error, destFileName, app.getString(R.string.unsupported_type_error));
@ -115,10 +116,11 @@ class XmlImportTask extends BaseImportAsyncTask<Void, Void, String> {
return null; return null;
} }
private void checkImportType() { protected static ImportType checkImportType(OsmandApplication app, Uri uri) {
ImportType importType = null;
InputStream is = null; InputStream is = null;
try { try {
is = app.getContentResolver().openInputStream(intentUri); is = app.getContentResolver().openInputStream(uri);
if (is != null) { if (is != null) {
XmlPullParser parser = PlatformUtil.newXMLPullParser(); XmlPullParser parser = PlatformUtil.newXMLPullParser();
parser.setInput(is, "UTF-8"); parser.setInput(is, "UTF-8");
@ -130,6 +132,10 @@ class XmlImportTask extends BaseImportAsyncTask<Void, Void, String> {
importType = ImportType.ROUTING; importType = ImportType.ROUTING;
} else if ("renderingStyle".equals(name)) { } else if ("renderingStyle".equals(name)) {
importType = ImportType.RENDERING; importType = ImportType.RENDERING;
} else if ("gpx".equals(name)) {
importType = ImportType.GPX;
} else if ("kml".equals(name)) {
importType = ImportType.KML;
} }
break; break;
} }
@ -145,5 +151,6 @@ class XmlImportTask extends BaseImportAsyncTask<Void, Void, String> {
} finally { } finally {
Algorithms.closeStream(is); Algorithms.closeStream(is);
} }
return importType;
} }
} }