From 7b04f8d4eda3052a43d152793818eb3f00334cb1 Mon Sep 17 00:00:00 2001 From: Eric House Date: Tue, 19 Mar 2019 08:16:30 -0700 Subject: [PATCH] cleanup and refactor Move some work into superclass where instrumentation can be added to greater effect. And remove from BTService an earlier attempt (at stall detection.) --- .../org/eehouse/android/xw4/BTService.java | 52 +++---------------- .../org/eehouse/android/xw4/RelayService.java | 5 +- .../org/eehouse/android/xw4/SMSService.java | 5 +- .../org/eehouse/android/xw4/XWJIService.java | 13 +++++ 4 files changed, 27 insertions(+), 48 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 c5dc1e321..4b5cfb603 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 @@ -85,7 +85,12 @@ public class BTService extends XWJIService { private static final String TAG = BTService.class.getSimpleName(); private static final String BOGUS_MARSHMALLOW_ADDR = "02:00:00:00:00:00"; private static final String KEY_KEEPALIVE_UNTIL_SECS = "keep_secs"; + private final static int sJobID = 218719979; + static { + XWJIService.register( BTService.class, sJobID ); + } + // half minute for testing; maybe 15 on ship? Or make it a debug config. private static int DEFAULT_KEEPALIVE_SECONDS = 15 * 60; private static int CONNECT_SLEEP_MS = 2500; @@ -246,56 +251,13 @@ public class BTService extends XWJIService { private static void enqueueWork( Context context, Intent intent ) { if ( BTEnabled() ) { - setCheckTimerOnce( context ); - enqueueWork( context, BTService.class, sJobID, intent ); + enqueueWork( context, BTService.class, intent ); // Log.d( TAG, "enqueueWork(%s)", cmdFrom( intent, BTAction.values() ) ); } else { Log.d( TAG, "enqueueWork(): BT disabled so doing nothing" ); } } - // It appears that the OS won't launch my service (calling onCreate() and - // onHandleWork()) after an install, including via adb, until the app's - // been launched manually. So we'll check for that case and post a - // notification if it appears to be a problem. - private static Thread[] sLaunchChecker = {null}; - - private static void clearCheckTimer() - { - synchronized ( sLaunchChecker ) { - if ( null != sLaunchChecker[0] ) { - sLaunchChecker[0].interrupt(); - } - } - } - - private static void setCheckTimerOnce( final Context context ) - { - synchronized ( sLaunchChecker ) { - if ( null == sLaunchChecker[0] ) { - sLaunchChecker[0] = new Thread( new Runnable() { - @Override - public void run() { - try { - synchronized ( sLaunchChecker ) { - sLaunchChecker.notify(); - } - Thread.sleep( 10 * 1000 ); - - Utils.showLaunchSinceInstall( context ); - } catch ( InterruptedException ie ) { - } - } - } ); - - sLaunchChecker[0].start(); - // Don't return until the thread's interruptable - try { sLaunchChecker.wait(); } - catch ( InterruptedException ex ) {} - } - } - } - public static void onACLConnected( Context context ) { Log.d( TAG, "onACLConnected()" ); @@ -385,8 +347,6 @@ public class BTService extends XWJIService { Log.d( TAG, "%s.onCreate()", this ); super.onCreate(); - clearCheckTimer(); - mHelper = new BTServiceHelper( this ); m_btMsgSink = new BTMsgSink(); 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 696f21636..31f38c272 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,9 @@ public class RelayService extends XWJIService // app runs. I think I was getting failures when a new instance launched // and found older jobs in the JobIntentService's work queue. private final static int sJobID = 218719978; + static { + XWJIService.register( RelayService.class, sJobID ); + } // One day, in seconds. Probably should be configurable. private static final long MAX_KEEPALIVE_SECS = 24 * 60 * 60; @@ -217,7 +220,7 @@ public class RelayService extends XWJIService private static void enqueueWork( Context context, Intent intent ) { - enqueueWork( context, RelayService.class, sJobID, intent ); + enqueueWork( context, RelayService.class, intent ); // Log.d( TAG, "called enqueueWork(cmd=%s)", cmdFrom( intent, MsgCmds.values() ) ); } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/SMSService.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/SMSService.java index 52b2d9e1f..5ba5be3a5 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/SMSService.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/SMSService.java @@ -59,6 +59,9 @@ public class SMSService extends XWJIService { private static final String TAG = SMSService.class.getSimpleName(); private final static int sJobID = 218719980; + static { + XWJIService.register( SMSService.class, sJobID ); + } private static final String MSG_SENT = "MSG_SENT"; private static final String MSG_DELIVERED = "MSG_DELIVERED"; @@ -262,7 +265,7 @@ public class SMSService extends XWJIService { private static void enqueueWork( Context context, Intent intent ) { - enqueueWork( context, SMSService.class, sJobID, intent ); + enqueueWork( context, SMSService.class, intent ); Log.d( TAG, "called enqueueWork(%s)", intent ); } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/XWJIService.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/XWJIService.java index 49565c119..579dc73bb 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/XWJIService.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/XWJIService.java @@ -25,6 +25,9 @@ import android.support.v4.app.JobIntentService; import android.content.Context; import android.content.Intent; +import java.util.HashMap; +import java.util.Map; + abstract class XWJIService extends JobIntentService { static final String CMD_KEY = "CMD"; private static final String TIMESTAMP = "TIMESTAMP"; @@ -35,6 +38,11 @@ abstract class XWJIService extends JobIntentService { abstract void onHandleWorkImpl( Intent intent, XWJICmds cmd, long timestamp ); abstract XWJICmds[] getCmds(); + + private static Map sJobIDs = new HashMap<>(); + static void register( Class clazz, int jobID ) { + sJobIDs.put( clazz, jobID ); + } @Override public final void onHandleWork( Intent intent ) @@ -49,6 +57,11 @@ abstract class XWJIService extends JobIntentService { onHandleWorkImpl( intent, cmd, timestamp ); } + protected static void enqueueWork( Context context, Class clazz, Intent intent ) + { + enqueueWork( context, clazz, sJobIDs.get(clazz), intent ); + } + static XWJICmds cmdFrom( Intent intent, XWJICmds[] values ) { int ord = intent.getIntExtra( CMD_KEY, -1 );