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 {
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

View file

@ -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();

View file

@ -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);

View file

@ -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() {