@@ -10,23 +10,22 @@ import androidx.work.WorkerParameters
1010import com.google.common.util.concurrent.ListenableFuture
1111import io.flutter.embedding.engine.FlutterEngine
1212import io.flutter.embedding.engine.dart.DartExecutor
13+ import io.flutter.embedding.engine.loader.FlutterLoader
1314import io.flutter.embedding.engine.plugins.shim.ShimPluginRegistry
1415import io.flutter.plugin.common.MethodCall
1516import io.flutter.plugin.common.MethodChannel
1617import io.flutter.view.FlutterCallbackInformation
17- import io.flutter.view.FlutterMain
1818import java.util.*
1919
2020/* **
2121 * A simple worker that will post your input back to your Flutter application.
2222 *
2323 * It will block the background thread until a value of either true or false is received back from Flutter code.
24- *
2524 */
2625class BackgroundWorker (
27- private val ctx : Context ,
26+ applicationContext : Context ,
2827 private val workerParams : WorkerParameters
29- ) : ListenableWorker(ctx , workerParams), MethodChannel.MethodCallHandler {
28+ ) : ListenableWorker(applicationContext , workerParams), MethodChannel.MethodCallHandler {
3029
3130 private lateinit var backgroundChannel: MethodChannel
3231
@@ -37,8 +36,11 @@ class BackgroundWorker(
3736 const val DART_TASK_KEY = " be.tramckrijte.workmanager.DART_TASK"
3837 const val IS_IN_DEBUG_MODE_KEY = " be.tramckrijte.workmanager.IS_IN_DEBUG_MODE_KEY"
3938
40- const val BACKGROUND_CHANNEL_NAME = " be.tramckrijte.workmanager/background_channel_work_manager"
39+ const val BACKGROUND_CHANNEL_NAME =
40+ " be.tramckrijte.workmanager/background_channel_work_manager"
4141 const val BACKGROUND_CHANNEL_INITIALIZED = " backgroundChannelInitialized"
42+
43+ private val flutterLoader = FlutterLoader ()
4244 }
4345
4446 private val payload
@@ -51,40 +53,55 @@ class BackgroundWorker(
5153 get() = workerParams.inputData.getBoolean(IS_IN_DEBUG_MODE_KEY , false )
5254
5355 private val randomThreadIdentifier = Random ().nextInt()
54- private lateinit var engine: FlutterEngine
56+ private var engine: FlutterEngine ? = null
5557
56- private var destroying = false
5758 private var startTime: Long = 0
5859 private val resolvableFuture = ResolvableFuture .create<Result >()
5960
6061 override fun startWork (): ListenableFuture <Result > {
6162 startTime = System .currentTimeMillis()
6263
63- engine = FlutterEngine (ctx)
64- FlutterMain .ensureInitializationComplete(ctx, null )
64+ engine = FlutterEngine (applicationContext)
6565
66- val callbackHandle = SharedPreferenceHelper .getCallbackHandle(ctx)
67- val callbackInfo = FlutterCallbackInformation .lookupCallbackInformation(callbackHandle)
68- val dartBundlePath = FlutterMain .findAppBundlePath()
69-
70- if (isInDebug) {
71- DebugHelper .postTaskStarting(
72- ctx,
73- randomThreadIdentifier,
74- dartTask,
75- payload,
76- callbackHandle,
77- callbackInfo,
78- dartBundlePath
79- )
66+ if (! flutterLoader.initialized()) {
67+ flutterLoader.startInitialization(applicationContext)
8068 }
8169
82- // Backwards compatibility with v1. We register all the user's plugins.
83- WorkmanagerPlugin .pluginRegistryCallback?.registerWith(ShimPluginRegistry (engine))
84- engine.dartExecutor.executeDartCallback(DartExecutor .DartCallback (ctx.assets, dartBundlePath, callbackInfo))
70+ flutterLoader.ensureInitializationCompleteAsync(
71+ applicationContext,
72+ null ,
73+ Handler (Looper .getMainLooper())
74+ ) {
75+ val callbackHandle = SharedPreferenceHelper .getCallbackHandle(applicationContext)
76+ val callbackInfo = FlutterCallbackInformation .lookupCallbackInformation(callbackHandle)
77+ val dartBundlePath = flutterLoader.findAppBundlePath()
78+
79+ if (isInDebug) {
80+ DebugHelper .postTaskStarting(
81+ applicationContext,
82+ randomThreadIdentifier,
83+ dartTask,
84+ payload,
85+ callbackHandle,
86+ callbackInfo,
87+ dartBundlePath
88+ )
89+ }
90+
91+ // Backwards compatibility with v1. We register all the user's plugins.
92+ WorkmanagerPlugin .pluginRegistryCallback?.registerWith(ShimPluginRegistry (engine!! ))
8593
86- backgroundChannel = MethodChannel (engine.dartExecutor, BACKGROUND_CHANNEL_NAME )
87- backgroundChannel.setMethodCallHandler(this @BackgroundWorker)
94+ backgroundChannel = MethodChannel (engine!! .dartExecutor, BACKGROUND_CHANNEL_NAME )
95+ backgroundChannel.setMethodCallHandler(this @BackgroundWorker)
96+
97+ engine!! .dartExecutor.executeDartCallback(
98+ DartExecutor .DartCallback (
99+ applicationContext.assets,
100+ dartBundlePath,
101+ callbackInfo
102+ )
103+ )
104+ }
88105
89106 return resolvableFuture
90107 }
@@ -98,7 +115,7 @@ class BackgroundWorker(
98115
99116 if (isInDebug) {
100117 DebugHelper .postTaskCompleteNotification(
101- ctx ,
118+ applicationContext ,
102119 randomThreadIdentifier,
103120 dartTask,
104121 payload,
@@ -115,11 +132,8 @@ class BackgroundWorker(
115132
116133 // If stopEngine is called from `onStopped`, it may not be from the main thread.
117134 Handler (Looper .getMainLooper()).post {
118- if (! destroying) {
119- if (this ::engine.isInitialized)
120- engine.destroy()
121- destroying = true
122- }
135+ engine?.destroy()
136+ engine = null
123137 }
124138 }
125139
@@ -134,7 +148,11 @@ class BackgroundWorker(
134148 stopEngine(Result .failure())
135149 }
136150
137- override fun error (errorCode : String? , errorMessage : String? , errorDetails : Any? ) {
151+ override fun error (
152+ errorCode : String? ,
153+ errorMessage : String? ,
154+ errorDetails : Any?
155+ ) {
138156 Log .e(TAG , " errorCode: $errorCode , errorMessage: $errorMessage " )
139157 stopEngine(Result .failure())
140158 }
0 commit comments