|
|
@@ -1,5 +1,6 @@
|
|
|
package info.knacki.prometheusandroidexporter;
|
|
|
|
|
|
+import android.app.ActivityManager;
|
|
|
import android.app.Notification;
|
|
|
import android.app.NotificationManager;
|
|
|
import android.app.PendingIntent;
|
|
|
@@ -18,13 +19,21 @@ import java.util.concurrent.TimeUnit;
|
|
|
import java.util.logging.Level;
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
+import info.knacki.prometheusandroidexporter.collector.CPUCollector;
|
|
|
import info.knacki.prometheusandroidexporter.collector.DeviceCollector;
|
|
|
import info.knacki.prometheusandroidexporter.collector.MemoryCollector;
|
|
|
import info.knacki.prometheusandroidexporter.collector.NetworkCollector;
|
|
|
import info.knacki.prometheusandroidexporter.collector.PowerCollector;
|
|
|
+import info.knacki.prometheusandroidexporter.collector.ProcessCollector;
|
|
|
import info.knacki.prometheusandroidexporter.collector.StorageCollector;
|
|
|
+import info.knacki.prometheusandroidexporter.receiver.NetworkReceiver;
|
|
|
|
|
|
public class MainService extends Service {
|
|
|
+ public final static Logger log = Logger.getLogger(MainService.class.getName());
|
|
|
+ private final Binder fLocalBinder = this.new Binder();
|
|
|
+ private Notification.Builder fNotif;
|
|
|
+ private boolean fPaused;
|
|
|
+
|
|
|
public MainService() {
|
|
|
}
|
|
|
|
|
|
@@ -48,30 +57,69 @@ public class MainService extends Service {
|
|
|
CollectorManager collectorManager = CollectorManager.GetInstance();
|
|
|
collectorManager.tick(this);
|
|
|
InitCollectors(collectorManager);
|
|
|
- ScheduleCollection();
|
|
|
- try {
|
|
|
- HttpService.Register(getApplicationContext());
|
|
|
- }
|
|
|
- catch (IOException e) {
|
|
|
- Logger.getLogger(MainService.class.getName()).log(Level.SEVERE, "Cannot start server: ", e);
|
|
|
- }
|
|
|
NotifyIcon();
|
|
|
+ fPaused = true;
|
|
|
+ NetworkReceiver.Register(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ public class Binder extends android.os.Binder {
|
|
|
+ public MainService GetService() {
|
|
|
+ return MainService.this;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Nullable
|
|
|
@Override
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
- return null;
|
|
|
+ return fLocalBinder;
|
|
|
}
|
|
|
|
|
|
private void InitCollectors(CollectorManager manager) {
|
|
|
//manager.RegisterCollector(new TestCollector());
|
|
|
- manager.RegisterCollector(new DeviceCollector());
|
|
|
+ manager.RegisterCollector(new DeviceCollector(this));
|
|
|
manager.RegisterCollector(new PowerCollector());
|
|
|
manager.RegisterCollector(new MemoryCollector());
|
|
|
manager.RegisterCollector(new NetworkCollector());
|
|
|
+ manager.RegisterCollector(new ProcessCollector());
|
|
|
+
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
|
|
|
manager.RegisterCollector(new StorageCollector());
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
|
|
|
+ manager.RegisterCollector(new CPUCollector());
|
|
|
+ }
|
|
|
+
|
|
|
+ public void Resume() {
|
|
|
+ if (fPaused) {
|
|
|
+ synchronized (this) {
|
|
|
+ if (fPaused) {
|
|
|
+ log.warning("RESUME!~~~~");
|
|
|
+ try {
|
|
|
+ HttpService.Register();
|
|
|
+ }
|
|
|
+ catch (IOException e) {
|
|
|
+ log.log(Level.SEVERE, "Cannot start http server: ", e);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ ScheduleCollection();
|
|
|
+ UpdateIcon(R.mipmap.ic_launcher, "Service is running");
|
|
|
+ fPaused = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void Pause() {
|
|
|
+ if (!fPaused) {
|
|
|
+ synchronized (this) {
|
|
|
+ if (!fPaused) {
|
|
|
+ log.warning("PAUSE!~~~~");
|
|
|
+ fPaused = true;
|
|
|
+ HttpService.Stop();
|
|
|
+ WorkManager.getInstance(this).cancelAllWork();
|
|
|
+ UpdateIcon(R.mipmap.ic_launcher_idle, "Service waiting for wifi");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private void ScheduleCollection() {
|
|
|
@@ -79,14 +127,21 @@ public class MainService extends Service {
|
|
|
WorkManager.getInstance(this).enqueue(job);
|
|
|
}
|
|
|
|
|
|
- private void NotifyIcon() {
|
|
|
- Notification notif = (new Notification.Builder(this)
|
|
|
- .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT))
|
|
|
- .setSmallIcon(R.mipmap.ic_launcher)
|
|
|
- .setContentTitle("Prometheus Android Exporter")
|
|
|
- .setContentText("Service running")).build();
|
|
|
+ private void UpdateIcon(int icon, String text) {
|
|
|
+ Notification notif = fNotif
|
|
|
+ .setSmallIcon(icon)
|
|
|
+ .setContentText(text)
|
|
|
+ .build();
|
|
|
notif.flags = Notification.FLAG_ONGOING_EVENT;
|
|
|
((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(1, notif);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void NotifyIcon() {
|
|
|
+ fNotif = new Notification.Builder(this)
|
|
|
+ .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT))
|
|
|
+ .setContentTitle("Prometheus Android Exporter");
|
|
|
+ Notification notif = fNotif.build();
|
|
|
+ UpdateIcon(R.mipmap.ic_launcher_idle, "Service booting");
|
|
|
startForeground(1, notif);
|
|
|
}
|
|
|
}
|