From 7a023b9fdcce34f2d03bb3e1934f7416c2f5d6aa Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Wed, 18 Mar 2015 14:25:27 -0700 Subject: [PATCH] Acquire wakelocks for incoming messages. Closes #2717 // FREEBIE --- build.gradle | 4 +-- .../securesms/gcm/GcmBroadcastReceiver.java | 8 +++-- .../securesms/jobs/MmsDownloadJob.java | 2 ++ .../securesms/jobs/MmsReceiveJob.java | 1 + .../securesms/jobs/PushDecryptJob.java | 3 ++ .../securesms/jobs/PushReceiveJob.java | 1 + .../securesms/jobs/SmsReceiveJob.java | 1 + .../service/MessageRetrievalService.java | 30 +++++++++---------- 8 files changed, 30 insertions(+), 20 deletions(-) diff --git a/build.gradle b/build.gradle index ef58095cd9..d1b4dcd926 100644 --- a/build.gradle +++ b/build.gradle @@ -55,7 +55,7 @@ dependencies { compile 'com.madgag.spongycastle:prov:1.51.0.0' provided 'com.squareup.dagger:dagger-compiler:1.2.2' - compile 'org.whispersystems:jobmanager:0.10.0' + compile 'org.whispersystems:jobmanager:0.11.0' compile 'org.whispersystems:libpastelog:1.0.6' compile 'org.whispersystems:textsecure-android:1.2.2' @@ -94,7 +94,7 @@ dependencyVerification { 'com.squareup.dagger:dagger:789aca24537022e49f91fc6444078d9de8f1dd99e1bfb090f18491b186967883', 'com.doomonafireball.betterpickers:library:132ecd685c95a99e7377c4e27bfadbb2d7ed0bea995944060cd62d4369fdaf3d', 'com.madgag.spongycastle:prov:b8c3fec3a59aac1aa04ccf4dad7179351e54ef7672f53f508151b614c131398a', - 'org.whispersystems:jobmanager:01f35586c43aa3806f1c18d3d6a5a972def98103ba1a5a9ca3eec08d15f974b7', + 'org.whispersystems:jobmanager:ea9cb943c4892fb90c1eea1be30efeb85cefca213d52c788419553b58d0ed70d', 'org.whispersystems:libpastelog:550d33c565380d90f4c671e7b8ed5f3a6da55a9fda468373177106b2eb5220b2', 'org.whispersystems:textsecure-android:a5e81ff87a4f531ea4997b9a18437fa5d2679e846a4b9b0b1d85371d77189e44', 'com.android.support:support-annotations:fdee2354787ef66b268e75958de3f7f6c4f8f325510a6dac9f49c929f83a63de', diff --git a/src/org/thoughtcrime/securesms/gcm/GcmBroadcastReceiver.java b/src/org/thoughtcrime/securesms/gcm/GcmBroadcastReceiver.java index 843a2120ff..d15c786854 100644 --- a/src/org/thoughtcrime/securesms/gcm/GcmBroadcastReceiver.java +++ b/src/org/thoughtcrime/securesms/gcm/GcmBroadcastReceiver.java @@ -1,8 +1,8 @@ package org.thoughtcrime.securesms.gcm; -import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.support.v4.content.WakefulBroadcastReceiver; import android.text.TextUtils; import android.util.Log; @@ -13,7 +13,7 @@ import org.thoughtcrime.securesms.jobs.PushReceiveJob; import org.thoughtcrime.securesms.service.MessageRetrievalService; import org.thoughtcrime.securesms.util.TextSecurePreferences; -public class GcmBroadcastReceiver extends BroadcastReceiver { +public class GcmBroadcastReceiver extends WakefulBroadcastReceiver { private static final String TAG = GcmBroadcastReceiver.class.getSimpleName(); @@ -46,6 +46,8 @@ public class GcmBroadcastReceiver extends BroadcastReceiver { } private void handleReceivedNotification(Context context) { - MessageRetrievalService.registerPushReceived(context); + Intent intent = new Intent(context, MessageRetrievalService.class); + intent.setAction(MessageRetrievalService.ACTION_PUSH_RECEIVED); + startWakefulService(context, intent); } } \ No newline at end of file diff --git a/src/org/thoughtcrime/securesms/jobs/MmsDownloadJob.java b/src/org/thoughtcrime/securesms/jobs/MmsDownloadJob.java index 63607f0c14..5036552c9b 100644 --- a/src/org/thoughtcrime/securesms/jobs/MmsDownloadJob.java +++ b/src/org/thoughtcrime/securesms/jobs/MmsDownloadJob.java @@ -30,6 +30,7 @@ import org.whispersystems.libaxolotl.NoSessionException; import org.whispersystems.libaxolotl.util.guava.Optional; import java.io.IOException; +import java.util.concurrent.TimeUnit; import ws.com.google.android.mms.InvalidHeaderValueException; import ws.com.google.android.mms.MmsException; @@ -55,6 +56,7 @@ public class MmsDownloadJob extends MasterSecretJob { .withRequirement(new MasterSecretRequirement(context)) .withRequirement(new NetworkRequirement(context)) .withGroupId("mms-operation") + .withWakeLock(true, 30, TimeUnit.SECONDS) .create()); this.messageId = messageId; diff --git a/src/org/thoughtcrime/securesms/jobs/MmsReceiveJob.java b/src/org/thoughtcrime/securesms/jobs/MmsReceiveJob.java index 9107a51fb4..e6fe208492 100644 --- a/src/org/thoughtcrime/securesms/jobs/MmsReceiveJob.java +++ b/src/org/thoughtcrime/securesms/jobs/MmsReceiveJob.java @@ -22,6 +22,7 @@ public class MmsReceiveJob extends ContextJob { public MmsReceiveJob(Context context, byte[] data) { super(context, JobParameters.newBuilder() + .withWakeLock(true) .withPersistence().create()); this.data = data; diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index 3df29c1234..0d887e4d38 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -46,6 +46,8 @@ import org.whispersystems.textsecure.api.messages.TextSecureEnvelope; import org.whispersystems.textsecure.api.messages.TextSecureGroup; import org.whispersystems.textsecure.api.messages.TextSecureMessage; +import java.util.concurrent.TimeUnit; + import ws.com.google.android.mms.MmsException; public class PushDecryptJob extends MasterSecretJob { @@ -64,6 +66,7 @@ public class PushDecryptJob extends MasterSecretJob { .withPersistence() .withRequirement(new MasterSecretRequirement(context)) .withGroupId(sender) + .withWakeLock(true, 5, TimeUnit.SECONDS) .create()); this.messageId = pushMessageId; this.smsMessageId = smsMessageId; diff --git a/src/org/thoughtcrime/securesms/jobs/PushReceiveJob.java b/src/org/thoughtcrime/securesms/jobs/PushReceiveJob.java index 652860ff37..170bd2de1d 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushReceiveJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushReceiveJob.java @@ -30,6 +30,7 @@ public class PushReceiveJob extends ContextJob { public PushReceiveJob(Context context, String data) { super(context, JobParameters.newBuilder() .withPersistence() + .withWakeLock(true) .create()); this.data = data; diff --git a/src/org/thoughtcrime/securesms/jobs/SmsReceiveJob.java b/src/org/thoughtcrime/securesms/jobs/SmsReceiveJob.java index 818cb4d28f..24b984ae4a 100644 --- a/src/org/thoughtcrime/securesms/jobs/SmsReceiveJob.java +++ b/src/org/thoughtcrime/securesms/jobs/SmsReceiveJob.java @@ -29,6 +29,7 @@ public class SmsReceiveJob extends ContextJob { public SmsReceiveJob(Context context, Object[] pdus) { super(context, JobParameters.newBuilder() .withPersistence() + .withWakeLock(true) .create()); this.pdus = pdus; diff --git a/src/org/thoughtcrime/securesms/service/MessageRetrievalService.java b/src/org/thoughtcrime/securesms/service/MessageRetrievalService.java index 60c761c45c..aab26c5946 100644 --- a/src/org/thoughtcrime/securesms/service/MessageRetrievalService.java +++ b/src/org/thoughtcrime/securesms/service/MessageRetrievalService.java @@ -8,6 +8,7 @@ import android.util.Log; import org.thoughtcrime.securesms.ApplicationContext; import org.thoughtcrime.securesms.dependencies.InjectableType; +import org.thoughtcrime.securesms.gcm.GcmBroadcastReceiver; import org.thoughtcrime.securesms.jobs.PushReceiveJob; import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.whispersystems.jobqueue.requirements.NetworkRequirement; @@ -18,6 +19,8 @@ import org.whispersystems.textsecure.api.TextSecureMessagePipe; import org.whispersystems.textsecure.api.TextSecureMessageReceiver; import org.whispersystems.textsecure.api.messages.TextSecureEnvelope; +import java.util.LinkedList; +import java.util.List; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -38,8 +41,8 @@ public class MessageRetrievalService extends Service implements Runnable, Inject @Inject public TextSecureMessageReceiver receiver; - private int activeActivities = 0; - private boolean pushPending = false; + private int activeActivities = 0; + private List pushPending = new LinkedList<>(); @Override public void onCreate() { @@ -58,7 +61,7 @@ public class MessageRetrievalService extends Service implements Runnable, Inject if (ACTION_ACTIVITY_STARTED.equals(intent.getAction())) incrementActive(); else if (ACTION_ACTIVITY_FINISHED.equals(intent.getAction())) decrementActive(); - else if (ACTION_PUSH_RECEIVED.equals(intent.getAction())) incrementPushReceived(); + else if (ACTION_PUSH_RECEIVED.equals(intent.getAction())) incrementPushReceived(intent); return START_STICKY; } @@ -127,22 +130,25 @@ public class MessageRetrievalService extends Service implements Runnable, Inject notifyAll(); } - private synchronized void incrementPushReceived() { - pushPending = true; + private synchronized void incrementPushReceived(Intent intent) { + pushPending.add(intent); notifyAll(); } private synchronized void decrementPushReceived() { - pushPending = false; - notifyAll(); + if (!pushPending.isEmpty()) { + Intent intent = pushPending.remove(0); + GcmBroadcastReceiver.completeWakefulIntent(intent); + notifyAll(); + } } private synchronized boolean isConnectionNecessary() { Log.w(TAG, String.format("Network requirement: %s, active activities: %s, push pending: %s", - networkRequirement.isPresent(), activeActivities, pushPending)); + networkRequirement.isPresent(), activeActivities, pushPending.size())); return TextSecurePreferences.isWebsocketRegistered(this) && - (activeActivities > 0 || pushPending) && + (activeActivities > 0 || !pushPending.isEmpty()) && networkRequirement.isPresent(); } @@ -173,10 +179,4 @@ public class MessageRetrievalService extends Service implements Runnable, Inject intent.setAction(MessageRetrievalService.ACTION_ACTIVITY_FINISHED); activity.startService(intent); } - - public static void registerPushReceived(Context context) { - Intent intent = new Intent(context, MessageRetrievalService.class); - intent.setAction(MessageRetrievalService.ACTION_PUSH_RECEIVED); - context.startService(intent); - } }