Search in sources :

Example 1 with SettingsItemsFactory

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;
}
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)

Aggregations

NonNull (androidx.annotation.NonNull)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 TreeSet (java.util.TreeSet)1 OperationLog (net.osmand.OperationLog)1 OsmandApplication (net.osmand.plus.OsmandApplication)1 UploadedFileInfo (net.osmand.plus.backup.BackupDbHelper.UploadedFileInfo)1 SettingsItemReader (net.osmand.plus.settings.backend.backup.SettingsItemReader)1 SettingsItemsFactory (net.osmand.plus.settings.backend.backup.SettingsItemsFactory)1 FileSettingsItem (net.osmand.plus.settings.backend.backup.items.FileSettingsItem)1 GpxSettingsItem (net.osmand.plus.settings.backend.backup.items.GpxSettingsItem)1 SettingsItem (net.osmand.plus.settings.backend.backup.items.SettingsItem)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1