mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-23 07:27:22 +01:00
add stop button to notification
And keep the service alive (and the notification present) for 15 minutes.
This commit is contained in:
parent
ea03c855e4
commit
bfad324bb4
6 changed files with 38 additions and 15 deletions
|
@ -211,6 +211,9 @@
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
|
<action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="org.eehouse.android.ACTION_STOP_BT" />
|
||||||
|
</intent-filter>
|
||||||
</receiver>
|
</receiver>
|
||||||
|
|
||||||
<receiver android:name="SMSReceiver" >
|
<receiver android:name="SMSReceiver" >
|
||||||
|
|
|
@ -29,6 +29,9 @@ import android.content.Intent;
|
||||||
public class BTReceiver extends BroadcastReceiver {
|
public class BTReceiver extends BroadcastReceiver {
|
||||||
private static final String TAG = BTReceiver.class.getSimpleName();
|
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
|
@Override
|
||||||
public void onReceive( Context context, Intent intent )
|
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)",
|
Log.d( TAG, "BTReceiver.onReceive(action=%s, intent=%s)",
|
||||||
action, intent.toString() );
|
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 );
|
BTService.onACLConnected( context );
|
||||||
} else if ( action.equals( BluetoothAdapter.ACTION_STATE_CHANGED ) ) {
|
break;
|
||||||
|
case BluetoothAdapter.ACTION_STATE_CHANGED:
|
||||||
int newState =
|
int newState =
|
||||||
intent.getIntExtra( BluetoothAdapter.EXTRA_STATE, -1 );
|
intent.getIntExtra( BluetoothAdapter.EXTRA_STATE, -1 );
|
||||||
switch ( newState ) {
|
switch ( newState ) {
|
||||||
|
|
|
@ -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 BOGUS_MARSHMALLOW_ADDR = "02:00:00:00:00:00";
|
||||||
private static final String KEY_KEEPALIVE_UNTIL_SECS = "keep_secs";
|
private static final String KEY_KEEPALIVE_UNTIL_SECS = "keep_secs";
|
||||||
// half minute for testing; maybe 15 on ship? Or make it a debug config.
|
// 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 long RESEND_TIMEOUT = 5; // seconds
|
||||||
private static final int MAX_SEND_FAIL = 3;
|
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 static final int BT_PROTO = BT_PROTO_JSONS; // change in a release or two
|
||||||
|
|
||||||
private enum BTAction { _NONE,
|
private enum BTAction { _NONE,
|
||||||
|
CANCEL_SERVICE,
|
||||||
START_FOREGROUND,
|
START_FOREGROUND,
|
||||||
START_BACKGROUND,
|
START_BACKGROUND,
|
||||||
SCAN,
|
SCAN,
|
||||||
|
@ -265,7 +266,7 @@ public class BTService extends XWService {
|
||||||
startService( context,
|
startService( context,
|
||||||
getIntentTo( context,
|
getIntentTo( context,
|
||||||
inForeground ? BTAction.START_FOREGROUND
|
inForeground ? BTAction.START_FOREGROUND
|
||||||
: BTAction.START_BACKGROUND ) );;
|
: BTAction.START_BACKGROUND ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -299,6 +300,12 @@ public class BTService extends XWService {
|
||||||
BTListenerThread.startYourself( context );
|
BTListenerThread.startYourself( context );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void stopBackground( Context context )
|
||||||
|
{
|
||||||
|
startService( context,
|
||||||
|
getIntentTo( context, BTAction.CANCEL_SERVICE ) );
|
||||||
|
}
|
||||||
|
|
||||||
public static void radioChanged( Context context, boolean cameOn )
|
public static void radioChanged( Context context, boolean cameOn )
|
||||||
{
|
{
|
||||||
Intent intent = getIntentTo( context, BTAction.RADIO );
|
Intent intent = getIntentTo( context, BTAction.RADIO );
|
||||||
|
@ -471,6 +478,7 @@ public class BTService extends XWService {
|
||||||
BTAction cmd = BTAction.values()[ordinal];
|
BTAction cmd = BTAction.values()[ordinal];
|
||||||
Log.i( TAG, "handleCommand; cmd=%s", cmd.toString() );
|
Log.i( TAG, "handleCommand; cmd=%s", cmd.toString() );
|
||||||
switch( cmd ) {
|
switch( cmd ) {
|
||||||
|
case CANCEL_SERVICE:
|
||||||
case START_FOREGROUND:
|
case START_FOREGROUND:
|
||||||
stopForeground( true ); // Kill the notification
|
stopForeground( true ); // Kill the notification
|
||||||
break;
|
break;
|
||||||
|
@ -563,11 +571,19 @@ public class BTService extends XWService {
|
||||||
PendingIntent pendIntent = PendingIntent
|
PendingIntent pendIntent = PendingIntent
|
||||||
.getActivity( this, Utils.nextRandomInt(), notifIntent,
|
.getActivity( this, Utils.nextRandomInt(), notifIntent,
|
||||||
PendingIntent.FLAG_ONE_SHOT );
|
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 =
|
m_notification =
|
||||||
new NotificationCompat.Builder( this, Utils.getChannelId(this) )
|
new NotificationCompat.Builder( this, Utils.getChannelId(this) )
|
||||||
.setSmallIcon( R.drawable.notify_btservice )
|
.setSmallIcon( R.drawable.notify_btservice )
|
||||||
.setContentText( getString(R.string.bkng_notify_text) )
|
.setContentText( getString(R.string.bkng_notify_text) )
|
||||||
.setContentIntent( pendIntent )
|
.setContentIntent( pendIntent )
|
||||||
|
.addAction( R.drawable.notify_btservice,
|
||||||
|
getString(R.string.bkng_stop_text), stopPI )
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2270,8 +2270,7 @@ public class GamesListDelegate extends ListDelegateBase
|
||||||
private void tryBackgroundIntent( Intent intent )
|
private void tryBackgroundIntent( Intent intent )
|
||||||
{
|
{
|
||||||
if ( intent.getBooleanExtra( BACKGROUND_EXTRA, false ) ) {
|
if ( intent.getBooleanExtra( BACKGROUND_EXTRA, false ) ) {
|
||||||
makeNotAgainBuilder( R.string.not_again_btservice,
|
makeOkOnlyBuilder( R.string.btservice_expl )
|
||||||
R.string.key_notagain_btservice )
|
|
||||||
.show();
|
.show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -136,8 +136,6 @@
|
||||||
<string name="key_invite_multi">key_invite_multi</string>
|
<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_na_rematch_two_only">key_notagain_rematch_two_only</string>
|
||||||
<string name="key_notagain_dfltname">key_notagain_dfltname</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_bt">key_na_comms_bt</string>
|
||||||
<string name="key_na_comms_p2p">key_na_comms_p2p</string>
|
<string name="key_na_comms_p2p">key_na_comms_p2p</string>
|
||||||
<string name="key_na_comms_sms">key_na_comms_sms</string>
|
<string name="key_na_comms_sms">key_na_comms_sms</string>
|
||||||
|
|
|
@ -2030,6 +2030,7 @@
|
||||||
<string name="summary_enable_p2p">Experimental, uses lots of battery</string>
|
<string name="summary_enable_p2p">Experimental, uses lots of battery</string>
|
||||||
|
|
||||||
<string name="bkng_notify_text">Accepting Bluetooth messages…</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>
|
<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
|
player name \"%1$s\". Would you like to personalize with your own
|
||||||
name before you create this game?</string>
|
name before you create this game?</string>
|
||||||
|
|
||||||
<string name="not_again_btservice">This notification is present
|
<string name="btservice_expl">This notification is present whenever
|
||||||
whenever CrossWords is ready to receive Bluetooth messages in the
|
CrossWords is running in the background to accept Bluetooth messages.
|
||||||
background. This readiness has a slight impact on battery. If you
|
Because messages come in clusters, it usually runs for
|
||||||
don\'t want to listen in the background disable the \"Enable
|
about 15 minutes after the last message is received.
|
||||||
Background Listener\" preference. CrossWords will still receive
|
</string>
|
||||||
Bluetooth moves and invitations while in the foreground, but any
|
|
||||||
sent while it\'s in the background will be lost.</string>
|
|
||||||
|
|
||||||
<string name="no_invites">This game has sent no invitations</string>
|
<string name="no_invites">This game has sent no invitations</string>
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue