From e6b34dd622d5086ebb353a9ca9dc1cbb13ab088f Mon Sep 17 00:00:00 2001 From: Thomas Date: Wed, 13 Dec 2023 18:04:15 +0100 Subject: [PATCH] Fix somme issues with worker and Android 14 --- app/src/main/AndroidManifest.xml | 5 ++++- .../android/mastodon/jobs/ComposeWorker.java | 15 ++++++++++++--- .../android/mastodon/jobs/FetchHomeWorker.java | 15 +++++++++++++-- .../mastodon/jobs/NotificationsWorker.java | 15 +++++++++++++-- .../mastodon/jobs/ScheduleBoostWorker.java | 15 +++++++++++++-- .../mastodon/jobs/ScheduleThreadWorker.java | 15 +++++++++++++-- .../peertube/worker/NotificationsWorker.java | 8 +++++++- 7 files changed, 75 insertions(+), 13 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9238be553..f9aba4bbd 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -579,6 +579,9 @@ android:foregroundServiceType="dataSync" android:exported="false" /> - + \ No newline at end of file diff --git a/app/src/main/java/app/fedilab/android/mastodon/jobs/ComposeWorker.java b/app/src/main/java/app/fedilab/android/mastodon/jobs/ComposeWorker.java index 920f952c0..2a80e3afc 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/jobs/ComposeWorker.java +++ b/app/src/main/java/app/fedilab/android/mastodon/jobs/ComposeWorker.java @@ -24,6 +24,7 @@ import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.ServiceInfo; import android.graphics.BitmapFactory; import android.os.Build; import android.os.Bundle; @@ -493,7 +494,12 @@ public class ComposeWorker extends Worker { .setOngoing(true) .setDefaults(NotificationCompat.DEFAULT_ALL) .setPriority(Notification.PRIORITY_DEFAULT); - return Futures.immediateFuture(new ForegroundInfo(NOTIFICATION_INT_CHANNEL_ID, notificationBuilder.build())); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + return Futures.immediateFuture(new ForegroundInfo(NOTIFICATION_INT_CHANNEL_ID, notificationBuilder.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC)); + } else { + return Futures.immediateFuture(new ForegroundInfo(NOTIFICATION_INT_CHANNEL_ID, notificationBuilder.build())); + } } @NonNull @@ -515,8 +521,11 @@ public class ComposeWorker extends Worker { .setSilent(true) .setDefaults(NotificationCompat.DEFAULT_ALL) .setPriority(Notification.PRIORITY_HIGH); - - return new ForegroundInfo(NOTIFICATION_INT_CHANNEL_ID, notificationBuilder.build()); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + return new ForegroundInfo(NOTIFICATION_INT_CHANNEL_ID, notificationBuilder.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC); + } else { + return new ForegroundInfo(NOTIFICATION_INT_CHANNEL_ID, notificationBuilder.build()); + } } @RequiresApi(Build.VERSION_CODES.O) diff --git a/app/src/main/java/app/fedilab/android/mastodon/jobs/FetchHomeWorker.java b/app/src/main/java/app/fedilab/android/mastodon/jobs/FetchHomeWorker.java index 2848421b7..5a4099fe0 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/jobs/FetchHomeWorker.java +++ b/app/src/main/java/app/fedilab/android/mastodon/jobs/FetchHomeWorker.java @@ -19,6 +19,7 @@ import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.Context; import android.content.SharedPreferences; +import android.content.pm.ServiceInfo; import android.graphics.BitmapFactory; import android.os.Build; @@ -103,7 +104,12 @@ public class FetchHomeWorker extends Worker { .setContentText(getApplicationContext().getString(R.string.fetch_notifications)) .setDefaults(NotificationCompat.DEFAULT_ALL) .setPriority(Notification.PRIORITY_DEFAULT); - return Futures.immediateFuture(new ForegroundInfo(FETCH_HOME_CHANNEL_ID, notificationBuilder.build())); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + return Futures.immediateFuture(new ForegroundInfo(FETCH_HOME_CHANNEL_ID, notificationBuilder.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC)); + } else { + return Futures.immediateFuture(new ForegroundInfo(FETCH_HOME_CHANNEL_ID, notificationBuilder.build())); + } } @NonNull @@ -126,7 +132,12 @@ public class FetchHomeWorker extends Worker { .setDefaults(NotificationCompat.DEFAULT_ALL) .setSilent(true) .setPriority(Notification.PRIORITY_LOW); - return new ForegroundInfo(FETCH_HOME_CHANNEL_ID, notificationBuilder.build()); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + return new ForegroundInfo(FETCH_HOME_CHANNEL_ID, notificationBuilder.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC); + } else { + return new ForegroundInfo(FETCH_HOME_CHANNEL_ID, notificationBuilder.build()); + } } @NonNull diff --git a/app/src/main/java/app/fedilab/android/mastodon/jobs/NotificationsWorker.java b/app/src/main/java/app/fedilab/android/mastodon/jobs/NotificationsWorker.java index f708c4391..7b828eb6a 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/jobs/NotificationsWorker.java +++ b/app/src/main/java/app/fedilab/android/mastodon/jobs/NotificationsWorker.java @@ -18,6 +18,7 @@ import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.Context; +import android.content.pm.ServiceInfo; import android.graphics.BitmapFactory; import android.os.Build; @@ -74,7 +75,12 @@ public class NotificationsWorker extends Worker { .setContentText(getApplicationContext().getString(R.string.fetch_notifications)) .setDefaults(NotificationCompat.DEFAULT_ALL) .setPriority(Notification.PRIORITY_DEFAULT); - return Futures.immediateFuture(new ForegroundInfo(FETCH_NOTIFICATION_CHANNEL_ID, notificationBuilder.build())); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + return Futures.immediateFuture(new ForegroundInfo(FETCH_NOTIFICATION_CHANNEL_ID, notificationBuilder.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC)); + } else { + return Futures.immediateFuture(new ForegroundInfo(FETCH_NOTIFICATION_CHANNEL_ID, notificationBuilder.build())); + } } @NonNull @@ -97,7 +103,12 @@ public class NotificationsWorker extends Worker { .setDefaults(NotificationCompat.DEFAULT_ALL) .setSilent(true) .setPriority(Notification.PRIORITY_LOW); - return new ForegroundInfo(FETCH_NOTIFICATION_CHANNEL_ID, notificationBuilder.build()); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + return new ForegroundInfo(FETCH_NOTIFICATION_CHANNEL_ID, notificationBuilder.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC); + } else { + return new ForegroundInfo(FETCH_NOTIFICATION_CHANNEL_ID, notificationBuilder.build()); + } } @NonNull diff --git a/app/src/main/java/app/fedilab/android/mastodon/jobs/ScheduleBoostWorker.java b/app/src/main/java/app/fedilab/android/mastodon/jobs/ScheduleBoostWorker.java index add0a8b40..04fe8dbac 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/jobs/ScheduleBoostWorker.java +++ b/app/src/main/java/app/fedilab/android/mastodon/jobs/ScheduleBoostWorker.java @@ -18,6 +18,7 @@ import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.Context; +import android.content.pm.ServiceInfo; import android.graphics.BitmapFactory; import android.os.Build; @@ -77,7 +78,12 @@ public class ScheduleBoostWorker extends Worker { .setContentText(getApplicationContext().getString(R.string.schedule_boost)) .setDefaults(NotificationCompat.DEFAULT_ALL) .setPriority(Notification.PRIORITY_DEFAULT); - return Futures.immediateFuture(new ForegroundInfo(NOTIFICATION_INT_CHANNEL_ID, notificationBuilder.build())); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + return Futures.immediateFuture(new ForegroundInfo(NOTIFICATION_INT_CHANNEL_ID, notificationBuilder.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC)); + } else { + return Futures.immediateFuture(new ForegroundInfo(NOTIFICATION_INT_CHANNEL_ID, notificationBuilder.build())); + } } @NonNull @@ -97,7 +103,12 @@ public class ScheduleBoostWorker extends Worker { .setContentText(getApplicationContext().getString(R.string.schedule_boost)) .setDefaults(NotificationCompat.DEFAULT_ALL) .setPriority(Notification.PRIORITY_DEFAULT); - return new ForegroundInfo(NOTIFICATION_INT_CHANNEL_ID, notificationBuilder.build()); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + return new ForegroundInfo(NOTIFICATION_INT_CHANNEL_ID, notificationBuilder.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC); + } else { + return new ForegroundInfo(NOTIFICATION_INT_CHANNEL_ID, notificationBuilder.build()); + } } diff --git a/app/src/main/java/app/fedilab/android/mastodon/jobs/ScheduleThreadWorker.java b/app/src/main/java/app/fedilab/android/mastodon/jobs/ScheduleThreadWorker.java index b5f4dcc89..4a48a8d27 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/jobs/ScheduleThreadWorker.java +++ b/app/src/main/java/app/fedilab/android/mastodon/jobs/ScheduleThreadWorker.java @@ -20,6 +20,7 @@ import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.Context; +import android.content.pm.ServiceInfo; import android.graphics.BitmapFactory; import android.os.Build; @@ -66,7 +67,12 @@ public class ScheduleThreadWorker extends Worker { .setContentText(getApplicationContext().getString(R.string.scheduled_toots)) .setDefaults(NotificationCompat.DEFAULT_ALL) .setPriority(Notification.PRIORITY_DEFAULT); - return Futures.immediateFuture(new ForegroundInfo(NOTIFICATION_INT_CHANNEL_ID, notificationBuilder.build())); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + return Futures.immediateFuture(new ForegroundInfo(NOTIFICATION_INT_CHANNEL_ID, notificationBuilder.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC)); + } else { + return Futures.immediateFuture(new ForegroundInfo(NOTIFICATION_INT_CHANNEL_ID, notificationBuilder.build())); + } } @NonNull @@ -86,7 +92,12 @@ public class ScheduleThreadWorker extends Worker { .setContentText(getApplicationContext().getString(R.string.scheduled_toots)) .setDefaults(NotificationCompat.DEFAULT_ALL) .setPriority(Notification.PRIORITY_DEFAULT); - return new ForegroundInfo(NOTIFICATION_INT_CHANNEL_ID, notificationBuilder.build()); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + return new ForegroundInfo(NOTIFICATION_INT_CHANNEL_ID, notificationBuilder.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC); + } else { + return new ForegroundInfo(NOTIFICATION_INT_CHANNEL_ID, notificationBuilder.build()); + } } @NonNull diff --git a/app/src/main/java/app/fedilab/android/peertube/worker/NotificationsWorker.java b/app/src/main/java/app/fedilab/android/peertube/worker/NotificationsWorker.java index 7aaea5edb..9cd1aa68e 100644 --- a/app/src/main/java/app/fedilab/android/peertube/worker/NotificationsWorker.java +++ b/app/src/main/java/app/fedilab/android/peertube/worker/NotificationsWorker.java @@ -22,6 +22,7 @@ import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.ServiceInfo; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Build; @@ -330,6 +331,11 @@ public class NotificationsWorker extends Worker { .setSound(null) .setAutoCancel(true) .setOngoing(true); - return new ForegroundInfo(pendingNotificationID, notificationBuilder.build()); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + return new ForegroundInfo(pendingNotificationID, notificationBuilder.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC); + } else { + return new ForegroundInfo(pendingNotificationID, notificationBuilder.build()); + } } }