fix: android background backups (#21795)

* upload using dart client

* add connectivity api

* respect backup network setting

* comment as to why we need to wait for setForegroundAsync call

* log assets skipped due to network constraint

* dynamic spawning -> false

---------

Co-authored-by: shenlong-tanwen <139912620+shalong-tanwen@users.noreply.github.com>
Co-authored-by: Alex <alex.tran1502@gmail.com>
This commit is contained in:
shenlong 2025-09-11 22:31:15 +05:30 committed by GitHub
parent 39c1ebf698
commit 722a464e23
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
22 changed files with 755 additions and 27 deletions

View file

@ -5,6 +5,8 @@ import android.os.Build
import android.os.ext.SdkExtensions
import app.alextran.immich.background.BackgroundWorkerApiImpl
import app.alextran.immich.background.BackgroundWorkerFgHostApi
import app.alextran.immich.connectivity.ConnectivityApi
import app.alextran.immich.connectivity.ConnectivityApiImpl
import app.alextran.immich.images.ThumbnailApi
import app.alextran.immich.images.ThumbnailsImpl
import app.alextran.immich.sync.NativeSyncApi
@ -34,6 +36,7 @@ class MainActivity : FlutterFragmentActivity() {
NativeSyncApi.setUp(messenger, nativeSyncApiImpl)
ThumbnailApi.setUp(messenger, ThumbnailsImpl(ctx))
BackgroundWorkerFgHostApi.setUp(messenger, BackgroundWorkerApiImpl(ctx))
ConnectivityApi.setUp(messenger, ConnectivityApiImpl(ctx))
}
}
}

View file

