@ -19,10 +19,12 @@ import org.thoughtcrime.securesms.jobs.PushNotificationReceiveJob;
import org.thoughtcrime.securesms.service.GenericForegroundService ;
import org.thoughtcrime.securesms.util.PowerManagerCompat ;
import org.thoughtcrime.securesms.util.TextSecurePreferences ;
import org.thoughtcrime.securesms.util.concurrent.SignalExecutors ;
import org.whispersystems.signalservice.api.SignalServiceMessageReceiver ;
import org.whispersystems.signalservice.internal.util.Util ;
import java.io.IOException ;
import java.util.concurrent.Executor ;
import java.util.concurrent.atomic.AtomicBoolean ;
import javax.inject.Inject ;
@ -31,6 +33,10 @@ public class GcmBroadcastReceiver extends WakefulBroadcastReceiver implements In
private static final String TAG = GcmBroadcastReceiver . class . getSimpleName ( ) ;
private static final Executor MESSAGE_EXECUTOR = SignalExecutors . newCachedSingleThreadExecutor ( "GcmMessageProcessing" ) ;
private static int activeCount = 0 ;
@Inject SignalServiceMessageReceiver messageReceiver ;
@Override
@ -62,6 +68,11 @@ public class GcmBroadcastReceiver extends WakefulBroadcastReceiver implements In
}
private void handleReceivedNotification ( Context context ) {
if ( ! incrementActiveGcmCount ( ) ) {
Log . i ( TAG , "Skipping GCM processing -- there's already one enqueued." ) ;
return ;
}
TextSecurePreferences . setNeedsMessagePull ( context , true ) ;
long startTime = System . currentTimeMillis ( ) ;
@ -81,9 +92,7 @@ public class GcmBroadcastReceiver extends WakefulBroadcastReceiver implements In
callback . finish ( ) ;
}
new Thread ( "GcmMessageProcessing" ) {
@Override
public void run ( ) {
MESSAGE_EXECUTOR . execute ( ( ) - > {
try {
new PushNotificationReceiveJob ( context ) . pullAndProcessMessages ( messageReceiver , TAG , startTime ) ;
} catch ( IOException e ) {
@ -100,10 +109,11 @@ public class GcmBroadcastReceiver extends WakefulBroadcastReceiver implements In
}
taskCompleted . set ( true ) ;
}
decrementActiveGcmCount ( ) ;
Log . i ( TAG , "Processing complete." ) ;
}
}
} . start ( ) ;
} ) ;
if ( ! foregroundRunning . get ( ) ) {
new Thread ( "GcmForegroundServiceTimer" ) {
@ -121,4 +131,16 @@ public class GcmBroadcastReceiver extends WakefulBroadcastReceiver implements In
} . start ( ) ;
}
}
private static synchronized boolean incrementActiveGcmCount ( ) {
if ( activeCount < 2 ) {
activeCount + + ;
return true ;
}
return false ;
}
private static synchronized void decrementActiveGcmCount ( ) {
activeCount - - ;
}
}