Notification action to stop the background service #248

Merged
akinwale merged 2 commits from notification-action into master 2018-08-22 14:51:00 +02:00
6 changed files with 60 additions and 6 deletions
Showing only changes of commit ac5259d257 - Show all commits

View file

@ -148,7 +148,7 @@ android.react_src = ./app
# (list) Gradle dependencies to add (currently works only with sdl2_gradle # (list) Gradle dependencies to add (currently works only with sdl2_gradle
# bootstrap) # 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 # (str) python-for-android branch to use, defaults to master
#p4a.branch = stable #p4a.branch = stable

View file

@ -148,7 +148,7 @@ android.react_src = ./app
# (list) Gradle dependencies to add (currently works only with sdl2_gradle # (list) Gradle dependencies to add (currently works only with sdl2_gradle
# bootstrap) # 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 # (str) python-for-android branch to use, defaults to master
#p4a.branch = stable #p4a.branch = stable

View file

@ -148,7 +148,7 @@ android.react_src = ./app
# (list) Gradle dependencies to add (currently works only with sdl2_gradle # (list) Gradle dependencies to add (currently works only with sdl2_gradle
# bootstrap) # 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 # (str) python-for-android branch to use, defaults to master
#p4a.branch = stable #p4a.branch = stable

View file

@ -5,13 +5,16 @@ import android.app.Notification;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Intent; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Binder; import android.os.Binder;
import android.os.IBinder; import android.os.IBinder;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log; import android.util.Log;
import java.io.File; 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"; 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 String TAG = "LbrynetService";
public static LbrynetService serviceInstance; public static LbrynetService serviceInstance;
private BroadcastReceiver stopServiceReceiver;
@Override @Override
public boolean canDisplayNotification() { public boolean canDisplayNotification() {
return true; 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 @Override
protected void doStartForeground(Bundle extras) { protected void doStartForeground(Bundle extras) {
String serviceTitle = extras.getString("serviceTitle"); String serviceTitle = extras.getString("serviceTitle");
@ -64,13 +87,19 @@ public class LbrynetService extends PythonService {
Intent contextIntent = new Intent(context, MainActivity.class); Intent contextIntent = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent, PendingIntent.FLAG_UPDATE_CURRENT); 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); 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) .setContentText(serviceDescription)
.setContentIntent(pIntent) .setContentIntent(pIntent)
.setWhen(System.currentTimeMillis()) .setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_lbry) .setSmallIcon(R.drawable.ic_lbry)
.setOngoing(true) .setOngoing(true)
.addAction(android.R.drawable.ic_menu_close_clear_cancel, "Stop", stopPendingIntent)
.build(); .build();
startForeground(1, notification); startForeground(1, notification);
} }
@ -94,12 +123,18 @@ public class LbrynetService extends PythonService {
getApplicationContext(), "", LbrynetService.class, "lbrynetservice"); getApplicationContext(), "", LbrynetService.class, "lbrynetservice");
} }
// Register broadcast receiver
return super.onStartCommand(intent, flags, startId); return super.onStartCommand(intent, flags, startId);
} }
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
if (stopServiceReceiver != null) {
unregisterReceiver(stopServiceReceiver);
stopServiceReceiver = null;
}
serviceInstance = null; serviceInstance = null;
} }

View file

@ -4,6 +4,7 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.app.Activity; import android.app.Activity;
import android.app.ActivityManager; import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; 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 static final int PHONE_STATE_PERMISSION_REQ_CODE = 202;
private BroadcastReceiver stopServiceReceiver;
private ReactRootView mReactRootView; private ReactRootView mReactRootView;
private ReactInstanceManager mReactInstanceManager; private ReactInstanceManager mReactInstanceManager;
@ -79,6 +82,17 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
currentActivity = this; 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 // Start the daemon service if it is not started
serviceRunning = isServiceRunning(LbrynetService.class); serviceRunning = isServiceRunning(LbrynetService.class);
if (!serviceRunning) { if (!serviceRunning) {
@ -230,6 +244,11 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand
} }
} }
if (stopServiceReceiver != null) {
unregisterReceiver(stopServiceReceiver);
stopServiceReceiver = null;
}
super.onDestroy(); super.onDestroy();
if (mReactInstanceManager != null) { if (mReactInstanceManager != null) {

View file

@ -14,4 +14,4 @@ public class NotificationDeletedReceiver extends BroadcastReceiver {
DownloadManagerModule.groupCreated = false; DownloadManagerModule.groupCreated = false;
} }
} }
} }