Skip to content

Commit 6a09b41

Browse files
mx1upened
andauthored
add isScheduled call: checks whether a period task is scheduled (android only) (#535)
Co-authored-by: Sebastian Roth <[email protected]>
1 parent bdd61b3 commit 6a09b41

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

android/src/main/kotlin/dev/fluttercommunity/workmanager/Extractor.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import androidx.work.PeriodicWorkRequest
1313
import androidx.work.WorkRequest
1414
import dev.fluttercommunity.workmanager.WorkManagerCall.CancelTask.ByTag.KEYS.UNREGISTER_TASK_TAG_KEY
1515
import dev.fluttercommunity.workmanager.WorkManagerCall.CancelTask.ByUniqueName.KEYS.UNREGISTER_TASK_UNIQUE_NAME_KEY
16+
import dev.fluttercommunity.workmanager.WorkManagerCall.IsScheduled.ByUniqueName.KEYS.IS_SCHEDULED_UNIQUE_NAME_KEY
1617
import dev.fluttercommunity.workmanager.WorkManagerCall.Initialize.KEYS.INITIALIZE_TASK_CALL_HANDLE_KEY
1718
import dev.fluttercommunity.workmanager.WorkManagerCall.Initialize.KEYS.INITIALIZE_TASK_IS_IN_DEBUG_MODE_KEY
1819
import dev.fluttercommunity.workmanager.WorkManagerCall.RegisterTask.KEYS.REGISTER_TASK_BACK_OFF_POLICY_DELAY_MILLIS_KEY
@@ -131,6 +132,14 @@ sealed class WorkManagerCall {
131132
}
132133
}
133134

135+
sealed class IsScheduled : WorkManagerCall() {
136+
data class ByUniqueName(val uniqueName: String) : IsScheduled() {
137+
companion object KEYS {
138+
const val IS_SCHEDULED_UNIQUE_NAME_KEY = "uniqueName"
139+
}
140+
}
141+
}
142+
134143
sealed class CancelTask : WorkManagerCall() {
135144
data class ByUniqueName(val uniqueName: String) : CancelTask() {
136145
companion object KEYS {
@@ -164,6 +173,8 @@ object Extractor {
164173
REGISTER_ONE_OFF_TASK("registerOneOffTask"),
165174
REGISTER_PERIODIC_TASK("registerPeriodicTask"),
166175

176+
IS_SCHEDULED_BY_UNIQUE_NAME("isScheduledByUniqueName"),
177+
167178
CANCEL_TASK_BY_UNIQUE_NAME("cancelTaskByUniqueName"),
168179
CANCEL_TASK_BY_TAG("cancelTaskByTag"),
169180
CANCEL_ALL("cancelAllTasks"),
@@ -228,6 +239,12 @@ object Extractor {
228239
)
229240
}
230241

242+
PossibleWorkManagerCall.IS_SCHEDULED_BY_UNIQUE_NAME -> {
243+
WorkManagerCall.IsScheduled.ByUniqueName(
244+
call.argument(IS_SCHEDULED_UNIQUE_NAME_KEY)!!
245+
)
246+
}
247+
231248
PossibleWorkManagerCall.CANCEL_TASK_BY_UNIQUE_NAME -> WorkManagerCall.CancelTask.ByUniqueName(
232249
call.argument(UNREGISTER_TASK_UNIQUE_NAME_KEY)!!
233250
)

android/src/main/kotlin/dev/fluttercommunity/workmanager/WorkmanagerCallHandler.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import androidx.work.ExistingWorkPolicy
88
import androidx.work.OneTimeWorkRequest
99
import androidx.work.OutOfQuotaPolicy
1010
import androidx.work.PeriodicWorkRequest
11+
import androidx.work.WorkInfo
1112
import androidx.work.WorkManager
1213
import dev.fluttercommunity.workmanager.BackgroundWorker.Companion.DART_TASK_KEY
1314
import dev.fluttercommunity.workmanager.BackgroundWorker.Companion.IS_IN_DEBUG_MODE_KEY
@@ -36,6 +37,11 @@ class WorkmanagerCallHandler(private val ctx: Context) : MethodChannel.MethodCal
3637
extractedCall,
3738
result
3839
)
40+
is WorkManagerCall.IsScheduled -> IsScheduledHandler.handle(
41+
ctx,
42+
extractedCall,
43+
result
44+
)
3945
is WorkManagerCall.CancelTask -> UnregisterTaskHandler.handle(
4046
ctx,
4147
extractedCall,
@@ -140,6 +146,23 @@ private object RegisterTaskHandler : CallHandler<WorkManagerCall.RegisterTask> {
140146
}
141147
}
142148

149+
private object IsScheduledHandler : CallHandler<WorkManagerCall.IsScheduled> {
150+
override fun handle(
151+
context: Context,
152+
convertedCall: WorkManagerCall.IsScheduled,
153+
result: MethodChannel.Result
154+
) {
155+
when (convertedCall) {
156+
is WorkManagerCall.IsScheduled.ByUniqueName -> {
157+
val workInfos = WM.getWorkInfoByUniqueName(context, convertedCall.uniqueName).get()
158+
val scheduled = workInfos.isNotEmpty()
159+
&& workInfos.all { it.state == WorkInfo.State.ENQUEUED || it.state == WorkInfo.State.RUNNING }
160+
return result.success(scheduled)
161+
}
162+
}
163+
}
164+
}
165+
143166
private object UnregisterTaskHandler : CallHandler<WorkManagerCall.CancelTask> {
144167
override fun handle(
145168
context: Context,
@@ -274,6 +297,9 @@ object WM {
274297
.build()
275298
}
276299

300+
fun getWorkInfoByUniqueName(context: Context, uniqueWorkName: String) =
301+
context.workManager().getWorkInfosForUniqueWork(uniqueWorkName)
302+
277303
fun cancelByUniqueName(context: Context, uniqueWorkName: String) =
278304
context.workManager().cancelUniqueWork(uniqueWorkName)
279305

example/lib/main.dart

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,11 @@ class _MyAppState extends State<MyApp> {
141141
}
142142
},
143143
),
144-
SizedBox(height: 16),
144+
SizedBox(height: 8),
145+
Text(
146+
"Register task",
147+
style: Theme.of(context).textTheme.headlineSmall,
148+
),
145149

146150
//This task runs once.
147151
//Most likely this will trigger immediately
@@ -194,6 +198,10 @@ class _MyAppState extends State<MyApp> {
194198
);
195199
}),
196200
SizedBox(height: 8),
201+
Text(
202+
"Register periodic task (android only)",
203+
style: Theme.of(context).textTheme.headlineSmall,
204+
),
197205
//This task runs periodically
198206
//It will wait at least 10 seconds before its first launch
199207
//Since we have not provided a frequency it will be the default 15 minutes
@@ -265,6 +273,17 @@ class _MyAppState extends State<MyApp> {
265273
: null,
266274
),
267275
SizedBox(height: 16),
276+
ElevatedButton(
277+
child: Text("isscheduled (Android)"),
278+
onPressed: Platform.isAndroid
279+
? () async {
280+
final workInfo = await Workmanager().isScheduledByUniqueName(
281+
simplePeriodicTask,
282+
);
283+
print('isscheduled = $workInfo');
284+
}
285+
: null),
286+
SizedBox(height: 8),
268287
Text(
269288
"Task cancellation",
270289
style: Theme.of(context).textTheme.headlineSmall,

lib/src/workmanager.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,17 @@ class Workmanager {
275275
),
276276
);
277277

278+
/// Checks whether a period task is scheduled by its [uniqueName].
279+
///
280+
/// Scheduled means the work state is either ENQUEUED or RUNNING
281+
///
282+
/// Only available on Android.
283+
Future<bool> isScheduledByUniqueName(final String uniqueName) async {
284+
return await _foregroundChannel.invokeMethod(
285+
"isScheduledByUniqueName",
286+
{"uniqueName": uniqueName},
287+
);
288+
278289
/// Schedule a background long running task, currently only available on iOS.
279290
///
280291
/// Processing tasks are for long processes like data processing and app maintenance.

0 commit comments

Comments
 (0)