From b050ff257643426232307ba2c5b0c252bd03520a Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 12 Jan 2017 16:02:09 +0100 Subject: [PATCH] only call UI thread from downloading thread every 250ms --- .../conversations/http/HttpDownloadConnection.java | 8 ++++---- .../conversations/http/HttpUploadConnection.java | 2 +- .../services/AbstractConnectionManager.java | 14 ++++++++++++++ .../xmpp/jingle/JingleConnection.java | 10 +++++----- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java b/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java index 356856dc..970ace6f 100644 --- a/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java +++ b/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java @@ -120,7 +120,7 @@ public class HttpDownloadConnection implements Transferable { } else { message.setTransferable(null); } - mXmppConnectionService.updateConversationUi(); + mHttpConnectionManager.updateConversationUi(true); } private void finish() { @@ -131,7 +131,7 @@ public class HttpDownloadConnection implements Transferable { if (message.getEncryption() == Message.ENCRYPTION_PGP) { notify = message.getConversation().getAccount().getPgpDecryptionService().decrypt(message, notify); } - mXmppConnectionService.updateConversationUi(); + mHttpConnectionManager.updateConversationUi(true); if (notify) { mXmppConnectionService.getNotificationService().push(message); } @@ -139,7 +139,7 @@ public class HttpDownloadConnection implements Transferable { private void changeStatus(int status) { this.mStatus = status; - mXmppConnectionService.updateConversationUi(); + mHttpConnectionManager.updateConversationUi(true); } private void showToastForException(Exception e) { @@ -340,7 +340,7 @@ public class HttpDownloadConnection implements Transferable { public void updateProgress(int i) { this.mProgress = i; - mXmppConnectionService.updateConversationUi(); + mHttpConnectionManager.updateConversationUi(false); } @Override diff --git a/src/main/java/eu/siacs/conversations/http/HttpUploadConnection.java b/src/main/java/eu/siacs/conversations/http/HttpUploadConnection.java index 63a3884b..06a325d0 100644 --- a/src/main/java/eu/siacs/conversations/http/HttpUploadConnection.java +++ b/src/main/java/eu/siacs/conversations/http/HttpUploadConnection.java @@ -182,7 +182,7 @@ public class HttpUploadConnection implements Transferable { while (((count = mFileInputStream.read(buffer)) != -1) && !canceled) { transmitted += count; os.write(buffer, 0, count); - mXmppConnectionService.updateConversationUi(); + mHttpConnectionManager.updateConversationUi(false); } os.flush(); os.close(); diff --git a/src/main/java/eu/siacs/conversations/services/AbstractConnectionManager.java b/src/main/java/eu/siacs/conversations/services/AbstractConnectionManager.java index dfe4cb28..18512997 100644 --- a/src/main/java/eu/siacs/conversations/services/AbstractConnectionManager.java +++ b/src/main/java/eu/siacs/conversations/services/AbstractConnectionManager.java @@ -5,6 +5,7 @@ import android.content.Context; import android.content.pm.PackageManager; import android.os.Build; import android.os.PowerManager; +import android.os.SystemClock; import android.util.Log; import android.util.Pair; @@ -22,6 +23,7 @@ import java.io.OutputStream; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; +import java.util.concurrent.atomic.AtomicLong; import javax.crypto.Cipher; import javax.crypto.CipherInputStream; @@ -36,6 +38,9 @@ import eu.siacs.conversations.entities.DownloadableFile; public class AbstractConnectionManager { protected XmppConnectionService mXmppConnectionService; + private static final int UI_REFRESH_THRESHOLD = 250; + private static final AtomicLong LAST_UI_UPDATE_CALL = new AtomicLong(0); + public AbstractConnectionManager(XmppConnectionService service) { this.mXmppConnectionService = service; } @@ -136,6 +141,15 @@ public class AbstractConnectionManager { } } + public void updateConversationUi(boolean force) { + synchronized (LAST_UI_UPDATE_CALL) { + if (force || SystemClock.elapsedRealtime() - LAST_UI_UPDATE_CALL.get() >= UI_REFRESH_THRESHOLD) { + LAST_UI_UPDATE_CALL.set(SystemClock.elapsedRealtime()); + mXmppConnectionService.updateConversationUi(); + } + } + } + public PowerManager.WakeLock createWakeLock(String name) { PowerManager powerManager = (PowerManager) mXmppConnectionService.getSystemService(Context.POWER_SERVICE); return powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,name); diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java index 5461b9c6..0c0c054d 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java @@ -388,7 +388,7 @@ public class JingleConnection implements Transferable { long size = Long.parseLong(fileSize.getContent()); message.setBody(Long.toString(size)); conversation.add(message); - mXmppConnectionService.updateConversationUi(); + mJingleConnectionManager.updateConversationUi(true); if (mJingleConnectionManager.hasStoragePermission() && size < this.mJingleConnectionManager.getAutoAcceptFileSize() && mXmppConnectionService.isDataSaverDisabled()) { @@ -510,7 +510,7 @@ public class JingleConnection implements Transferable { private void sendAccept() { mJingleStatus = JINGLE_STATUS_ACCEPTED; this.mStatus = Transferable.STATUS_DOWNLOADING; - mXmppConnectionService.updateConversationUi(); + this.mJingleConnectionManager.updateConversationUi(true); this.mJingleConnectionManager.getPrimaryCandidate(this.account, new OnPrimaryCandidateFound() { @Override public void onPrimaryCandidateFound(boolean success, final JingleCandidate candidate) { @@ -842,7 +842,7 @@ public class JingleConnection implements Transferable { if (this.file!=null) { file.delete(); } - this.mXmppConnectionService.updateConversationUi(); + this.mJingleConnectionManager.updateConversationUi(true); } else { this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND_FAILED); @@ -868,7 +868,7 @@ public class JingleConnection implements Transferable { if (this.file!=null) { file.delete(); } - this.mXmppConnectionService.updateConversationUi(); + this.mJingleConnectionManager.updateConversationUi(true); } else { this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND_FAILED, @@ -1016,7 +1016,7 @@ public class JingleConnection implements Transferable { public void updateProgress(int i) { this.mProgress = i; - mXmppConnectionService.updateConversationUi(); + mJingleConnectionManager.updateConversationUi(false); } public String getTransportId() {