optimise download manager update broadcasts
This commit is contained in:
parent
7adcdc43cf
commit
4cd8c80226
3 changed files with 45 additions and 8 deletions
2
app
2
app
|
@ -1 +1 @@
|
||||||
Subproject commit 24d580ea13a36f1e7515af8208cef924f049796c
|
Subproject commit 55f61a85f01f2d1b786c4cd0f4e8cc46a986717b
|
|
@ -18,10 +18,11 @@ import com.facebook.react.bridge.ReactMethod;
|
||||||
import io.lbry.browser.receivers.NotificationDeletedReceiver;
|
import io.lbry.browser.receivers.NotificationDeletedReceiver;
|
||||||
|
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Random;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class DownloadManager {
|
public class DownloadManager {
|
||||||
private Context context;
|
private Context context;
|
||||||
|
@ -30,6 +31,9 @@ public class DownloadManager {
|
||||||
|
|
||||||
private List<String> completedDownloads = new ArrayList<String>();
|
private List<String> completedDownloads = new ArrayList<String>();
|
||||||
|
|
||||||
|
// 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<String, Double> writtenDownloadBytes = new HashMap<String, Double>();
|
||||||
|
|
||||||
private HashMap<Integer, NotificationCompat.Builder> builders = new HashMap<Integer, NotificationCompat.Builder>();
|
private HashMap<Integer, NotificationCompat.Builder> builders = new HashMap<Integer, NotificationCompat.Builder>();
|
||||||
|
|
||||||
private HashMap<String, Integer> downloadIdNotificationIdMap = new HashMap<String, Integer>();
|
private HashMap<String, Integer> downloadIdNotificationIdMap = new HashMap<String, Integer>();
|
||||||
|
@ -120,6 +124,26 @@ public class DownloadManager {
|
||||||
return intent;
|
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) {
|
public void startDownload(String id, String filename) {
|
||||||
if (filename == null || filename.trim().length() == 0) {
|
if (filename == null || filename.trim().length() == 0) {
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -257,6 +257,7 @@ public class LbrynetService extends PythonService {
|
||||||
String uri = String.format("lbry://%s#%s", claimName, claimId);
|
String uri = String.format("lbry://%s#%s", claimName, claimId);
|
||||||
|
|
||||||
if (!downloadManager.isDownloadActive(uri) && !downloadManager.isDownloadCompleted(uri)) {
|
if (!downloadManager.isDownloadActive(uri) && !downloadManager.isDownloadCompleted(uri)) {
|
||||||
|
downloadManager.clearWrittenBytesForDownload(uri);
|
||||||
File file = new File(downloadPath);
|
File file = new File(downloadPath);
|
||||||
Intent intent = createDownloadEventIntent(uri, outpoint, item.toString());
|
Intent intent = createDownloadEventIntent(uri, outpoint, item.toString());
|
||||||
intent.putExtra("action", "start");
|
intent.putExtra("action", "start");
|
||||||
|
@ -320,18 +321,29 @@ public class LbrynetService extends PythonService {
|
||||||
|
|
||||||
File file = new File(downloadPath);
|
File file = new File(downloadPath);
|
||||||
Intent intent = createDownloadEventIntent(uri, outpoint, item.toString());
|
Intent intent = createDownloadEventIntent(uri, outpoint, item.toString());
|
||||||
|
boolean shouldSendBroadcast = true;
|
||||||
if (downloadManager.isDownloadActive(uri)) {
|
if (downloadManager.isDownloadActive(uri)) {
|
||||||
if (writtenBytes >= totalBytes || completed) {
|
if (writtenBytes >= totalBytes || completed) {
|
||||||
// completed download
|
// completed download
|
||||||
|
downloadManager.clearWrittenBytesForDownload(uri);
|
||||||
intent.putExtra("action", "complete");
|
intent.putExtra("action", "complete");
|
||||||
downloadManager.completeDownload(uri, file.getName(), totalBytes);
|
downloadManager.completeDownload(uri, file.getName(), totalBytes);
|
||||||
} else {
|
} else {
|
||||||
intent.putExtra("action", "update");
|
double prevWrittenBytes = downloadManager.getWrittenBytesForDownload(uri);
|
||||||
intent.putExtra("progress", (writtenBytes / totalBytes) * 100);
|
if (prevWrittenBytes == writtenBytes) {
|
||||||
downloadManager.updateDownload(uri, file.getName(), writtenBytes, totalBytes);
|
// 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);
|
context.sendBroadcast(intent);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -340,6 +352,7 @@ public class LbrynetService extends PythonService {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!completed && downloadPath != null) {
|
if (!completed && downloadPath != null) {
|
||||||
|
downloadManager.clearWrittenBytesForDownload(uri);
|
||||||
intent.putExtra("action", "start");
|
intent.putExtra("action", "start");
|
||||||
downloadManager.startDownload(uri, file.getName());
|
downloadManager.startDownload(uri, file.getName());
|
||||||
if (context != null) {
|
if (context != null) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue