Skip to content

Commit

Permalink
Fix foreground services
Browse files Browse the repository at this point in the history
  • Loading branch information
marcocipriani01 committed Mar 16, 2021
1 parent 0d4e97e commit 7710cae
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -113,21 +117,21 @@ 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) {
builder.setProgress(0, 0, true);
} else {
builder.setProgress(totalCaptures, totalCaptures - camera.getRemainingCaptures(), false);
}
startForeground(1, builder.build());
startForeground(NOTIFICATION_ID, builder.build());
}

@Override
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7710cae

Please sign in to comment.