From 5c20a67e6687cd700e2c19291d1829254c043e07 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Sun, 15 Jan 2017 11:46:20 +0100 Subject: only call UI thread from downloading thread every 250ms --- .../de/pixart/messenger/http/HttpDownloadConnection.java | 8 ++++---- .../java/de/pixart/messenger/http/HttpUploadConnection.java | 2 +- .../messenger/services/AbstractConnectionManager.java | 13 +++++++++++++ .../de/pixart/messenger/xmpp/jingle/JingleConnection.java | 10 +++++----- 4 files changed, 23 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/main/java/de/pixart/messenger/http/HttpDownloadConnection.java b/src/main/java/de/pixart/messenger/http/HttpDownloadConnection.java index ea0b5c1e8..d49111dbf 100644 --- a/src/main/java/de/pixart/messenger/http/HttpDownloadConnection.java +++ b/src/main/java/de/pixart/messenger/http/HttpDownloadConnection.java @@ -126,7 +126,7 @@ public class HttpDownloadConnection implements Transferable { } else { message.setTransferable(null); } - mXmppConnectionService.updateConversationUi(); + mHttpConnectionManager.updateConversationUi(true); } private void finish() { @@ -137,7 +137,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); } @@ -145,7 +145,7 @@ public class HttpDownloadConnection implements Transferable { private void changeStatus(int status) { this.mStatus = status; - mXmppConnectionService.updateConversationUi(); + mHttpConnectionManager.updateConversationUi(true); } private void showToastForException(Exception e) { @@ -350,7 +350,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/de/pixart/messenger/http/HttpUploadConnection.java b/src/main/java/de/pixart/messenger/http/HttpUploadConnection.java index afb0b04df..19d92b353 100644 --- a/src/main/java/de/pixart/messenger/http/HttpUploadConnection.java +++ b/src/main/java/de/pixart/messenger/http/HttpUploadConnection.java @@ -184,7 +184,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/de/pixart/messenger/services/AbstractConnectionManager.java b/src/main/java/de/pixart/messenger/services/AbstractConnectionManager.java index fe101edc6..5a65d23c3 100644 --- a/src/main/java/de/pixart/messenger/services/AbstractConnectionManager.java +++ b/src/main/java/de/pixart/messenger/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; @@ -35,6 +37,8 @@ import de.pixart.messenger.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; @@ -145,6 +149,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/de/pixart/messenger/xmpp/jingle/JingleConnection.java b/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnection.java index 29b9db382..c95359663 100644 --- a/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnection.java +++ b/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnection.java @@ -392,7 +392,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()) { @@ -514,7 +514,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) { @@ -846,7 +846,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); @@ -872,7 +872,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, @@ -1020,7 +1020,7 @@ public class JingleConnection implements Transferable { public void updateProgress(int i) { this.mProgress = i; - mXmppConnectionService.updateConversationUi(); + mJingleConnectionManager.updateConversationUi(false); } public String getTransportId() { -- cgit v1.2.3