use of net.osmand.plus.settings.backend.backup.SettingsItemReader in project Osmand by osmandapp.
the class ItinerarySettingsItem method getReader.
@Nullable
@Override
public SettingsItemReader<ItinerarySettingsItem> getReader() {
return new SettingsItemReader<ItinerarySettingsItem>(this) {
@Override
public void readFromStream(@NonNull InputStream inputStream, String entryName) throws IllegalArgumentException {
List<ItineraryGroupInfo> groupInfos = new ArrayList<>();
GPXFile gpxFile = GPXUtilities.loadGPXFile(inputStream, dataHelper.getGPXExtensionsReader(groupInfos));
if (gpxFile.error != null) {
warnings.add(app.getString(R.string.settings_item_read_error, String.valueOf(getType())));
SettingsHelper.LOG.error("Failed read gpx file", gpxFile.error);
} else {
Map<String, MapMarker> markers = new LinkedHashMap<>();
Map<String, MapMarkersGroup> groups = new LinkedHashMap<>();
dataHelper.collectMarkersGroups(gpxFile, groups, groupInfos, markers);
items.addAll(groups.values());
}
}
};
}
use of net.osmand.plus.settings.backend.backup.SettingsItemReader in project Osmand by osmandapp.
the class MarkersSettingsItem method getReader.
@Nullable
@Override
public SettingsItemReader<MarkersSettingsItem> getReader() {
return new SettingsItemReader<MarkersSettingsItem>(this) {
@Override
public void readFromStream(@NonNull InputStream inputStream, String entryName) throws IllegalArgumentException {
GPXFile gpxFile = GPXUtilities.loadGPXFile(inputStream);
if (gpxFile.error != null) {
warnings.add(app.getString(R.string.settings_item_read_error, String.valueOf(getType())));
SettingsHelper.LOG.error("Failed read gpx file", gpxFile.error);
} else {
List<MapMarker> mapMarkers = markersHelper.getDataHelper().readMarkersFromGpx(gpxFile, false);
items.addAll(mapMarkers);
}
}
};
}
use of net.osmand.plus.settings.backend.backup.SettingsItemReader 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;
}
use of net.osmand.plus.settings.backend.backup.SettingsItemReader in project Osmand by osmandapp.
the class HistoryMarkersSettingsItem method getReader.
@Nullable
@Override
public SettingsItemReader<HistoryMarkersSettingsItem> getReader() {
return new SettingsItemReader<HistoryMarkersSettingsItem>(this) {
@Override
public void readFromStream(@NonNull InputStream inputStream, String entryName) throws IllegalArgumentException {
GPXFile gpxFile = GPXUtilities.loadGPXFile(inputStream);
if (gpxFile.error != null) {
warnings.add(app.getString(R.string.settings_item_read_error, String.valueOf(getType())));
SettingsHelper.LOG.error("Failed read gpx file", gpxFile.error);
} else {
List<MapMarker> mapMarkers = markersHelper.getDataHelper().readMarkersFromGpx(gpxFile, true);
items.addAll(mapMarkers);
}
}
};
}
use of net.osmand.plus.settings.backend.backup.SettingsItemReader in project Osmand by osmandapp.
the class SettingsItem method getJsonReader.
@NonNull
protected SettingsItemReader<? extends SettingsItem> getJsonReader() {
return new SettingsItemReader<SettingsItem>(this) {
@Override
public void readFromStream(@NonNull InputStream inputStream, String entryName) throws IOException, IllegalArgumentException {
StringBuilder buf = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String str;
while ((str = in.readLine()) != null) {
buf.append(str);
}
} catch (IOException e) {
throw new IOException("Cannot read json body", e);
}
String json = buf.toString();
if (json.length() == 0) {
throw new IllegalArgumentException("Json body is empty");
}
try {
readItemsFromJson(new JSONObject(json));
} catch (JSONException e) {
throw new IllegalArgumentException("Json parsing error", e);
}
}
};
}
Aggregations