add stop action button to LBRY service notification (#248)
This commit is contained in:
parent
f926dc28be
commit
b99ffa0395
3 changed files with 61 additions and 3 deletions
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,6 +49,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 BroadcastReceiver backgroundMediaReceiver;
|
private BroadcastReceiver backgroundMediaReceiver;
|
||||||
|
|
||||||
private ReactRootView mReactRootView;
|
private ReactRootView mReactRootView;
|
||||||
|
@ -86,6 +88,9 @@ 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)
|
||||||
|
registerStopReceiver();
|
||||||
|
|
||||||
// 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) {
|
||||||
|
@ -111,6 +116,18 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand
|
||||||
setContentView(mReactRootView);
|
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() {
|
private void registerBackgroundMediaReceiver() {
|
||||||
// Background media receiver
|
// Background media receiver
|
||||||
IntentFilter backgroundMediaFilter = new IntentFilter();
|
IntentFilter backgroundMediaFilter = new IntentFilter();
|
||||||
|
@ -266,6 +283,12 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand
|
||||||
|
|
||||||
if (backgroundMediaReceiver != null) {
|
if (backgroundMediaReceiver != null) {
|
||||||
unregisterReceiver(backgroundMediaReceiver);
|
unregisterReceiver(backgroundMediaReceiver);
|
||||||
|
backgroundMediaReceiver = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stopServiceReceiver != null) {
|
||||||
|
unregisterReceiver(stopServiceReceiver);
|
||||||
|
stopServiceReceiver = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
|
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
|
||||||
|
|
Loading…
Reference in a new issue