Add zip files import with check for file signature

This commit is contained in:
Vitaliy 2020-10-07 05:23:02 +03:00
parent fea6b424ca
commit 62c1197319
3 changed files with 106 additions and 11 deletions

View file

@ -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,7 +264,7 @@ public class ImportHelper {
}
}
private void handleOsmAndSettingsImport(Uri uri, String name, String latestChanges, int version,
protected void handleOsmAndSettingsImport(Uri uri, String name, String latestChanges, int version,
CallbackWithObject<List<SettingsItem>> 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) {

View file

@ -110,12 +110,12 @@ class UriImportTask extends BaseImportAsyncTask<Void, Void, String> {
}
} 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<Void, Void, String> {
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 "";
}

View file

@ -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<Void, Void, ImportType> {
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);
}
}
}