From 62c119731978546079d97595b3de7ddf78ebcbd5 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Wed, 7 Oct 2020 05:23:02 +0300 Subject: [PATCH] Add zip files import with check for file signature --- .../osmand/plus/importfiles/ImportHelper.java | 13 ++- .../plus/importfiles/UriImportTask.java | 14 +-- .../plus/importfiles/ZipImportTask.java | 90 +++++++++++++++++++ 3 files changed, 106 insertions(+), 11 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/importfiles/ZipImportTask.java diff --git a/OsmAnd/src/net/osmand/plus/importfiles/ImportHelper.java b/OsmAnd/src/net/osmand/plus/importfiles/ImportHelper.java index 62e0f1dbd0..c67ae8834e 100644 --- a/OsmAnd/src/net/osmand/plus/importfiles/ImportHelper.java +++ b/OsmAnd/src/net/osmand/plus/importfiles/ImportHelper.java @@ -89,7 +89,8 @@ public class ImportHelper { ROUTING(ROUTING_FILE_EXT), RENDERING(RENDERER_INDEX_EXT), GPX(GPX_FILE_EXT), - KML(KML_SUFFIX); + KML(KML_SUFFIX), + KMZ(KMZ_SUFFIX); ImportType(String extension) { this.extension = extension; @@ -237,7 +238,7 @@ public class ImportHelper { executeImportTask(new FavoritesImportTask(activity, gpxFile, fileName, forceImportFavourites)); } - private void handleKmzImport(Uri kmzFile, String name, boolean save, boolean useImportDir) { + protected void handleKmzImport(Uri kmzFile, String name, boolean save, boolean useImportDir) { executeImportTask(new KmzImportTask(this, activity, kmzFile, name, save, useImportDir)); } @@ -263,8 +264,8 @@ public class ImportHelper { } } - private void handleOsmAndSettingsImport(Uri uri, String name, String latestChanges, int version, - CallbackWithObject> callback) { + protected void handleOsmAndSettingsImport(Uri uri, String name, String latestChanges, int version, + CallbackWithObject> callback) { executeImportTask(new SettingsImportTask(activity, uri, name, latestChanges, version, callback)); } @@ -276,6 +277,10 @@ public class ImportHelper { executeImportTask(new UriImportTask(this, activity, uri, save, useImportDir)); } + protected void handleZipImport(Uri uri, boolean save, boolean useImportDir) { + executeImportTask(new ZipImportTask(this, activity, uri, save, useImportDir)); + } + @Nullable public static String copyFile(OsmandApplication app, @NonNull File dest, @NonNull Uri uri, boolean overwrite) { if (dest.exists() && !overwrite) { diff --git a/OsmAnd/src/net/osmand/plus/importfiles/UriImportTask.java b/OsmAnd/src/net/osmand/plus/importfiles/UriImportTask.java index 27b4a0fa73..c2de2d072f 100644 --- a/OsmAnd/src/net/osmand/plus/importfiles/UriImportTask.java +++ b/OsmAnd/src/net/osmand/plus/importfiles/UriImportTask.java @@ -110,12 +110,12 @@ class UriImportTask extends BaseImportAsyncTask { } } else if (OBF_FILE_SIGNATURE == fileSignature) { String name = createUniqueFileName(app, "map", MAPS_PATH, BINARY_MAP_INDEX_EXT); - importHelper.handleObfImport(tempUri, name); + importHelper.handleObfImport(tempUri, name + BINARY_MAP_INDEX_EXT); } else if (ZIP_FILE_SIGNATURE == fileSignature) { -// importHelper.handleKmzImport(tempUri, null, save, useImportDir); + importHelper.handleZipImport(tempUri, save, useImportDir); } else if (SQLITE_FILE_SIGNATURE == fileSignature) { String name = createUniqueFileName(app, "online_map", TILES_INDEX_DIR, SQLITE_EXT); - importHelper.handleSqliteTileImport(tempUri, name); + importHelper.handleSqliteTileImport(tempUri, name + SQLITE_EXT); } } else { app.showShortToastMessage(app.getString(R.string.file_import_error, tempFileName, error)); @@ -124,13 +124,13 @@ class UriImportTask extends BaseImportAsyncTask { private String getTempFileName() { if (XML_FILE_SIGNATURE == fileSignature) { - return createUniqueFileName(app, "xml_file", TEMP_DIR, ROUTING_FILE_EXT); + return createUniqueFileName(app, "xml_file", TEMP_DIR, ROUTING_FILE_EXT) + ROUTING_FILE_EXT; } else if (OBF_FILE_SIGNATURE == fileSignature) { - return createUniqueFileName(app, "map", TEMP_DIR, BINARY_MAP_INDEX_EXT); + return createUniqueFileName(app, "map", TEMP_DIR, BINARY_MAP_INDEX_EXT) + BINARY_MAP_INDEX_EXT; } else if (ZIP_FILE_SIGNATURE == fileSignature) { - return createUniqueFileName(app, "zip_file", TEMP_DIR, ZIP_EXT); + return createUniqueFileName(app, "zip_file", TEMP_DIR, ZIP_EXT) + ZIP_EXT; } else if (SQLITE_FILE_SIGNATURE == fileSignature) { - return createUniqueFileName(app, "online_map", TEMP_DIR, SQLITE_EXT); + return createUniqueFileName(app, "online_map", TEMP_DIR, SQLITE_EXT) + SQLITE_EXT; } return ""; } diff --git a/OsmAnd/src/net/osmand/plus/importfiles/ZipImportTask.java b/OsmAnd/src/net/osmand/plus/importfiles/ZipImportTask.java new file mode 100644 index 0000000000..3704f2192b --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/importfiles/ZipImportTask.java @@ -0,0 +1,90 @@ +package net.osmand.plus.importfiles; + +import android.net.Uri; + +import androidx.annotation.NonNull; +import androidx.fragment.app.FragmentActivity; + +import net.osmand.plus.importfiles.ImportHelper.ImportType; +import net.osmand.util.Algorithms; + +import java.io.InputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +import static net.osmand.FileUtils.createUniqueFileName; +import static net.osmand.IndexConstants.GPX_FILE_EXT; +import static net.osmand.IndexConstants.GPX_IMPORT_DIR; +import static net.osmand.IndexConstants.GPX_INDEX_DIR; +import static net.osmand.IndexConstants.OSMAND_SETTINGS_FILE_EXT; +import static net.osmand.IndexConstants.TEMP_DIR; +import static net.osmand.plus.importfiles.ImportHelper.KML_SUFFIX; + +public class ZipImportTask extends BaseImportAsyncTask { + + private ImportHelper importHelper; + private Uri uri; + private boolean save; + private boolean useImportDir; + + public ZipImportTask(@NonNull ImportHelper importHelper, @NonNull FragmentActivity activity, + @NonNull Uri uri, boolean save, boolean useImportDir) { + super(activity); + this.importHelper = importHelper; + this.uri = uri; + this.save = save; + this.useImportDir = useImportDir; + } + + @Override + protected ImportType doInBackground(Void... voids) { + ImportType importType = null; + InputStream is = null; + ZipInputStream zis = null; + try { + is = app.getContentResolver().openInputStream(uri); + if (is != null) { + zis = new ZipInputStream(is); + ZipEntry entry; + while ((entry = zis.getNextEntry()) != null) { + String fileName = checkEntryName(entry.getName()); + if (fileName.endsWith(KML_SUFFIX)) { + importType = ImportType.KMZ; + break; + } else if (fileName.equals("items.json")) { + importType = ImportType.SETTINGS; + break; + } + } + } + } catch (Exception e) { + ImportHelper.log.error(e.getMessage(), e); + } finally { + Algorithms.closeStream(is); + Algorithms.closeStream(zis); + } + return importType; + } + + private String checkEntryName(String entryName) { + String fileExt = OSMAND_SETTINGS_FILE_EXT + "/"; + int index = entryName.indexOf(fileExt); + if (index != -1) { + entryName = entryName.substring(index + fileExt.length()); + } + return entryName; + } + + @Override + protected void onPostExecute(ImportType importType) { + hideProgress(); + if (importType == ImportType.KMZ) { + String dir = useImportDir ? GPX_IMPORT_DIR : GPX_INDEX_DIR; + String name = createUniqueFileName(app, "track", dir, GPX_FILE_EXT); + importHelper.handleKmzImport(uri, name + GPX_FILE_EXT, save, useImportDir); + } else if (importType == ImportType.SETTINGS) { + String name = createUniqueFileName(app, "settings", TEMP_DIR, OSMAND_SETTINGS_FILE_EXT); + importHelper.handleOsmAndSettingsImport(uri, name + OSMAND_SETTINGS_FILE_EXT, null, -1, null); + } + } +} \ No newline at end of file