@@ -114,11 +114,11 @@ public static void showPlayingNotification(final Context context, final Download
114
114
handler .post (new Runnable () {
115
115
@ Override
116
116
public void run () {
117
- downloadService . stopForeground (true );
117
+ stopForeground (downloadService , true );
118
118
showDownloadingNotification (context , downloadService , handler , downloadService .getCurrentDownloading (), downloadService .getBackgroundDownloads ().size ());
119
119
120
120
try {
121
- downloadService . startForeground (NOTIFICATION_ID_PLAYING , notification );
121
+ startForeground (downloadService , NOTIFICATION_ID_PLAYING , notification );
122
122
} catch (Exception e ) {
123
123
Log .e (TAG , "Failed to start notifications after stopping foreground download" );
124
124
}
@@ -130,15 +130,15 @@ public void run() {
130
130
public void run () {
131
131
if (playing ) {
132
132
try {
133
- downloadService . startForeground (NOTIFICATION_ID_PLAYING , notification );
133
+ startForeground (downloadService , NOTIFICATION_ID_PLAYING , notification );
134
134
} catch (Exception e ) {
135
135
Log .e (TAG , "Failed to start notifications while playing" );
136
136
}
137
137
} else {
138
138
playShowing = false ;
139
139
persistentPlayingShowing = true ;
140
140
NotificationManager notificationManager = (NotificationManager ) context .getSystemService (Context .NOTIFICATION_SERVICE );
141
- downloadService . stopForeground (false );
141
+ stopForeground (downloadService , false );
142
142
143
143
try {
144
144
notificationManager .notify (NOTIFICATION_ID_PLAYING , notification );
@@ -334,7 +334,7 @@ public static void hidePlayingNotification(final Context context, final Download
334
334
handler .post (new Runnable () {
335
335
@ Override
336
336
public void run () {
337
- downloadService . stopForeground (true );
337
+ stopForeground (downloadService , true );
338
338
339
339
if (persistentPlayingShowing ) {
340
340
NotificationManager notificationManager = (NotificationManager ) context .getSystemService (Context .NOTIFICATION_SERVICE );
@@ -413,7 +413,7 @@ public static void showDownloadingNotification(final Context context, final Down
413
413
handler .post (new Runnable () {
414
414
@ Override
415
415
public void run () {
416
- downloadService . startForeground (NOTIFICATION_ID_DOWNLOADING , notification );
416
+ startForeground (downloadService , NOTIFICATION_ID_DOWNLOADING , notification );
417
417
}
418
418
});
419
419
}
@@ -429,7 +429,7 @@ public static void hideDownloadingNotification(final Context context, final Down
429
429
handler .post (new Runnable () {
430
430
@ Override
431
431
public void run () {
432
- downloadService . stopForeground (true );
432
+ stopForeground (downloadService , true );
433
433
}
434
434
});
435
435
}
@@ -461,8 +461,8 @@ public static void shutGoogleUpNotification(final DownloadService downloadServic
461
461
.setChannelId ("downloading-channel" );
462
462
463
463
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 );
466
466
}
467
467
468
468
public static void showSyncNotification (final Context context , int stringId , String extra ) {
@@ -537,4 +537,14 @@ private static NotificationChannel getSyncNotificationChannel(Context context) {
537
537
538
538
return syncChannel ;
539
539
}
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
+ }
540
550
}
0 commit comments