add stop button to notification

And keep the service alive (and the notification present) for 15
minutes.
This commit is contained in:
Eric House 2018-12-10 19:17:05 -08:00
parent ea03c855e4
commit bfad324bb4
6 changed files with 38 additions and 15 deletions

View file

@ -211,6 +211,9 @@
<intent-filter>
<action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
</intent-filter>
<intent-filter>
<action android:name="org.eehouse.android.ACTION_STOP_BT" />
</intent-filter>
</receiver>
<receiver android:name="SMSReceiver" >

View file

@ -29,6 +29,9 @@ import android.content.Intent;
public class BTReceiver extends BroadcastReceiver {
private static final String TAG = BTReceiver.class.getSimpleName();
// This string is also used in AndroidManifest (as a string literal)
static final String ACTION_STOP_BT = "org.eehouse.android.ACTION_STOP_BT";
@Override
public void onReceive( Context context, Intent intent )
{
@ -37,9 +40,14 @@ public class BTReceiver extends BroadcastReceiver {
Log.d( TAG, "BTReceiver.onReceive(action=%s, intent=%s)",
action, intent.toString() );
if ( action.equals( BluetoothDevice.ACTION_ACL_CONNECTED ) ) {
switch (action ) {
case ACTION_STOP_BT:
BTService.stopBackground( context );
break;
case BluetoothDevice.ACTION_ACL_CONNECTED:
BTService.onACLConnected( context );
} else if ( action.equals( BluetoothAdapter.ACTION_STATE_CHANGED ) ) {
break;
case BluetoothAdapter.ACTION_STATE_CHANGED:
int newState =
intent.getIntExtra( BluetoothAdapter.EXTRA_STATE, -1 );
switch ( newState ) {

View file

@ -80,7 +80,7 @@ public class BTService extends XWService {
private static final String BOGUS_MARSHMALLOW_ADDR = "02:00:00:00:00:00";
private static final String KEY_KEEPALIVE_UNTIL_SECS = "keep_secs";
// half minute for testing; maybe 15 on ship? Or make it a debug config.
private static int DEFAULT_KEEPALIVE_SECONDS = 30;
private static int DEFAULT_KEEPALIVE_SECONDS = 15 * 60;
private static final long RESEND_TIMEOUT = 5; // seconds
private static final int MAX_SEND_FAIL = 3;
@ -91,6 +91,7 @@ public class BTService extends XWService {
private static final int BT_PROTO = BT_PROTO_JSONS; // change in a release or two
private enum BTAction { _NONE,
CANCEL_SERVICE,
START_FOREGROUND,
START_BACKGROUND,
SCAN,
@ -265,7 +266,7 @@ public class BTService extends XWService {
startService( context,
getIntentTo( context,
inForeground ? BTAction.START_FOREGROUND
: BTAction.START_BACKGROUND ) );;
: BTAction.START_BACKGROUND ) );
}
}
@ -299,6 +300,12 @@ public class BTService extends XWService {
BTListenerThread.startYourself( context );
}
public static void stopBackground( Context context )
{
startService( context,
getIntentTo( context, BTAction.CANCEL_SERVICE ) );
}
public static void radioChanged( Context context, boolean cameOn )
{
Intent intent = getIntentTo( context, BTAction.RADIO );
@ -471,6 +478,7 @@ public class BTService extends XWService {
BTAction cmd = BTAction.values()[ordinal];
Log.i( TAG, "handleCommand; cmd=%s", cmd.toString() );
switch( cmd ) {
case CANCEL_SERVICE:
case START_FOREGROUND:
stopForeground( true ); // Kill the notification
break;
@ -563,11 +571,19 @@ public class BTService extends XWService {
PendingIntent pendIntent = PendingIntent
.getActivity( this, Utils.nextRandomInt(), notifIntent,
PendingIntent.FLAG_ONE_SHOT );
Intent stopIntent = new Intent(this, BTReceiver.class);
stopIntent.setAction( BTReceiver.ACTION_STOP_BT );
PendingIntent stopPI =
PendingIntent.getBroadcast(this, 0, stopIntent, 0);
m_notification =
new NotificationCompat.Builder( this, Utils.getChannelId(this) )
.setSmallIcon( R.drawable.notify_btservice )
.setContentText( getString(R.string.bkng_notify_text) )
.setContentIntent( pendIntent )
.addAction( R.drawable.notify_btservice,
getString(R.string.bkng_stop_text), stopPI )
.build();
}

View file

@ -2270,8 +2270,7 @@ public class GamesListDelegate extends ListDelegateBase
private void tryBackgroundIntent( Intent intent )
{
if ( intent.getBooleanExtra( BACKGROUND_EXTRA, false ) ) {
makeNotAgainBuilder( R.string.not_again_btservice,
R.string.key_notagain_btservice )
makeOkOnlyBuilder( R.string.btservice_expl )
.show();
}
}

View file

@ -136,8 +136,6 @@
<string name="key_invite_multi">key_invite_multi</string>
<string name="key_na_rematch_two_only">key_notagain_rematch_two_only</string>
<string name="key_notagain_dfltname">key_notagain_dfltname</string>
<string name="key_notagain_btservice">key_notagain_btservice</string>
<string name="key_na_comms_bt">key_na_comms_bt</string>
<string name="key_na_comms_p2p">key_na_comms_p2p</string>
<string name="key_na_comms_sms">key_na_comms_sms</string>

View file

@ -2030,6 +2030,7 @@
<string name="summary_enable_p2p">Experimental, uses lots of battery</string>
<string name="bkng_notify_text">Accepting Bluetooth messages…</string>
<string name="bkng_stop_text">Stop</string>
<!-- -->
<string name="confirm_sms_title">Confirm your SMS plan</string>
@ -2646,13 +2647,11 @@
player name \"%1$s\". Would you like to personalize with your own
name before you create this game?</string>
<string name="not_again_btservice">This notification is present
whenever CrossWords is ready to receive Bluetooth messages in the
background. This readiness has a slight impact on battery. If you
don\'t want to listen in the background disable the \"Enable
Background Listener\" preference. CrossWords will still receive
Bluetooth moves and invitations while in the foreground, but any
sent while it\'s in the background will be lost.</string>
<string name="btservice_expl">This notification is present whenever
CrossWords is running in the background to accept Bluetooth messages.
Because messages come in clusters, it usually runs for
about 15 minutes after the last message is received.
</string>
<string name="no_invites">This game has sent no invitations</string>