|
|
|
@ -23,22 +23,28 @@ public class ForegroundService extends Service {
@@ -23,22 +23,28 @@ public class ForegroundService extends Service {
|
|
|
|
|
|
|
|
|
|
private static volatile DaemonWrapper daemon; |
|
|
|
|
|
|
|
|
|
private static final Object initDeinitLock = new Object(); |
|
|
|
|
|
|
|
|
|
private final DaemonWrapper.StateUpdateListener daemonStateUpdatedListener = |
|
|
|
|
new DaemonWrapper.StateUpdateListener() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void daemonStateUpdate() { |
|
|
|
|
try { |
|
|
|
|
synchronized (ForegroundService.this) { |
|
|
|
|
if (shown) cancelNotification(); |
|
|
|
|
showNotification(); |
|
|
|
|
} |
|
|
|
|
} catch (Throwable tr) { |
|
|
|
|
Log.e(TAG,"error ignored",tr); |
|
|
|
|
} |
|
|
|
|
public void daemonStateUpdate(DaemonWrapper.State oldValue, DaemonWrapper.State newValue) { |
|
|
|
|
updateNotificationText(); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
private void updateNotificationText() { |
|
|
|
|
try { |
|
|
|
|
synchronized (initDeinitLock) { |
|
|
|
|
if (shown) cancelNotification(); |
|
|
|
|
showNotification(); |
|
|
|
|
} |
|
|
|
|
} catch (Throwable tr) { |
|
|
|
|
Log.e(TAG,"error ignored",tr); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private NotificationManager notificationManager; |
|
|
|
|
|
|
|
|
@ -63,7 +69,9 @@ public class ForegroundService extends Service {
@@ -63,7 +69,9 @@ public class ForegroundService extends Service {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static void initCheck() { |
|
|
|
|
if(instance!=null && daemon!=null) instance.setListener(); |
|
|
|
|
synchronized (initDeinitLock) { |
|
|
|
|
if (instance != null && daemon != null) instance.setListener(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@ -75,7 +83,7 @@ public class ForegroundService extends Service {
@@ -75,7 +83,7 @@ public class ForegroundService extends Service {
|
|
|
|
|
|
|
|
|
|
private void setListener() { |
|
|
|
|
daemon.addStateChangeListener(daemonStateUpdatedListener); |
|
|
|
|
if (!shown) daemonStateUpdatedListener.daemonStateUpdate(); |
|
|
|
|
updateNotificationText(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@ -96,18 +104,23 @@ public class ForegroundService extends Service {
@@ -96,18 +104,23 @@ public class ForegroundService extends Service {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static void deinitCheck() { |
|
|
|
|
if(daemon!=null && instance!=null)daemon.removeStateChangeListener(instance.daemonStateUpdatedListener); |
|
|
|
|
synchronized (initDeinitLock) { |
|
|
|
|
if (daemon != null && instance != null) |
|
|
|
|
daemon.removeStateChangeListener(instance.daemonStateUpdatedListener); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private synchronized void cancelNotification() { |
|
|
|
|
// Cancel the persistent notification.
|
|
|
|
|
notificationManager.cancel(NOTIFICATION); |
|
|
|
|
private void cancelNotification() { |
|
|
|
|
synchronized (initDeinitLock) { |
|
|
|
|
// Cancel the persistent notification.
|
|
|
|
|
notificationManager.cancel(NOTIFICATION); |
|
|
|
|
|
|
|
|
|
stopForeground(true); |
|
|
|
|
stopForeground(true); |
|
|
|
|
|
|
|
|
|
// Tell the user we stopped.
|
|
|
|
|
//Toast.makeText(this, R.string.i2pd_service_stopped, Toast.LENGTH_SHORT).show();
|
|
|
|
|
shown=false; |
|
|
|
|
// Tell the user we stopped.
|
|
|
|
|
//Toast.makeText(this, R.string.i2pd_service_stopped, Toast.LENGTH_SHORT).show();
|
|
|
|
|
shown = false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@ -122,39 +135,41 @@ public class ForegroundService extends Service {
@@ -122,39 +135,41 @@ public class ForegroundService extends Service {
|
|
|
|
|
/** |
|
|
|
|
* Show a notification while this service is running. |
|
|
|
|
*/ |
|
|
|
|
private synchronized void showNotification() { |
|
|
|
|
if(daemon!=null) { |
|
|
|
|
// In this sample, we'll use the same text for the ticker and the expanded notification
|
|
|
|
|
CharSequence text = getText(daemon.getState().getStatusStringResourceId()); |
|
|
|
|
|
|
|
|
|
// The PendingIntent to launch our activity if the user selects this notification
|
|
|
|
|
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, |
|
|
|
|
new Intent(this, I2PDActivity.class), 0); |
|
|
|
|
|
|
|
|
|
// If earlier version channel ID is not used
|
|
|
|
|
// https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context)
|
|
|
|
|
String channelId = Build.VERSION.SDK_INT >= 26 ? createNotificationChannel() : ""; |
|
|
|
|
|
|
|
|
|
// Set the info for the views that show in the notification panel.
|
|
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId) |
|
|
|
|
.setOngoing(true) |
|
|
|
|
.setSmallIcon(R.drawable.itoopie_notification_icon); // the status icon
|
|
|
|
|
if (Build.VERSION.SDK_INT >= 16) |
|
|
|
|
builder = builder.setPriority(Notification.PRIORITY_DEFAULT); |
|
|
|
|
if (Build.VERSION.SDK_INT >= 21) |
|
|
|
|
builder = builder.setCategory(Notification.CATEGORY_SERVICE); |
|
|
|
|
Notification notification = builder |
|
|
|
|
.setTicker(text) // the status text
|
|
|
|
|
.setWhen(System.currentTimeMillis()) // the time stamp
|
|
|
|
|
.setContentTitle(getText(R.string.app_name)) // the label of the entry
|
|
|
|
|
.setContentText(text) // the contents of the entry
|
|
|
|
|
.setContentIntent(contentIntent) // The intent to send when the entry is clicked
|
|
|
|
|
.build(); |
|
|
|
|
|
|
|
|
|
// Send the notification.
|
|
|
|
|
//mNM.notify(NOTIFICATION, notification);
|
|
|
|
|
startForeground(NOTIFICATION, notification); |
|
|
|
|
shown = true; |
|
|
|
|
private void showNotification() { |
|
|
|
|
synchronized (initDeinitLock) { |
|
|
|
|
if (daemon != null) { |
|
|
|
|
// In this sample, we'll use the same text for the ticker and the expanded notification
|
|
|
|
|
CharSequence text = getText(daemon.getState().getStatusStringResourceId()); |
|
|
|
|
|
|
|
|
|
// The PendingIntent to launch our activity if the user selects this notification
|
|
|
|
|
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, |
|
|
|
|
new Intent(this, I2PDActivity.class), 0); |
|
|
|
|
|
|
|
|
|
// If earlier version channel ID is not used
|
|
|
|
|
// https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context)
|
|
|
|
|
String channelId = Build.VERSION.SDK_INT >= 26 ? createNotificationChannel() : ""; |
|
|
|
|
|
|
|
|
|
// Set the info for the views that show in the notification panel.
|
|
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId) |
|
|
|
|
.setOngoing(true) |
|
|
|
|
.setSmallIcon(R.drawable.itoopie_notification_icon); // the status icon
|
|
|
|
|
if (Build.VERSION.SDK_INT >= 16) |
|
|
|
|
builder = builder.setPriority(Notification.PRIORITY_DEFAULT); |
|
|
|
|
if (Build.VERSION.SDK_INT >= 21) |
|
|
|
|
builder = builder.setCategory(Notification.CATEGORY_SERVICE); |
|
|
|
|
Notification notification = builder |
|
|
|
|
.setTicker(text) // the status text
|
|
|
|
|
.setWhen(System.currentTimeMillis()) // the time stamp
|
|
|
|
|
.setContentTitle(getText(R.string.app_name)) // the label of the entry
|
|
|
|
|
.setContentText(text) // the contents of the entry
|
|
|
|
|
.setContentIntent(contentIntent) // The intent to send when the entry is clicked
|
|
|
|
|
.build(); |
|
|
|
|
|
|
|
|
|
// Send the notification.
|
|
|
|
|
//mNM.notify(NOTIFICATION, notification);
|
|
|
|
|
startForeground(NOTIFICATION, notification); |
|
|
|
|
shown = true; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|