use of net.osmand.plus.settings.backend.backup.SettingsItemsFactory in project Osmand by osmandapp.
the class BackupImporter method getRemoteItems.
@NonNull
private List<SettingsItem> getRemoteItems(@NonNull List<RemoteFile> remoteFiles, boolean readItems) throws IllegalArgumentException, IOException {
if (remoteFiles.isEmpty()) {
return Collections.emptyList();
}
List<SettingsItem> items = new ArrayList<>();
try {
OperationLog operationLog = new OperationLog("getRemoteItems", BackupHelper.DEBUG);
operationLog.startOperation();
JSONObject json = new JSONObject();
JSONArray itemsJson = new JSONArray();
json.put("items", itemsJson);
Map<File, RemoteFile> remoteInfoFilesMap = new HashMap<>();
Map<String, RemoteFile> remoteItemFilesMap = new HashMap<>();
List<RemoteFile> remoteInfoFiles = new ArrayList<>();
Set<String> remoteInfoNames = new HashSet<>();
List<RemoteFile> noInfoRemoteItemFiles = new ArrayList<>();
OsmandApplication app = backupHelper.getApp();
File tempDir = FileUtils.getTempDir(app);
List<RemoteFile> uniqueRemoteFiles = new ArrayList<>();
Set<String> uniqueFileIds = new TreeSet<>();
for (RemoteFile rf : remoteFiles) {
String fileId = rf.getTypeNamePath();
if (uniqueFileIds.add(fileId) && !rf.isDeleted()) {
uniqueRemoteFiles.add(rf);
}
}
operationLog.log("build uniqueRemoteFiles");
Map<String, UploadedFileInfo> infoMap = backupHelper.getDbHelper().getUploadedFileInfoMap();
BackupInfo backupInfo = backupHelper.getBackup().getBackupInfo();
List<RemoteFile> filesToDelete = backupInfo != null ? backupInfo.filesToDelete : Collections.emptyList();
for (RemoteFile remoteFile : uniqueRemoteFiles) {
String fileName = remoteFile.getTypeNamePath();
if (fileName.endsWith(INFO_EXT)) {
boolean delete = false;
String origFileName = remoteFile.getName().substring(0, remoteFile.getName().length() - INFO_EXT.length());
for (RemoteFile file : filesToDelete) {
if (file.getName().equals(origFileName)) {
delete = true;
break;
}
}
UploadedFileInfo fileInfo = infoMap.get(remoteFile.getType() + "___" + origFileName);
long uploadTime = fileInfo != null ? fileInfo.getUploadTime() : 0;
if (readItems && (uploadTime != remoteFile.getClienttimems() || delete)) {
remoteInfoFilesMap.put(new File(tempDir, fileName), remoteFile);
}
String itemFileName = fileName.substring(0, fileName.length() - INFO_EXT.length());
remoteInfoNames.add(itemFileName);
remoteInfoFiles.add(remoteFile);
} else if (!remoteItemFilesMap.containsKey(fileName)) {
remoteItemFilesMap.put(fileName, remoteFile);
}
}
operationLog.log("build maps");
for (Entry<String, RemoteFile> remoteFileEntry : remoteItemFilesMap.entrySet()) {
String itemFileName = remoteFileEntry.getKey();
RemoteFile remoteFile = remoteFileEntry.getValue();
boolean hasInfo = false;
for (String remoteInfoName : remoteInfoNames) {
if (itemFileName.equals(remoteInfoName) || itemFileName.startsWith(remoteInfoName + "/")) {
hasInfo = true;
break;
}
}
if (!hasInfo && !remoteFile.isRecordedVoiceFile()) {
noInfoRemoteItemFiles.add(remoteFile);
}
}
operationLog.log("build noInfoRemoteItemFiles");
if (readItems) {
generateItemsJson(itemsJson, remoteInfoFilesMap, noInfoRemoteItemFiles);
} else {
generateItemsJson(itemsJson, remoteInfoFiles, noInfoRemoteItemFiles);
}
operationLog.log("generateItemsJson");
SettingsItemsFactory itemsFactory = new SettingsItemsFactory(app, json);
operationLog.log("create setting items");
List<SettingsItem> settingsItemList = itemsFactory.getItems();
if (settingsItemList.isEmpty()) {
return Collections.emptyList();
}
updateFilesInfo(remoteItemFilesMap, settingsItemList);
items.addAll(settingsItemList);
operationLog.log("updateFilesInfo");
if (readItems) {
Map<RemoteFile, SettingsItemReader<? extends SettingsItem>> remoteFilesForRead = new HashMap<>();
for (SettingsItem item : settingsItemList) {
if (item.shouldReadOnCollecting()) {
List<RemoteFile> foundRemoteFiles = getItemRemoteFiles(item, remoteItemFilesMap);
for (RemoteFile remoteFile : foundRemoteFiles) {
SettingsItemReader<? extends SettingsItem> reader = item.getReader();
if (reader != null) {
remoteFilesForRead.put(remoteFile, reader);
}
}
}
}
Map<File, RemoteFile> remoteFilesForDownload = new HashMap<>();
for (RemoteFile remoteFile : remoteFilesForRead.keySet()) {
String fileName = remoteFile.getTypeNamePath();
remoteFilesForDownload.put(new File(tempDir, fileName), remoteFile);
}
if (!remoteFilesForDownload.isEmpty()) {
downloadAndReadItemFiles(remoteFilesForRead, remoteFilesForDownload);
}
operationLog.log("readItems");
}
operationLog.finishOperation();
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Error reading items", e);
} catch (JSONException e) {
throw new IllegalArgumentException("Error parsing items", e);
} catch (IOException e) {
throw new IOException(e);
}
return items;
}
Aggregations