From 88fe2739c1e93df44deef48da0287ccb842f865d Mon Sep 17 00:00:00 2001 From: Eric House Date: Sat, 24 Nov 2018 13:04:14 -0800 Subject: [PATCH] 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. --- .../org/eehouse/android/xw4/BTService.java | 21 +++++++++++++------ .../eehouse/android/xw4/OnBootReceiver.java | 4 +++- .../org/eehouse/android/xw4/RelayService.java | 13 +++++++++--- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BTService.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BTService.java index e969812f0..a58d03898 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BTService.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BTService.java @@ -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 ); } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/OnBootReceiver.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/OnBootReceiver.java index 3a1088efe..75525e924 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/OnBootReceiver.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/OnBootReceiver.java @@ -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 ); } } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RelayService.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RelayService.java index b89c623eb..501891edd 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RelayService.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RelayService.java @@ -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" ); } }