Skip to content

Commit

Permalink
Don't call shutGoogleUpNotification when service is already in foregr…
Browse files Browse the repository at this point in the history
…ound

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.
  • Loading branch information
glennguy committed Oct 29, 2018
1 parent e3e97a1 commit 5076b67
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public class DownloadService extends Service {
private boolean downloadOngoing = false;
private float volume = 1.0f;
private long delayUpdateProgress = DEFAULT_DELAY_UPDATE_PROGRESS;
private boolean foregroundService = false;

private AudioEffectsController effectsController;
private RemoteControlState remoteState = LOCAL;
Expand Down Expand Up @@ -309,7 +310,7 @@ public boolean onError(MediaPlayer mediaPlayer, int what, int more) {
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
lifecycleSupport.onStart(intent);
if(Build.VERSION.SDK_INT >= 26) {
if(Build.VERSION.SDK_INT >= 26 && !this.isForeground()) {
Notifications.shutGoogleUpNotification(this);
}
return START_NOT_STICKY;
Expand Down Expand Up @@ -1063,6 +1064,14 @@ public synchronized boolean shouldFastForward() {
return size() == 1 || (currentPlaying != null && !currentPlaying.isSong());
}

public synchronized boolean isForeground() {
return this.foregroundService;
}

public synchronized void setIsForeground(boolean foreground) {
this.foregroundService = foreground;
}

public synchronized List<DownloadFile> getDownloads() {
List<DownloadFile> temp = new ArrayList<DownloadFile>();
temp.addAll(downloadList);
Expand Down
28 changes: 19 additions & 9 deletions app/src/main/java/github/daneren2005/dsub/util/Notifications.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ public static void showPlayingNotification(final Context context, final Download
handler.post(new Runnable() {
@Override
public void run() {
downloadService.stopForeground(true);
stopForeground(downloadService, true);
showDownloadingNotification(context, downloadService, handler, downloadService.getCurrentDownloading(), downloadService.getBackgroundDownloads().size());

try {
downloadService.startForeground(NOTIFICATION_ID_PLAYING, notification);
startForeground(downloadService, NOTIFICATION_ID_PLAYING, notification);
} catch(Exception e) {
Log.e(TAG, "Failed to start notifications after stopping foreground download");
}
Expand All @@ -130,15 +130,15 @@ public void run() {
public void run() {
if (playing) {
try {
downloadService.startForeground(NOTIFICATION_ID_PLAYING, notification);
startForeground(downloadService, NOTIFICATION_ID_PLAYING, notification);
} catch(Exception e) {
Log.e(TAG, "Failed to start notifications while playing");
}
} else {
playShowing = false;
persistentPlayingShowing = true;
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
downloadService.stopForeground(false);
stopForeground(downloadService, false);

try {
notificationManager.notify(NOTIFICATION_ID_PLAYING, notification);
Expand Down Expand Up @@ -334,7 +334,7 @@ public static void hidePlayingNotification(final Context context, final Download
handler.post(new Runnable() {
@Override
public void run() {
downloadService.stopForeground(true);
stopForeground(downloadService, true);

if(persistentPlayingShowing) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Expand Down Expand Up @@ -413,7 +413,7 @@ public static void showDownloadingNotification(final Context context, final Down
handler.post(new Runnable() {
@Override
public void run() {
downloadService.startForeground(NOTIFICATION_ID_DOWNLOADING, notification);
startForeground(downloadService, NOTIFICATION_ID_DOWNLOADING, notification);
}
});
}
Expand All @@ -429,7 +429,7 @@ public static void hideDownloadingNotification(final Context context, final Down
handler.post(new Runnable() {
@Override
public void run() {
downloadService.stopForeground(true);
stopForeground(downloadService, true);
}
});
}
Expand Down Expand Up @@ -461,8 +461,8 @@ public static void shutGoogleUpNotification(final DownloadService downloadServic
.setChannelId("downloading-channel");

final Notification notification = builder.build();
downloadService.startForeground(NOTIFICATION_ID_SHUT_GOOGLE_UP, notification);
downloadService.stopForeground(true);
startForeground(downloadService, NOTIFICATION_ID_SHUT_GOOGLE_UP, notification);
stopForeground(downloadService, true);
}

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

return syncChannel;
}

private static void startForeground(DownloadService downloadService, int notificationId, Notification notification) {
downloadService.startForeground(notificationId, notification);
downloadService.setIsForeground(true);
}

private static void stopForeground(DownloadService downloadService, boolean removeNotification) {
downloadService.stopForeground(removeNotification);
downloadService.setIsForeground(false);
}
}

0 comments on commit 5076b67

Please sign in to comment.