From e098ebdaf45847429742f964e19a63408d6107f9 Mon Sep 17 00:00:00 2001 From: Erik Duisters Date: Sat, 26 Jan 2019 16:59:39 +0100 Subject: [PATCH] Add packet type "kdeconnect.share.request.update" to allow android to update the share notification --- .../CompositeReceiveFileRunnable.java | 27 ++++++++++++++++--- .../Plugins/SharePlugin/SharePlugin.java | 13 ++++++++- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/org/kde/kdeconnect/Plugins/SharePlugin/CompositeReceiveFileRunnable.java b/src/org/kde/kdeconnect/Plugins/SharePlugin/CompositeReceiveFileRunnable.java index 0e1a4960..0f216c69 100644 --- a/src/org/kde/kdeconnect/Plugins/SharePlugin/CompositeReceiveFileRunnable.java +++ b/src/org/kde/kdeconnect/Plugins/SharePlugin/CompositeReceiveFileRunnable.java @@ -68,6 +68,7 @@ public class CompositeReceiveFileRunnable implements Runnable { private final List networkPacketList; private int totalNumFiles; private long totalPayloadSize; + private boolean isRunning; CompositeReceiveFileRunnable(Device device, CallBack callBack) { this.device = device; @@ -85,16 +86,28 @@ public class CompositeReceiveFileRunnable implements Runnable { handler = new Handler(Looper.getMainLooper()); } + boolean isRunning() { return isRunning; } + + void updateTotals(int numberOfFiles, long totalPayloadSize) { + synchronized (lock) { + this.totalNumFiles = numberOfFiles; + this.totalPayloadSize = totalPayloadSize; + + shareNotification.setTitle(device.getContext().getResources() + .getQuantityString(R.plurals.incoming_file_title, totalNumFiles, totalNumFiles, device.getName())); + } + } + void addNetworkPacket(NetworkPacket networkPacket) { - if (!networkPacketList.contains(networkPacket)) { - synchronized (lock) { + synchronized (lock) { + if (!networkPacketList.contains(networkPacket)) { networkPacketList.add(networkPacket); totalNumFiles = networkPacket.getInt(SharePlugin.KEY_NUMBER_OF_FILES, 1); totalPayloadSize = networkPacket.getLong(SharePlugin.KEY_TOTAL_PAYLOAD_SIZE); shareNotification.setTitle(device.getContext().getResources() - .getQuantityString(R.plurals.incoming_file_title, totalNumFiles, totalNumFiles, device.getName())); + .getQuantityString(R.plurals.incoming_file_title, totalNumFiles, totalNumFiles, device.getName())); } } } @@ -111,6 +124,8 @@ public class CompositeReceiveFileRunnable implements Runnable { try { DocumentFile fileDocument = null; + isRunning = true; + while (!done) { synchronized (lock) { currentNetworkPacket = networkPacketList.get(0); @@ -150,7 +165,7 @@ public class CompositeReceiveFileRunnable implements Runnable { if (listIsEmpty) { try { - Thread.sleep(250); + Thread.sleep(1000); } catch (InterruptedException ignored) {} synchronized (lock) { @@ -165,6 +180,8 @@ public class CompositeReceiveFileRunnable implements Runnable { } } + isRunning = false; + int numFiles; synchronized (lock) { numFiles = totalNumFiles; @@ -185,6 +202,8 @@ public class CompositeReceiveFileRunnable implements Runnable { } handler.post(() -> callBack.onSuccess(this)); } catch (Exception e) { + isRunning = false; + int failedFiles; synchronized (lock) { failedFiles = (totalNumFiles - currentFileNum + 1); diff --git a/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java b/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java index de7c2aaf..4beb2d5f 100644 --- a/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java +++ b/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java @@ -59,6 +59,7 @@ import androidx.core.content.ContextCompat; public class SharePlugin extends Plugin { private final static String PACKET_TYPE_SHARE_REQUEST = "kdeconnect.share.request"; + final static String PACKET_TYPE_SHARE_REQUEST_UPDATE = "kdeconnect.share.request.update"; final static String KEY_NUMBER_OF_FILES = "numberOfFiles"; final static String KEY_TOTAL_PAYLOAD_SIZE = "totalPayloadSize"; @@ -122,6 +123,16 @@ public class SharePlugin extends Plugin { @WorkerThread public boolean onPacketReceived(NetworkPacket np) { try { + if (np.getType().equals(PACKET_TYPE_SHARE_REQUEST_UPDATE)) { + if (receiveFileRunnable != null && receiveFileRunnable.isRunning()) { + receiveFileRunnable.updateTotals(np.getInt(KEY_NUMBER_OF_FILES), np.getLong(KEY_TOTAL_PAYLOAD_SIZE)); + } else { + Log.d("SharePlugin", "Received update packet but CompositeUploadJob is null or not running"); + } + + return true; + } + if (np.has("filename")) { if (isPermissionGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { receiveFile(np); @@ -388,7 +399,7 @@ public class SharePlugin extends Plugin { @Override public String[] getSupportedPacketTypes() { - return new String[]{PACKET_TYPE_SHARE_REQUEST}; + return new String[]{PACKET_TYPE_SHARE_REQUEST, PACKET_TYPE_SHARE_REQUEST_UPDATE}; } @Override