From ac5259d25726ac949c6ed5348c213ad227aad4e6 Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Fri, 17 Aug 2018 16:23:33 +0100 Subject: [PATCH] add stop action button to LBRY service notification --- buildozer.spec.sample | 2 +- buildozer.spec.travis | 2 +- buildozer.spec.vagrant | 2 +- .../java/io/lbry/browser/LbrynetService.java | 39 ++++++++++++++++++- .../java/io/lbry/browser/MainActivity.java | 19 +++++++++ .../NotificationDeletedReceiver.java | 2 +- 6 files changed, 60 insertions(+), 6 deletions(-) diff --git a/buildozer.spec.sample b/buildozer.spec.sample index b64c8f41..8a5ead67 100644 --- a/buildozer.spec.sample +++ b/buildozer.spec.sample @@ -148,7 +148,7 @@ android.react_src = ./app # (list) Gradle dependencies to add (currently works only with sdl2_gradle # bootstrap) -android.gradle_dependencies = com.android.support:appcompat-v7:27.1.1, com.facebook.react:react-native:0.55.3, com.mixpanel.android:mixpanel-android:5+, com.google.android.gms:play-services-gcm:11.0.4+, com.facebook.fresco:fresco:1.9.0, com.facebook.fresco:animated-gif:1.9.0 +android.gradle_dependencies = com.android.support:appcompat-v7:27.1.1, com.android.support:support-v4:27.1.1, com.facebook.react:react-native:0.55.3, com.mixpanel.android:mixpanel-android:5+, com.google.android.gms:play-services-gcm:11.0.4+, com.facebook.fresco:fresco:1.9.0, com.facebook.fresco:animated-gif:1.9.0 # (str) python-for-android branch to use, defaults to master #p4a.branch = stable diff --git a/buildozer.spec.travis b/buildozer.spec.travis index b64c8f41..8a5ead67 100644 --- a/buildozer.spec.travis +++ b/buildozer.spec.travis @@ -148,7 +148,7 @@ android.react_src = ./app # (list) Gradle dependencies to add (currently works only with sdl2_gradle # bootstrap) -android.gradle_dependencies = com.android.support:appcompat-v7:27.1.1, com.facebook.react:react-native:0.55.3, com.mixpanel.android:mixpanel-android:5+, com.google.android.gms:play-services-gcm:11.0.4+, com.facebook.fresco:fresco:1.9.0, com.facebook.fresco:animated-gif:1.9.0 +android.gradle_dependencies = com.android.support:appcompat-v7:27.1.1, com.android.support:support-v4:27.1.1, com.facebook.react:react-native:0.55.3, com.mixpanel.android:mixpanel-android:5+, com.google.android.gms:play-services-gcm:11.0.4+, com.facebook.fresco:fresco:1.9.0, com.facebook.fresco:animated-gif:1.9.0 # (str) python-for-android branch to use, defaults to master #p4a.branch = stable diff --git a/buildozer.spec.vagrant b/buildozer.spec.vagrant index 5cedf6e4..870412f7 100644 --- a/buildozer.spec.vagrant +++ b/buildozer.spec.vagrant @@ -148,7 +148,7 @@ android.react_src = ./app # (list) Gradle dependencies to add (currently works only with sdl2_gradle # bootstrap) -android.gradle_dependencies = com.android.support:appcompat-v7:27.1.1, com.facebook.react:react-native:0.55.3, com.mixpanel.android:mixpanel-android:5+, com.google.android.gms:play-services-gcm:11.0.4+, com.facebook.fresco:fresco:1.9.0, com.facebook.fresco:animated-gif:1.9.0 +android.gradle_dependencies = com.android.support:appcompat-v7:27.1.1, com.android.support:support-v4:27.1.1, com.facebook.react:react-native:0.55.3, com.mixpanel.android:mixpanel-android:5+, com.google.android.gms:play-services-gcm:11.0.4+, com.facebook.fresco:fresco:1.9.0, com.facebook.fresco:animated-gif:1.9.0 # (str) python-for-android branch to use, defaults to master #p4a.branch = stable diff --git a/src/main/java/io/lbry/browser/LbrynetService.java b/src/main/java/io/lbry/browser/LbrynetService.java index 548cf602..254f9367 100644 --- a/src/main/java/io/lbry/browser/LbrynetService.java +++ b/src/main/java/io/lbry/browser/LbrynetService.java @@ -5,13 +5,16 @@ import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; -import android.content.Intent; +import android.content.BroadcastReceiver; import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; import android.os.Build; import android.os.Bundle; import android.os.Binder; import android.os.IBinder; import android.support.v4.app.NotificationCompat; +import android.support.v4.content.ContextCompat; import android.util.Log; import java.io.File; @@ -36,15 +39,35 @@ public class LbrynetService extends PythonService { private static final String NOTIFICATION_CHANNEL_ID = "io.lbry.browser.DAEMON_NOTIFICATION_CHANNEL"; + public static final String ACTION_STOP_SERVICE = "io.lbry.browser.ACTION_STOP_SERVICE"; + public static String TAG = "LbrynetService"; public static LbrynetService serviceInstance; + private BroadcastReceiver stopServiceReceiver; + @Override public boolean canDisplayNotification() { return true; } + @Override + public void onCreate() { + super.onCreate(); + + // Register the stop service receiver + IntentFilter intentFilter = new IntentFilter(); + intentFilter.addAction(ACTION_STOP_SERVICE); + stopServiceReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + LbrynetService.this.stopSelf(); + } + }; + registerReceiver(stopServiceReceiver, intentFilter); + } + @Override protected void doStartForeground(Bundle extras) { String serviceTitle = extras.getString("serviceTitle"); @@ -64,13 +87,19 @@ public class LbrynetService extends PythonService { Intent contextIntent = new Intent(context, MainActivity.class); PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent, PendingIntent.FLAG_UPDATE_CURRENT); + + Intent stopIntent = new Intent(ACTION_STOP_SERVICE); + PendingIntent stopPendingIntent = PendingIntent.getBroadcast(context, 0, stopIntent, 0); + NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID); - Notification notification = builder.setContentTitle(serviceTitle) + Notification notification = builder.setColor(ContextCompat.getColor(context, R.color.lbrygreen)) + .setContentTitle(serviceTitle) .setContentText(serviceDescription) .setContentIntent(pIntent) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.ic_lbry) .setOngoing(true) + .addAction(android.R.drawable.ic_menu_close_clear_cancel, "Stop", stopPendingIntent) .build(); startForeground(1, notification); } @@ -94,12 +123,18 @@ public class LbrynetService extends PythonService { getApplicationContext(), "", LbrynetService.class, "lbrynetservice"); } + // Register broadcast receiver + return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); + if (stopServiceReceiver != null) { + unregisterReceiver(stopServiceReceiver); + stopServiceReceiver = null; + } serviceInstance = null; } diff --git a/src/main/java/io/lbry/browser/MainActivity.java b/src/main/java/io/lbry/browser/MainActivity.java index 68249a29..f77606bc 100644 --- a/src/main/java/io/lbry/browser/MainActivity.java +++ b/src/main/java/io/lbry/browser/MainActivity.java @@ -4,6 +4,7 @@ import android.os.Build; import android.os.Bundle; import android.app.Activity; import android.app.ActivityManager; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; @@ -44,6 +45,8 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand private static final int PHONE_STATE_PERMISSION_REQ_CODE = 202; + private BroadcastReceiver stopServiceReceiver; + private ReactRootView mReactRootView; private ReactInstanceManager mReactInstanceManager; @@ -79,6 +82,17 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand super.onCreate(savedInstanceState); currentActivity = this; + // Register the stop service receiver (so that we close the activity if the user requests the service to stop) + IntentFilter intentFilter = new IntentFilter(); + intentFilter.addAction(LbrynetService.ACTION_STOP_SERVICE); + stopServiceReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + MainActivity.this.finish(); + } + }; + registerReceiver(stopServiceReceiver, intentFilter); + // Start the daemon service if it is not started serviceRunning = isServiceRunning(LbrynetService.class); if (!serviceRunning) { @@ -230,6 +244,11 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand } } + if (stopServiceReceiver != null) { + unregisterReceiver(stopServiceReceiver); + stopServiceReceiver = null; + } + super.onDestroy(); if (mReactInstanceManager != null) { diff --git a/src/main/java/io/lbry/browser/receivers/NotificationDeletedReceiver.java b/src/main/java/io/lbry/browser/receivers/NotificationDeletedReceiver.java index a617e399..b8575b53 100644 --- a/src/main/java/io/lbry/browser/receivers/NotificationDeletedReceiver.java +++ b/src/main/java/io/lbry/browser/receivers/NotificationDeletedReceiver.java @@ -14,4 +14,4 @@ public class NotificationDeletedReceiver extends BroadcastReceiver { DownloadManagerModule.groupCreated = false; } } -} \ No newline at end of file +} -- 2.45.2