Search in sources :

Example 1 with RemoteFile

use of net.osmand.plus.backup.RemoteFile in project Osmand by osmandapp.

the class BaseBackupTypesFragment method getDataList.

protected Map<ExportSettingsCategory, SettingsCategoryItems> getDataList() {
    Map<String, RemoteFile> remoteFiles = backupHelper.getBackup().getRemoteFiles(getRemoteFilesType());
    if (remoteFiles == null) {
        remoteFiles = Collections.emptyMap();
    }
    Map<ExportSettingsType, List<?>> settingsToOperate = new EnumMap<>(ExportSettingsType.class);
    for (ExportSettingsType type : ExportSettingsType.getEnabledTypes()) {
        List<RemoteFile> filesByType = new ArrayList<>();
        for (RemoteFile remoteFile : remoteFiles.values()) {
            if (ExportSettingsType.getExportSettingsTypeForRemoteFile(remoteFile) == type) {
                filesByType.add(remoteFile);
            }
        }
        settingsToOperate.put(type, filesByType);
    }
    return SettingsHelper.getSettingsToOperateByCategory(settingsToOperate, true);
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) EnumMap(java.util.EnumMap) RemoteFile(net.osmand.plus.backup.RemoteFile) ExportSettingsType(net.osmand.plus.settings.backend.ExportSettingsType)

Example 2 with RemoteFile

use of net.osmand.plus.backup.RemoteFile in project Osmand by osmandapp.

the class BaseDeleteFilesCommand method deleteFiles.

protected void deleteFiles(@NonNull List<RemoteFile> remoteFiles) {
    Map<String, String> commonParameters = new HashMap<>();
    commonParameters.put("deviceid", getHelper().getDeviceId());
    commonParameters.put("accessToken", getHelper().getAccessToken());
    List<DeleteRemoteFileTask> tasks = new ArrayList<>();
    for (RemoteFile remoteFile : remoteFiles) {
        Map<String, String> parameters = new HashMap<>(commonParameters);
        parameters.put("name", remoteFile.getName());
        parameters.put("type", remoteFile.getType());
        if (byVersion) {
            parameters.put("updatetime", String.valueOf(remoteFile.getUpdatetimems()));
        }
        Request r = new Request(byVersion ? DELETE_FILE_VERSION_URL : DELETE_FILE_URL, parameters, null, false, true);
        tasks.add(new DeleteRemoteFileTask(getApp(), r, remoteFile, byVersion));
    }
    ThreadPoolTaskExecutor<DeleteRemoteFileTask> executor = new ThreadPoolTaskExecutor<>(new OnThreadPoolTaskExecutorListener<DeleteRemoteFileTask>() {

        @Override
        public void onTaskStarted(@NonNull DeleteRemoteFileTask task) {
        }

        @Override
        public void onTaskFinished(@NonNull DeleteRemoteFileTask task) {
            publishProgress(task);
        }

        @Override
        public void onTasksFinished(@NonNull List<DeleteRemoteFileTask> tasks) {
            BaseDeleteFilesCommand.this.tasks = tasks;
        }
    });
    executor.run(tasks);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Request(net.osmand.plus.utils.AndroidNetworkUtils.Request) ThreadPoolTaskExecutor(net.osmand.plus.backup.ThreadPoolTaskExecutor) RemoteFile(net.osmand.plus.backup.RemoteFile)

Example 3 with RemoteFile

use of net.osmand.plus.backup.RemoteFile in project Osmand by osmandapp.

the class DeleteAllFilesCommand method getDeleteAllFilesListener.

private OnRequestResultListener getDeleteAllFilesListener() {
    return (resultJson, error, resultCode) -> {
        int status;
        String message;
        List<RemoteFile> remoteFiles = new ArrayList<>();
        if (!Algorithms.isEmpty(error)) {
            status = STATUS_SERVER_ERROR;
            message = "Download file list error: " + new BackupError(error);
        } else if (!Algorithms.isEmpty(resultJson)) {
            try {
                JSONObject result = new JSONObject(resultJson);
                JSONArray files = result.getJSONArray("allFiles");
                for (int i = 0; i < files.length(); i++) {
                    remoteFiles.add(new RemoteFile(files.getJSONObject(i)));
                }
                status = STATUS_SUCCESS;
                message = "OK";
            } catch (JSONException e) {
                status = STATUS_PARSE_JSON_ERROR;
                message = "Download file list error: json parsing";
            }
        } else {
            status = STATUS_EMPTY_RESPONSE_ERROR;
            message = "Download file list error: empty response";
        }
        if (status != STATUS_SUCCESS) {
            publishProgress(status, message);
        } else {
            List<RemoteFile> filesToDelete = new ArrayList<>();
            if (types != null) {
                for (RemoteFile file : remoteFiles) {
                    ExportSettingsType exportType = ExportSettingsType.getExportSettingsTypeForRemoteFile(file);
                    if (types.contains(exportType)) {
                        filesToDelete.add(file);
                    }
                }
            } else {
                filesToDelete.addAll(remoteFiles);
            }
            if (!filesToDelete.isEmpty()) {
                publishProgress(filesToDelete);
                deleteFiles(filesToDelete);
            } else {
                publishProgress(Collections.emptyMap());
            }
        }
    };
}
Also used : BackupHelper(net.osmand.plus.backup.BackupHelper) OnRequestResultListener(net.osmand.plus.utils.AndroidNetworkUtils.OnRequestResultListener) STATUS_EMPTY_RESPONSE_ERROR(net.osmand.plus.backup.BackupHelper.STATUS_EMPTY_RESPONSE_ERROR) STATUS_SUCCESS(net.osmand.plus.backup.BackupHelper.STATUS_SUCCESS) NonNull(androidx.annotation.NonNull) LIST_FILES_URL(net.osmand.plus.backup.BackupHelper.LIST_FILES_URL) HashMap(java.util.HashMap) OnDeleteFilesListener(net.osmand.plus.backup.BackupListeners.OnDeleteFilesListener) BackupError(net.osmand.plus.backup.BackupError) AndroidNetworkUtils(net.osmand.plus.utils.AndroidNetworkUtils) ExportSettingsType(net.osmand.plus.settings.backend.ExportSettingsType) ArrayList(java.util.ArrayList) STATUS_PARSE_JSON_ERROR(net.osmand.plus.backup.BackupHelper.STATUS_PARSE_JSON_ERROR) JSONException(org.json.JSONException) List(java.util.List) Nullable(androidx.annotation.Nullable) JSONObject(org.json.JSONObject) STATUS_SERVER_ERROR(net.osmand.plus.backup.BackupHelper.STATUS_SERVER_ERROR) Map(java.util.Map) Algorithms(net.osmand.util.Algorithms) RemoteFile(net.osmand.plus.backup.RemoteFile) Collections(java.util.Collections) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) ArrayList(java.util.ArrayList) List(java.util.List) BackupError(net.osmand.plus.backup.BackupError) RemoteFile(net.osmand.plus.backup.RemoteFile) ExportSettingsType(net.osmand.plus.settings.backend.ExportSettingsType)

