Acquire wakelocks for incoming messages.

Closes #2717

// FREEBIE
This commit is contained in:
Moxie Marlinspike 2015-03-18 14:25:27 -07:00
parent 9f804047f5
commit 7a023b9fdc
8 changed files with 30 additions and 20 deletions

View File

@ -55,7 +55,7 @@ dependencies {
compile 'com.madgag.spongycastle:prov:1.51.0.0' compile 'com.madgag.spongycastle:prov:1.51.0.0'
provided 'com.squareup.dagger:dagger-compiler:1.2.2' 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:libpastelog:1.0.6'
compile 'org.whispersystems:textsecure-android:1.2.2' compile 'org.whispersystems:textsecure-android:1.2.2'
@ -94,7 +94,7 @@ dependencyVerification {
'com.squareup.dagger:dagger:789aca24537022e49f91fc6444078d9de8f1dd99e1bfb090f18491b186967883', 'com.squareup.dagger:dagger:789aca24537022e49f91fc6444078d9de8f1dd99e1bfb090f18491b186967883',
'com.doomonafireball.betterpickers:library:132ecd685c95a99e7377c4e27bfadbb2d7ed0bea995944060cd62d4369fdaf3d', 'com.doomonafireball.betterpickers:library:132ecd685c95a99e7377c4e27bfadbb2d7ed0bea995944060cd62d4369fdaf3d',
'com.madgag.spongycastle:prov:b8c3fec3a59aac1aa04ccf4dad7179351e54ef7672f53f508151b614c131398a', 'com.madgag.spongycastle:prov:b8c3fec3a59aac1aa04ccf4dad7179351e54ef7672f53f508151b614c131398a',
'org.whispersystems:jobmanager:01f35586c43aa3806f1c18d3d6a5a972def98103ba1a5a9ca3eec08d15f974b7', 'org.whispersystems:jobmanager:ea9cb943c4892fb90c1eea1be30efeb85cefca213d52c788419553b58d0ed70d',
'org.whispersystems:libpastelog:550d33c565380d90f4c671e7b8ed5f3a6da55a9fda468373177106b2eb5220b2', 'org.whispersystems:libpastelog:550d33c565380d90f4c671e7b8ed5f3a6da55a9fda468373177106b2eb5220b2',
'org.whispersystems:textsecure-android:a5e81ff87a4f531ea4997b9a18437fa5d2679e846a4b9b0b1d85371d77189e44', 'org.whispersystems:textsecure-android:a5e81ff87a4f531ea4997b9a18437fa5d2679e846a4b9b0b1d85371d77189e44',
'com.android.support:support-annotations:fdee2354787ef66b268e75958de3f7f6c4f8f325510a6dac9f49c929f83a63de', 'com.android.support:support-annotations:fdee2354787ef66b268e75958de3f7f6c4f8f325510a6dac9f49c929f83a63de',

View File

@ -1,8 +1,8 @@
package org.thoughtcrime.securesms.gcm; package org.thoughtcrime.securesms.gcm;
import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
@ -13,7 +13,7 @@ import org.thoughtcrime.securesms.jobs.PushReceiveJob;
import org.thoughtcrime.securesms.service.MessageRetrievalService; import org.thoughtcrime.securesms.service.MessageRetrievalService;
import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.TextSecurePreferences;
public class GcmBroadcastReceiver extends BroadcastReceiver { public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
private static final String TAG = GcmBroadcastReceiver.class.getSimpleName(); private static final String TAG = GcmBroadcastReceiver.class.getSimpleName();
@ -46,6 +46,8 @@ public class GcmBroadcastReceiver extends BroadcastReceiver {
} }
private void handleReceivedNotification(Context context) { private void handleReceivedNotification(Context context) {
MessageRetrievalService.registerPushReceived(context); Intent intent = new Intent(context, MessageRetrievalService.class);
intent.setAction(MessageRetrievalService.ACTION_PUSH_RECEIVED);
startWakefulService(context, intent);
} }
} }

View File

@ -30,6 +30,7 @@ import org.whispersystems.libaxolotl.NoSessionException;
import org.whispersystems.libaxolotl.util.guava.Optional; import org.whispersystems.libaxolotl.util.guava.Optional;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.TimeUnit;
import ws.com.google.android.mms.InvalidHeaderValueException; import ws.com.google.android.mms.InvalidHeaderValueException;
import ws.com.google.android.mms.MmsException; import ws.com.google.android.mms.MmsException;
@ -55,6 +56,7 @@ public class MmsDownloadJob extends MasterSecretJob {
.withRequirement(new MasterSecretRequirement(context)) .withRequirement(new MasterSecretRequirement(context))
.withRequirement(new NetworkRequirement(context)) .withRequirement(new NetworkRequirement(context))
.withGroupId("mms-operation") .withGroupId("mms-operation")
.withWakeLock(true, 30, TimeUnit.SECONDS)
.create()); .create());
this.messageId = messageId; this.messageId = messageId;

View File

