Search in sources :

Example 6 with RemoteFile

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

Example 7 with RemoteFile

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

the class BackupSettingsFragment method setupSizeSummary.

private void setupSizeSummary(TextView summary, Map<String, RemoteFile> remoteFiles) {
    if (!Algorithms.isEmpty(remoteFiles)) {
        int filesSize = 0;
        for (RemoteFile remoteFile : remoteFiles.values()) {
            filesSize += remoteFile.getZipSize();
        }
        summary.setText(AndroidUtils.formatSize(app, filesSize));
        AndroidUiHelper.updateVisibility(summary, true);
    } else {
        AndroidUiHelper.updateVisibility(summary, false);
    }
}
Also used : RemoteFile(net.osmand.plus.backup.RemoteFile)

Example 8 with RemoteFile

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

the class RestoreSettingsFragment method collectAndReadSettings.

private void collectAndReadSettings() {
    BackupCollectListener collectListener = new BackupCollectListener() {

        @Nullable
        private SettingsItem getRestoreItem(@NonNull List<SettingsItem> items, @NonNull RemoteFile remoteFile) {
            for (SettingsItem item : items) {
                if (BackupHelper.applyItem(item, remoteFile.getType(), remoteFile.getName())) {
                    return item;
                }
            }
            return null;
        }

        @Override
        public void onBackupCollectFinished(boolean succeed, boolean empty, @NonNull List<SettingsItem> items, @NonNull List<RemoteFile> remoteFiles) {
            FragmentActivity activity = getActivity();
            if (AndroidUtils.isActivityNotDestroyed(activity)) {
                toolbarLayout.setTitle(getString(R.string.restore_from_osmand_cloud));
                description.setText(R.string.choose_what_to_restore);
                buttonsContainer.setVisibility(View.VISIBLE);
                progressBar.setVisibility(View.GONE);
                if (succeed) {
                    PrepareBackupResult backup = app.getBackupHelper().getBackup();
                    BackupInfo info = backup.getBackupInfo();
                    Set<SettingsItem> itemsForRestore = new HashSet<>();
                    if (info != null) {
                        for (RemoteFile remoteFile : info.filesToDownload) {
                            SettingsItem restoreItem = getRestoreItem(items, remoteFile);
                            if (restoreItem != null) {
                                itemsForRestore.add(restoreItem);
                            }
                        }
                    }
                    setSettingsItems(new ArrayList<>(itemsForRestore));
                    dataList = SettingsHelper.getSettingsToOperateByCategory(settingsItems, false, false);
                    adapter.updateSettingsItems(dataList, selectedItemsMap);
                }
            }
        }
    };
    try {
        settingsHelper.collectSettings(RESTORE_ITEMS_KEY, true, collectListener);
    } catch (IllegalStateException e) {
        LOG.error(e.getMessage(), e);
    }
}
Also used : BackupCollectListener(net.osmand.plus.backup.NetworkSettingsHelper.BackupCollectListener) BackupInfo(net.osmand.plus.backup.BackupInfo) FragmentActivity(androidx.fragment.app.FragmentActivity) SettingsItem(net.osmand.plus.settings.backend.backup.items.SettingsItem) NonNull(androidx.annotation.NonNull) PrepareBackupResult(net.osmand.plus.backup.PrepareBackupResult) ArrayList(java.util.ArrayList) List(java.util.List) RemoteFile(net.osmand.plus.backup.RemoteFile) HashSet(java.util.HashSet)

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