Search in sources :

Example 1 with BackupError

use of net.osmand.plus.backup.BackupError 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 2 with BackupError

use of net.osmand.plus.backup.BackupError 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 3 with BackupError

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

the class RegisterUserCommand method doInBackground.

@Override
protected Object doInBackground(Object... objects) {
    Map<String, String> params = new HashMap<>();
    params.put("email", email);
    params.put("login", String.valueOf(login));
    final String orderId = Algorithms.isEmpty(promoCode) ? getHelper().getOrderId() : promoCode;
    if (!Algorithms.isEmpty(orderId)) {
        params.put("orderid", orderId);
    }
    final String deviceId = getApp().getUserAndroidId();
    params.put("deviceid", deviceId);
    OperationLog operationLog = createOperationLog("registerUser");
    AndroidNetworkUtils.sendRequest(getApp(), USER_REGISTER_URL, params, "Register user", false, true, (resultJson, error, resultCode) -> {
        int status;
        String message;
        BackupError backupError = null;
        if (!Algorithms.isEmpty(error)) {
            backupError = new BackupError(error);
            message = "User registration error: " + backupError + "\nEmail=" + email + "\nOrderId=" + orderId + "\nDeviceId=" + deviceId;
            status = STATUS_SERVER_ERROR;
        } else if (!Algorithms.isEmpty(resultJson)) {
            try {
                JSONObject result = new JSONObject(resultJson);
                if (result.has("status") && "ok".equals(result.getString("status"))) {
                    message = "You have been registered successfully. Please check for email with activation code.";
                    status = STATUS_SUCCESS;
                } else {
                    message = "User registration error: unknown";
                    status = STATUS_SERVER_ERROR;
                }
            } catch (JSONException e) {
                message = "User registration error: json parsing";
                status = STATUS_PARSE_JSON_ERROR;
            }
        } else {
            message = "User registration error: empty response";
            status = STATUS_EMPTY_RESPONSE_ERROR;
        }
        publishProgress(status, message, backupError);
        operationLog.finishOperation(status + " " + message);
    });
    return null;
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) OperationLog(net.osmand.OperationLog) JSONException(org.json.JSONException) BackupError(net.osmand.plus.backup.BackupError)

Example 4 with BackupError

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

the class RegisterUserCommand method onProgressUpdate.

@Override
protected void onProgressUpdate(Object... values) {
    for (OnRegisterUserListener listener : getListeners()) {
        int status = (Integer) values[0];
        String message = (String) values[1];
        BackupError backupError = (BackupError) values[2];
        listener.onRegisterUser(status, message, backupError);
    }
}
Also used : OnRegisterUserListener(net.osmand.plus.backup.BackupListeners.OnRegisterUserListener) BackupError(net.osmand.plus.backup.BackupError)

Example 5 with BackupError

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

the class BaseDeleteFilesCommand method onPostExecute.

@Override
protected void onPostExecute(Object o) {
    List<OnDeleteFilesListener> listeners = getListeners();
    if (!listeners.isEmpty()) {
        Map<RemoteFile, String> errors = new HashMap<>();
        for (DeleteRemoteFileTask task : tasks) {
            boolean success;
            String message = null;
            RequestResponse response = task.response;
            if (response != null) {
                String errorStr = response.getError();
                if (!Algorithms.isEmpty(errorStr)) {
                    message = new BackupError(errorStr).toString();
                    success = false;
                } else {
                    String responseStr = response.getResponse();
                    try {
                        JSONObject result = new JSONObject(responseStr);
                        if (result.has("status") && "ok".equals(result.getString("status"))) {
                            success = true;
                        } else {
                            message = "Unknown error";
                            success = false;
                        }
                    } catch (JSONException e) {
                        message = "Json parsing error";
                        success = false;
                    }
                }
                if (!success) {
                    errors.put(task.remoteFile, message);
                }
            }
        }
        for (OnDeleteFilesListener listener : listeners) {
            listener.onFilesDeleteDone(errors);
        }
    }
    if (listener != null) {
        getListeners().remove(listener);
    }
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) JSONException(org.json.JSONException) OnDeleteFilesListener(net.osmand.plus.backup.BackupListeners.OnDeleteFilesListener) RequestResponse(net.osmand.plus.utils.AndroidNetworkUtils.RequestResponse) RemoteFile(net.osmand.plus.backup.RemoteFile) BackupError(net.osmand.plus.backup.BackupError)

Aggregations

BackupError (net.osmand.plus.backup.BackupError)7 HashMap (java.util.HashMap)5 JSONException (org.json.JSONException)5 JSONObject (org.json.JSONObject)5 BackupHelper (net.osmand.plus.backup.BackupHelper)3 OnDeleteFilesListener (net.osmand.plus.backup.BackupListeners.OnDeleteFilesListener)3 RemoteFile (net.osmand.plus.backup.RemoteFile)3 NonNull (androidx.annotation.NonNull)2 Nullable (androidx.annotation.Nullable)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 OperationLog (net.osmand.OperationLog)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 ExportSettingsType (net.osmand.plus.settings.backend.ExportSettingsType)2