Search in sources :

Example 1 with OnDeleteFilesListener

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

the class BackupExporter method exportItems.

private void exportItems() throws IOException {
    AtomicInteger dataProgress = new AtomicInteger(0);
    Set<Object> itemsProgress = Collections.synchronizedSet(new HashSet<>());
    Map<String, String> errors = new ConcurrentHashMap<>();
    OnUploadItemListener uploadItemListener = getOnUploadItemListener(itemsProgress, dataProgress, errors);
    OnDeleteFilesListener deleteFilesListener = getOnDeleteFilesListener(itemsProgress, dataProgress);
    NetworkWriter networkWriter = new NetworkWriter(backupHelper, uploadItemListener);
    writeItems(networkWriter);
    deleteFiles(deleteFilesListener);
    deleteOldFiles(deleteFilesListener);
    if (!isCancelled()) {
        backupHelper.updateBackupUploadTime();
    }
    if (listener != null) {
        listener.networkExportDone(errors);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OnDeleteFilesListener(net.osmand.plus.backup.BackupListeners.OnDeleteFilesListener) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) OnUploadItemListener(net.osmand.plus.backup.NetworkWriter.OnUploadItemListener)

Example 2 with OnDeleteFilesListener

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

the class DeleteAllFilesCommand method onProgressUpdate.

@Override
protected void onProgressUpdate(Object... objects) {
    super.onProgressUpdate(objects);
    for (OnDeleteFilesListener listener : getListeners()) {
        Object obj = objects[0];
        if (obj instanceof Map) {
            listener.onFilesDeleteDone((Map) obj);
        } else if (obj instanceof List) {
            listener.onFilesDeleteStarted((List) obj);
        } else if (obj instanceof Integer && objects.length == 2) {
            int status = (Integer) obj;
            String message = (String) objects[1];
            listener.onFilesDeleteError(status, message);
        }
    }
}
Also used : OnDeleteFilesListener(net.osmand.plus.backup.BackupListeners.OnDeleteFilesListener) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with OnDeleteFilesListener

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

the class DeleteOldFilesCommand method onProgressUpdate.

@Override
protected void onProgressUpdate(Object... objects) {
    super.onProgressUpdate(objects);
    for (OnDeleteFilesListener listener : getListeners()) {
        Object obj = objects[0];
        if (obj instanceof Map) {
            listener.onFilesDeleteDone((Map) obj);
        } else if (obj instanceof List) {
            listener.onFilesDeleteStarted((List) obj);
        } else if (obj instanceof Integer && objects.length == 2) {
            int status = (Integer) obj;
            String message = (String) objects[1];
            listener.onFilesDeleteError(status, message);
        }
    }
}
Also used : OnDeleteFilesListener(net.osmand.plus.backup.BackupListeners.OnDeleteFilesListener) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with OnDeleteFilesListener

use of net.osmand.plus.backup.BackupListeners.OnDeleteFilesListener 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)

Example 5 with OnDeleteFilesListener

use of net.osmand.plus.backup.BackupListeners.OnDeleteFilesListener 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

OnDeleteFilesListener (net.osmand.plus.backup.BackupListeners.OnDeleteFilesListener)5 JSONObject (org.json.JSONObject)4 HashMap (java.util.HashMap)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 RemoteFile (net.osmand.plus.backup.RemoteFile)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 BackupError (net.osmand.plus.backup.BackupError)1 OnUploadItemListener (net.osmand.plus.backup.NetworkWriter.OnUploadItemListener)1 RequestResponse (net.osmand.plus.utils.AndroidNetworkUtils.RequestResponse)1 JSONException (org.json.JSONException)1