recast SMSService as a JobIntentService

Didn't think it was required, but once the app started getting messages
in the background it stoped working or crashed. Changes are minimal
because this service isn't responsible for receiving messages: it
doesn't have the long-lived threads of the BT and Relay services.
This commit is contained in:
Eric House 2019-02-27 07:25:33 -08:00
parent 20cd99f73b
commit 415ce0f5a2
2 changed files with 77 additions and 94 deletions

View file

@ -188,6 +188,7 @@
/>
<service android:name="SMSService"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE"
/>
<service android:name="RelayService"
android:exported="false"

View file

@ -55,15 +55,17 @@ import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class SMSService extends XWService {
public class SMSService extends XWJIService {
private static final String TAG = SMSService.class.getSimpleName();
private final static int sJobID = 218719980;
private static final String MSG_SENT = "MSG_SENT";
private static final String MSG_DELIVERED = "MSG_DELIVERED";
private static final int SMS_PROTO_VERSION_WITHPORT = 1;
private static final int SMS_PROTO_VERSION = SMS_PROTO_VERSION_WITHPORT;
private enum SMSAction { _NONE,
private enum SMSAction implements XWJICmds { _NONE,
INVITE,
SEND,
REMOVE,
@ -176,7 +178,7 @@ public class SMSService extends XWService {
public static void stopService( Context context )
{
Intent intent = getIntentTo( context, SMSAction.STOP_SELF );
startService( context, intent );
enqueueWork( context, intent );
}
// NBS case
@ -186,7 +188,7 @@ public class SMSService extends XWService {
Intent intent = getIntentTo( context, SMSAction.HANDLEDATA )
.putExtra( BUFFER, buffer )
.putExtra( PHONE, phone );
startService( context, intent );
enqueueWork( context, intent );
}
public static void inviteRemote( Context context, String phone,
@ -197,7 +199,7 @@ public class SMSService extends XWService {
Intent intent = getIntentTo( context, SMSAction.INVITE )
.putExtra( PHONE, phone )
.putExtra( GAMEDATA_BA, data );
startService( context, intent );
enqueueWork( context, intent );
}
public static int sendPacket( Context context, String phone,
@ -209,7 +211,7 @@ public class SMSService extends XWService {
.putExtra( PHONE, phone )
.putExtra( MultiService.GAMEID, gameID )
.putExtra( BINBUFFER, binmsg );
startService( context, intent );
enqueueWork( context, intent );
nSent = binmsg.length;
} else {
Log.i( TAG, "sendPacket: dropping because SMS disabled" );
@ -223,14 +225,14 @@ public class SMSService extends XWService {
Intent intent = getIntentTo( context, SMSAction.REMOVE )
.putExtra( PHONE, phone )
.putExtra( MultiService.GAMEID, gameID );
startService( context, intent );
enqueueWork( context, intent );
}
public static void onGameDictDownload( Context context, Intent intentOld )
{
Intent intent = getIntentTo( context, SMSAction.ADDED_MISSING );
intent.fillIn( intentOld, 0 );
startService( context, intent );
enqueueWork( context, intent );
}
public static String fromPublicFmt( String msg )
@ -258,21 +260,15 @@ public class SMSService extends XWService {
return result;
}
private static void startService( Context context, Intent intent )
private static void enqueueWork( Context context, Intent intent )
{
Log.d( TAG, "startService(%s)", intent );
try {
context.startService( intent );
} catch ( java.lang.IllegalStateException ise ) {
Log.e( TAG, "startService(): %s", ise.getMessage() );
}
enqueueWork( context, SMSService.class, sJobID, intent );
Log.d( TAG, "called enqueueWork(%s)", intent );
}
private static Intent getIntentTo( Context context, SMSAction cmd )
{
Intent intent = new Intent( context, SMSService.class )
.putExtra( CMD_STR, cmd.ordinal() );
return intent;
return getIntentTo( context, SMSService.class, cmd );
}
private static boolean showToasts( Context context )
@ -287,46 +283,22 @@ public class SMSService extends XWService {
@Override
public void onCreate()
{
super.onCreate();
mHelper = new SMSServiceHelper( this );
if ( Utils.deviceSupportsNBS( this ) ) {
registerReceivers();
} else {
stopSelf();
}
}
@Override
public void onDestroy()
{
if ( null != m_sentReceiver ) {
unregisterReceiver( m_sentReceiver );
m_sentReceiver = null;
}
if ( null != m_receiveReceiver ) {
unregisterReceiver( m_receiveReceiver );
m_receiveReceiver = null;
}
if ( null != m_prefsListener ) {
SharedPreferences sp
= PreferenceManager.getDefaultSharedPreferences( this );
sp.unregisterOnSharedPreferenceChangeListener( m_prefsListener );
m_prefsListener = null;
}
super.onDestroy();
}
XWJICmds[] getCmds() { return SMSAction.values(); }
@Override
public int onStartCommand( Intent intent, int flags, int startId )
void onHandleWorkImpl( Intent intent, XWJICmds jicmd, long timestamp )
{
// Log.d( TAG, "onStartCommand(%s)", intent );
int result = Service.START_NOT_STICKY;
if ( null != intent ) {
int ordinal = intent.getIntExtra( CMD_STR, -1 );
if ( -1 == ordinal ) {
// ???
} else {
SMSAction cmd = SMSAction.values()[ordinal];
Log.d( TAG, "onHandleWorkImpl()" );
SMSAction cmd = (SMSAction)jicmd;
switch( cmd ) {
case STOP_SELF:
stopSelf();
@ -371,17 +343,27 @@ public class SMSService extends XWService {
}
}
result = Service.START_STICKY;
@Override
public void onDestroy()
{
if ( null != m_sentReceiver ) {
unregisterReceiver( m_sentReceiver );
m_sentReceiver = null;
}
if ( null != m_receiveReceiver ) {
unregisterReceiver( m_receiveReceiver );
m_receiveReceiver = null;
}
if ( null != m_prefsListener ) {
SharedPreferences sp
= PreferenceManager.getDefaultSharedPreferences( this );
sp.unregisterOnSharedPreferenceChangeListener( m_prefsListener );
m_prefsListener = null;
}
if ( Service.START_NOT_STICKY == result
|| !XWPrefs.getNBSEnabled( this ) ) {
stopSelf( startId );
super.onDestroy();
}
return result;
} // onStartCommand
private void inviteRemote( String phone, byte[] asBytes )
{
resendFor( phone, SMS_CMD.INVITE, 0, asBytes, true );
@ -450,7 +432,7 @@ public class SMSService extends XWService {
Intent intent = getIntentTo( SMSService.this,
SMSAction.RESEND );
intent.putExtra( PHONE, phone );
startService( intent );
enqueueWork( SMSService.this, intent );
} catch ( InterruptedException ie ) {
Log.e( TAG, ie.getMessage() );
}