Search in sources :

Example 21 with SettingsItem

use of net.osmand.plus.settings.backend.backup.items.SettingsItem in project Osmand by osmandapp.

the class BackupImporter method updateFilesInfo.

private void updateFilesInfo(@NonNull Map<String, RemoteFile> remoteFiles, @NonNull List<SettingsItem> settingsItemList) {
    Map<String, RemoteFile> remoteFilesMap = new HashMap<>(remoteFiles);
    for (SettingsItem settingsItem : settingsItemList) {
        List<RemoteFile> foundRemoteFiles = getItemRemoteFiles(settingsItem, remoteFilesMap);
        for (RemoteFile remoteFile : foundRemoteFiles) {
            settingsItem.setLastModifiedTime(remoteFile.getClienttimems());
            remoteFile.item = settingsItem;
            if (settingsItem instanceof FileSettingsItem) {
                FileSettingsItem fileSettingsItem = (FileSettingsItem) settingsItem;
                fileSettingsItem.setSize(remoteFile.getFilesize());
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) 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) FileSettingsItem(net.osmand.plus.settings.backend.backup.items.FileSettingsItem)

Example 22 with SettingsItem

use of net.osmand.plus.settings.backend.backup.items.SettingsItem in project Osmand by osmandapp.

the class BackupImporter method importItems.

void importItems(@NonNull List<SettingsItem> items, boolean forceReadData) throws IllegalArgumentException {
    if (Algorithms.isEmpty(items)) {
        throw new IllegalArgumentException("No items");
    }
    Collection<RemoteFile> remoteFiles = backupHelper.getBackup().getRemoteFiles(RemoteFilesType.UNIQUE).values();
    if (Algorithms.isEmpty(remoteFiles)) {
        throw new IllegalArgumentException("No remote files");
    }
    OperationLog operationLog = new OperationLog("importItems", BackupHelper.DEBUG);
    operationLog.startOperation();
    List<ItemFileImportTask> tasks = new ArrayList<>();
    Map<RemoteFile, SettingsItem> remoteFileItems = new HashMap<>();
    for (RemoteFile remoteFile : remoteFiles) {
        SettingsItem item = null;
        for (SettingsItem settingsItem : items) {
            String fileName = remoteFile.item != null ? remoteFile.item.getFileName() : null;
            if (fileName != null && settingsItem.applyFileName(fileName)) {
                item = settingsItem;
                remoteFileItems.put(remoteFile, item);
                break;
            }
        }
        if (item != null && (!item.shouldReadOnCollecting() || forceReadData)) {
            tasks.add(new ItemFileImportTask(remoteFile, item, forceReadData));
        }
    }
    ThreadPoolTaskExecutor<ItemFileImportTask> executor = createExecutor();
    executor.run(tasks);
    for (Entry<RemoteFile, SettingsItem> fileItem : remoteFileItems.entrySet()) {
        fileItem.getValue().setLocalModifiedTime(fileItem.getKey().getClienttimems());
    }
    operationLog.finishOperation();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) OperationLog(net.osmand.OperationLog) 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)

Example 23 with SettingsItem

use of net.osmand.plus.settings.backend.backup.items.SettingsItem in project Osmand by osmandapp.

the class NetworkWriter method uploadItemInfo.

@Nullable
private String uploadItemInfo(@NonNull SettingsItem item, @NonNull String fileName, long uploadTime) throws IOException {
    try {
        JSONObject json = item.toJsonObj();
        boolean hasFile = json.has("file");
        boolean hasSubtype = json.has("subtype");
        if (json.length() > (hasFile ? 2 + (hasSubtype ? 1 : 0) : 1)) {
            String itemJson = json.toString();
            InputStream inputStream = new ByteArrayInputStream(itemJson.getBytes("UTF-8"));
            StreamWriter streamWriter = (outputStream, progress) -> {
                Algorithms.streamCopy(inputStream, outputStream, progress, 1024);
                outputStream.flush();
            };
            return backupHelper.uploadFile(fileName, item.getType().name(), streamWriter, uploadTime, getUploadFileListener(item));
        } else {
            return null;
        }
    } catch (JSONException | UserNotRegisteredException e) {
        throw new IOException(e.getMessage(), e);
    }
}
Also used : SettingsItemWriter(net.osmand.plus.settings.backend.backup.SettingsItemWriter) StreamWriter(net.osmand.StreamWriter) OnUploadFileListener(net.osmand.plus.backup.BackupListeners.OnUploadFileListener) NonNull(androidx.annotation.NonNull) FileSettingsItem(net.osmand.plus.settings.backend.backup.items.FileSettingsItem) IOException(java.io.IOException) SettingsItem(net.osmand.plus.settings.backend.backup.items.SettingsItem) InterruptedIOException(java.io.InterruptedIOException) File(java.io.File) JSONException(org.json.JSONException) List(java.util.List) Nullable(androidx.annotation.Nullable) JSONObject(org.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) AbstractWriter(net.osmand.plus.settings.backend.backup.AbstractWriter) Algorithms(net.osmand.util.Algorithms) InputStream(java.io.InputStream) JSONObject(org.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StreamWriter(net.osmand.StreamWriter) JSONException(org.json.JSONException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) Nullable(androidx.annotation.Nullable)

Example 24 with SettingsItem

use of net.osmand.plus.settings.backend.backup.items.SettingsItem in project Osmand by osmandapp.

the class BackupInfo method createItemsToDelete.

private void createItemsToDelete() {
    Set<SettingsItem> items = new HashSet<>();
    for (RemoteFile remoteFile : filteredFilesToDelete) {
        SettingsItem item = remoteFile.item;
        if (item != null) {
            items.add(item);
        }
    }
    itemsToDelete = new ArrayList<>(items);
}
Also used : SettingsItem(net.osmand.plus.settings.backend.backup.items.SettingsItem) HashSet(java.util.HashSet)

Example 25 with SettingsItem

use of net.osmand.plus.settings.backend.backup.items.SettingsItem in project Osmand by osmandapp.

the class BackupInfo method createItemsToUpload.

private void createItemsToUpload() {
    Set<SettingsItem> items = new HashSet<>();
    for (LocalFile localFile : filteredFilesToUpload) {
        SettingsItem item = localFile.item;
        if (item != null) {
            items.add(item);
        }
    }
    itemsToUpload = new ArrayList<>(items);
}
Also used : SettingsItem(net.osmand.plus.settings.backend.backup.items.SettingsItem) HashSet(java.util.HashSet)

Aggregations

SettingsItem (net.osmand.plus.settings.backend.backup.items.SettingsItem)37 ArrayList (java.util.ArrayList)19 FileSettingsItem (net.osmand.plus.settings.backend.backup.items.FileSettingsItem)18 File (java.io.File)11 List (java.util.List)11 IOException (java.io.IOException)10 GpxSettingsItem (net.osmand.plus.settings.backend.backup.items.GpxSettingsItem)10 ProfileSettingsItem (net.osmand.plus.settings.backend.backup.items.ProfileSettingsItem)10 NonNull (androidx.annotation.NonNull)8 AvoidRoadsSettingsItem (net.osmand.plus.settings.backend.backup.items.AvoidRoadsSettingsItem)7 MapSourcesSettingsItem (net.osmand.plus.settings.backend.backup.items.MapSourcesSettingsItem)7 PoiUiFiltersSettingsItem (net.osmand.plus.settings.backend.backup.items.PoiUiFiltersSettingsItem)7 QuickActionsSettingsItem (net.osmand.plus.settings.backend.backup.items.QuickActionsSettingsItem)7 Nullable (androidx.annotation.Nullable)6 HashMap (java.util.HashMap)6 PluginSettingsItem (net.osmand.plus.settings.backend.backup.items.PluginSettingsItem)6 HashSet (java.util.HashSet)5 FileInputStream (java.io.FileInputStream)4 FragmentActivity (androidx.fragment.app.FragmentActivity)3 Map (java.util.Map)3