55import static org .schabi .newpipe .player .notification .NotificationConstants .ACTION_CLOSE ;
66
77import android .annotation .SuppressLint ;
8+ import android .app .Notification ;
89import android .app .PendingIntent ;
10+ import android .content .Context ;
911import android .content .Intent ;
1012import android .content .pm .ServiceInfo ;
1113import android .graphics .Bitmap ;
2426import org .schabi .newpipe .R ;
2527import org .schabi .newpipe .player .Player ;
2628import org .schabi .newpipe .player .PlayerIntentType ;
29+ import org .schabi .newpipe .player .PlayerService ;
2730import org .schabi .newpipe .player .mediasession .MediaSessionPlayerUi ;
2831import org .schabi .newpipe .util .NavigationHelper ;
2932
@@ -90,12 +93,9 @@ private synchronized NotificationCompat.Builder createNotification() {
9093 Log .d (TAG , "createNotification()" );
9194 }
9295 notificationManager = NotificationManagerCompat .from (player .getContext ());
93- final NotificationCompat .Builder builder =
94- new NotificationCompat .Builder (player .getContext (),
95- player .getContext ().getString (R .string .notification_channel_id ));
96- final MediaStyle mediaStyle = new MediaStyle ();
9796
9897 // setup media style (compact notification slots and media session)
98+ final MediaStyle mediaStyle = new MediaStyle ();
9999 if (Build .VERSION .SDK_INT < Build .VERSION_CODES .TIRAMISU ) {
100100 // notification actions are ignored on Android 13+, and are replaced by code in
101101 // MediaSessionPlayerUi
@@ -108,18 +108,9 @@ private synchronized NotificationCompat.Builder createNotification() {
108108 .ifPresent (mediaStyle ::setMediaSession );
109109
110110 // setup notification builder
111- builder .setStyle (mediaStyle )
112- .setPriority (NotificationCompat .PRIORITY_HIGH )
113- .setVisibility (NotificationCompat .VISIBILITY_PUBLIC )
114- .setCategory (NotificationCompat .CATEGORY_TRANSPORT )
115- .setShowWhen (false )
116- .setSmallIcon (R .drawable .ic_newpipe_triangle_white )
117- .setColor (ContextCompat .getColor (player .getContext (),
118- R .color .dark_background_color ))
111+ final var builder = setupNotificationBuilder (player .getContext (), mediaStyle )
119112 .setColorized (player .getPrefs ().getBoolean (
120- player .getContext ().getString (R .string .notification_colorize_key ), true ))
121- .setDeleteIntent (PendingIntentCompat .getBroadcast (player .getContext (),
122- NOTIFICATION_ID , new Intent (ACTION_CLOSE ), FLAG_UPDATE_CURRENT , false ));
113+ player .getContext ().getString (R .string .notification_colorize_key ), true ));
123114
124115 // set the initial value for the video thumbnail, updatable with updateNotificationThumbnail
125116 setLargeIcon (builder );
@@ -168,17 +159,17 @@ public boolean shouldUpdateBufferingSlot() {
168159 && notificationBuilder .mActions .get (2 ).actionIntent != null );
169160 }
170161
162+ public static void startForegroundWithDummyNotification (final PlayerService service ) {
163+ final var builder = setupNotificationBuilder (service , new MediaStyle ());
164+ startForeground (service , builder .build ());
165+ }
166+
171167 public void createNotificationAndStartForeground () {
172168 if (notificationBuilder == null ) {
173169 notificationBuilder = createNotification ();
174170 }
175171 updateNotification ();
176-
177- // ServiceInfo constants are not used below Android Q, so 0 is set here
178- final int serviceType = Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q
179- ? ServiceInfo .FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK : 0 ;
180- ServiceCompat .startForeground (player .getService (), NOTIFICATION_ID ,
181- notificationBuilder .build (), serviceType );
172+ startForeground (player .getService (), notificationBuilder .build ());
182173 }
183174
184175 public void cancelNotificationAndStopForeground () {
@@ -192,6 +183,34 @@ public void cancelNotificationAndStopForeground() {
192183 }
193184
194185
186+ /////////////////////////////////////////////////////
187+ // STATIC FUNCTIONS IN COMMON BETWEEN DUMMY AND REAL NOTIFICATION
188+ /////////////////////////////////////////////////////
189+
190+ private static NotificationCompat .Builder setupNotificationBuilder (final Context context ,
191+ final MediaStyle style ) {
192+ return new NotificationCompat .Builder (context ,
193+ context .getString (R .string .notification_channel_id ))
194+ .setStyle (style )
195+ .setPriority (NotificationCompat .PRIORITY_HIGH )
196+ .setVisibility (NotificationCompat .VISIBILITY_PUBLIC )
197+ .setCategory (NotificationCompat .CATEGORY_TRANSPORT )
198+ .setShowWhen (false )
199+ .setSmallIcon (R .drawable .ic_newpipe_triangle_white )
200+ .setColor (ContextCompat .getColor (context , R .color .dark_background_color ))
201+ .setDeleteIntent (PendingIntentCompat .getBroadcast (context ,
202+ NOTIFICATION_ID , new Intent (ACTION_CLOSE ), FLAG_UPDATE_CURRENT , false ));
203+ }
204+
205+ private static void startForeground (final PlayerService service ,
206+ final Notification notification ) {
207+ // ServiceInfo constants are not used below Android Q, so 0 is set here
208+ final int serviceType = Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q
209+ ? ServiceInfo .FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK : 0 ;
210+ ServiceCompat .startForeground (service , NOTIFICATION_ID , notification , serviceType );
211+ }
212+
213+
195214 /////////////////////////////////////////////////////
196215 // ACTIONS
197216 /////////////////////////////////////////////////////
0 commit comments