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
3 changed files with 61 additions and 3 deletions

View file

@ -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;
}

View file

@ -49,6 +49,8 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand
private static final int PHONE_STATE_PERMISSION_REQ_CODE = 202;
private BroadcastReceiver stopServiceReceiver;
private BroadcastReceiver backgroundMediaReceiver;
private ReactRootView mReactRootView;
@ -86,6 +88,9 @@ 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)
registerStopReceiver();
// Start the daemon service if it is not started
serviceRunning = isServiceRunning(LbrynetService.class);
if (!serviceRunning) {
@ -111,6 +116,18 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand
setContentView(mReactRootView);
}
private void registerStopReceiver() {
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);
}
private void registerBackgroundMediaReceiver() {
// Background media receiver
IntentFilter backgroundMediaFilter = new IntentFilter();
@ -266,6 +283,12 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand
if (backgroundMediaReceiver != null) {
unregisterReceiver(backgroundMediaReceiver);
backgroundMediaReceiver = null;
}
if (stopServiceReceiver != null) {
unregisterReceiver(stopServiceReceiver);
stopServiceReceiver = null;
}
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);