Skip to content

updated package code to use Flutters v2 embedding API #595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: workmanager_workspace
name: workmanager

environment:
sdk: '>=2.17.0 <3.0.0'
Original file line number Diff line number Diff line change
@@ -11,20 +11,21 @@ import com.google.common.util.concurrent.ListenableFuture
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.dart.DartExecutor
import io.flutter.embedding.engine.loader.FlutterLoader
import io.flutter.embedding.engine.loader.FlutterCallbackInformation
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.view.FlutterCallbackInformation
import java.util.Random

/***
* A simple worker that will post your input back to your Flutter application.
/**
* A simple worker that posts your input back to your Flutter application.
*
* It will block the background thread until a value of either true or false is received back from Flutter code.
*/
class BackgroundWorker(
applicationContext: Context,
private val workerParams: WorkerParameters,
) : ListenableWorker(applicationContext, workerParams), MethodChannel.MethodCallHandler {

private lateinit var backgroundChannel: MethodChannel

companion object {
@@ -41,13 +42,13 @@ class BackgroundWorker(
private val flutterLoader = FlutterLoader()
}

private val payload
private val payload: String?
get() = workerParams.inputData.getString(PAYLOAD_KEY)

private val dartTask
private val dartTask: String
get() = workerParams.inputData.getString(DART_TASK_KEY)!!

private val isInDebug
private val isInDebug: Boolean
get() = workerParams.inputData.getBoolean(IS_IN_DEBUG_MODE_KEY, false)

private val randomThreadIdentifier = Random().nextInt()
@@ -75,7 +76,7 @@ class BackgroundWorker(
flutterLoader.ensureInitializationCompleteAsync(
applicationContext,
null,
Handler(Looper.getMainLooper()),
Handler(Looper.getMainLooper())
) {
val callbackHandle = SharedPreferenceHelper.getCallbackHandle(applicationContext)
val callbackInfo = FlutterCallbackInformation.lookupCallbackInformation(callbackHandle)
@@ -89,7 +90,7 @@ class BackgroundWorker(
payload,
callbackHandle,
callbackInfo,
dartBundlePath,
dartBundlePath
)
}

@@ -101,8 +102,8 @@ class BackgroundWorker(
DartExecutor.DartCallback(
applicationContext.assets,
dartBundlePath,
callbackInfo,
),
callbackInfo
)
)
}
}
@@ -124,17 +125,16 @@ class BackgroundWorker(
dartTask,
payload,
fetchDuration,
result ?: Result.failure(),
result ?: Result.failure()
)
}

// No result indicates we were signalled to stop by WorkManager. The result is already
// STOPPED, so no need to resolve another one.
// If a result is provided, complete the future.
if (result != null) {
this.completer?.set(result)
}

// If stopEngine is called from `onStopped`, it may not be from the main thread.
// Ensure engine is destroyed on the main thread.
Handler(Looper.getMainLooper()).post {
engine?.destroy()
engine = null
@@ -165,10 +165,10 @@ class BackgroundWorker(
}

override fun success(receivedResult: Any?) {
val wasSuccessFul = receivedResult?.let { it as Boolean? } == true
stopEngine(if (wasSuccessFul) Result.success() else Result.retry())
val wasSuccessful = receivedResult as? Boolean == true
stopEngine(if (wasSuccessful) Result.success() else Result.retry())
}
},
}
)
}
}
Original file line number Diff line number Diff line change
@@ -5,6 +5,11 @@ import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodChannel

/**
* A Flutter plugin that provides a foreground channel for workmanager operations.
*
* This implementation uses Flutter's v2 embedding API.
*/
class WorkmanagerPlugin : FlutterPlugin {
private var methodChannel: MethodChannel? = null
private var workmanagerCallHandler: WorkmanagerCallHandler? = null
@@ -31,4 +36,4 @@ class WorkmanagerPlugin : FlutterPlugin {
methodChannel = null
workmanagerCallHandler = null
}
}
}