Example 4 with RemoteFile

use of net.osmand.plus.backup.RemoteFile in project Osmand by osmandapp.

the class DeleteOldFilesCommand method getDeleteOldFilesListener.

private OnRequestResultListener getDeleteOldFilesListener() {
    return (resultJson, error, resultCode) -> {
        int status;
        String message;
        List<RemoteFile> remoteFiles = new ArrayList<>();
        if (!Algorithms.isEmpty(error)) {
            status = STATUS_SERVER_ERROR;
            message = "Download file list error: " + new BackupError(error);
        } else if (!Algorithms.isEmpty(resultJson)) {
            try {
                JSONObject result = new JSONObject(resultJson);
                JSONArray allFiles = result.getJSONArray("allFiles");
                for (int i = 0; i < allFiles.length(); i++) {
                    remoteFiles.add(new RemoteFile(allFiles.getJSONObject(i)));
                }
                JSONArray uniqueFiles = result.getJSONArray("uniqueFiles");
                for (int i = 0; i < uniqueFiles.length(); i++) {
                    remoteFiles.remove(new RemoteFile(uniqueFiles.getJSONObject(i)));
                }
                status = STATUS_SUCCESS;
                message = "OK";
            } catch (JSONException e) {
                status = STATUS_PARSE_JSON_ERROR;
                message = "Download file list error: json parsing";
            }
        } else {
            status = STATUS_EMPTY_RESPONSE_ERROR;
            message = "Download file list error: empty response";
        }
        if (status != STATUS_SUCCESS) {
            publishProgress(status, message);
        } else {
            List<RemoteFile> filesToDelete = new ArrayList<>();
            if (types != null) {
                for (RemoteFile file : remoteFiles) {
                    ExportSettingsType exportType = ExportSettingsType.getExportSettingsTypeForRemoteFile(file);
                    if (types.contains(exportType)) {
                        filesToDelete.add(file);
                    }
                }
            } else {
                filesToDelete.addAll(remoteFiles);
            }
            if (!filesToDelete.isEmpty()) {
                publishProgress(filesToDelete);
                deleteFiles(filesToDelete);
            } else {
                publishProgress(Collections.emptyMap());
            }
        }
    };
}
Also used : BackupHelper(net.osmand.plus.backup.BackupHelper) OnRequestResultListener(net.osmand.plus.utils.AndroidNetworkUtils.OnRequestResultListener) STATUS_EMPTY_RESPONSE_ERROR(net.osmand.plus.backup.BackupHelper.STATUS_EMPTY_RESPONSE_ERROR) STATUS_SUCCESS(net.osmand.plus.backup.BackupHelper.STATUS_SUCCESS) NonNull(androidx.annotation.NonNull) LIST_FILES_URL(net.osmand.plus.backup.BackupHelper.LIST_FILES_URL) HashMap(java.util.HashMap) OnDeleteFilesListener(net.osmand.plus.backup.BackupListeners.OnDeleteFilesListener) BackupError(net.osmand.plus.backup.BackupError) AndroidNetworkUtils(net.osmand.plus.utils.AndroidNetworkUtils) ExportSettingsType(net.osmand.plus.settings.backend.ExportSettingsType) ArrayList(java.util.ArrayList) STATUS_PARSE_JSON_ERROR(net.osmand.plus.backup.BackupHelper.STATUS_PARSE_JSON_ERROR) JSONException(org.json.JSONException) List(java.util.List) Nullable(androidx.annotation.Nullable) JSONObject(org.json.JSONObject) STATUS_SERVER_ERROR(net.osmand.plus.backup.BackupHelper.STATUS_SERVER_ERROR) Map(java.util.Map) Algorithms(net.osmand.util.Algorithms) RemoteFile(net.osmand.plus.backup.RemoteFile) Collections(java.util.Collections) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) ArrayList(java.util.ArrayList) List(java.util.List) BackupError(net.osmand.plus.backup.BackupError) RemoteFile(net.osmand.plus.backup.RemoteFile) ExportSettingsType(net.osmand.plus.settings.backend.ExportSettingsType)

