Search in sources :

Example 11 with OperationLog

use of net.osmand.OperationLog 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 12 with OperationLog

use of net.osmand.OperationLog in project Osmand by osmandapp.

the class BackupHelper method updateOrderId.

void updateOrderId(@Nullable OnUpdateSubscriptionListener listener) {
    Map<String, String> params = new HashMap<>();
    params.put("email", getEmail());
    String orderId = getOrderId();
    if (Algorithms.isEmpty(orderId)) {
        if (listener != null) {
            String message = "Order id is empty";
            String error = "{\"error\":{\"errorCode\":" + STATUS_NO_ORDER_ID_ERROR + ",\"message\":\"" + message + "\"}}";
            listener.onUpdateSubscription(STATUS_NO_ORDER_ID_ERROR, message, error);
        }
        return;
    } else {
        params.put("orderid", orderId);
    }
    String androidId = getAndroidId();
    if (!Algorithms.isEmpty(androidId)) {
        params.put("deviceid", androidId);
    }
    OperationLog operationLog = new OperationLog("updateOrderId", DEBUG);
    AndroidNetworkUtils.sendRequest(app, UPDATE_ORDER_ID_URL, params, "Update order id", false, true, (resultJson, error, resultCode) -> {
        int status;
        String message;
        if (!Algorithms.isEmpty(error)) {
            message = "Update order id error: " + new BackupError(error);
            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 = "Order id have been updated successfully";
                    status = STATUS_SUCCESS;
                } else {
                    message = "Update order id error: unknown";
                    status = STATUS_SERVER_ERROR;
                }
            } catch (JSONException e) {
                message = "Update order id error: json parsing";
                status = STATUS_PARSE_JSON_ERROR;
            }
        } else {
            message = "Update order id error: empty response";
            status = STATUS_EMPTY_RESPONSE_ERROR;
        }
        if (listener != null) {
            listener.onUpdateSubscription(status, message, error);
        }
        operationLog.finishOperation(status + " " + message);
    });
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) OperationLog(net.osmand.OperationLog) JSONException(org.json.JSONException) SuppressLint(android.annotation.SuppressLint)

Example 13 with OperationLog

use of net.osmand.OperationLog 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

OperationLog (net.osmand.OperationLog)13 HashMap (java.util.HashMap)9 ArrayList (java.util.ArrayList)6 JSONException (org.json.JSONException)6 SuppressLint (android.annotation.SuppressLint)5 IOException (java.io.IOException)5 JSONObject (org.json.JSONObject)5 NonNull (androidx.annotation.NonNull)4 SettingsItem (net.osmand.plus.settings.backend.backup.items.SettingsItem)4 UploadedFileInfo (net.osmand.plus.backup.BackupDbHelper.UploadedFileInfo)3 FileSettingsItem (net.osmand.plus.settings.backend.backup.items.FileSettingsItem)3 GpxSettingsItem (net.osmand.plus.settings.backend.backup.items.GpxSettingsItem)3 AsyncTask (android.os.AsyncTask)2 Nullable (androidx.annotation.Nullable)2 File (java.io.File)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 BackupError (net.osmand.plus.backup.BackupError)2 ExportSettingsType (net.osmand.plus.settings.backend.ExportSettingsType)2 AbstractProgress (net.osmand.plus.settings.backend.backup.AbstractProgress)2 JSONArray (org.json.JSONArray)2