@ -111,6 +111,7 @@ interface BackgroundWorkerFgHostApi {
/** Generated interface from Pigeon that represents a handler of messages from Flutter. */
interface BackgroundWorkerBgHostApi {
fun onInitialized()
fun showNotification(title: String, content: String)
fun close()
companion object {
@ -138,6 +139,25 @@ interface BackgroundWorkerBgHostApi {
channel.setMessageHandler(null)
}
}
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.immich_mobile.BackgroundWorkerBgHostApi.showNotification$separatedMessageChannelSuffix", codec)
if (api != null) {
channel.setMessageHandler { message, reply ->
val args = message as List<Any?>
val titleArg = args[0] as String
val contentArg = args[1] as String
val wrapped: List<Any?> = try {
api.showNotification(titleArg, contentArg)
listOf(null)
} catch (exception: Throwable) {
BackgroundWorkerPigeonUtils.wrapError(exception)
}
reply.reply(wrapped)
}
} else {
channel.setMessageHandler(null)
}
}
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.immich_mobile.BackgroundWorkerBgHostApi.close$separatedMessageChannelSuffix", codec)
if (api != null) {

View file

@ -1,18 +1,27 @@
package app.alextran.immich.background
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.os.PowerManager
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.work.ForegroundInfo
import androidx.work.ListenableWorker
import androidx.work.WorkerParameters
import app.alextran.immich.MainActivity
import app.alextran.immich.R
import com.google.common.util.concurrent.ListenableFuture
import com.google.common.util.concurrent.SettableFuture
import io.flutter.FlutterInjector
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.dart.DartExecutor
import io.flutter.embedding.engine.loader.FlutterLoader
import java.util.concurrent.TimeUnit
private const val TAG = "BackgroundWorker"
@ -40,15 +49,32 @@ class BackgroundWorker(context: Context, params: WorkerParameters) :
/// Flag to track whether the background task has completed to prevent duplicate completions
private var isComplete = false
private val notificationManager =
ctx.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
private var foregroundFuture: ListenableFuture<Void>? = null
init {
if (!loader.initialized()) {
loader.startInitialization(ctx)
}
}
companion object {
private const val NOTIFICATION_CHANNEL_ID = "immich::background_worker::notif"
private const val NOTIFICATION_ID = 100
}
override fun startWork(): ListenableFuture<Result> {
Log.i(TAG, "Starting background upload worker")
val notificationChannel = NotificationChannel(
NOTIFICATION_CHANNEL_ID,
NOTIFICATION_CHANNEL_ID,
NotificationManager.IMPORTANCE_LOW
)
notificationManager.createNotificationChannel(notificationChannel)
loader.ensureInitializationCompleteAsync(ctx, null, Handler(Looper.getMainLooper())) {
engine = FlutterEngine(ctx)
@ -82,6 +108,34 @@ class BackgroundWorker(context: Context, params: WorkerParameters) :
flutterApi?.onAndroidUpload { handleHostResult(it) }
}
// TODO: Move this to a separate NotificationManager class
override fun showNotification(title: String, content: String) {
val notification = NotificationCompat.Builder(applicationContext, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setOnlyAlertOnce(true)
.setOngoing(true)
.setTicker(title)
.setContentTitle(title)
.setContentText(content)
.build()
if (isIgnoringBatteryOptimizations()) {
foregroundFuture = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
setForegroundAsync(
ForegroundInfo(
NOTIFICATION_ID,
notification,
FOREGROUND_SERVICE_TYPE_DATA_SYNC
)
)
} else {
setForegroundAsync(ForegroundInfo(NOTIFICATION_ID, notification))
}
} else {
notificationManager.notify(NOTIFICATION_ID, notification)
}
}
override fun close() {
if (isComplete) {
return
@ -95,6 +149,8 @@ class BackgroundWorker(context: Context, params: WorkerParameters) :
}
}
waitForForegroundPromotion()
Handler(Looper.getMainLooper()).postDelayed({
complete(Result.failure())
}, 5000)
@ -135,6 +191,32 @@ class BackgroundWorker(context: Context, params: WorkerParameters) :
engine?.destroy()
engine = null
flutterApi = null
notificationManager.cancel(NOTIFICATION_ID)
waitForForegroundPromotion()
completionHandler.set(success)
}
/**
* Returns `true` if the app is ignoring battery optimizations
*/
private fun isIgnoringBatteryOptimizations(): Boolean {
val powerManager = ctx.getSystemService(Context.POWER_SERVICE) as PowerManager
return powerManager.isIgnoringBatteryOptimizations(ctx.packageName)
}
/**
* Calls to setForegroundAsync() that do not complete before completion of a ListenableWorker will signal an IllegalStateException
* https://android-review.googlesource.com/c/platform/frameworks/support/+/1262743
* Wait for a short period of time for the foreground promotion to complete before completing the worker
*/
private fun waitForForegroundPromotion() {
val foregroundFuture = this.foregroundFuture
if (foregroundFuture != null && !foregroundFuture.isCancelled && !foregroundFuture.isDone) {
try {
foregroundFuture.get(500, TimeUnit.MILLISECONDS)
} catch (e: Exception) {
// ignored, there is nothing to be done
}
}
}
}

View file

@ -0,0 +1,116 @@
// Autogenerated from Pigeon (v26.0.0), do not edit directly.
// See also: https://pub.dev/packages/pigeon
@file:Suppress("UNCHECKED_CAST", "ArrayInDataClass")
package app.alextran.immich.connectivity
import android.util.Log
import io.flutter.plugin.common.BasicMessageChannel
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.MessageCodec
import io.flutter.plugin.common.StandardMethodCodec
import io.flutter.plugin.common.StandardMessageCodec
import java.io.ByteArrayOutputStream
import java.nio.ByteBuffer
private object ConnectivityPigeonUtils {
fun wrapResult(result: Any?): List<Any?> {
return listOf(result)
}
fun wrapError(exception: Throwable): List<Any?> {
return if (exception is FlutterError) {
listOf(
exception.code,
exception.message,
exception.details
)
} else {
listOf(
exception.javaClass.simpleName,
exception.toString(),
"Cause: " + exception.cause + ", Stacktrace: " + Log.getStackTraceString(exception)
)
}
}
}
/**
* Error class for passing custom error details to Flutter via a thrown PlatformException.
* @property code The error code.
* @property message The error message.
* @property details The error details. Must be a datatype supported by the api codec.
*/
class FlutterError (
val code: String,
override val message: String? = null,
val details: Any? = null
) : Throwable()
enum class NetworkCapability(val raw: Int) {
CELLULAR(0),
WIFI(1),
VPN(2),
UNMETERED(3);
companion object {
fun ofRaw(raw: Int): NetworkCapability? {
return values().firstOrNull { it.raw == raw }
}
}
}
private open class ConnectivityPigeonCodec : StandardMessageCodec() {
override fun readValueOfType(type: Byte, buffer: ByteBuffer): Any? {
return when (type) {
129.toByte() -> {
return (readValue(buffer) as Long?)?.let {
NetworkCapability.ofRaw(it.toInt())
}
}
else -> super.readValueOfType(type, buffer)
}
}
override fun writeValue(stream: ByteArrayOutputStream, value: Any?) {
when (value) {
is NetworkCapability -> {
stream.write(129)
writeValue(stream, value.raw)
}
else -> super.writeValue(stream, value)
}
}
}
/** Generated interface from Pigeon that represents a handler of messages from Flutter. */
interface ConnectivityApi {
fun getCapabilities(): List<NetworkCapability>
companion object {
/** The codec used by ConnectivityApi. */
val codec: MessageCodec<Any?> by lazy {
ConnectivityPigeonCodec()
}
/** Sets up an instance of `ConnectivityApi` to handle messages through the `binaryMessenger`. */
@JvmOverloads
fun setUp(binaryMessenger: BinaryMessenger, api: ConnectivityApi?, messageChannelSuffix: String = "") {
val separatedMessageChannelSuffix = if (messageChannelSuffix.isNotEmpty()) ".$messageChannelSuffix" else ""
val taskQueue = binaryMessenger.makeBackgroundTaskQueue()
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.immich_mobile.ConnectivityApi.getCapabilities$separatedMessageChannelSuffix", codec, taskQueue)
if (api != null) {
channel.setMessageHandler { _, reply ->
val wrapped: List<Any?> = try {
listOf(api.getCapabilities())
} catch (exception: Throwable) {
ConnectivityPigeonUtils.wrapError(exception)
}
reply.reply(wrapped)
}
} else {
channel.setMessageHandler(null)
}
}
}
}
}

View file

@ -0,0 +1,39 @@
package app.alextran.immich.connectivity
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.net.wifi.WifiManager
class ConnectivityApiImpl(context: Context) : ConnectivityApi {
private val connectivityManager =
context.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
private val wifiManager =
context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
override fun getCapabilities(): List<NetworkCapability> {
val capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
?: return emptyList()
val hasWifi = capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) ||
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI_AWARE)
val hasCellular = capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
val hasVpn = capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)
val isUnmetered = capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)
return buildList {
if (hasWifi) add(NetworkCapability.WIFI)
if (hasCellular) add(NetworkCapability.CELLULAR)
if (hasVpn) {
add(NetworkCapability.VPN)
if (!hasWifi && !hasCellular) {
if (wifiManager.isWifiEnabled) add(NetworkCapability.WIFI)
// If VPN is active, but neither WIFI nor CELLULAR is reported as active,
// assume CELLULAR if WIFI is not enabled
else add(NetworkCapability.CELLULAR)
}
}
if (isUnmetered) add(NetworkCapability.UNMETERED)
}
}
}