diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/backup/FileSettingsItem.java b/OsmAnd/src/net/osmand/plus/settings/backend/backup/FileSettingsItem.java index dc001eb9f5..563a04fc75 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/backup/FileSettingsItem.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/backup/FileSettingsItem.java @@ -210,7 +210,12 @@ public class FileSettingsItem extends StreamSettingsItem { } public long getSize() { - return size == 0 ? file != null && !file.isDirectory() ? file.length() : size : size; + if (size != 0) { + return size; + } else if (file != null && !file.isDirectory()) { + return file.length(); + } + return 0; } public void setSize(long size) { @@ -307,26 +312,26 @@ public class FileSettingsItem extends StreamSettingsItem { @Override public void writeEntry(String fileName, @NonNull ZipOutputStream zos) throws IOException { - zipDirsWithFiles(file, zos); + writeDirWithFiles(file, zos); } - public void zipDirsWithFiles(File file, ZipOutputStream zos) throws IOException { - if (file == null) { - return; - } - if (file.isDirectory()) { - File[] fs = file.listFiles(); - if (fs != null) { - for (File c : fs) { - zipDirsWithFiles(c, zos); + public void writeDirWithFiles(File file, ZipOutputStream zos) throws IOException { + if (file != null) { + if (file.isDirectory()) { + File[] files = file.listFiles(); + if (files != null) { + for (File subfolderFile : files) { + writeDirWithFiles(subfolderFile, zos); + } } + } else { + String subtypeFolder = getSubtype().getSubtypeFolder(); + String zipEntryName = Algorithms.isEmpty(subtypeFolder) + ? file.getName() + : file.getPath().substring(file.getPath().indexOf(subtypeFolder) - 1); + setInputStream(new FileInputStream(file)); + super.writeEntry(zipEntryName, zos); } - } else { - String zipEntryName = Algorithms.isEmpty(getSubtype().getSubtypeFolder()) - ? file.getName() - : file.getPath().substring(file.getPath().indexOf(getSubtype().getSubtypeFolder()) - 1); - setInputStream(new FileInputStream(file)); - super.writeEntry(zipEntryName, zos); } } }; diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsExporter.java b/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsExporter.java index 40917bd7c5..38c637e0c7 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsExporter.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsExporter.java @@ -22,13 +22,14 @@ class SettingsExporter { private Map items; private Map additionalParams; - private boolean exportItemsFiles; - private boolean exportCancel; - private final ExportProgress exportProgress; + private ExportProgressListener progressListener; - SettingsExporter(boolean exportItemsFiles, ExportProgress exportProgress) { + private boolean cancelled; + private boolean exportItemsFiles; + + SettingsExporter(ExportProgressListener progressListener, boolean exportItemsFiles) { + this.progressListener = progressListener; this.exportItemsFiles = exportItemsFiles; - this.exportProgress = exportProgress; items = new LinkedHashMap<>(); additionalParams = new LinkedHashMap<>(); } @@ -40,8 +41,8 @@ class SettingsExporter { items.put(item.getName(), item); } - public void setExportCancel(boolean exportCancel) { - this.exportCancel = exportCancel; + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; } void addAdditionalParam(String key, String value) { @@ -79,15 +80,14 @@ class SettingsExporter { } writer.writeEntry(fileName, zos); } - if (exportCancel) { - exportCancel = false; + if (cancelled) { return; } if (item instanceof FileSettingsItem) { int size = (int) ((FileSettingsItem) item).getSize() / (1 << 20); progress += size; - if (exportProgress != null) { - exportProgress.setProgress(progress); + if (progressListener != null) { + progressListener.updateProgress(progress); } } } diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsHelper.java b/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsHelper.java index a9b66189ad..9c47589674 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsHelper.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsHelper.java @@ -37,6 +37,7 @@ import net.osmand.plus.quickaction.QuickActionRegistry; import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.backend.ApplicationMode.ApplicationModeBean; import net.osmand.plus.settings.backend.ExportSettingsType; +import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; import org.json.JSONException; @@ -53,8 +54,8 @@ import java.util.Map; import java.util.Set; import static net.osmand.IndexConstants.OSMAND_SETTINGS_FILE_EXT; -import static net.osmand.plus.settings.backend.backup.FileSettingsItem.*; import static net.osmand.plus.activities.LocalIndexHelper.LocalIndexType; +import static net.osmand.plus.settings.backend.backup.FileSettingsItem.FileSubtype; /* Usage: @@ -141,8 +142,12 @@ public class SettingsHelper { return importTask == null || importTask.isImportDone(); } - public ExportAsyncTask getExportAsyncTask(File file) { - return exportAsyncTasks.get(file); + public boolean cancelExportForFile(File file) { + ExportAsyncTask exportTask = exportAsyncTasks.get(file); + if (exportTask != null && (exportTask.getStatus() == AsyncTask.Status.RUNNING)) { + return exportTask.cancel(true); + } + return false; } public boolean isFileExporting(File file) { @@ -206,31 +211,23 @@ public class SettingsHelper { } } - public interface ExportProgress { - void setProgress(int value); + public interface ExportProgressListener { + void updateProgress(int value); } @SuppressLint("StaticFieldLeak") public class ExportAsyncTask extends AsyncTask { - private final SettingsExporter exporter; - private final File file; + private File file; + private SettingsExporter exporter; private SettingsExportListener listener; - ExportAsyncTask(@NonNull File settingsFile, - @Nullable SettingsExportListener listener, - @NonNull List items, boolean exportItemsFiles) { + @Nullable SettingsExportListener listener, + @NonNull List items, boolean exportItemsFiles) { this.file = settingsFile; this.listener = listener; - ExportProgress exportProgress = new ExportProgress() { - @Override - public void setProgress(int value) { - exporter.setExportCancel(isCancelled()); - publishProgress(value); - } - }; - this.exporter = new SettingsExporter(exportItemsFiles, exportProgress); + this.exporter = new SettingsExporter(getProgressListener(), exportItemsFiles); for (SettingsItem item : items) { exporter.addSettingsItem(item); } @@ -266,9 +263,17 @@ public class SettingsHelper { @Override protected void onCancelled() { - super.onCancelled(); - //noinspection ResultOfMethodCallIgnored - file.delete(); + Algorithms.removeAllFiles(file); + } + + private ExportProgressListener getProgressListener() { + return new ExportProgressListener() { + @Override + public void updateProgress(int value) { + exporter.setCancelled(isCancelled()); + publishProgress(value); + } + }; } } @@ -448,8 +453,6 @@ public class SettingsHelper { public void exportSettings(@NonNull File fileDir, @NonNull String fileName, @Nullable SettingsExportListener listener, @NonNull List items, boolean exportItemsFiles) { File file = new File(fileDir, fileName + OSMAND_SETTINGS_FILE_EXT); ExportAsyncTask exportAsyncTask = new ExportAsyncTask(file, listener, items, exportItemsFiles); - - exportAsyncTasks.put(file, exportAsyncTask); exportAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } @@ -580,7 +583,7 @@ public class SettingsHelper { List files = getFilesByType(localIndexInfoList, LocalIndexType.MAP_DATA, LocalIndexType.TILES_DATA, LocalIndexType.SRTM_DATA, LocalIndexType.WIKI_DATA); if (!files.isEmpty()) { - sortData(files); + sortLocalFiles(files); dataList.put(ExportSettingsType.OFFLINE_MAPS, files); } files = getFilesByType(localIndexInfoList, LocalIndexType.TTS_VOICE_DATA); @@ -841,7 +844,7 @@ public class SettingsHelper { return settingsToOperate; } - private void sortData(List files) { + private void sortLocalFiles(List files) { final Collator collator = OsmAndCollator.primaryCollator(); Collections.sort(files, new Comparator() { @Override diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/ExportProfileBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/fragments/ExportProfileBottomSheet.java index ba1d630bef..8d5b484dfb 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/ExportProfileBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/ExportProfileBottomSheet.java @@ -338,7 +338,7 @@ public class ExportProfileBottomSheet extends BasePreferenceBottomSheet { } private void cancelExport() { - app.getSettingsHelper().getExportAsyncTask(getExportFile()).cancel(true); + app.getSettingsHelper().cancelExportForFile(getExportFile()); progress.dismiss(); dismiss(); }