Skip to content

Commit 5076b67

Browse files
committed
Don't call shutGoogleUpNotification when service is already in foreground
Since adding the call to shutGoogleUpNotification to onStartCommand, using the controls on the notification would always remove the current notification which is pretty annoying. This fixes that unwanted behaviour and I think is much nicer than blindly calling it every time.
1 parent e3e97a1 commit 5076b67

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

Diff for: app/src/main/java/github/daneren2005/dsub/service/DownloadService.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ public class DownloadService extends Service {
170170
private boolean downloadOngoing = false;
171171
private float volume = 1.0f;
172172
private long delayUpdateProgress = DEFAULT_DELAY_UPDATE_PROGRESS;
173+
private boolean foregroundService = false;
173174

174175
private AudioEffectsController effectsController;
175176
private RemoteControlState remoteState = LOCAL;
@@ -309,7 +310,7 @@ public boolean onError(MediaPlayer mediaPlayer, int what, int more) {
309310
public int onStartCommand(Intent intent, int flags, int startId) {
310311
super.onStartCommand(intent, flags, startId);
311312
lifecycleSupport.onStart(intent);
312-
if(Build.VERSION.SDK_INT >= 26) {
313+
if(Build.VERSION.SDK_INT >= 26 && !this.isForeground()) {
313314
Notifications.shutGoogleUpNotification(this);
314315
}
315316
return START_NOT_STICKY;
@@ -1063,6 +1064,14 @@ public synchronized boolean shouldFastForward() {
10631064
return size() == 1 || (currentPlaying != null && !currentPlaying.isSong());
10641065
}
10651066

1067+
public synchronized boolean isForeground() {
1068+
return this.foregroundService;
1069+
}
1070+
1071+
public synchronized void setIsForeground(boolean foreground) {
1072+
this.foregroundService = foreground;
1073+
}
1074+
10661075
public synchronized List<DownloadFile> getDownloads() {
10671076
List<DownloadFile> temp = new ArrayList<DownloadFile>();
10681077
temp.addAll(downloadList);

Diff for: app/src/main/java/github/daneren2005/dsub/util/Notifications.java

+19-9
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ public static void showPlayingNotification(final Context context, final Download
114114
handler.post(new Runnable() {
115115
@Override
116116
public void run() {
117-
downloadService.stopForeground(true);
117+
stopForeground(downloadService, true);
118118
showDownloadingNotification(context, downloadService, handler, downloadService.getCurrentDownloading(), downloadService.getBackgroundDownloads().size());
119119

120120
try {
121-
downloadService.startForeground(NOTIFICATION_ID_PLAYING, notification);
121+
startForeground(downloadService, NOTIFICATION_ID_PLAYING, notification);
122122
} catch(Exception e) {
123123
Log.e(TAG, "Failed to start notifications after stopping foreground download");
124124
}
@@ -130,15 +130,15 @@ public void run() {
130130
public void run() {
131131
if (playing) {
132132
try {
133-
downloadService.startForeground(NOTIFICATION_ID_PLAYING, notification);
133+
startForeground(downloadService, NOTIFICATION_ID_PLAYING, notification);
134134
} catch(Exception e) {
135135
Log.e(TAG, "Failed to start notifications while playing");
136136
}
137137
} else {
138138
playShowing = false;
139139
persistentPlayingShowing = true;
140140
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
141-
downloadService.stopForeground(false);
141+
stopForeground(downloadService, false);
142142

143143
try {
144144
notificationManager.notify(NOTIFICATION_ID_PLAYING, notification);
@@ -334,7 +334,7 @@ public static void hidePlayingNotification(final Context context, final Download
334334
handler.post(new Runnable() {
335335
@Override
336336
public void run() {
337-
downloadService.stopForeground(true);
337+
stopForeground(downloadService, true);
338338

339339
if(persistentPlayingShowing) {
340340
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
@@ -413,7 +413,7 @@ public static void showDownloadingNotification(final Context context, final Down
413413
handler.post(new Runnable() {
414414
@Override
415415
public void run() {
416-
downloadService.startForeground(NOTIFICATION_ID_DOWNLOADING, notification);
416+
startForeground(downloadService, NOTIFICATION_ID_DOWNLOADING, notification);
417417
}
418418
});
419419
}
@@ -429,7 +429,7 @@ public static void hideDownloadingNotification(final Context context, final Down
429429
handler.post(new Runnable() {
430430
@Override
431431
public void run() {
432-
downloadService.stopForeground(true);
432+
stopForeground(downloadService, true);
433433
}
434434
});
435435
}
@@ -461,8 +461,8 @@ public static void shutGoogleUpNotification(final DownloadService downloadServic
461461
.setChannelId("downloading-channel");
462462

463463
final Notification notification = builder.build();
464-
downloadService.startForeground(NOTIFICATION_ID_SHUT_GOOGLE_UP, notification);
465-
downloadService.stopForeground(true);
464+
startForeground(downloadService, NOTIFICATION_ID_SHUT_GOOGLE_UP, notification);
465+
stopForeground(downloadService, true);
466466
}
467467

468468
public static void showSyncNotification(final Context context, int stringId, String extra) {
@@ -537,4 +537,14 @@ private static NotificationChannel getSyncNotificationChannel(Context context) {
537537

538538
return syncChannel;
539539
}
540+
541+
private static void startForeground(DownloadService downloadService, int notificationId, Notification notification) {
542+
downloadService.startForeground(notificationId, notification);
543+
downloadService.setIsForeground(true);
544+
}
545+
546+
private static void stopForeground(DownloadService downloadService, boolean removeNotification) {
547+
downloadService.stopForeground(removeNotification);
548+
downloadService.setIsForeground(false);
549+
}
540550
}

0 commit comments

Comments
 (0)