Skip to content

Commit cf0ccf1

Browse files
toniheiicbaker
authored andcommitted
Declare foreground service type for DownloadService
This ensures the DownloadService stays functional on Android 14 where defining this type is required. On Android 14 and above, the app also needs to define the DATA_SYNC permission, which is added to the demo app as well. In the future, this service type will no longer be supported and DownloadService needs to be rewritten with another background scheduling framework. Issue: #11239 PiperOrigin-RevId: 548994842
1 parent aa2313d commit cf0ccf1

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

demos/main/src/main/AndroidManifest.xml

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
2424
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
2525
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
26+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"/>
2627
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
2728

2829
<uses-feature android:name="android.software.leanback" android:required="false"/>
@@ -94,7 +95,8 @@
9495
</activity>
9596

9697
<service android:name=".DemoDownloadService"
97-
android:exported="false">
98+
android:exported="false"
99+
android:foregroundServiceType="dataSync">
98100
<intent-filter>
99101
<action android:name="com.google.android.exoplayer.downloadService.action.RESTART"/>
100102
<category android:name="android.intent.category.DEFAULT"/>

library/common/src/main/java/com/google/android/exoplayer2/util/Util.java

+56
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import android.Manifest.permission;
3737
import android.annotation.SuppressLint;
3838
import android.app.Activity;
39+
import android.app.Notification;
40+
import android.app.Service;
3941
import android.app.UiModeManager;
4042
import android.content.BroadcastReceiver;
4143
import android.content.ComponentName;
@@ -296,6 +298,35 @@ public static ComponentName startForegroundService(Context context, Intent inten
296298
}
297299
}
298300

301+
/**
302+
* Sets the notification required for a foreground service.
303+
*
304+
* @param service The foreground {@link Service}.
305+
* @param notificationId The notification id.
306+
* @param notification The {@link Notification}.
307+
* @param foregroundServiceType The foreground service type defined in {@link
308+
* android.content.pm.ServiceInfo}.
309+
* @param foregroundServiceManifestType The required foreground service type string for the {@code
310+
* <service>} element in the manifest.
311+
*/
312+
public static void setForegroundServiceNotification(
313+
Service service,
314+
int notificationId,
315+
Notification notification,
316+
int foregroundServiceType,
317+
String foregroundServiceManifestType) {
318+
if (Util.SDK_INT >= 29) {
319+
Api29.startForeground(
320+
service,
321+
notificationId,
322+
notification,
323+
foregroundServiceType,
324+
foregroundServiceManifestType);
325+
} else {
326+
service.startForeground(notificationId, notification);
327+
}
328+
}
329+
299330
/**
300331
* Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE}
301332
* permission read the specified {@link Uri}s, requesting the permission if necessary.
@@ -3234,4 +3265,29 @@ public static Drawable getDrawable(Context context, Resources resources, @Drawab
32343265
return resources.getDrawable(res, context.getTheme());
32353266
}
32363267
}
3268+
3269+
@RequiresApi(29)
3270+
private static class Api29 {
3271+
3272+
@DoNotInline
3273+
public static void startForeground(
3274+
Service mediaSessionService,
3275+
int notificationId,
3276+
Notification notification,
3277+
int foregroundServiceType,
3278+
String foregroundServiceManifestType) {
3279+
try {
3280+
// startForeground() will throw if the service's foregroundServiceType is not defined.
3281+
mediaSessionService.startForeground(notificationId, notification, foregroundServiceType);
3282+
} catch (RuntimeException e) {
3283+
Log.e(
3284+
TAG,
3285+
"The service must be declared with a foregroundServiceType that includes "
3286+
+ foregroundServiceManifestType);
3287+
throw e;
3288+
}
3289+
}
3290+
3291+
private Api29() {}
3292+
}
32373293
}

library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717

1818
import static com.google.android.exoplayer2.offline.Download.STOP_REASON_NONE;
1919

20+
import android.annotation.SuppressLint;
2021
import android.app.Notification;
2122
import android.app.NotificationManager;
2223
import android.app.Service;
2324
import android.content.Context;
2425
import android.content.Intent;
26+
import android.content.pm.ServiceInfo;
2527
import android.os.Handler;
2628
import android.os.IBinder;
2729
import android.os.Looper;
@@ -915,14 +917,20 @@ public void invalidate() {
915917
}
916918
}
917919

920+
@SuppressLint("InlinedApi") // Using compile time constant FOREGROUND_SERVICE_TYPE_DATA_SYNC
918921
private void update() {
919922
DownloadManager downloadManager =
920923
Assertions.checkNotNull(downloadManagerHelper).downloadManager;
921924
List<Download> downloads = downloadManager.getCurrentDownloads();
922925
@RequirementFlags int notMetRequirements = downloadManager.getNotMetRequirements();
923926
Notification notification = getForegroundNotification(downloads, notMetRequirements);
924927
if (!notificationDisplayed) {
925-
startForeground(notificationId, notification);
928+
Util.setForegroundServiceNotification(
929+
/* service= */ DownloadService.this,
930+
notificationId,
931+
notification,
932+
ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC,
933+
"dataSync");
926934
notificationDisplayed = true;
927935
} else {
928936
// Update the notification via NotificationManager rather than by repeatedly calling

0 commit comments

Comments
 (0)