Search in sources :

Example 6 with BackupError

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

the class RegisterDeviceCommand method onProgressUpdate.

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

Example 7 with BackupError

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

the class RegisterDeviceCommand method doInBackground.

@Override
protected Object doInBackground(Object... objects) {
    Map<String, String> params = new HashMap<>();
    BackupHelper helper = getHelper();
    params.put("email", helper.getEmail());
    String orderId = helper.getOrderId();
    if (orderId != null) {
        params.put("orderid", orderId);
    }
    String androidId = helper.getAndroidId();
    if (!Algorithms.isEmpty(androidId)) {
        params.put("deviceid", androidId);
    }
    params.put("token", token);
    OperationLog operationLog = createOperationLog("registerDevice");
    AndroidNetworkUtils.sendRequest(getApp(), DEVICE_REGISTER_URL, params, "Register device", false, true, (resultJson, error, resultCode) -> {
        int status;
        String message;
        BackupError backupError = null;
        if (resultCode != null && isTemporallyUnavailableErrorCode(resultCode)) {
            message = "Device registration error code: " + resultCode;
            error = "{\"error\":{\"errorCode\":" + STATUS_SERVER_TEMPORALLY_UNAVAILABLE_ERROR + ",\"message\":\"" + message + "\"}}";
        }
        if (!Algorithms.isEmpty(error)) {
            backupError = new BackupError(error);
            message = "Device registration error: " + backupError;
            status = STATUS_SERVER_ERROR;
        } else if (!Algorithms.isEmpty(resultJson)) {
            OsmandSettings settings = getApp().getSettings();
            try {
                JSONObject result = new JSONObject(resultJson);
                settings.BACKUP_DEVICE_ID.set(result.getString("id"));
                settings.BACKUP_USER_ID.set(result.getString("userid"));
                settings.BACKUP_NATIVE_DEVICE_ID.set(result.getString("deviceid"));
                settings.BACKUP_ACCESS_TOKEN.set(result.getString("accesstoken"));
                settings.BACKUP_ACCESS_TOKEN_UPDATE_TIME.set(result.getString("udpatetime"));
                message = "Device have been registered successfully";
                status = STATUS_SUCCESS;
            } catch (JSONException e) {
                message = "Device registration error: json parsing";
                status = STATUS_PARSE_JSON_ERROR;
            }
        } else {
            message = "Device 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) BackupHelper(net.osmand.plus.backup.BackupHelper) OperationLog(net.osmand.OperationLog) JSONException(org.json.JSONException) BackupError(net.osmand.plus.backup.BackupError) OsmandSettings(net.osmand.plus.settings.backend.OsmandSettings)

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