@@ -2,14 +2,7 @@ package dev.fluttercommunity.workmanager
22
33import android.os.Build
44import androidx.annotation.VisibleForTesting
5- import androidx.work.BackoffPolicy
6- import androidx.work.Constraints
7- import androidx.work.ExistingPeriodicWorkPolicy
8- import androidx.work.ExistingWorkPolicy
9- import androidx.work.NetworkType
10- import androidx.work.OutOfQuotaPolicy
11- import androidx.work.PeriodicWorkRequest
12- import androidx.work.WorkRequest
5+ import androidx.work.*
136import dev.fluttercommunity.workmanager.WorkManagerCall.CancelTask.ByTag.KEYS.UNREGISTER_TASK_TAG_KEY
147import dev.fluttercommunity.workmanager.WorkManagerCall.CancelTask.ByUniqueName.KEYS.UNREGISTER_TASK_UNIQUE_NAME_KEY
158import dev.fluttercommunity.workmanager.WorkManagerCall.Initialize.KEYS.INITIALIZE_TASK_CALL_HANDLE_KEY
@@ -159,7 +152,27 @@ sealed class WorkManagerCall {
159152
160153 class Failed (val code : String ) : WorkManagerCall()
161154}
162-
155+ data class SetForeground (
156+ val foregroundServiceType : Int ,
157+ val notificationId : Int ,
158+ val notificationChannelId : String ,
159+ val notificationChannelName : String ,
160+ val notificationChannelDescription : String ,
161+ val notificationChannelImportance : Int ,
162+ val notificationTitle : String ,
163+ val notificationDescription : String ,
164+ ) {
165+ companion object KEYS {
166+ const val FOREGROUND_SERVICE_TYPE_KEY = " foregroundServiceType"
167+ const val NOTIFICATION_ID_KEY = " notificationId"
168+ const val NOTIFICATION_CHANNEL_ID_KEY = " notificationChannelId"
169+ const val NOTIFICATION_CHANNEL_NAME_KEY = " notificationChannelName"
170+ const val NOTIFICATION_CHANNEL_DESCRIPTION_KEY = " notificationChannelDescription"
171+ const val NOTIFICATION_CHANNEL_IMPORTANCE_KEY = " notificationChannelImportance"
172+ const val NOTIFICATION_TITLE_KEY = " notificationTitle"
173+ const val NOTIFICATION_DESCRIPTION_KEY = " notificationDescription"
174+ }
175+ }
163176private enum class TaskType (val minimumBackOffDelay : Long ) {
164177 ONE_OFF (WorkRequest .MIN_BACKOFF_MILLIS ),
165178 PERIODIC (WorkRequest .MIN_BACKOFF_MILLIS ),
@@ -190,6 +203,34 @@ object Extractor {
190203 }
191204 }
192205
206+ fun parseSetForegroundCall (call : MethodCall ): SetForeground {
207+ val foregroundServiceType =
208+ call.argument<Int >(SetForeground .KEYS .FOREGROUND_SERVICE_TYPE_KEY )!!
209+ val notificationId = call.argument<Int >(SetForeground .KEYS .NOTIFICATION_ID_KEY )!!
210+ val notificationChannelId =
211+ call.argument<String >(SetForeground .KEYS .NOTIFICATION_CHANNEL_ID_KEY )!!
212+ val notificationChannelName =
213+ call.argument<String >(SetForeground .KEYS .NOTIFICATION_CHANNEL_NAME_KEY )!!
214+ val notificationChannelDescription =
215+ call.argument<String >(SetForeground .KEYS .NOTIFICATION_CHANNEL_DESCRIPTION_KEY )!!
216+ val notificationChannelImportance =
217+ call.argument<Int >(SetForeground .KEYS .NOTIFICATION_CHANNEL_IMPORTANCE_KEY )!!
218+ val notificationTitle =
219+ call.argument<String >(SetForeground .KEYS .NOTIFICATION_TITLE_KEY )!!
220+ val notificationDescription =
221+ call.argument<String >(SetForeground .KEYS .NOTIFICATION_DESCRIPTION_KEY )!!
222+ return SetForeground (
223+ foregroundServiceType,
224+ notificationId,
225+ notificationChannelId,
226+ notificationChannelName,
227+ notificationChannelDescription,
228+ notificationChannelImportance,
229+ notificationTitle,
230+ notificationDescription,
231+ )
232+ }
233+
193234 fun extractWorkManagerCallFromRawMethodName (call : MethodCall ): WorkManagerCall =
194235 when (PossibleWorkManagerCall .fromRawMethodName(call.method)) {
195236 PossibleWorkManagerCall .INITIALIZE -> {
@@ -202,6 +243,7 @@ object Extractor {
202243 WorkManagerCall .Initialize (handle, inDebugMode)
203244 }
204245 }
246+
205247 PossibleWorkManagerCall .REGISTER_ONE_OFF_TASK -> {
206248 WorkManagerCall .RegisterTask .OneOffTask (
207249 isInDebugMode = call.argument<Boolean >(REGISTER_TASK_IS_IN_DEBUG_MODE_KEY )!! ,
@@ -213,13 +255,14 @@ object Extractor {
213255 constraintsConfig = extractConstraintConfigFromCall(call),
214256 outOfQuotaPolicy = extractOutOfQuotaPolicyFromCall(call),
215257 backoffPolicyConfig =
216- extractBackoffPolicyConfigFromCall(
217- call,
218- TaskType .ONE_OFF ,
219- ),
258+ extractBackoffPolicyConfigFromCall(
259+ call,
260+ TaskType .ONE_OFF ,
261+ ),
220262 payload = extractPayload(call),
221263 )
222264 }
265+
223266 PossibleWorkManagerCall .REGISTER_PERIODIC_TASK -> {
224267 WorkManagerCall .RegisterTask .PeriodicTask (
225268 isInDebugMode = call.argument<Boolean >(REGISTER_TASK_IS_IN_DEBUG_MODE_KEY )!! ,
@@ -232,10 +275,10 @@ object Extractor {
232275 initialDelaySeconds = extractInitialDelayFromCall(call),
233276 constraintsConfig = extractConstraintConfigFromCall(call),
234277 backoffPolicyConfig =
235- extractBackoffPolicyConfigFromCall(
236- call,
237- TaskType .PERIODIC ,
238- ),
278+ extractBackoffPolicyConfigFromCall(
279+ call,
280+ TaskType .PERIODIC ,
281+ ),
239282 outOfQuotaPolicy = extractOutOfQuotaPolicyFromCall(call),
240283 payload = extractPayload(call),
241284 )
@@ -251,12 +294,14 @@ object Extractor {
251294 WorkManagerCall .CancelTask .ByUniqueName (
252295 call.argument(UNREGISTER_TASK_UNIQUE_NAME_KEY )!! ,
253296 )
297+
254298 PossibleWorkManagerCall .CANCEL_TASK_BY_TAG ->
255299 WorkManagerCall .CancelTask .ByTag (
256300 call.argument(
257301 UNREGISTER_TASK_TAG_KEY ,
258302 )!! ,
259303 )
304+
260305 PossibleWorkManagerCall .CANCEL_ALL -> WorkManagerCall .CancelTask .All
261306
262307 PossibleWorkManagerCall .UNKNOWN -> WorkManagerCall .Unknown
0 commit comments