Search in sources :

Example 1 with SettingsItemReader

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());
            }
        }
    };
}
Also used : MapMarker(net.osmand.plus.mapmarkers.MapMarker) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ItineraryGroupInfo(net.osmand.plus.mapmarkers.ItineraryDataHelper.ItineraryGroupInfo) LinkedHashMap(java.util.LinkedHashMap) SettingsItemReader(net.osmand.plus.settings.backend.backup.SettingsItemReader) NonNull(androidx.annotation.NonNull) MapMarkersGroup(net.osmand.plus.mapmarkers.MapMarkersGroup) GPXFile(net.osmand.GPXUtilities.GPXFile) Nullable(androidx.annotation.Nullable)

Example 2 with SettingsItemReader

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);
            }
        }
    };
}
Also used : SettingsItemReader(net.osmand.plus.settings.backend.backup.SettingsItemReader) MapMarker(net.osmand.plus.mapmarkers.MapMarker) InputStream(java.io.InputStream) NonNull(androidx.annotation.NonNull) GPXFile(net.osmand.GPXUtilities.GPXFile) Nullable(androidx.annotation.Nullable)

Example 3 with SettingsItemReader

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;
}
Also used : OsmandApplication(net.osmand.plus.OsmandApplication) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) OperationLog(net.osmand.OperationLog) UploadedFileInfo(net.osmand.plus.backup.BackupDbHelper.UploadedFileInfo) SettingsItemReader(net.osmand.plus.settings.backend.backup.SettingsItemReader) SettingsItem(net.osmand.plus.settings.backend.backup.items.SettingsItem) GpxSettingsItem(net.osmand.plus.settings.backend.backup.items.GpxSettingsItem) FileSettingsItem(net.osmand.plus.settings.backend.backup.items.FileSettingsItem) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) SettingsItemsFactory(net.osmand.plus.settings.backend.backup.SettingsItemsFactory) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) JSONObject(org.json.JSONObject) File(java.io.File) NonNull(androidx.annotation.NonNull)

Example 4 with SettingsItemReader

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);
            }
        }
    };
}
Also used : SettingsItemReader(net.osmand.plus.settings.backend.backup.SettingsItemReader) MapMarker(net.osmand.plus.mapmarkers.MapMarker) InputStream(java.io.InputStream) NonNull(androidx.annotation.NonNull) GPXFile(net.osmand.GPXUtilities.GPXFile) Nullable(androidx.annotation.Nullable)

Example 5 with SettingsItemReader

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);
            }
        }
    };
}
Also used : SettingsItemReader(net.osmand.plus.settings.backend.backup.SettingsItemReader) InputStreamReader(java.io.InputStreamReader) JSONObject(org.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NonNull(androidx.annotation.NonNull) BufferedReader(java.io.BufferedReader) JSONException(org.json.JSONException) IOException(java.io.IOException) NonNull(androidx.annotation.NonNull)

Aggregations

NonNull (androidx.annotation.NonNull)6 SettingsItemReader (net.osmand.plus.settings.backend.backup.SettingsItemReader)6 InputStream (java.io.InputStream)5 Nullable (androidx.annotation.Nullable)4 GPXFile (net.osmand.GPXUtilities.GPXFile)4 MapMarker (net.osmand.plus.mapmarkers.MapMarker)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 InputStreamReader (java.io.InputStreamReader)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 TreeSet (java.util.TreeSet)1 OperationLog (net.osmand.OperationLog)1 FavouritePoint (net.osmand.data.FavouritePoint)1