put wake locks on out of band file transfers

This commit is contained in:
Daniel Gultsch 2015-08-10 19:48:36 +02:00
parent d30515a85a
commit fd81491b05
5 changed files with 32 additions and 25 deletions

View File

@ -2,6 +2,7 @@ package eu.siacs.conversations.http;
import android.content.Intent;
import android.net.Uri;
import android.os.PowerManager;
import android.util.Log;
import java.io.BufferedInputStream;
@ -228,7 +229,9 @@ public class HttpDownloadConnection implements Transferable {
private void download() throws IOException {
InputStream is = null;
PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock("http_download_"+message.getUuid());
try {
wakeLock.acquire();
HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection();
if (connection instanceof HttpsURLConnection) {
mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive);
@ -253,6 +256,7 @@ public class HttpDownloadConnection implements Transferable {
} finally {
FileBackend.close(os);
FileBackend.close(is);
wakeLock.release();
}
}

View File

@ -3,6 +3,7 @@ package eu.siacs.conversations.http;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.PowerManager;
import android.util.Log;
import android.util.Pair;
@ -143,7 +144,9 @@ public class HttpUploadConnection implements Transferable {
private void upload() {
OutputStream os = null;
HttpURLConnection connection = null;
PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock("http_upload_"+message.getUuid());
try {
wakeLock.acquire();
Log.d(Config.LOGTAG, "uploading to " + mPutUrl.toString());
connection = (HttpURLConnection) mPutUrl.openConnection();
if (connection instanceof HttpsURLConnection) {
@ -211,6 +214,7 @@ public class HttpUploadConnection implements Transferable {
if (connection != null) {
connection.disconnect();
}
wakeLock.release();
}
}
}

View File

@ -1,5 +1,7 @@
package eu.siacs.conversations.services;
import android.content.Context;
import android.os.PowerManager;
import android.util.Log;
import android.util.Pair;
@ -118,4 +120,9 @@ public class AbstractConnectionManager {
return null;
}
}
public PowerManager.WakeLock createWakeLock(String name) {
PowerManager powerManager = (PowerManager) mXmppConnectionService.getSystemService(Context.POWER_SERVICE);
return powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,name);
}
}

View File

@ -28,7 +28,6 @@ import eu.siacs.conversations.entities.TransferablePlaceholder;
import eu.siacs.conversations.persistance.FileBackend;
import eu.siacs.conversations.services.AbstractConnectionManager;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.Xmlns;
import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.OnIqPacketReceived;
import eu.siacs.conversations.xmpp.jid.Jid;
@ -1014,4 +1013,8 @@ public class JingleConnection implements Transferable {
public int getProgress() {
return this.mProgress;
}
public AbstractConnectionManager getConnectionManager() {
return this.mJingleConnectionManager;
}
}

View File

@ -1,5 +1,6 @@
package eu.siacs.conversations.xmpp.jingle;
import android.os.PowerManager;
import android.util.Log;
import java.io.FileNotFoundException;
@ -96,14 +97,15 @@ public class JingleSocks5Transport extends JingleTransport {
}
public void send(final DownloadableFile file,
final OnFileTransmissionStatusChanged callback) {
public void send(final DownloadableFile file, final OnFileTransmissionStatusChanged callback) {
new Thread(new Runnable() {
@Override
public void run() {
InputStream fileInputStream = null;
final PowerManager.WakeLock wakeLock = connection.getConnectionManager().createWakeLock("jingle_send_"+connection.getSessionId());
try {
wakeLock.acquire();
MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.reset();
fileInputStream = connection.getFileInputStream();
@ -138,6 +140,7 @@ public class JingleSocks5Transport extends JingleTransport {
callback.onFileTransferAborted();
} finally {
FileBackend.close(fileInputStream);
wakeLock.release();
}
}
}).start();
@ -150,7 +153,9 @@ public class JingleSocks5Transport extends JingleTransport {
@Override
public void run() {
OutputStream fileOutputStream = null;
final PowerManager.WakeLock wakeLock = connection.getConnectionManager().createWakeLock("jingle_receive_"+connection.getSessionId());
try {
wakeLock.acquire();
MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.reset();
inputStream.skip(45);
@ -166,7 +171,7 @@ public class JingleSocks5Transport extends JingleTransport {
double size = file.getExpectedSize();
long remainingSize = file.getExpectedSize();
byte[] buffer = new byte[8192];
int count = buffer.length;
int count;
while (remainingSize > 0) {
count = inputStream.read(buffer);
if (count == -1) {
@ -194,7 +199,9 @@ public class JingleSocks5Transport extends JingleTransport {
Log.d(Config.LOGTAG, connection.getAccount().getJid().toBareJid() + ": "+e.getMessage());
callback.onFileTransferAborted();
} finally {
wakeLock.release();
FileBackend.close(fileOutputStream);
FileBackend.close(inputStream);
}
}
}).start();
@ -209,27 +216,9 @@ public class JingleSocks5Transport extends JingleTransport {
}
public void disconnect() {
if (this.outputStream != null) {
try {
this.outputStream.close();
} catch (IOException e) {
}
}
if (this.inputStream != null) {
try {
this.inputStream.close();
} catch (IOException e) {
}
}
if (this.socket != null) {
try {
this.socket.close();
} catch (IOException e) {
}
}
FileBackend.close(inputStream);
FileBackend.close(outputStream);
FileBackend.close(socket);
}
public boolean isEstablished() {