diff --git a/app/src/main/java/io/github/marcocipriani01/telescopetouch/AppForegroundService.java b/app/src/main/java/io/github/marcocipriani01/telescopetouch/AppForegroundService.java index d81f012ce..2bd4b67f3 100644 --- a/app/src/main/java/io/github/marcocipriani01/telescopetouch/AppForegroundService.java +++ b/app/src/main/java/io/github/marcocipriani01/telescopetouch/AppForegroundService.java @@ -48,6 +48,11 @@ public class AppForegroundService extends Service implements ConnectionManager.M public static final String SERVICE_ACTION_EXIT = "action_stop"; public static final String SERVICE_ACTION_DISCONNECT = "action_disconnect"; private static final String NOTIFICATION_CHANNEL = "TELESCOPE_TOUCH_SERVICE"; + private static final int NOTIFICATION_ID = 1; + private static final int PENDING_INTENT_OPEN_APP = 1; + private static final int PENDING_INTENT_MOUNT = 2; + private static final int PENDING_INTENT_DISCONNECT = 3; + private static final int PENDING_INTENT_EXIT = 4; private final Handler handler = new Handler(Looper.getMainLooper()); @Override @@ -57,11 +62,6 @@ public int onStartCommand(Intent intent, int flags, int startId) { case SERVICE_START: start(); break; - case SERVICE_ACTION_EXIT: - stopForeground(true); - stopSelf(); - System.exit(0); - break; case SERVICE_STOP: stopForeground(true); stopSelf(); @@ -72,6 +72,11 @@ public int onStartCommand(Intent intent, int flags, int startId) { stopForeground(true); stopSelf(); break; + case SERVICE_ACTION_EXIT: + stopForeground(true); + stopSelf(); + System.exit(0); + break; } } return super.onStartCommand(intent, flags, startId); @@ -98,25 +103,25 @@ private void start() { Intent connectionIntent = new Intent(this, MainActivity.class); connectionIntent.putExtra(MainActivity.ACTION, MainActivity.ACTION_CONNECT); stackBuilder.addNextIntentWithParentStack(connectionIntent); - builder.setContentIntent(stackBuilder.getPendingIntent(1, PendingIntent.FLAG_UPDATE_CURRENT)); + builder.setContentIntent(stackBuilder.getPendingIntent(PENDING_INTENT_OPEN_APP, PendingIntent.FLAG_UPDATE_CURRENT)); if (connectionManager.isConnected()) { Intent mountIntent = new Intent(this, MainActivity.class); mountIntent.putExtra(MainActivity.ACTION, MainActivity.ACTION_MOUNT_CONTROL); stackBuilder.addNextIntentWithParentStack(mountIntent); builder.addAction(new NotificationCompat.Action(null, getString(R.string.mount), - stackBuilder.getPendingIntent(2, PendingIntent.FLAG_UPDATE_CURRENT))); + stackBuilder.getPendingIntent(PENDING_INTENT_MOUNT, PendingIntent.FLAG_UPDATE_CURRENT))); Intent disconnectIntent = new Intent(this, AppForegroundService.class); disconnectIntent.setAction(SERVICE_ACTION_DISCONNECT); builder.addAction(new NotificationCompat.Action(null, getString(R.string.disconnect), - PendingIntent.getService(this, 3, disconnectIntent, PendingIntent.FLAG_UPDATE_CURRENT))); + PendingIntent.getService(this, PENDING_INTENT_DISCONNECT, disconnectIntent, PendingIntent.FLAG_UPDATE_CURRENT))); } else { Intent closeIntent = new Intent(this, AppForegroundService.class); closeIntent.setAction(SERVICE_ACTION_EXIT); builder.addAction(new NotificationCompat.Action(null, getString(R.string.exit), - PendingIntent.getService(this, 4, closeIntent, PendingIntent.FLAG_UPDATE_CURRENT))); + PendingIntent.getService(this, PENDING_INTENT_EXIT, closeIntent, PendingIntent.FLAG_UPDATE_CURRENT))); } - startForeground(1, builder.build()); + startForeground(NOTIFICATION_ID, builder.build()); } @Override diff --git a/app/src/main/java/io/github/marcocipriani01/telescopetouch/indi/CameraForegroundService.java b/app/src/main/java/io/github/marcocipriani01/telescopetouch/indi/CameraForegroundService.java index 7abe6ea56..93c29f8dd 100644 --- a/app/src/main/java/io/github/marcocipriani01/telescopetouch/indi/CameraForegroundService.java +++ b/app/src/main/java/io/github/marcocipriani01/telescopetouch/indi/CameraForegroundService.java @@ -34,8 +34,6 @@ import androidx.core.app.NotificationManagerCompat; import androidx.core.app.TaskStackBuilder; -import org.indilib.i4j.client.INDIValueException; - import java.util.Collection; import io.github.marcocipriani01.telescopetouch.R; @@ -53,6 +51,9 @@ public class CameraForegroundService extends Service public static final String SERVICE_ACTION_STOP_CAPTURE = "stop_capture"; private static final String TAG = TelescopeTouchApp.getTag(CameraForegroundService.class); private static final String NOTIFICATION_CHANNEL = "CCD_CAPTURE_SERVICE"; + private static final int NOTIFICATION_ID = 2; + private static final int PENDING_INTENT_CCD_VIEWER = 5; + private static final int PENDING_INTENT_STOP_LOOP = 6; private final Handler handler = new Handler(Looper.getMainLooper()); private INDICamera camera; private NotificationManagerCompat notificationManager; @@ -73,16 +74,19 @@ public int onStartCommand(Intent intent, int flags, int startId) { start(); } break; - case SERVICE_ACTION_STOP_CAPTURE: - try { - camera.abort(); - } catch (INDIValueException e) { - Log.e(TAG, e.getMessage(), e); - } + case SERVICE_STOP: stopForeground(true); stopSelf(); break; - case SERVICE_STOP: + case SERVICE_ACTION_STOP_CAPTURE: + camera = intent.getParcelableExtra(INDI_CAMERA_EXTRA); + if (camera != null) { + try { + camera.abort(); + } catch (Exception e) { + Log.e(TAG, e.getMessage(), e); + } + } stopForeground(true); stopSelf(); break; @@ -113,13 +117,13 @@ private void start() { Intent appIntent = new Intent(this, MainActivity.class); appIntent.putExtra(MainActivity.ACTION, MainActivity.ACTION_CCD_CAPTURE); stackBuilder.addNextIntentWithParentStack(appIntent); - builder.setContentIntent(stackBuilder.getPendingIntent(1, PendingIntent.FLAG_UPDATE_CURRENT)); + builder.setContentIntent(stackBuilder.getPendingIntent(PENDING_INTENT_CCD_VIEWER, PendingIntent.FLAG_UPDATE_CURRENT)); Intent stopLoopIntent = new Intent(this, CameraForegroundService.class); stopLoopIntent.setAction(SERVICE_ACTION_STOP_CAPTURE); stopLoopIntent.putExtra(CameraForegroundService.INDI_CAMERA_EXTRA, camera); builder.addAction(new NotificationCompat.Action(null, getString(R.string.stop_capture), - PendingIntent.getService(this, 2, stopLoopIntent, PendingIntent.FLAG_UPDATE_CURRENT))); + PendingIntent.getService(this, PENDING_INTENT_STOP_LOOP, stopLoopIntent, PendingIntent.FLAG_UPDATE_CURRENT))); int totalCaptures = camera.getLoopTotalCaptures(); if (totalCaptures == 0) { @@ -127,7 +131,7 @@ private void start() { } else { builder.setProgress(totalCaptures, totalCaptures - camera.getRemainingCaptures(), false); } - startForeground(1, builder.build()); + startForeground(NOTIFICATION_ID, builder.build()); } @Override @@ -172,7 +176,7 @@ public void onCamerasListChange() { @Override public void onLoopProgressChange(int progress, int total) { builder.setProgress(total, progress, false); - notificationManager.notify(1, builder.build()); + notificationManager.notify(NOTIFICATION_ID, builder.build()); } @Override