Search in sources :

Example 1 with OnUploadFileListener

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

the class NetworkWriter method getUploadFileListener.

@NonNull
private OnUploadFileListener getUploadFileListener(@NonNull final SettingsItem item) {
    return new OnUploadFileListener() {

        @Override
        public void onFileUploadStarted(@NonNull String type, @NonNull String fileName, int work) {
            if (listener != null) {
                listener.onItemUploadStarted(item, fileName, work);
            }
        }

        @Override
        public void onFileUploadProgress(@NonNull String type, @NonNull String fileName, int progress, int deltaWork) {
            if (listener != null) {
                listener.onItemUploadProgress(item, fileName, progress, deltaWork);
            }
        }

        @Override
        public void onFileUploadDone(@NonNull String type, @NonNull String fileName, long uploadTime, @Nullable String error) {
            if (item instanceof FileSettingsItem) {
                FileSettingsItem fileItem = (FileSettingsItem) item;
                String itemFileName = BackupHelper.getFileItemName(fileItem);
                if (backupHelper.getApp().getAppPath(itemFileName).isDirectory()) {
                    backupHelper.updateFileUploadTime(item.getType().name(), itemFileName, uploadTime);
                }
                if (fileItem.needMd5Digest() && !Algorithms.isEmpty(fileItem.getMd5Digest())) {
                    backupHelper.updateFileMd5Digest(item.getType().name(), fileName, fileItem.getMd5Digest());
                }
            }
            if (listener != null) {
                listener.onItemFileUploadDone(item, fileName, uploadTime, error);
            }
        }

        @Override
        public boolean isUploadCancelled() {
            return isCancelled();
        }
    };
}
Also used : OnUploadFileListener(net.osmand.plus.backup.BackupListeners.OnUploadFileListener) FileSettingsItem(net.osmand.plus.settings.backend.backup.items.FileSettingsItem) NonNull(androidx.annotation.NonNull) Nullable(androidx.annotation.Nullable) NonNull(androidx.annotation.NonNull)

Example 2 with OnUploadFileListener

use of net.osmand.plus.backup.BackupListeners.OnUploadFileListener in project OsmAnd by osmandapp.

the class NetworkWriter method getUploadDirListener.

@NonNull
private OnUploadFileListener getUploadDirListener(@NonNull SettingsItem item, @NonNull String itemFileName, int itemWork) {
    return new OnUploadFileListener() {

        private int itemProgress = 0;

        private int deltaProgress = 0;

        private boolean uploadStarted = false;

        @Override
        public void onFileUploadStarted(@NonNull String type, @NonNull String fileName, int work) {
            if (!uploadStarted && listener != null) {
                uploadStarted = true;
                listener.onItemUploadStarted(item, itemFileName, itemWork);
            }
        }

        @Override
        public void onFileUploadProgress(@NonNull String type, @NonNull String fileName, int progress, int deltaWork) {
            if (listener != null) {
                deltaProgress += deltaWork;
                if ((deltaProgress > (itemWork / 100)) || ((itemProgress + deltaProgress) >= itemWork)) {
                    itemProgress += deltaProgress;
                    listener.onItemUploadProgress(item, itemFileName, itemProgress, deltaProgress);
                    deltaProgress = 0;
                }
            }
        }

        @Override
        public void onFileUploadDone(@NonNull String type, @NonNull String fileName, long uploadTime, @Nullable String error) {
            if (item instanceof FileSettingsItem) {
                FileSettingsItem fileItem = (FileSettingsItem) item;
                String itemFileName = BackupHelper.getFileItemName(fileItem);
                if (backupHelper.getApp().getAppPath(itemFileName).isDirectory()) {
                    backupHelper.updateFileUploadTime(item.getType().name(), itemFileName, uploadTime);
                }
                if (fileItem.needMd5Digest() && !Algorithms.isEmpty(fileItem.getMd5Digest())) {
                    backupHelper.updateFileMd5Digest(item.getType().name(), fileName, fileItem.getMd5Digest());
                }
            }
            if (listener != null) {
                listener.onItemFileUploadDone(item, fileName, uploadTime, error);
            }
        }

        @Override
        public boolean isUploadCancelled() {
            return isCancelled();
        }
    };
}
Also used : OnUploadFileListener(net.osmand.plus.backup.BackupListeners.OnUploadFileListener) FileSettingsItem(net.osmand.plus.settings.backend.backup.items.FileSettingsItem) NonNull(androidx.annotation.NonNull) Nullable(androidx.annotation.Nullable) NonNull(androidx.annotation.NonNull)

Example 3 with OnUploadFileListener

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

the class NetworkWriter method uploadDirWithFiles.

