From 4cd8c80226f65789202dda6aa65da57c81ffde50 Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Mon, 13 Jan 2020 12:05:02 +0100 Subject: [PATCH] optimise download manager update broadcasts --- app | 2 +- .../java/io/lbry/browser/DownloadManager.java | 30 +++++++++++++++++-- .../java/io/lbry/browser/LbrynetService.java | 21 ++++++++++--- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/app b/app index 24d580ea..55f61a85 160000 --- a/app +++ b/app @@ -1 +1 @@ -Subproject commit 24d580ea13a36f1e7515af8208cef924f049796c +Subproject commit 55f61a85f01f2d1b786c4cd0f4e8cc46a986717b diff --git a/src/main/java/io/lbry/browser/DownloadManager.java b/src/main/java/io/lbry/browser/DownloadManager.java index ffb7f4bf..b0673d53 100644 --- a/src/main/java/io/lbry/browser/DownloadManager.java +++ b/src/main/java/io/lbry/browser/DownloadManager.java @@ -18,10 +18,11 @@ import com.facebook.react.bridge.ReactMethod; import io.lbry.browser.receivers.NotificationDeletedReceiver; import java.text.DecimalFormat; -import java.util.HashMap; -import java.util.Random; -import java.util.List; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.List; +import java.util.Random; public class DownloadManager { private Context context; @@ -30,6 +31,9 @@ public class DownloadManager { private List completedDownloads = new ArrayList(); + // maintain a map of uris to writtenBytes, so that we check if it's changed and don't flood RN with update events every 500ms + private Map writtenDownloadBytes = new HashMap(); + private HashMap builders = new HashMap(); private HashMap downloadIdNotificationIdMap = new HashMap(); @@ -120,6 +124,26 @@ public class DownloadManager { return intent; } + public void updateWrittenBytesForDownload(String id, double writtenBytes) { + if (!writtenDownloadBytes.containsKey(id)) { + writtenDownloadBytes.put(id, writtenBytes); + } + } + + public double getWrittenBytesForDownload(String id) { + if (writtenDownloadBytes.containsKey(id)) { + return writtenDownloadBytes.get(id); + } + + return -1; + } + + public void clearWrittenBytesForDownload(String id) { + if (writtenDownloadBytes.containsKey(id)) { + writtenDownloadBytes.remove(id); + } + } + public void startDownload(String id, String filename) { if (filename == null || filename.trim().length() == 0) { return; diff --git a/src/main/java/io/lbry/browser/LbrynetService.java b/src/main/java/io/lbry/browser/LbrynetService.java index 6fc7e2be..e3126629 100644 --- a/src/main/java/io/lbry/browser/LbrynetService.java +++ b/src/main/java/io/lbry/browser/LbrynetService.java @@ -257,6 +257,7 @@ public class LbrynetService extends PythonService { String uri = String.format("lbry://%s#%s", claimName, claimId); if (!downloadManager.isDownloadActive(uri) && !downloadManager.isDownloadCompleted(uri)) { + downloadManager.clearWrittenBytesForDownload(uri); File file = new File(downloadPath); Intent intent = createDownloadEventIntent(uri, outpoint, item.toString()); intent.putExtra("action", "start"); @@ -320,18 +321,29 @@ public class LbrynetService extends PythonService { File file = new File(downloadPath); Intent intent = createDownloadEventIntent(uri, outpoint, item.toString()); + boolean shouldSendBroadcast = true; if (downloadManager.isDownloadActive(uri)) { if (writtenBytes >= totalBytes || completed) { // completed download + downloadManager.clearWrittenBytesForDownload(uri); intent.putExtra("action", "complete"); downloadManager.completeDownload(uri, file.getName(), totalBytes); } else { - intent.putExtra("action", "update"); - intent.putExtra("progress", (writtenBytes / totalBytes) * 100); - downloadManager.updateDownload(uri, file.getName(), writtenBytes, totalBytes); + double prevWrittenBytes = downloadManager.getWrittenBytesForDownload(uri); + if (prevWrittenBytes == writtenBytes) { + // no change, don't send an update event + shouldSendBroadcast = false; + } + downloadManager.updateWrittenBytesForDownload(uri, writtenBytes); + + if (shouldSendBroadcast) { + intent.putExtra("action", "update"); + intent.putExtra("progress", (writtenBytes / totalBytes) * 100); + downloadManager.updateDownload(uri, file.getName(), writtenBytes, totalBytes); + } } - if (context != null) { + if (context != null && shouldSendBroadcast) { context.sendBroadcast(intent); } } else { @@ -340,6 +352,7 @@ public class LbrynetService extends PythonService { continue; } if (!completed && downloadPath != null) { + downloadManager.clearWrittenBytesForDownload(uri); intent.putExtra("action", "start"); downloadManager.startDownload(uri, file.getName()); if (context != null) {