Fix somme issues with worker and Android 14

This commit is contained in:
Thomas 2023-12-13 18:04:15 +01:00
parent 5cca26e0c4
commit e6b34dd622
7 changed files with 75 additions and 13 deletions

View file

@ -579,6 +579,9 @@
android:foregroundServiceType="dataSync" android:foregroundServiceType="dataSync"
android:exported="false" /> android:exported="false" />
<service
android:name="androidx.work.impl.foreground.SystemForegroundService"
android:foregroundServiceType="dataSync"
tools:node="merge" />
</application> </application>
</manifest> </manifest>

View file

@ -24,6 +24,7 @@ import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.ServiceInfo;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
@ -493,8 +494,13 @@ public class ComposeWorker extends Worker {
.setOngoing(true) .setOngoing(true)
.setDefaults(NotificationCompat.DEFAULT_ALL) .setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_DEFAULT); .setPriority(Notification.PRIORITY_DEFAULT);
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())); return Futures.immediateFuture(new ForegroundInfo(NOTIFICATION_INT_CHANNEL_ID, notificationBuilder.build()));
} }
}
@NonNull @NonNull
private ForegroundInfo createForegroundInfo() { private ForegroundInfo createForegroundInfo() {
@ -515,9 +521,12 @@ public class ComposeWorker extends Worker {
.setSilent(true) .setSilent(true)
.setDefaults(NotificationCompat.DEFAULT_ALL) .setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_HIGH); .setPriority(Notification.PRIORITY_HIGH);
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()); return new ForegroundInfo(NOTIFICATION_INT_CHANNEL_ID, notificationBuilder.build());
} }
}
@RequiresApi(Build.VERSION_CODES.O) @RequiresApi(Build.VERSION_CODES.O)
private void createChannel() { private void createChannel() {

View file

@ -19,6 +19,7 @@ import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.ServiceInfo;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.os.Build; import android.os.Build;
@ -103,8 +104,13 @@ public class FetchHomeWorker extends Worker {
.setContentText(getApplicationContext().getString(R.string.fetch_notifications)) .setContentText(getApplicationContext().getString(R.string.fetch_notifications))
.setDefaults(NotificationCompat.DEFAULT_ALL) .setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_DEFAULT); .setPriority(Notification.PRIORITY_DEFAULT);
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())); return Futures.immediateFuture(new ForegroundInfo(FETCH_HOME_CHANNEL_ID, notificationBuilder.build()));
} }
}
@NonNull @NonNull
private ForegroundInfo createForegroundInfo() { private ForegroundInfo createForegroundInfo() {
@ -126,8 +132,13 @@ public class FetchHomeWorker extends Worker {
.setDefaults(NotificationCompat.DEFAULT_ALL) .setDefaults(NotificationCompat.DEFAULT_ALL)
.setSilent(true) .setSilent(true)
.setPriority(Notification.PRIORITY_LOW); .setPriority(Notification.PRIORITY_LOW);
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()); return new ForegroundInfo(FETCH_HOME_CHANNEL_ID, notificationBuilder.build());
} }
}
@NonNull @NonNull
@Override @Override

View file

@ -18,6 +18,7 @@ import android.app.Notification;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.content.pm.ServiceInfo;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.os.Build; import android.os.Build;
@ -74,8 +75,13 @@ public class NotificationsWorker extends Worker {
.setContentText(getApplicationContext().getString(R.string.fetch_notifications)) .setContentText(getApplicationContext().getString(R.string.fetch_notifications))
.setDefaults(NotificationCompat.DEFAULT_ALL) .setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_DEFAULT); .setPriority(Notification.PRIORITY_DEFAULT);
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())); return Futures.immediateFuture(new ForegroundInfo(FETCH_NOTIFICATION_CHANNEL_ID, notificationBuilder.build()));
} }
}
@NonNull @NonNull
private ForegroundInfo createForegroundInfo() { private ForegroundInfo createForegroundInfo() {
@ -97,8 +103,13 @@ public class NotificationsWorker extends Worker {
.setDefaults(NotificationCompat.DEFAULT_ALL) .setDefaults(NotificationCompat.DEFAULT_ALL)
.setSilent(true) .setSilent(true)
.setPriority(Notification.PRIORITY_LOW); .setPriority(Notification.PRIORITY_LOW);
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()); return new ForegroundInfo(FETCH_NOTIFICATION_CHANNEL_ID, notificationBuilder.build());
} }
}
@NonNull @NonNull
@Override @Override

