From fb8841008f5131c1a6688e366e3ab29634615eac Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Mon, 27 Mar 2023 12:29:37 -0500 Subject: [PATCH] Throttle BobTransfer re-attempts --- .../com/cheogram/android/BobTransfer.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/cheogram/java/com/cheogram/android/BobTransfer.java b/src/cheogram/java/com/cheogram/android/BobTransfer.java index 74714bf4d4c6ca55c6e7c54f95317ed240176ad3..95fab8eab16af95e4b08b209aa1dc5bc5b58fb3c 100644 --- a/src/cheogram/java/com/cheogram/android/BobTransfer.java +++ b/src/cheogram/java/com/cheogram/android/BobTransfer.java @@ -4,6 +4,8 @@ import android.net.Uri; import android.util.Base64; import android.util.Log; +import java.util.Map; +import java.util.HashMap; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; @@ -36,6 +38,7 @@ public class BobTransfer implements Transferable { protected Account account; protected Jid to; protected XmppConnectionService xmppConnectionService; + protected static Map attempts = new HashMap<>(); public static Cid cid(Uri uri) { if (uri == null || uri.getScheme() == null || !uri.getScheme().equals("cid")) return null; @@ -84,7 +87,8 @@ public class BobTransfer implements Transferable { return true; } - if (xmppConnectionService.hasInternetConnection()) { + if (xmppConnectionService.hasInternetConnection() && attempts.getOrDefault(uri, 0L) + 10000L < System.currentTimeMillis()) { + attempts.put(uri, System.currentTimeMillis()); changeStatus(Transferable.STATUS_DOWNLOADING); IqPacket request = new IqPacket(IqPacket.TYPE.GET); @@ -113,14 +117,19 @@ public class BobTransfer implements Transferable { } final OutputStream outputStream = AbstractConnectionManager.createOutputStream(new DownloadableFile(file.getAbsolutePath()), false, false); - outputStream.write(bytes); - outputStream.flush(); - outputStream.close(); - finish(file); + if (outputStream != null && bytes != null) { + outputStream.write(bytes); + outputStream.flush(); + outputStream.close(); + finish(file); + } else { + Log.w(Config.LOGTAG, "Could not write BobTransfer, null outputStream"); + finish(null); + } } catch (final IOException | XmppConnectionService.BlockedMediaException e) { + Log.w(Config.LOGTAG, "Could not write BobTransfer: " + e); finish(null); - xmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_write_file); } } });