holding wake locks during onStartCommand

Daniel Gultsch created

Change summary

src/eu/siacs/conversations/crypto/OtrEngine.java               |  7 -
src/eu/siacs/conversations/services/XmppConnectionService.java | 12 +++
src/eu/siacs/conversations/utils/MessageParser.java            | 11 +--
src/eu/siacs/conversations/xml/TagWriter.java                  |  5 -
src/eu/siacs/conversations/xmpp/XmppConnection.java            |  6 +-
5 files changed, 20 insertions(+), 21 deletions(-)

Detailed changes

src/eu/siacs/conversations/crypto/OtrEngine.java 🔗

@@ -19,7 +19,6 @@ import android.util.Log;
 
 import eu.siacs.conversations.entities.Account;
 import eu.siacs.conversations.persistance.DatabaseBackend;
-import eu.siacs.conversations.xml.Element;
 import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
 
 import net.java.otr4j.OtrEngineHost;
@@ -199,15 +198,13 @@ public class OtrEngine implements OtrEngineHost {
 	@Override
 	public void smpError(SessionID arg0, int arg1, boolean arg2)
 			throws OtrException {
-		// TODO Auto-generated method stub
-
+		throw new OtrException(new Exception("smp error"));
 	}
 
 	@Override
 	public void unencryptedMessageReceived(SessionID arg0, String arg1)
 			throws OtrException {
-		// TODO Auto-generated method stub
-
+		throw new OtrException(new Exception("unencrypted message received"));
 	}
 
 	@Override

src/eu/siacs/conversations/services/XmppConnectionService.java 🔗

@@ -61,6 +61,7 @@ import android.os.Binder;
 import android.os.Bundle;
 import android.os.IBinder;
 import android.os.PowerManager;
+import android.os.PowerManager.WakeLock;
 import android.os.SystemClock;
 import android.preference.PreferenceManager;
 import android.provider.ContactsContract;
@@ -337,6 +338,7 @@ public class XmppConnectionService extends Service {
 	private PgpEngine mPgpEngine = null;
 	private Intent pingIntent;
 	private PendingIntent pendingPingIntent = null;
+	private WakeLock wakeLock;
 
 	public PgpEngine getPgpEngine() {
 		if (pgpServiceConnection.isBound()) {
@@ -415,6 +417,7 @@ public class XmppConnectionService extends Service {
 
 	@Override
 	public int onStartCommand(Intent intent, int flags, int startId) {
+		this.wakeLock.acquire();
 		// Log.d(LOGTAG,"calling start service. caller was:"+intent.getAction());
 		ConnectivityManager cm = (ConnectivityManager) getApplicationContext()
 				.getSystemService(Context.CONNECTIVITY_SERVICE);
@@ -483,6 +486,9 @@ public class XmppConnectionService extends Service {
 				}
 			}
 		}
+		if (wakeLock.isHeld()) {
+			wakeLock.release();
+		}
 		return START_STICKY;
 	}
 
@@ -498,6 +504,9 @@ public class XmppConnectionService extends Service {
 				getApplicationContext(), "org.sufficientlysecure.keychain");
 		this.pgpServiceConnection.bindToService();
 
+		PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
+		this.wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
+				"XmppConnection");
 	}
 
 	@Override
@@ -554,8 +563,7 @@ public class XmppConnectionService extends Service {
 		SharedPreferences sharedPref = PreferenceManager
 				.getDefaultSharedPreferences(getApplicationContext());
 		account.setResource(sharedPref.getString("resource", "mobile").toLowerCase(Locale.getDefault()));
-		PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
-		XmppConnection connection = new XmppConnection(account, pm);
+		XmppConnection connection = new XmppConnection(account, this.wakeLock);
 		connection.setOnMessagePacketReceivedListener(this.messageListener);
 		connection.setOnStatusChangedListener(this.statusListener);
 		connection.setOnPresencePacketReceivedListener(this.presenceListener);

src/eu/siacs/conversations/utils/MessageParser.java 🔗

@@ -84,16 +84,15 @@ public class MessageParser {
 				conversation.resetOtrSession();
 				Log.d(LOGTAG,"otr session stoped");
 			}
+			//isEmpty is a work around for some weird clients which send emtpty strings over otr
+			if ((body == null)||(body.isEmpty())) {
+				return null;
+			}
+			return new Message(conversation, packet.getFrom(), body, Message.ENCRYPTION_OTR,Message.STATUS_RECIEVED);
 		} catch (Exception e) {
 			conversation.resetOtrSession();
 			return null;
 		}
-		
-		//isEmpty is a work around for some weird clients which send emtpty strings over otr
-		if ((body == null)||(body.isEmpty())) {
-			return null;
-		}
-		return new Message(conversation, packet.getFrom(), body, Message.ENCRYPTION_OTR,Message.STATUS_RECIEVED);
 	}
 	
 	public static Message parseGroupchat(MessagePacket packet, Account account, XmppConnectionService service) {

src/eu/siacs/conversations/xml/TagWriter.java 🔗

@@ -33,14 +33,9 @@ public class TagWriter {
 		}
 	};
 	
-	
 	public TagWriter() {
 	}
 	
-	public TagWriter(OutputStream out) {
-		this.setOutputStream(out);
-	}
-	
 	public void setOutputStream(OutputStream out) {
 		this.outputStream = new OutputStreamWriter(out);
 	}

src/eu/siacs/conversations/xmpp/XmppConnection.java 🔗

@@ -32,6 +32,7 @@ import org.xmlpull.v1.XmlPullParserException;
 
 import android.os.Bundle;
 import android.os.PowerManager;
+import android.os.PowerManager.WakeLock;
 import android.os.SystemClock;
 import android.util.Log;
 import eu.siacs.conversations.entities.Account;
@@ -91,10 +92,9 @@ public class XmppConnection implements Runnable {
 	private OnTLSExceptionReceived tlsListener = null;
 	private OnBindListener bindListener = null;
 
-	public XmppConnection(Account account, PowerManager pm) {
+	public XmppConnection(Account account, WakeLock wakeLock) {
 		this.account = account;
-		wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
-				"XmppConnection");
+		this.wakeLock = wakeLock;
 		tagReader = new XmlReader(wakeLock);
 		tagWriter = new TagWriter();
 	}