Small clean up

This commit is contained in:
Vitaliy 2020-11-05 18:38:52 +02:00
parent 96a8352020
commit 1b3247780a
4 changed files with 62 additions and 54 deletions

View file

@ -210,7 +210,12 @@ public class FileSettingsItem extends StreamSettingsItem {
} }
public long getSize() { 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) { public void setSize(long size) {
@ -307,26 +312,26 @@ public class FileSettingsItem extends StreamSettingsItem {
@Override @Override
public void writeEntry(String fileName, @NonNull ZipOutputStream zos) throws IOException { 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 { public void writeDirWithFiles(File file, ZipOutputStream zos) throws IOException {
if (file == null) { if (file != null) {
return; if (file.isDirectory()) {
} File[] files = file.listFiles();
if (file.isDirectory()) { if (files != null) {
File[] fs = file.listFiles(); for (File subfolderFile : files) {
if (fs != null) { writeDirWithFiles(subfolderFile, zos);
for (File c : fs) { }
zipDirsWithFiles(c, 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);
} }
} }
}; };

View file

@ -22,13 +22,14 @@ class SettingsExporter {
private Map<String, SettingsItem> items; private Map<String, SettingsItem> items;
private Map<String, String> additionalParams; private Map<String, String> additionalParams;
private boolean exportItemsFiles; private ExportProgressListener progressListener;
private boolean exportCancel;
private final ExportProgress exportProgress;
SettingsExporter(boolean exportItemsFiles, ExportProgress exportProgress) { private boolean cancelled;
private boolean exportItemsFiles;
SettingsExporter(ExportProgressListener progressListener, boolean exportItemsFiles) {
this.progressListener = progressListener;
this.exportItemsFiles = exportItemsFiles; this.exportItemsFiles = exportItemsFiles;
this.exportProgress = exportProgress;
items = new LinkedHashMap<>(); items = new LinkedHashMap<>();
additionalParams = new LinkedHashMap<>(); additionalParams = new LinkedHashMap<>();
} }
@ -40,8 +41,8 @@ class SettingsExporter {
items.put(item.getName(), item); items.put(item.getName(), item);
} }
public void setExportCancel(boolean exportCancel) { public void setCancelled(boolean cancelled) {
this.exportCancel = exportCancel; this.cancelled = cancelled;
} }
void addAdditionalParam(String key, String value) { void addAdditionalParam(String key, String value) {
@ -79,15 +80,14 @@ class SettingsExporter {
} }
writer.writeEntry(fileName, zos); writer.writeEntry(fileName, zos);
} }
if (exportCancel) { if (cancelled) {
exportCancel = false;
return; return;
} }
if (item instanceof FileSettingsItem) { if (item instanceof FileSettingsItem) {
int size = (int) ((FileSettingsItem) item).getSize() / (1 << 20); int size = (int) ((FileSettingsItem) item).getSize() / (1 << 20);
progress += size; progress += size;
if (exportProgress != null) { if (progressListener != null) {
exportProgress.setProgress(progress); progressListener.updateProgress(progress);
} }
} }
} }

View file

@ -37,6 +37,7 @@ import net.osmand.plus.quickaction.QuickActionRegistry;
import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.settings.backend.ApplicationMode.ApplicationModeBean; import net.osmand.plus.settings.backend.ApplicationMode.ApplicationModeBean;
import net.osmand.plus.settings.backend.ExportSettingsType; import net.osmand.plus.settings.backend.ExportSettingsType;
import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.json.JSONException; import org.json.JSONException;
@ -53,8 +54,8 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import static net.osmand.IndexConstants.OSMAND_SETTINGS_FILE_EXT; 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.activities.LocalIndexHelper.LocalIndexType;
import static net.osmand.plus.settings.backend.backup.FileSettingsItem.FileSubtype;
/* /*
Usage: Usage:
@ -141,8 +142,12 @@ public class SettingsHelper {
return importTask == null || importTask.isImportDone(); return importTask == null || importTask.isImportDone();
} }
public ExportAsyncTask getExportAsyncTask(File file) { public boolean cancelExportForFile(File file) {
return exportAsyncTasks.get(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) { public boolean isFileExporting(File file) {
@ -206,31 +211,23 @@ public class SettingsHelper {
} }
} }
public interface ExportProgress { public interface ExportProgressListener {
void setProgress(int value); void updateProgress(int value);
} }
@SuppressLint("StaticFieldLeak") @SuppressLint("StaticFieldLeak")
public class ExportAsyncTask extends AsyncTask<Void, Integer, Boolean> { public class ExportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
private final SettingsExporter exporter; private File file;
private final File file; private SettingsExporter exporter;
private SettingsExportListener listener; private SettingsExportListener listener;
ExportAsyncTask(@NonNull File settingsFile, ExportAsyncTask(@NonNull File settingsFile,
@Nullable SettingsExportListener listener, @Nullable SettingsExportListener listener,
@NonNull List<SettingsItem> items, boolean exportItemsFiles) { @NonNull List<SettingsItem> items, boolean exportItemsFiles) {
this.file = settingsFile; this.file = settingsFile;
this.listener = listener; this.listener = listener;
ExportProgress exportProgress = new ExportProgress() { this.exporter = new SettingsExporter(getProgressListener(), exportItemsFiles);
@Override
public void setProgress(int value) {
exporter.setExportCancel(isCancelled());
publishProgress(value);
}
};
this.exporter = new SettingsExporter(exportItemsFiles, exportProgress);
for (SettingsItem item : items) { for (SettingsItem item : items) {
exporter.addSettingsItem(item); exporter.addSettingsItem(item);
} }
@ -266,9 +263,17 @@ public class SettingsHelper {
@Override @Override
protected void onCancelled() { protected void onCancelled() {
super.onCancelled(); Algorithms.removeAllFiles(file);
//noinspection ResultOfMethodCallIgnored }
file.delete();
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<SettingsItem> items, boolean exportItemsFiles) { public void exportSettings(@NonNull File fileDir, @NonNull String fileName, @Nullable SettingsExportListener listener, @NonNull List<SettingsItem> items, boolean exportItemsFiles) {
File file = new File(fileDir, fileName + OSMAND_SETTINGS_FILE_EXT); File file = new File(fileDir, fileName + OSMAND_SETTINGS_FILE_EXT);
ExportAsyncTask exportAsyncTask = new ExportAsyncTask(file, listener, items, exportItemsFiles); ExportAsyncTask exportAsyncTask = new ExportAsyncTask(file, listener, items, exportItemsFiles);
exportAsyncTasks.put(file, exportAsyncTask); exportAsyncTasks.put(file, exportAsyncTask);
exportAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); exportAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
@ -580,7 +583,7 @@ public class SettingsHelper {
List<File> files = getFilesByType(localIndexInfoList, LocalIndexType.MAP_DATA, LocalIndexType.TILES_DATA, List<File> files = getFilesByType(localIndexInfoList, LocalIndexType.MAP_DATA, LocalIndexType.TILES_DATA,
LocalIndexType.SRTM_DATA, LocalIndexType.WIKI_DATA); LocalIndexType.SRTM_DATA, LocalIndexType.WIKI_DATA);
if (!files.isEmpty()) { if (!files.isEmpty()) {
sortData(files); sortLocalFiles(files);
dataList.put(ExportSettingsType.OFFLINE_MAPS, files); dataList.put(ExportSettingsType.OFFLINE_MAPS, files);
} }
files = getFilesByType(localIndexInfoList, LocalIndexType.TTS_VOICE_DATA); files = getFilesByType(localIndexInfoList, LocalIndexType.TTS_VOICE_DATA);
@ -841,7 +844,7 @@ public class SettingsHelper {
return settingsToOperate; return settingsToOperate;
} }
private void sortData(List<File> files) { private void sortLocalFiles(List<File> files) {
final Collator collator = OsmAndCollator.primaryCollator(); final Collator collator = OsmAndCollator.primaryCollator();
Collections.sort(files, new Comparator<File>() { Collections.sort(files, new Comparator<File>() {
@Override @Override

View file

@ -338,7 +338,7 @@ public class ExportProfileBottomSheet extends BasePreferenceBottomSheet {
} }
private void cancelExport() { private void cancelExport() {
app.getSettingsHelper().getExportAsyncTask(getExportFile()).cancel(true); app.getSettingsHelper().cancelExportForFile(getExportFile());
progress.dismiss(); progress.dismiss();
dismiss(); dismiss();
} }