Skip to content

Commit

Permalink
Merge pull request #12192 from nextcloud/bugfix/add-missing-foregroun…
Browse files Browse the repository at this point in the history
…d-service-type

Fix Crash on Android 14 When Start Foreground Service
  • Loading branch information
tobiasKaminsky authored Nov 30, 2023
2 parents 0dedcda + 5c673e1 commit bb9a403
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
7 changes: 7 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />

<!-- Needed for Android 14 (API level 34) -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />

<!-- Some Chromebooks don't support touch. Although not essential,
it's a good idea to explicitly include this declaration. -->
Expand Down Expand Up @@ -378,15 +381,19 @@
android:exported="false" />
<service
android:name=".files.services.FileDownloader"
android:foregroundServiceType="dataSync"
android:exported="false" />
<service
android:name="com.nextcloud.client.files.downloader.FileTransferService"
android:foregroundServiceType="dataSync"
android:exported="false" />
<service
android:name=".files.services.FileUploader"
android:foregroundServiceType="dataSync"
android:exported="false" />
<service
android:name="com.nextcloud.client.media.PlayerService"
android:foregroundServiceType="mediaPlayback"
android:exported="false" />

<activity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package com.nextcloud.client.files.downloader
import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.pm.ServiceInfo
import android.os.Build
import android.os.IBinder
import com.nextcloud.client.account.User
import com.nextcloud.client.core.AsyncRunner
Expand Down Expand Up @@ -107,10 +109,18 @@ class FileTransferService : Service() {
}

if (!isRunning) {
startForeground(
AppNotificationManager.TRANSFER_NOTIFICATION_ID,
notificationsManager.buildDownloadServiceForegroundNotification()
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(
AppNotificationManager.TRANSFER_NOTIFICATION_ID,
notificationsManager.buildDownloadServiceForegroundNotification(),
ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
)
} else {
startForeground(
AppNotificationManager.TRANSFER_NOTIFICATION_ID,
notificationsManager.buildDownloadServiceForegroundNotification()
)
}
}

val request: Request = intent.getParcelableExtra(EXTRA_REQUEST)!!
Expand Down
15 changes: 13 additions & 2 deletions app/src/main/java/com/nextcloud/client/media/PlayerService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ package com.nextcloud.client.media
import android.app.PendingIntent
import android.app.Service
import android.content.Intent
import android.content.pm.ServiceInfo
import android.media.AudioManager
import android.os.Build
import android.os.Bundle
import android.os.IBinder
import android.widget.MediaController
Expand Down Expand Up @@ -167,11 +169,20 @@ class PlayerService : Service() {
notificationBuilder.setContentTitle(ticker)
notificationBuilder.setContentText(content)

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationBuilder.setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_MEDIA)
}

startForeground(R.string.media_notif_ticker, notificationBuilder.build())
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(
R.string.media_notif_ticker,
notificationBuilder.build(),
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
)
} else {
startForeground(R.string.media_notif_ticker, notificationBuilder.build())
}

isRunning = true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.pm.ServiceInfo;
import android.graphics.BitmapFactory;
import android.os.Binder;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
Expand Down Expand Up @@ -200,7 +202,11 @@ public void onDestroy() {
public int onStartCommand(Intent intent, int flags, int startId) {
Log_OC.d(TAG, "Starting command with id " + startId);

startForeground(FOREGROUND_SERVICE_ID, mNotification);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(FOREGROUND_SERVICE_ID, mNotification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC);
} else {
startForeground(FOREGROUND_SERVICE_ID, mNotification);
}

if (intent == null || !intent.hasExtra(EXTRA_USER) || !intent.hasExtra(EXTRA_FILE)) {
Log_OC.e(TAG, "Not enough information provided in intent");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ServiceInfo;
import android.graphics.BitmapFactory;
import android.os.Binder;
import android.os.Build;
Expand Down Expand Up @@ -305,7 +306,11 @@ public void onDestroy() {
public int onStartCommand(Intent intent, int flags, int startId) {
Log_OC.d(TAG, "Starting command with id " + startId);

startForeground(FOREGROUND_SERVICE_ID, mNotification);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(FOREGROUND_SERVICE_ID, mNotification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC);
} else {
startForeground(FOREGROUND_SERVICE_ID, mNotification);
}

if (intent == null) {
Log_OC.e(TAG, "Intent is null");
Expand Down Expand Up @@ -603,7 +608,7 @@ public void onAccountsUpdated(Account[] accounts) {

/**
* Core upload method: sends the file(s) to upload WARNING: legacy code, must be in sync with @{{@link
* FilesUploadWorker#upload(UploadFileOperation, User)}
* FilesUploadWorker upload(UploadFileOperation, User)}
*
* @param uploadKey Key to access the upload to perform, contained in mPendingUploads
*/
Expand Down

0 comments on commit bb9a403

Please sign in to comment.