mirror of
https://github.com/oxen-io/session-android.git
synced 2025-12-03 08:02:24 +00:00
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<Intent> 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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user