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