fix crashes (NPE and RelayService trying to run in background)

Still need to figure out what RelayService does. It needn't run for long
except where GCM isn't present.
This commit is contained in:
Eric House 2018-11-24 13:04:14 -08:00
parent eb6831ea12
commit 88fe2739c1
3 changed files with 28 additions and 10 deletions

View file

@ -234,7 +234,8 @@ public class BTService extends XWService {
private static void onAppStateChange( Context context, boolean inForeground )
{
if ( sInForeground == null || sInForeground != inForeground ) {
Log.d( TAG, "onAppStateChange(inForeground=%b)", inForeground );
if ( null == sInForeground || inForeground() != inForeground ) {
sInForeground = inForeground;
Intent intent =
@ -245,6 +246,11 @@ public class BTService extends XWService {
}
}
private static boolean inForeground()
{
return sInForeground != null && sInForeground;
}
static void onAppToForeground( Context context )
{
onAppStateChange( context, true );
@ -348,14 +354,17 @@ public class BTService extends XWService {
private static void startService( Context context, Intent intent )
{
Log.d( TAG, "startService(%s)", intent );
boolean inForeground = inForeground();
Log.d( TAG, "startService(%s); inForeground = %b", intent, inForeground );
if ( ! sInForeground && canRunForegroundService() ) {
if ( ! inForeground && canRunForegroundService() ) {
if ( XWPrefs.getBTBackgroundEnabled( context ) ) {
context.startForegroundService( intent );
}
} else if ( sInForeground || Build.VERSION.SDK_INT < Build.VERSION_CODES.O ) {
} else if ( inForeground || Build.VERSION.SDK_INT < Build.VERSION_CODES.O ) {
context.startService( intent );
} else {
Log.d( TAG, "startService(); not starting" );
}
}
@ -399,7 +408,7 @@ public class BTService extends XWService {
{
int result = handleCommand( intent );
if ( Service.START_STICKY == result && !sInForeground ) {
if ( Service.START_STICKY == result && ! inForeground() ) {
startForeground();
}
@ -514,7 +523,7 @@ public class BTService extends XWService {
.setContentIntent(pendIntent)
.build();
Log.d( TAG, "calling startForeground()" );
Log.d( TAG, "calling service.startForeground()" );
startForeground( 1337, notification );
}

View file

@ -30,10 +30,12 @@ public class OnBootReceiver extends BroadcastReceiver {
@Override
public void onReceive( Context context, Intent intent )
{
if ( null != intent && null != intent.getAction()
if ( null != intent
&& null != intent.getAction()
&& intent.getAction().equals( Intent.ACTION_BOOT_COMPLETED ) ) {
Log.d( TAG, "got ACTION_BOOT_COMPLETED" );
startTimers( context );
BTService.onAppToBackground( context );
}
}

View file

@ -77,6 +77,8 @@ public class RelayService extends XWService
private static final String CMD_STR = "CMD";
private static Boolean sInForeground;
private static enum MsgCmds { INVALID,
PROCESS_GAME_MSGS,
PROCESS_DEV_MSGS,
@ -188,6 +190,11 @@ public class RelayService extends XWService
enabledChanged( context );
}
private static boolean inForeground()
{
return sInForeground != null && sInForeground;
}
public static void startService( Context context )
{
Log.i( TAG, "startService()" );
@ -199,10 +206,10 @@ public class RelayService extends XWService
{
Log.d( TAG, "startService(%s)", intent );
if ( false ) {
context.startForegroundService( intent );
} else {
if ( inForeground() || Build.VERSION.SDK_INT < Build.VERSION_CODES.O ) {
context.startService( intent );
} else {
Log.d( TAG, "startService(); not starting" );
}
}