|
@@ -1,7 +1,7 @@
|
|
|
package info.knacki.prometheusandroidexporter;
|
|
package info.knacki.prometheusandroidexporter;
|
|
|
|
|
|
|
|
-import android.app.ActivityManager;
|
|
|
|
|
import android.app.Notification;
|
|
import android.app.Notification;
|
|
|
|
|
+import android.app.NotificationChannel;
|
|
|
import android.app.NotificationManager;
|
|
import android.app.NotificationManager;
|
|
|
import android.app.PendingIntent;
|
|
import android.app.PendingIntent;
|
|
|
import android.app.Service;
|
|
import android.app.Service;
|
|
@@ -11,6 +11,7 @@ import android.os.Build;
|
|
|
import android.os.IBinder;
|
|
import android.os.IBinder;
|
|
|
|
|
|
|
|
import androidx.annotation.Nullable;
|
|
import androidx.annotation.Nullable;
|
|
|
|
|
+import androidx.core.app.NotificationCompat;
|
|
|
import androidx.work.PeriodicWorkRequest;
|
|
import androidx.work.PeriodicWorkRequest;
|
|
|
import androidx.work.WorkManager;
|
|
import androidx.work.WorkManager;
|
|
|
|
|
|
|
@@ -31,8 +32,9 @@ import info.knacki.prometheusandroidexporter.receiver.NetworkReceiver;
|
|
|
public class MainService extends Service {
|
|
public class MainService extends Service {
|
|
|
public final static Logger log = Logger.getLogger(MainService.class.getName());
|
|
public final static Logger log = Logger.getLogger(MainService.class.getName());
|
|
|
private final Binder fLocalBinder = this.new Binder();
|
|
private final Binder fLocalBinder = this.new Binder();
|
|
|
- private Notification.Builder fNotif;
|
|
|
|
|
|
|
+ private NotificationCompat.Builder fNotif;
|
|
|
private boolean fPaused;
|
|
private boolean fPaused;
|
|
|
|
|
+ private static final int gNotifId = 1;
|
|
|
|
|
|
|
|
public MainService() {
|
|
public MainService() {
|
|
|
}
|
|
}
|
|
@@ -62,6 +64,13 @@ public class MainService extends Service {
|
|
|
NetworkReceiver.Register(this);
|
|
NetworkReceiver.Register(this);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onDestroy() {
|
|
|
|
|
+ Pause();
|
|
|
|
|
+ super.onDestroy();
|
|
|
|
|
+ ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).cancel(gNotifId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public class Binder extends android.os.Binder {
|
|
public class Binder extends android.os.Binder {
|
|
|
public MainService GetService() {
|
|
public MainService GetService() {
|
|
|
return MainService.this;
|
|
return MainService.this;
|
|
@@ -94,7 +103,7 @@ public class MainService extends Service {
|
|
|
if (fPaused) {
|
|
if (fPaused) {
|
|
|
log.warning("RESUME!~~~~");
|
|
log.warning("RESUME!~~~~");
|
|
|
try {
|
|
try {
|
|
|
- HttpService.Register();
|
|
|
|
|
|
|
+ HttpService.Register(this);
|
|
|
}
|
|
}
|
|
|
catch (IOException e) {
|
|
catch (IOException e) {
|
|
|
log.log(Level.SEVERE, "Cannot start http server: ", e);
|
|
log.log(Level.SEVERE, "Cannot start http server: ", e);
|
|
@@ -125,6 +134,7 @@ public class MainService extends Service {
|
|
|
private void ScheduleCollection() {
|
|
private void ScheduleCollection() {
|
|
|
PeriodicWorkRequest job = new PeriodicWorkRequest.Builder(CollectorWorker.class, 15, TimeUnit.MINUTES).build();
|
|
PeriodicWorkRequest job = new PeriodicWorkRequest.Builder(CollectorWorker.class, 15, TimeUnit.MINUTES).build();
|
|
|
WorkManager.getInstance(this).enqueue(job);
|
|
WorkManager.getInstance(this).enqueue(job);
|
|
|
|
|
+ CollectorManager.GetInstance().tick(getApplicationContext());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void UpdateIcon(int icon, String text) {
|
|
private void UpdateIcon(int icon, String text) {
|
|
@@ -132,16 +142,22 @@ public class MainService extends Service {
|
|
|
.setSmallIcon(icon)
|
|
.setSmallIcon(icon)
|
|
|
.setContentText(text)
|
|
.setContentText(text)
|
|
|
.build();
|
|
.build();
|
|
|
|
|
+
|
|
|
notif.flags = Notification.FLAG_ONGOING_EVENT;
|
|
notif.flags = Notification.FLAG_ONGOING_EVENT;
|
|
|
- ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(1, notif);
|
|
|
|
|
|
|
+ ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(gNotifId, notif);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void NotifyIcon() {
|
|
private void NotifyIcon() {
|
|
|
- fNotif = new Notification.Builder(this)
|
|
|
|
|
|
|
+ String notifChannelId = getPackageName();
|
|
|
|
|
+ if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
|
|
|
|
+ NotificationChannel channel = new NotificationChannel(notifChannelId, MainService.class.getName(), NotificationManager.IMPORTANCE_DEFAULT);
|
|
|
|
|
+ ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).createNotificationChannel(channel);
|
|
|
|
|
+ }
|
|
|
|
|
+ fNotif = new NotificationCompat.Builder(this, notifChannelId)
|
|
|
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT))
|
|
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT))
|
|
|
.setContentTitle("Prometheus Android Exporter");
|
|
.setContentTitle("Prometheus Android Exporter");
|
|
|
Notification notif = fNotif.build();
|
|
Notification notif = fNotif.build();
|
|
|
UpdateIcon(R.mipmap.ic_launcher_idle, "Service booting");
|
|
UpdateIcon(R.mipmap.ic_launcher_idle, "Service booting");
|
|
|
- startForeground(1, notif);
|
|
|
|
|
|
|
+ startForeground(gNotifId, notif);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|