@ -22,6 +22,7 @@ public class MmsReceiveJob extends ContextJob {
public MmsReceiveJob(Context context, byte[] data) { public MmsReceiveJob(Context context, byte[] data) {
super(context, JobParameters.newBuilder() super(context, JobParameters.newBuilder()
.withWakeLock(true)
.withPersistence().create()); .withPersistence().create());
this.data = data; this.data = data;

View File

@ -46,6 +46,8 @@ import org.whispersystems.textsecure.api.messages.TextSecureEnvelope;
import org.whispersystems.textsecure.api.messages.TextSecureGroup; import org.whispersystems.textsecure.api.messages.TextSecureGroup;
import org.whispersystems.textsecure.api.messages.TextSecureMessage; import org.whispersystems.textsecure.api.messages.TextSecureMessage;
import java.util.concurrent.TimeUnit;
import ws.com.google.android.mms.MmsException; import ws.com.google.android.mms.MmsException;
public class PushDecryptJob extends MasterSecretJob { public class PushDecryptJob extends MasterSecretJob {
@ -64,6 +66,7 @@ public class PushDecryptJob extends MasterSecretJob {
.withPersistence() .withPersistence()
.withRequirement(new MasterSecretRequirement(context)) .withRequirement(new MasterSecretRequirement(context))
.withGroupId(sender) .withGroupId(sender)
.withWakeLock(true, 5, TimeUnit.SECONDS)
.create()); .create());
this.messageId = pushMessageId; this.messageId = pushMessageId;
this.smsMessageId = smsMessageId; this.smsMessageId = smsMessageId;

View File

@ -30,6 +30,7 @@ public class PushReceiveJob extends ContextJob {
public PushReceiveJob(Context context, String data) { public PushReceiveJob(Context context, String data) {
super(context, JobParameters.newBuilder() super(context, JobParameters.newBuilder()
.withPersistence() .withPersistence()
.withWakeLock(true)
.create()); .create());
this.data = data; this.data = data;

View File

@ -29,6 +29,7 @@ public class SmsReceiveJob extends ContextJob {
public SmsReceiveJob(Context context, Object[] pdus) { public SmsReceiveJob(Context context, Object[] pdus) {
super(context, JobParameters.newBuilder() super(context, JobParameters.newBuilder()
.withPersistence() .withPersistence()
.withWakeLock(true)
.create()); .create());
this.pdus = pdus; this.pdus = pdus;

View File

@ -8,6 +8,7 @@ import android.util.Log;
import org.thoughtcrime.securesms.ApplicationContext; import org.thoughtcrime.securesms.ApplicationContext;
import org.thoughtcrime.securesms.dependencies.InjectableType; import org.thoughtcrime.securesms.dependencies.InjectableType;
import org.thoughtcrime.securesms.gcm.GcmBroadcastReceiver;
import org.thoughtcrime.securesms.jobs.PushReceiveJob; import org.thoughtcrime.securesms.jobs.PushReceiveJob;
import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.jobqueue.requirements.NetworkRequirement; 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.TextSecureMessageReceiver;
import org.whispersystems.textsecure.api.messages.TextSecureEnvelope; import org.whispersystems.textsecure.api.messages.TextSecureEnvelope;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
@ -39,7 +42,7 @@ public class MessageRetrievalService extends Service implements Runnable, Inject
public TextSecureMessageReceiver receiver; public TextSecureMessageReceiver receiver;
private int activeActivities = 0; private int activeActivities = 0;
private boolean pushPending = false; private List<Intent> pushPending = new LinkedList<>();
@Override @Override
public void onCreate() { public void onCreate() {
@ -58,7 +61,7 @@ public class MessageRetrievalService extends Service implements Runnable, Inject
if (ACTION_ACTIVITY_STARTED.equals(intent.getAction())) incrementActive(); if (ACTION_ACTIVITY_STARTED.equals(intent.getAction())) incrementActive();
else if (ACTION_ACTIVITY_FINISHED.equals(intent.getAction())) decrementActive(); 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; return START_STICKY;
} }
@ -127,22 +130,25 @@ public class MessageRetrievalService extends Service implements Runnable, Inject
notifyAll(); notifyAll();
} }
private synchronized void incrementPushReceived() { private synchronized void incrementPushReceived(Intent intent) {
pushPending = true; pushPending.add(intent);
notifyAll(); notifyAll();
} }
private synchronized void decrementPushReceived() { private synchronized void decrementPushReceived() {
pushPending = false; if (!pushPending.isEmpty()) {
Intent intent = pushPending.remove(0);
GcmBroadcastReceiver.completeWakefulIntent(intent);
notifyAll(); notifyAll();
} }
}
private synchronized boolean isConnectionNecessary() { private synchronized boolean isConnectionNecessary() {
Log.w(TAG, String.format("Network requirement: %s, active activities: %s, push pending: %s", 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) && return TextSecurePreferences.isWebsocketRegistered(this) &&
(activeActivities > 0 || pushPending) && (activeActivities > 0 || !pushPending.isEmpty()) &&
networkRequirement.isPresent(); networkRequirement.isPresent();
} }
@ -173,10 +179,4 @@ public class MessageRetrievalService extends Service implements Runnable, Inject
intent.setAction(MessageRetrievalService.ACTION_ACTIVITY_FINISHED); intent.setAction(MessageRetrievalService.ACTION_ACTIVITY_FINISHED);
activity.startService(intent); 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);
}
} }