@Nullable
private String uploadDirWithFiles(@NonNull SettingsItemWriter<? extends SettingsItem> itemWriter, @NonNull String fileName, long uploadTime) throws UserNotRegisteredException, IOException {
    FileSettingsItem item = (FileSettingsItem) itemWriter.getItem();
    List<File> filesToUpload = backupHelper.collectItemFilesForUpload(item);
    long size = 0;
    for (File file : filesToUpload) {
        size += file.length();
    }
    OnUploadFileListener uploadListener = getUploadDirListener(item, fileName, (int) (size / 1024));
    for (File file : filesToUpload) {
        item.setFileToWrite(file);
        String name = BackupHelper.getFileItemName(file, item);
        String error = uploadItemFile(itemWriter, name, uploadListener, uploadTime);
        if (error != null) {
            return error;
        }
    }
    return null;
}
Also used : OnUploadFileListener(net.osmand.plus.backup.BackupListeners.OnUploadFileListener) FileSettingsItem(net.osmand.plus.settings.backend.backup.items.FileSettingsItem) File(java.io.File) Nullable(androidx.annotation.Nullable)

Example 4 with OnUploadFileListener

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

the class NetworkWriter method getUploadDirListener.

@NonNull
private OnUploadFileListener getUploadDirListener(@NonNull SettingsItem item, @NonNull String itemFileName, int itemWork) {
    return new OnUploadFileListener() {

        private int itemProgress = 0;

        private int deltaProgress = 0;

        private boolean uploadStarted = false;

        @Override
        public void onFileUploadStarted(@NonNull String type, @NonNull String fileName, int work) {
            if (!uploadStarted && listener != null) {
                uploadStarted = true;
                listener.onItemUploadStarted(item, itemFileName, itemWork);
            }
        }

        @Override
        public void onFileUploadProgress(@NonNull String type, @NonNull String fileName, int progress, int deltaWork) {
            if (listener != null) {
                deltaProgress += deltaWork;
                if ((deltaProgress > (itemWork / 100)) || ((itemProgress + deltaProgress) >= itemWork)) {
                    itemProgress += deltaProgress;
                    listener.onItemUploadProgress(item, itemFileName, itemProgress, deltaProgress);
                    deltaProgress = 0;
                }
            }
        }

        @Override
        public void onFileUploadDone(@NonNull String type, @NonNull String fileName, long uploadTime, @Nullable String error) {
            if (item instanceof FileSettingsItem) {
                FileSettingsItem fileItem = (FileSettingsItem) item;
                String itemFileName = BackupHelper.getFileItemName(fileItem);
                if (backupHelper.getApp().getAppPath(itemFileName).isDirectory()) {
                    backupHelper.updateFileUploadTime(item.getType().name(), itemFileName, uploadTime);
                }
                if (fileItem.needMd5Digest() && !Algorithms.isEmpty(fileItem.getMd5Digest())) {
                    backupHelper.updateFileMd5Digest(item.getType().name(), fileName, fileItem.getMd5Digest());
                }
            }
            if (listener != null) {
                listener.onItemFileUploadDone(item, fileName, uploadTime, error);
            }
        }

        @Override
        public boolean isUploadCancelled() {
            return isCancelled();
        }
    };
}
Also used : OnUploadFileListener(net.osmand.plus.backup.BackupListeners.OnUploadFileListener) FileSettingsItem(net.osmand.plus.settings.backend.backup.items.FileSettingsItem) NonNull(androidx.annotation.NonNull) Nullable(androidx.annotation.Nullable) NonNull(androidx.annotation.NonNull)

Example 5 with OnUploadFileListener

use of net.osmand.plus.backup.BackupListeners.OnUploadFileListener in project OsmAnd by osmandapp.

the class NetworkWriter method uploadDirWithFiles.

@Nullable
private String uploadDirWithFiles(@NonNull SettingsItemWriter<? extends SettingsItem> itemWriter, @NonNull String fileName, long uploadTime) throws UserNotRegisteredException, IOException {
    FileSettingsItem item = (FileSettingsItem) itemWriter.getItem();
    List<File> filesToUpload = backupHelper.collectItemFilesForUpload(item);
    long size = 0;
    for (File file : filesToUpload) {
        size += file.length();
    }
    OnUploadFileListener uploadListener = getUploadDirListener(item, fileName, (int) (size / 1024));
    for (File file : filesToUpload) {
        item.setFileToWrite(file);
        String name = BackupHelper.getFileItemName(file, item);
        String error = uploadItemFile(itemWriter, name, uploadListener, uploadTime);
        if (error != null) {
            return error;
        }
    }
    return null;
}
Also used : OnUploadFileListener(net.osmand.plus.backup.BackupListeners.OnUploadFileListener) FileSettingsItem(net.osmand.plus.settings.backend.backup.items.FileSettingsItem) File(java.io.File) Nullable(androidx.annotation.Nullable)

Aggregations

Nullable (androidx.annotation.Nullable)6 OnUploadFileListener (net.osmand.plus.backup.BackupListeners.OnUploadFileListener)6 FileSettingsItem (net.osmand.plus.settings.backend.backup.items.FileSettingsItem)6 NonNull (androidx.annotation.NonNull)4 File (java.io.File)2