use of net.osmand.plus.settings.backend.ExportSettingsType in project Osmand by osmandapp.
the class BackupInfo method createFilteredFilesToMerge.
private void createFilteredFilesToMerge(@NonNull OsmandApplication app) {
List<Pair<LocalFile, RemoteFile>> files = new ArrayList<>();
Set<SettingsItem> items = new HashSet<>();
BackupHelper helper = app.getBackupHelper();
for (Pair<LocalFile, RemoteFile> pair : filesToMerge) {
SettingsItem item = pair.first.item;
if (!items.contains(item)) {
ExportSettingsType exportType = ExportSettingsType.getExportSettingsTypeForRemoteFile(pair.second);
if (exportType != null && helper.getBackupTypePref(exportType).get()) {
files.add(pair);
items.add(item);
}
}
}
filteredFilesToMerge = files;
}
use of net.osmand.plus.settings.backend.ExportSettingsType in project Osmand by osmandapp.
the class DeleteAllFilesCommand method getDeleteAllFilesListener.
private OnRequestResultListener getDeleteAllFilesListener() {
return (resultJson, error, resultCode) -> {
int status;
String message;
List<RemoteFile> remoteFiles = new ArrayList<>();
if (!Algorithms.isEmpty(error)) {
status = STATUS_SERVER_ERROR;
message = "Download file list error: " + new BackupError(error);
} else if (!Algorithms.isEmpty(resultJson)) {
try {
JSONObject result = new JSONObject(resultJson);
JSONArray files = result.getJSONArray("allFiles");
for (int i = 0; i < files.length(); i++) {
remoteFiles.add(new RemoteFile(files.getJSONObject(i)));
}
status = STATUS_SUCCESS;
message = "OK";
} catch (JSONException e) {
status = STATUS_PARSE_JSON_ERROR;
message = "Download file list error: json parsing";
}
} else {
status = STATUS_EMPTY_RESPONSE_ERROR;
message = "Download file list error: empty response";
}
if (status != STATUS_SUCCESS) {
publishProgress(status, message);
} else {
List<RemoteFile> filesToDelete = new ArrayList<>();
if (types != null) {
for (RemoteFile file : remoteFiles) {
ExportSettingsType exportType = ExportSettingsType.getExportSettingsTypeForRemoteFile(file);
if (types.contains(exportType)) {
filesToDelete.add(file);
}
}
} else {
filesToDelete.addAll(remoteFiles);
}
if (!filesToDelete.isEmpty()) {
publishProgress(filesToDelete);
deleteFiles(filesToDelete);
} else {
publishProgress(Collections.emptyMap());
}
}
};
}
use of net.osmand.plus.settings.backend.ExportSettingsType in project Osmand by osmandapp.
the class BackupHelper method generateBackupInfo.
@SuppressLint("StaticFieldLeak")
void generateBackupInfo(@NonNull final Map<String, LocalFile> localFiles, @NonNull final Map<String, RemoteFile> uniqueRemoteFiles, @NonNull final Map<String, RemoteFile> deletedRemoteFiles, @Nullable final OnGenerateBackupInfoListener listener) {
OperationLog operationLog = new OperationLog("generateBackupInfo", DEBUG, 200);
operationLog.startOperation();
AsyncTask<Void, Void, BackupInfo> task = new AsyncTask<Void, Void, BackupInfo>() {
@Override
protected BackupInfo doInBackground(Void... voids) {
BackupInfo info = new BackupInfo();
/*
operationLog.log("=== localFiles ===");
for (LocalFile localFile : localFiles.values()) {
operationLog.log(localFile.toString());
}
operationLog.log("=== localFiles ===");
operationLog.log("=== uniqueRemoteFiles ===");
for (RemoteFile remoteFile : uniqueRemoteFiles.values()) {
operationLog.log(remoteFile.toString());
}
operationLog.log("=== uniqueRemoteFiles ===");
operationLog.log("=== deletedRemoteFiles ===");
for (RemoteFile remoteFile : deletedRemoteFiles.values()) {
operationLog.log(remoteFile.toString());
}
operationLog.log("=== deletedRemoteFiles ===");
*/
List<RemoteFile> remoteFiles = new ArrayList<>(uniqueRemoteFiles.values());
remoteFiles.addAll(deletedRemoteFiles.values());
for (RemoteFile remoteFile : remoteFiles) {
ExportSettingsType exportType = ExportSettingsType.getExportSettingsTypeForRemoteFile(remoteFile);
if (exportType == null || !ExportSettingsType.isTypeEnabled(exportType) || remoteFile.isRecordedVoiceFile()) {
continue;
}
LocalFile localFile = localFiles.get(remoteFile.getTypeNamePath());
if (localFile != null) {
long remoteUploadTime = remoteFile.getClienttimems();
long localUploadTime = localFile.uploadTime;
if (remoteFile.isDeleted()) {
info.localFilesToDelete.add(localFile);
} else if (remoteUploadTime == localUploadTime) {
if (localUploadTime < localFile.localModifiedTime) {
info.filesToUpload.add(localFile);
info.filesToDownload.add(remoteFile);
}
} else {
info.filesToMerge.add(new Pair<>(localFile, remoteFile));
info.filesToDownload.add(remoteFile);
}
long localFileSize = localFile.file == null ? 0 : localFile.file.length();
long remoteFileSize = remoteFile.getFilesize();
if (remoteFileSize > 0 && localFileSize > 0 && localFileSize != remoteFileSize && !info.filesToDownload.contains(remoteFile)) {
info.filesToDownload.add(remoteFile);
}
}
if (localFile == null && !remoteFile.isDeleted()) {
UploadedFileInfo fileInfo = dbHelper.getUploadedFileInfo(remoteFile.getType(), remoteFile.getName());
// suggest to remove only if file exists in db
if (fileInfo != null) {
info.filesToDelete.add(remoteFile);
}
info.filesToDownload.add(remoteFile);
}
}
for (LocalFile localFile : localFiles.values()) {
ExportSettingsType exportType = localFile.item != null ? ExportSettingsType.getExportSettingsTypeForItem(localFile.item) : null;
if (exportType == null || !ExportSettingsType.isTypeEnabled(exportType)) {
continue;
}
boolean hasRemoteFile = uniqueRemoteFiles.containsKey(localFile.getTypeFileName());
if (!hasRemoteFile) {
boolean isEmpty = localFile.item instanceof CollectionSettingsItem<?> && ((CollectionSettingsItem<?>) localFile.item).isEmpty();
if (!isEmpty) {
info.filesToUpload.add(localFile);
}
}
}
info.createItemCollections(app);
operationLog.log("=== filesToUpload ===");
for (LocalFile localFile : info.filesToUpload) {
operationLog.log(localFile.toString());
}
operationLog.log("=== filesToUpload ===");
operationLog.log("=== filesToDownload ===");
for (RemoteFile remoteFile : info.filesToDownload) {
operationLog.log(remoteFile.toString());
}
operationLog.log("=== filesToDownload ===");
operationLog.log("=== filesToDelete ===");
for (RemoteFile remoteFile : info.filesToDelete) {
operationLog.log(remoteFile.toString());
}
operationLog.log("=== filesToDelete ===");
operationLog.log("=== filesToMerge ===");
for (Pair<LocalFile, RemoteFile> filePair : info.filesToMerge) {
operationLog.log("LOCAL=" + filePair.first.toString() + " REMOTE=" + filePair.second.toString());
}
operationLog.log("=== filesToMerge ===");
return info;
}
@Override
protected void onPostExecute(BackupInfo backupInfo) {
operationLog.finishOperation(backupInfo.toString());
if (listener != null) {
listener.onBackupInfoGenerated(backupInfo, null);
}
}
};
task.executeOnExecutor(executor);
}
use of net.osmand.plus.settings.backend.ExportSettingsType in project Osmand by osmandapp.
the class BackupHelper method collectLocalFiles.
@SuppressLint("StaticFieldLeak")
void collectLocalFiles(@Nullable final OnCollectLocalFilesListener listener) {
OperationLog operationLog = new OperationLog("collectLocalFiles", DEBUG);
operationLog.startOperation();
AsyncTask<Void, LocalFile, List<LocalFile>> task = new AsyncTask<Void, LocalFile, List<LocalFile>>() {
BackupDbHelper dbHelper;
SQLiteConnection db;
Map<String, UploadedFileInfo> infos;
@Override
protected void onPreExecute() {
dbHelper = app.getBackupHelper().getDbHelper();
db = dbHelper.openConnection(true);
}
@Override
protected List<LocalFile> doInBackground(Void... voids) {
List<LocalFile> result = new ArrayList<>();
infos = dbHelper.getUploadedFileInfoMap();
List<SettingsItem> localItems = getLocalItems();
operationLog.log("getLocalItems");
for (SettingsItem item : localItems) {
String fileName = BackupHelper.getItemFileName(item);
if (item instanceof FileSettingsItem) {
FileSettingsItem fileItem = (FileSettingsItem) item;
File file = fileItem.getFile();
if (file.isDirectory()) {
if (item instanceof GpxSettingsItem) {
continue;
} else if (fileItem.getSubtype() == FileSubtype.VOICE) {
File jsFile = new File(file, file.getName() + "_" + IndexConstants.TTSVOICE_INDEX_EXT_JS);
if (jsFile.exists()) {
fileName = jsFile.getPath().replace(app.getAppPath(null).getPath() + "/", "");
createLocalFile(result, item, fileName, jsFile, jsFile.lastModified());
continue;
}
} else if (fileItem.getSubtype() == FileSubtype.TTS_VOICE) {
String langName = file.getName().replace(IndexConstants.VOICE_PROVIDER_SUFFIX, "");
File jsFile = new File(file, langName + "_" + IndexConstants.TTSVOICE_INDEX_EXT_JS);
if (jsFile.exists()) {
fileName = jsFile.getPath().replace(app.getAppPath(null).getPath() + "/", "");
createLocalFile(result, item, fileName, jsFile, jsFile.lastModified());
continue;
}
} else if (fileItem.getSubtype() == FileSubtype.TILES_MAP) {
continue;
}
List<File> dirs = new ArrayList<>();
dirs.add(file);
Algorithms.collectDirs(file, dirs);
operationLog.log("collectDirs " + file.getName() + " BEGIN");
for (File dir : dirs) {
File[] files = dir.listFiles();
if (files != null && files.length > 0) {
for (File f : files) {
if (!f.isDirectory()) {
fileName = f.getPath().replace(app.getAppPath(null).getPath() + "/", "");
createLocalFile(result, item, fileName, f, f.lastModified());
}
}
}
}
operationLog.log("collectDirs " + file.getName() + " END");
} else if (fileItem.getSubtype() == FileSubtype.TILES_MAP) {
if (file.getName().endsWith(SQLiteTileSource.EXT)) {
createLocalFile(result, item, fileName, file, file.lastModified());
}
} else {
createLocalFile(result, item, fileName, file, file.lastModified());
}
} else {
createLocalFile(result, item, fileName, null, item.getLastModifiedTime());
}
}
return result;
}
private void createLocalFile(@NonNull List<LocalFile> result, @NonNull SettingsItem item, @NonNull String fileName, @Nullable File file, long lastModifiedTime) {
LocalFile localFile = new LocalFile();
localFile.file = file;
localFile.item = item;
localFile.fileName = fileName;
localFile.localModifiedTime = lastModifiedTime;
if (infos != null) {
UploadedFileInfo fileInfo = infos.get(item.getType().name() + "___" + fileName);
if (fileInfo != null) {
localFile.uploadTime = fileInfo.getUploadTime();
String lastMd5 = fileInfo.getMd5Digest();
boolean needM5Digest = item instanceof StreamSettingsItem && ((StreamSettingsItem) item).needMd5Digest() && localFile.uploadTime < lastModifiedTime && !Algorithms.isEmpty(lastMd5);
if (needM5Digest && file != null && file.exists()) {
FileInputStream is = null;
try {
is = new FileInputStream(file);
String md5 = new String(Hex.encodeHex(DigestUtils.md5(is)));
if (md5.equals(lastMd5)) {
item.setLocalModifiedTime(localFile.uploadTime);
localFile.localModifiedTime = localFile.uploadTime;
}
} catch (IOException e) {
LOG.error(e.getMessage(), e);
} finally {
Algorithms.closeStream(is);
}
}
}
}
result.add(localFile);
publishProgress(localFile);
}
private List<SettingsItem> getLocalItems() {
List<ExportSettingsType> types = ExportSettingsType.getEnabledTypes();
Iterator<ExportSettingsType> it = types.iterator();
while (it.hasNext()) {
ExportSettingsType type = it.next();
if (!getBackupTypePref(type).get()) {
it.remove();
}
}
return app.getFileSettingsHelper().getFilteredSettingsItems(types, true, true);
}
@Override
protected void onProgressUpdate(LocalFile... localFiles) {
if (listener != null) {
listener.onFileCollected(localFiles[0]);
}
}
@Override
protected void onPostExecute(List<LocalFile> localFiles) {
if (db != null) {
db.close();
}
operationLog.finishOperation(" Files=" + localFiles.size());
if (listener != null) {
listener.onFilesCollected(localFiles);
}
}
};
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
use of net.osmand.plus.settings.backend.ExportSettingsType in project Osmand by osmandapp.
the class DeleteOldFilesCommand method getDeleteOldFilesListener.
private OnRequestResultListener getDeleteOldFilesListener() {
return (resultJson, error, resultCode) -> {
int status;
String message;
List<RemoteFile> remoteFiles = new ArrayList<>();
if (!Algorithms.isEmpty(error)) {
status = STATUS_SERVER_ERROR;
message = "Download file list error: " + new BackupError(error);
} else if (!Algorithms.isEmpty(resultJson)) {
try {
JSONObject result = new JSONObject(resultJson);
JSONArray allFiles = result.getJSONArray("allFiles");
for (int i = 0; i < allFiles.length(); i++) {
remoteFiles.add(new RemoteFile(allFiles.getJSONObject(i)));
}
JSONArray uniqueFiles = result.getJSONArray("uniqueFiles");
for (int i = 0; i < uniqueFiles.length(); i++) {
remoteFiles.remove(new RemoteFile(uniqueFiles.getJSONObject(i)));
}
status = STATUS_SUCCESS;
message = "OK";
} catch (JSONException e) {
status = STATUS_PARSE_JSON_ERROR;
message = "Download file list error: json parsing";
}
} else {
status = STATUS_EMPTY_RESPONSE_ERROR;
message = "Download file list error: empty response";
}
if (status != STATUS_SUCCESS) {
publishProgress(status, message);
} else {
List<RemoteFile> filesToDelete = new ArrayList<>();
if (types != null) {
for (RemoteFile file : remoteFiles) {
ExportSettingsType exportType = ExportSettingsType.getExportSettingsTypeForRemoteFile(file);
if (types.contains(exportType)) {
filesToDelete.add(file);
}
}
} else {
filesToDelete.addAll(remoteFiles);
}
if (!filesToDelete.isEmpty()) {
publishProgress(filesToDelete);
deleteFiles(filesToDelete);
} else {
publishProgress(Collections.emptyMap());
}
}
};
}
Aggregations