Skip to content

Commit

Permalink
Fix foreground service on Android 14
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 authored and AndyScherzinger committed Nov 27, 2023
1 parent 824772c commit a87678a
Show file tree
Hide file tree
Showing 5 changed files with 45 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 All @@ -33,6 +35,7 @@ import com.nextcloud.client.network.ConnectivityService
import com.nextcloud.client.notifications.AppNotificationManager
import com.owncloud.android.datamodel.FileDataStorageManager
import com.owncloud.android.datamodel.UploadsStorageManager
import com.owncloud.android.files.services.FileDownloader
import dagger.android.AndroidInjection
import javax.inject.Inject
import javax.inject.Named
Expand Down Expand Up @@ -107,10 +110,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
11 changes: 9 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,16 @@ 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 @@ -304,7 +305,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 @@ -602,7 +607,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 a87678a

Please sign in to comment.