Search in sources :

Example 1 with AbstractProgress

use of net.osmand.plus.settings.backend.backup.AbstractProgress in project Osmand by osmandapp.

the class BackupHelper method downloadFile.

@NonNull
String downloadFile(@NonNull File file, @NonNull RemoteFile remoteFile, @Nullable OnDownloadFileListener listener) throws UserNotRegisteredException {
    checkRegistered();
    OperationLog operationLog = new OperationLog("downloadFile " + file.getName(), DEBUG);
    String error;
    String type = remoteFile.getType();
    String fileName = remoteFile.getName();
    try {
        Map<String, String> params = new HashMap<>();
        params.put("deviceid", getDeviceId());
        params.put("accessToken", getAccessToken());
        params.put("name", fileName);
        params.put("type", type);
        StringBuilder sb = new StringBuilder(DOWNLOAD_FILE_URL);
        boolean firstParam = true;
        for (Entry<String, String> entry : params.entrySet()) {
            sb.append(firstParam ? "?" : "&").append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), "UTF-8"));
            firstParam = false;
        }
        IProgress progress = new AbstractProgress() {

            private int work = 0;

            private int progress = 0;

            private int deltaProgress = 0;

            @Override
            public void startWork(int work) {
                if (listener != null) {
                    this.work = work > 0 ? work : 1;
                    listener.onFileDownloadStarted(type, fileName, work);
                }
            }

            @Override
            public void progress(int deltaWork) {
                if (listener != null) {
                    deltaProgress += deltaWork;
                    if ((deltaProgress > (work / 100)) || ((progress + deltaProgress) >= work)) {
                        progress += deltaProgress;
                        listener.onFileDownloadProgress(type, fileName, progress, deltaProgress);
                        deltaProgress = 0;
                    }
                }
            }

            @Override
            public boolean isInterrupted() {
                if (listener != null) {
                    return listener.isDownloadCancelled();
                }
                return super.isInterrupted();
            }
        };
        progress.startWork((int) (remoteFile.getFilesize() / 1024));
        error = AndroidNetworkUtils.downloadFile(sb.toString(), file, true, progress);
    } catch (UnsupportedEncodingException e) {
        error = "UnsupportedEncodingException";
    }
    if (listener != null) {
        listener.onFileDownloadDone(type, fileName, error);
    }
    operationLog.finishOperation();
    return error;
}
Also used : IProgress(net.osmand.IProgress) HashMap(java.util.HashMap) OperationLog(net.osmand.OperationLog) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AbstractProgress(net.osmand.plus.settings.backend.backup.AbstractProgress) NonNull(androidx.annotation.NonNull)

Example 2 with AbstractProgress

use of net.osmand.plus.settings.backend.backup.AbstractProgress in project Osmand by osmandapp.

the class BackupHelper method uploadFile.

@Nullable
String uploadFile(@NonNull String fileName, @NonNull String type, @NonNull StreamWriter streamWriter, final long uploadTime, @Nullable final OnUploadFileListener listener) throws UserNotRegisteredException {
    checkRegistered();
    Map<String, String> params = new HashMap<>();
    params.put("deviceid", getDeviceId());
    params.put("accessToken", getAccessToken());
    params.put("name", fileName);
    params.put("type", type);
    params.put("clienttime", String.valueOf(uploadTime));
    Map<String, String> headers = new HashMap<>();
    headers.put("Accept-Encoding", "deflate, gzip");
    OperationLog operationLog = new OperationLog("uploadFile", DEBUG);
    operationLog.startOperation(type + " " + fileName);
    String error = AndroidNetworkUtils.uploadFile(UPLOAD_FILE_URL, streamWriter, fileName, true, params, headers, new AbstractProgress() {

        private int work = 0;

        private int progress = 0;

        private int deltaProgress = 0;

        @Override
        public void startWork(int work) {
            if (listener != null) {
                this.work = work > 0 ? work : 1;
                listener.onFileUploadStarted(type, fileName, work);
            }
        }

        @Override
        public void progress(int deltaWork) {
            if (listener != null) {
                deltaProgress += deltaWork;
                if ((deltaProgress > (work / 100)) || ((progress + deltaProgress) >= work)) {
                    progress += deltaProgress;
                    listener.onFileUploadProgress(type, fileName, progress, deltaProgress);
                    deltaProgress = 0;
                }
            }
        }

        @Override
        public boolean isInterrupted() {
            if (listener != null) {
                return listener.isUploadCancelled();
            }
            return super.isInterrupted();
        }
    });
    if (error == null) {
        updateFileUploadTime(type, fileName, uploadTime);
    }
    if (listener != null) {
        listener.onFileUploadDone(type, fileName, uploadTime, error);
    }
    operationLog.finishOperation(type + " " + fileName + (error != null ? " Error: " + new BackupError(error) : " OK"));
    return error;
}
Also used : HashMap(java.util.HashMap) OperationLog(net.osmand.OperationLog) AbstractProgress(net.osmand.plus.settings.backend.backup.AbstractProgress) SuppressLint(android.annotation.SuppressLint) Nullable(androidx.annotation.Nullable)

Aggregations

HashMap (java.util.HashMap)2 OperationLog (net.osmand.OperationLog)2 AbstractProgress (net.osmand.plus.settings.backend.backup.AbstractProgress)2 SuppressLint (android.annotation.SuppressLint)1 NonNull (androidx.annotation.NonNull)1 Nullable (androidx.annotation.Nullable)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 IProgress (net.osmand.IProgress)1