From 8e69723931e919e34b07dd265538b5ec4a3bb4c9 Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Fri, 30 Jan 2015 11:59:36 +0100 Subject: [PATCH] Added quit button to Service notification, removed unneccessary ACTION_STOP form Service --- app/src/main/AndroidManifest.xml | 1 + .../BluetoothCommunicationService.java | 41 +++++++++---------- .../gadgetbridge/StopServiceReceiver.java | 13 ++++++ 3 files changed, 34 insertions(+), 21 deletions(-) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/StopServiceReceiver.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d6115990..a1afb531 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -51,5 +51,6 @@ + diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java index f50a42fb..1599e7fb 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java @@ -31,8 +31,6 @@ public class BluetoothCommunicationService extends Service { public static final String ACTION_START = "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.start"; - public static final String ACTION_STOP - = "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.stop"; public static final String ACTION_NOTIFICATION_GENERIC = "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.notification_generic"; public static final String ACTION_NOTIFICATION_SMS @@ -60,7 +58,7 @@ public class BluetoothCommunicationService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { - if (!intent.getAction().equals(ACTION_START) && !intent.getAction().equals(ACTION_STOP) && mBtSocketIoThread == null) { + if (!intent.getAction().equals(ACTION_START) && mBtSocketIoThread == null) { return START_STICKY; } @@ -71,11 +69,15 @@ public class BluetoothCommunicationService extends Service { PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); + Intent stopIntent = new Intent(this, StopServiceReceiver.class); + PendingIntent pendingIntentStop = PendingIntent.getBroadcast(this, 0, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT); + Notification notification = new NotificationCompat.Builder(this) .setContentTitle("Gadgetbridge") .setTicker("Gadgetbridge Running") .setContentText("Gadgetbrige Running") .setSmallIcon(R.drawable.ic_launcher) + .addAction(android.R.drawable.ic_menu_close_clear_cancel, "Quit", pendingIntentStop) .setContentIntent(pendingIntent) .setOngoing(true).build(); @@ -113,23 +115,6 @@ public class BluetoothCommunicationService extends Service { } startForeground(1, notification); //FIXME: don't hardcode id } - } else if (intent.getAction().equals(ACTION_STOP)) { - try { - mBtSocketIoThread.join(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - try { - - mBtSocket.close(); - } catch (IOException e) { - e.printStackTrace(); - } - mBtSocket = null; - mBtSocketIoThread = null; - - stopForeground(true); - stopSelf(); } else if (intent.getAction().equals(ACTION_NOTIFICATION_GENERIC)) { String title = intent.getStringExtra("notification_title"); String body = intent.getStringExtra("notification_body"); @@ -163,6 +148,20 @@ public class BluetoothCommunicationService extends Service { @Override public void onDestroy() { super.onDestroy(); + if (mBtSocketIoThread != null) { + try { + mBtSocketIoThread.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + if (mBtSocket != null) { + try { + mBtSocket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } @Override @@ -298,4 +297,4 @@ public class BluetoothCommunicationService extends Service { } } } -} \ No newline at end of file +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/StopServiceReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/StopServiceReceiver.java new file mode 100644 index 00000000..302e0aba --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/StopServiceReceiver.java @@ -0,0 +1,13 @@ +package nodomain.freeyourgadget.gadgetbridge; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + +public class StopServiceReceiver extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + Intent stopIntent = new Intent(context, BluetoothCommunicationService.class); + context.stopService(stopIntent); + } +}