only call UI thread from downloading thread every 250ms

This commit is contained in:
Daniel Gultsch 2017-01-12 16:02:09 +01:00
parent bfacc180c5
commit b050ff2576
4 changed files with 24 additions and 10 deletions

View File

@ -120,7 +120,7 @@ public class HttpDownloadConnection implements Transferable {
} else { } else {
message.setTransferable(null); message.setTransferable(null);
} }
mXmppConnectionService.updateConversationUi(); mHttpConnectionManager.updateConversationUi(true);
} }
private void finish() { private void finish() {
@ -131,7 +131,7 @@ public class HttpDownloadConnection implements Transferable {
if (message.getEncryption() == Message.ENCRYPTION_PGP) { if (message.getEncryption() == Message.ENCRYPTION_PGP) {
notify = message.getConversation().getAccount().getPgpDecryptionService().decrypt(message, notify); notify = message.getConversation().getAccount().getPgpDecryptionService().decrypt(message, notify);
} }
mXmppConnectionService.updateConversationUi(); mHttpConnectionManager.updateConversationUi(true);
if (notify) { if (notify) {
mXmppConnectionService.getNotificationService().push(message); mXmppConnectionService.getNotificationService().push(message);
} }
@ -139,7 +139,7 @@ public class HttpDownloadConnection implements Transferable {
private void changeStatus(int status) { private void changeStatus(int status) {
this.mStatus = status; this.mStatus = status;
mXmppConnectionService.updateConversationUi(); mHttpConnectionManager.updateConversationUi(true);
} }
private void showToastForException(Exception e) { private void showToastForException(Exception e) {
@ -340,7 +340,7 @@ public class HttpDownloadConnection implements Transferable {
public void updateProgress(int i) { public void updateProgress(int i) {
this.mProgress = i; this.mProgress = i;
mXmppConnectionService.updateConversationUi(); mHttpConnectionManager.updateConversationUi(false);
} }
@Override @Override

View File

@ -182,7 +182,7 @@ public class HttpUploadConnection implements Transferable {
while (((count = mFileInputStream.read(buffer)) != -1) && !canceled) { while (((count = mFileInputStream.read(buffer)) != -1) && !canceled) {
transmitted += count; transmitted += count;
os.write(buffer, 0, count); os.write(buffer, 0, count);
mXmppConnectionService.updateConversationUi(); mHttpConnectionManager.updateConversationUi(false);
} }
os.flush(); os.flush();
os.close(); os.close();

View File

@ -5,6 +5,7 @@ import android.content.Context;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Build; import android.os.Build;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.SystemClock;
import android.util.Log; import android.util.Log;
import android.util.Pair; import android.util.Pair;
@ -22,6 +23,7 @@ import java.io.OutputStream;
import java.security.InvalidAlgorithmParameterException; import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.concurrent.atomic.AtomicLong;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.CipherInputStream; import javax.crypto.CipherInputStream;
@ -36,6 +38,9 @@ import eu.siacs.conversations.entities.DownloadableFile;
public class AbstractConnectionManager { public class AbstractConnectionManager {
protected XmppConnectionService mXmppConnectionService; 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) { public AbstractConnectionManager(XmppConnectionService service) {
this.mXmppConnectionService = 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) { public PowerManager.WakeLock createWakeLock(String name) {
PowerManager powerManager = (PowerManager) mXmppConnectionService.getSystemService(Context.POWER_SERVICE); PowerManager powerManager = (PowerManager) mXmppConnectionService.getSystemService(Context.POWER_SERVICE);
return powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,name); return powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,name);

View File

@ -388,7 +388,7 @@ public class JingleConnection implements Transferable {
long size = Long.parseLong(fileSize.getContent()); long size = Long.parseLong(fileSize.getContent());
message.setBody(Long.toString(size)); message.setBody(Long.toString(size));
conversation.add(message); conversation.add(message);
mXmppConnectionService.updateConversationUi(); mJingleConnectionManager.updateConversationUi(true);
if (mJingleConnectionManager.hasStoragePermission() if (mJingleConnectionManager.hasStoragePermission()
&& size < this.mJingleConnectionManager.getAutoAcceptFileSize() && size < this.mJingleConnectionManager.getAutoAcceptFileSize()
&& mXmppConnectionService.isDataSaverDisabled()) { && mXmppConnectionService.isDataSaverDisabled()) {
@ -510,7 +510,7 @@ public class JingleConnection implements Transferable {
private void sendAccept() { private void sendAccept() {
mJingleStatus = JINGLE_STATUS_ACCEPTED; mJingleStatus = JINGLE_STATUS_ACCEPTED;
this.mStatus = Transferable.STATUS_DOWNLOADING; this.mStatus = Transferable.STATUS_DOWNLOADING;
mXmppConnectionService.updateConversationUi(); this.mJingleConnectionManager.updateConversationUi(true);
this.mJingleConnectionManager.getPrimaryCandidate(this.account, new OnPrimaryCandidateFound() { this.mJingleConnectionManager.getPrimaryCandidate(this.account, new OnPrimaryCandidateFound() {
@Override @Override
public void onPrimaryCandidateFound(boolean success, final JingleCandidate candidate) { public void onPrimaryCandidateFound(boolean success, final JingleCandidate candidate) {
@ -842,7 +842,7 @@ public class JingleConnection implements Transferable {
if (this.file!=null) { if (this.file!=null) {
file.delete(); file.delete();
} }
this.mXmppConnectionService.updateConversationUi(); this.mJingleConnectionManager.updateConversationUi(true);
} else { } else {
this.mXmppConnectionService.markMessage(this.message, this.mXmppConnectionService.markMessage(this.message,
Message.STATUS_SEND_FAILED); Message.STATUS_SEND_FAILED);
@ -868,7 +868,7 @@ public class JingleConnection implements Transferable {
if (this.file!=null) { if (this.file!=null) {
file.delete(); file.delete();
} }
this.mXmppConnectionService.updateConversationUi(); this.mJingleConnectionManager.updateConversationUi(true);
} else { } else {
this.mXmppConnectionService.markMessage(this.message, this.mXmppConnectionService.markMessage(this.message,
Message.STATUS_SEND_FAILED, Message.STATUS_SEND_FAILED,
@ -1016,7 +1016,7 @@ public class JingleConnection implements Transferable {
public void updateProgress(int i) { public void updateProgress(int i) {
this.mProgress = i; this.mProgress = i;
mXmppConnectionService.updateConversationUi(); mJingleConnectionManager.updateConversationUi(false);
} }
public String getTransportId() { public String getTransportId() {