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.)
This commit is contained in:
Eric House 2019-03-19 08:16:30 -07:00
parent 97a3c3a2d8
commit 7b04f8d4ed
4 changed files with 27 additions and 48 deletions

View file

@ -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();

View file

@ -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() ) );
}

View file

@ -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 );
}

View file

@ -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";
@ -36,6 +39,11 @@ abstract class XWJIService extends JobIntentService {
abstract void onHandleWorkImpl( Intent intent, XWJICmds cmd, long timestamp );
abstract XWJICmds[] getCmds();
private static Map<Class, Integer> 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 );