Example 5 with RemoteFile

use of net.osmand.plus.backup.RemoteFile in project Osmand by osmandapp.

the class BaseDeleteFilesCommand method onProgressUpdate.

@Override
protected void onProgressUpdate(Object... objects) {
    for (OnDeleteFilesListener listener : getListeners()) {
        Object obj = objects[0];
        if (obj instanceof DeleteRemoteFileTask) {
            RemoteFile remoteFile = ((DeleteRemoteFileTask) obj).remoteFile;
            itemsProgress.add(remoteFile);
            listener.onFileDeleteProgress(remoteFile, itemsProgress.size());
        }
    }
}
Also used : OnDeleteFilesListener(net.osmand.plus.backup.BackupListeners.OnDeleteFilesListener) JSONObject(org.json.JSONObject) RemoteFile(net.osmand.plus.backup.RemoteFile)

Aggregations

RemoteFile (net.osmand.plus.backup.RemoteFile)8 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)4 List (java.util.List)4 OnDeleteFilesListener (net.osmand.plus.backup.BackupListeners.OnDeleteFilesListener)4 JSONObject (org.json.JSONObject)4 NonNull (androidx.annotation.NonNull)3 BackupError (net.osmand.plus.backup.BackupError)3 ExportSettingsType (net.osmand.plus.settings.backend.ExportSettingsType)3 JSONException (org.json.JSONException)3 Nullable (androidx.annotation.Nullable)2 Collections (java.util.Collections)2 Map (java.util.Map)2 BackupHelper (net.osmand.plus.backup.BackupHelper)2 LIST_FILES_URL (net.osmand.plus.backup.BackupHelper.LIST_FILES_URL)2 STATUS_EMPTY_RESPONSE_ERROR (net.osmand.plus.backup.BackupHelper.STATUS_EMPTY_RESPONSE_ERROR)2 STATUS_PARSE_JSON_ERROR (net.osmand.plus.backup.BackupHelper.STATUS_PARSE_JSON_ERROR)2 STATUS_SERVER_ERROR (net.osmand.plus.backup.BackupHelper.STATUS_SERVER_ERROR)2 STATUS_SUCCESS (net.osmand.plus.backup.BackupHelper.STATUS_SUCCESS)2 AndroidNetworkUtils (net.osmand.plus.utils.AndroidNetworkUtils)2