View file

@ -18,6 +18,7 @@ import android.app.Notification;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.content.pm.ServiceInfo;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.os.Build; import android.os.Build;
@ -77,8 +78,13 @@ public class ScheduleBoostWorker extends Worker {
.setContentText(getApplicationContext().getString(R.string.schedule_boost)) .setContentText(getApplicationContext().getString(R.string.schedule_boost))
.setDefaults(NotificationCompat.DEFAULT_ALL) .setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_DEFAULT); .setPriority(Notification.PRIORITY_DEFAULT);
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())); return Futures.immediateFuture(new ForegroundInfo(NOTIFICATION_INT_CHANNEL_ID, notificationBuilder.build()));
} }
}
@NonNull @NonNull
private ForegroundInfo createForegroundInfo() { private ForegroundInfo createForegroundInfo() {
@ -97,8 +103,13 @@ public class ScheduleBoostWorker extends Worker {
.setContentText(getApplicationContext().getString(R.string.schedule_boost)) .setContentText(getApplicationContext().getString(R.string.schedule_boost))
.setDefaults(NotificationCompat.DEFAULT_ALL) .setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_DEFAULT); .setPriority(Notification.PRIORITY_DEFAULT);
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()); return new ForegroundInfo(NOTIFICATION_INT_CHANNEL_ID, notificationBuilder.build());
} }
}
private OkHttpClient getOkHttpClient() { private OkHttpClient getOkHttpClient() {

View file

@ -20,6 +20,7 @@ import android.app.Notification;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.content.pm.ServiceInfo;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.os.Build; import android.os.Build;
@ -66,8 +67,13 @@ public class ScheduleThreadWorker extends Worker {
.setContentText(getApplicationContext().getString(R.string.scheduled_toots)) .setContentText(getApplicationContext().getString(R.string.scheduled_toots))
.setDefaults(NotificationCompat.DEFAULT_ALL) .setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_DEFAULT); .setPriority(Notification.PRIORITY_DEFAULT);
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())); return Futures.immediateFuture(new ForegroundInfo(NOTIFICATION_INT_CHANNEL_ID, notificationBuilder.build()));
} }
}
@NonNull @NonNull
private ForegroundInfo createForegroundInfo() { private ForegroundInfo createForegroundInfo() {
@ -86,8 +92,13 @@ public class ScheduleThreadWorker extends Worker {
.setContentText(getApplicationContext().getString(R.string.scheduled_toots)) .setContentText(getApplicationContext().getString(R.string.scheduled_toots))
.setDefaults(NotificationCompat.DEFAULT_ALL) .setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_DEFAULT); .setPriority(Notification.PRIORITY_DEFAULT);
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()); return new ForegroundInfo(NOTIFICATION_INT_CHANNEL_ID, notificationBuilder.build());
} }
}
@NonNull @NonNull
@Override @Override

View file

@ -22,6 +22,7 @@ import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.ServiceInfo;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.os.Build; import android.os.Build;
@ -330,6 +331,11 @@ public class NotificationsWorker extends Worker {
.setSound(null) .setSound(null)
.setAutoCancel(true) .setAutoCancel(true)
.setOngoing(true); .setOngoing(true);
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()); return new ForegroundInfo(pendingNotificationID, notificationBuilder.build());
} }
} }
}