Skip to content
Snippets Groups Projects
Commit ff7d8b37 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

syncservice: always call startForeground after startService

Change-Id: I6e09df9f614bff72889e79434afaeb906ff9dddc
parent d79b9a68
Branches
Tags
No related merge requests found
...@@ -49,6 +49,8 @@ public class SyncService extends Service { ...@@ -49,6 +49,8 @@ public class SyncService extends Service {
private int serviceUsers = 0; private int serviceUsers = 0;
private final Random mRandom = new Random(); private final Random mRandom = new Random();
private Notification notification = null;
@Inject @Inject
NotificationService mNotificationService; NotificationService mNotificationService;
...@@ -62,14 +64,13 @@ public class SyncService extends Service { ...@@ -62,14 +64,13 @@ public class SyncService extends Service {
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
String action = intent.getAction(); String action = intent.getAction();
if (ACTION_START.equals(action)) { if (ACTION_START.equals(action)) {
if (serviceUsers == 0) { if (notification == null) {
final Intent deleteIntent = new Intent(ACTION_STOP) final Intent deleteIntent = new Intent(ACTION_STOP)
.setClass(getApplicationContext(), SyncService.class); .setClass(getApplicationContext(), SyncService.class);
final Intent contentIntent = new Intent(Intent.ACTION_VIEW) final Intent contentIntent = new Intent(Intent.ACTION_VIEW)
.setClass(getApplicationContext(), HomeActivity.class) .setClass(getApplicationContext(), HomeActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notification = new NotificationCompat.Builder(this, NotificationServiceImpl.NOTIF_CHANNEL_SYNC)
Notification notif = new NotificationCompat.Builder(this, NotificationServiceImpl.NOTIF_CHANNEL_SYNC)
.setContentTitle(getString(R.string.notif_sync_title)) .setContentTitle(getString(R.string.notif_sync_title))
.setPriority(NotificationCompat.PRIORITY_DEFAULT) .setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE) .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
...@@ -81,11 +82,12 @@ public class SyncService extends Service { ...@@ -81,11 +82,12 @@ public class SyncService extends Service {
.setDeleteIntent(PendingIntent.getService(getApplicationContext(), mRandom.nextInt(), deleteIntent, 0)) .setDeleteIntent(PendingIntent.getService(getApplicationContext(), mRandom.nextInt(), deleteIntent, 0))
.setContentIntent(PendingIntent.getActivity(getApplicationContext(), mRandom.nextInt(), contentIntent, 0)) .setContentIntent(PendingIntent.getActivity(getApplicationContext(), mRandom.nextInt(), contentIntent, 0))
.build(); .build();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
startForeground(NOTIF_SYNC_SERVICE_ID, notif, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC); startForeground(NOTIF_SYNC_SERVICE_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC);
else else
startForeground(NOTIF_SYNC_SERVICE_ID, notif); startForeground(NOTIF_SYNC_SERVICE_ID, notification);
if (serviceUsers == 0) {
JamiApplication.getInstance().startDaemon(); JamiApplication.getInstance().startDaemon();
} }
serviceUsers++; serviceUsers++;
...@@ -105,6 +107,7 @@ public class SyncService extends Service { ...@@ -105,6 +107,7 @@ public class SyncService extends Service {
if (serviceUsers == 0) { if (serviceUsers == 0) {
stopForeground(true); stopForeground(true);
stopSelf(); stopSelf();
notification = null;
} }
} }
return START_NOT_STICKY; return START_NOT_STICKY;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment