2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-10-29 15:19:41 +00:00

Upload files using a CompositeUploadFileJob making the upload cancelable

This commit is contained in:
Erik Duisters
2019-04-07 17:54:12 +00:00
parent 3c97e1c067
commit a3fb423dd3
10 changed files with 402 additions and 176 deletions

View File

@@ -47,7 +47,7 @@ import androidx.core.content.FileProvider;
import androidx.documentfile.provider.DocumentFile;
public class CompositeReceiveFileJob extends BackgroundJob<Device, Void> {
private final ShareNotification shareNotification;
private final ReceiveNotification receiveNotification;
private NetworkPacket currentNetworkPacket;
private String currentFileName;
private int currentFileNum;
@@ -66,8 +66,8 @@ public class CompositeReceiveFileJob extends BackgroundJob<Device, Void> {
lock = new Object();
networkPacketList = new ArrayList<>();
shareNotification = new ShareNotification(device);
shareNotification.addCancelAction(getId());
receiveNotification = new ReceiveNotification(device);
receiveNotification.addCancelAction(getId());
currentFileNum = 0;
totalNumFiles = 0;
totalPayloadSize = 0;
@@ -87,7 +87,7 @@ public class CompositeReceiveFileJob extends BackgroundJob<Device, Void> {
this.totalNumFiles = numberOfFiles;
this.totalPayloadSize = totalPayloadSize;
shareNotification.setTitle(getDevice().getContext().getResources()
receiveNotification.setTitle(getDevice().getContext().getResources()
.getQuantityString(R.plurals.incoming_file_title, totalNumFiles, totalNumFiles, getDevice().getName()));
}
}
@@ -100,7 +100,7 @@ public class CompositeReceiveFileJob extends BackgroundJob<Device, Void> {
totalNumFiles = networkPacket.getInt(SharePlugin.KEY_NUMBER_OF_FILES, 1);
totalPayloadSize = networkPacket.getLong(SharePlugin.KEY_TOTAL_PAYLOAD_SIZE);
shareNotification.setTitle(getDevice().getContext().getResources()
receiveNotification.setTitle(getDevice().getContext().getResources()
.getQuantityString(R.plurals.incoming_file_title, totalNumFiles, totalNumFiles, getDevice().getName()));
}
}
@@ -149,6 +149,7 @@ public class CompositeReceiveFileJob extends BackgroundJob<Device, Void> {
publishFile(fileDocument, received);
}
} else {
//TODO: Only set progress to 100 if this is the only file/packet to send
setProgress(100);
publishFile(fileDocument, 0);
}
@@ -180,7 +181,7 @@ public class CompositeReceiveFileJob extends BackgroundJob<Device, Void> {
isRunning = false;
if (canceled) {
shareNotification.cancel();
receiveNotification.cancel();
return;
}
@@ -190,23 +191,23 @@ public class CompositeReceiveFileJob extends BackgroundJob<Device, Void> {
}
if (numFiles == 1 && currentNetworkPacket.has("open")) {
shareNotification.cancel();
receiveNotification.cancel();
openFile(fileDocument);
} else {
//Update the notification and allow to open the file from it
shareNotification.setFinished(getDevice().getContext().getResources().getQuantityString(R.plurals.received_files_title, numFiles, getDevice().getName(), numFiles));
receiveNotification.setFinished(getDevice().getContext().getResources().getQuantityString(R.plurals.received_files_title, numFiles, getDevice().getName(), numFiles));
if (totalNumFiles == 1 && fileDocument != null) {
shareNotification.setURI(fileDocument.getUri(), fileDocument.getType(), fileDocument.getName());
receiveNotification.setURI(fileDocument.getUri(), fileDocument.getType(), fileDocument.getName());
}
shareNotification.show();
receiveNotification.show();
}
reportResult(null);
} catch (ActivityNotFoundException e) {
shareNotification.setFinished(getDevice().getContext().getString(R.string.no_app_for_opening));
shareNotification.show();
receiveNotification.setFinished(getDevice().getContext().getString(R.string.no_app_for_opening));
receiveNotification.show();
} catch (Exception e) {
isRunning = false;
@@ -217,8 +218,8 @@ public class CompositeReceiveFileJob extends BackgroundJob<Device, Void> {
failedFiles = (totalNumFiles - currentFileNum + 1);
}
shareNotification.setFinished(getDevice().getContext().getResources().getQuantityString(R.plurals.received_files_fail_title, failedFiles, getDevice().getName(), failedFiles, totalNumFiles));
shareNotification.show();
receiveNotification.setFinished(getDevice().getContext().getResources().getQuantityString(R.plurals.received_files_fail_title, failedFiles, getDevice().getName(), failedFiles, totalNumFiles));
receiveNotification.show();
reportError(e);
} finally {
closeAllInputStreams();
@@ -238,7 +239,7 @@ public class CompositeReceiveFileJob extends BackgroundJob<Device, Void> {
//We need to check for already existing files only when storing in the default path.
//User-defined paths use the new Storage Access Framework that already handles this.
//If the file should be opened immediately store it in the standard location to avoid the FileProvider trouble (See ShareNotification::setURI)
//If the file should be opened immediately store it in the standard location to avoid the FileProvider trouble (See ReceiveNotification::setURI)
if (open || !ShareSettingsFragment.isCustomDestinationEnabled(getDevice().getContext())) {
final String defaultPath = ShareSettingsFragment.getDefaultDestinationDirectory().getAbsolutePath();
filenameToUse = FilesHelper.findNonExistingNameForNewFile(defaultPath, filenameToUse);
@@ -300,10 +301,10 @@ public class CompositeReceiveFileJob extends BackgroundJob<Device, Void> {
private void setProgress(int progress) {
synchronized (lock) {
shareNotification.setProgress(progress, getDevice().getContext().getResources()
receiveNotification.setProgress(progress, getDevice().getContext().getResources()
.getQuantityString(R.plurals.incoming_files_text, totalNumFiles, currentFileName, currentFileNum, totalNumFiles));
}
shareNotification.show();
receiveNotification.show();
}
private void publishFile(DocumentFile fileDocument, long size) {