Skip to content

Commit 9e122b4

Browse files
ygor-coelhoDev-hwang
authored andcommitted
fix: auto restart when timeout reach
1 parent d69560b commit 9e122b4

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

android/src/main/kotlin/com/pravera/flutter_foreground_task/PreferencesKey.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ object PreferencesKey {
5252
const val AUTO_RUN_ON_MY_PACKAGE_REPLACED = "autoRunOnMyPackageReplaced"
5353
const val ALLOW_WAKE_LOCK = "allowWakeLock"
5454
const val ALLOW_WIFI_LOCK = "allowWifiLock"
55+
const val ALLOW_AUTO_RESTART = "allowAutoRestart"
5556

5657
// task data
5758
const val CALLBACK_HANDLE = "callbackHandle"

android/src/main/kotlin/com/pravera/flutter_foreground_task/models/ForegroundTaskOptions.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ data class ForegroundTaskOptions(
99
val autoRunOnBoot: Boolean,
1010
val autoRunOnMyPackageReplaced: Boolean,
1111
val allowWakeLock: Boolean,
12-
val allowWifiLock: Boolean
12+
val allowWifiLock: Boolean,
13+
val allowAutoRestart: Boolean
1314
) {
1415
companion object {
1516
fun getData(context: Context): ForegroundTaskOptions {
@@ -34,13 +35,15 @@ data class ForegroundTaskOptions(
3435
val autoRunOnMyPackageReplaced = prefs.getBoolean(PrefsKey.AUTO_RUN_ON_MY_PACKAGE_REPLACED, false)
3536
val allowWakeLock = prefs.getBoolean(PrefsKey.ALLOW_WAKE_LOCK, true)
3637
val allowWifiLock = prefs.getBoolean(PrefsKey.ALLOW_WIFI_LOCK, false)
38+
val allowAutoRestart = prefs.getBoolean(PrefsKey.ALLOW_AUTO_RESTART, false)
3739

3840
return ForegroundTaskOptions(
3941
eventAction = eventAction,
4042
autoRunOnBoot = autoRunOnBoot,
4143
autoRunOnMyPackageReplaced = autoRunOnMyPackageReplaced,
4244
allowWakeLock = allowWakeLock,
43-
allowWifiLock = allowWifiLock
45+
allowWifiLock = allowWifiLock,
46+
allowAutoRestart = allowAutoRestart,
4447
)
4548
}
4649

@@ -58,13 +61,15 @@ data class ForegroundTaskOptions(
5861
val autoRunOnMyPackageReplaced = map?.get(PrefsKey.AUTO_RUN_ON_MY_PACKAGE_REPLACED) as? Boolean ?: false
5962
val allowWakeLock = map?.get(PrefsKey.ALLOW_WAKE_LOCK) as? Boolean ?: true
6063
val allowWifiLock = map?.get(PrefsKey.ALLOW_WIFI_LOCK) as? Boolean ?: false
64+
val allowAutoRestart = map?.get(PrefsKey.ALLOW_AUTO_RESTART) as? Boolean ?: false
6165

6266
with(prefs.edit()) {
6367
putString(PrefsKey.TASK_EVENT_ACTION, eventActionJsonString)
6468
putBoolean(PrefsKey.AUTO_RUN_ON_BOOT, autoRunOnBoot)
6569
putBoolean(PrefsKey.AUTO_RUN_ON_MY_PACKAGE_REPLACED, autoRunOnMyPackageReplaced)
6670
putBoolean(PrefsKey.ALLOW_WAKE_LOCK, allowWakeLock)
6771
putBoolean(PrefsKey.ALLOW_WIFI_LOCK, allowWifiLock)
72+
putBoolean(PrefsKey.ALLOW_AUTO_RESTART, allowAutoRestart)
6873
commit()
6974
}
7075
}
@@ -83,13 +88,15 @@ data class ForegroundTaskOptions(
8388
val autoRunOnMyPackageReplaced = map?.get(PrefsKey.AUTO_RUN_ON_MY_PACKAGE_REPLACED) as? Boolean
8489
val allowWakeLock = map?.get(PrefsKey.ALLOW_WAKE_LOCK) as? Boolean
8590
val allowWifiLock = map?.get(PrefsKey.ALLOW_WIFI_LOCK) as? Boolean
91+
val allowAutoRestart = map?.get(PrefsKey.ALLOW_AUTO_RESTART) as? Boolean
8692

8793
with(prefs.edit()) {
8894
eventActionJsonString?.let { putString(PrefsKey.TASK_EVENT_ACTION, it) }
8995
autoRunOnBoot?.let { putBoolean(PrefsKey.AUTO_RUN_ON_BOOT, it) }
9096
autoRunOnMyPackageReplaced?.let { putBoolean(PrefsKey.AUTO_RUN_ON_MY_PACKAGE_REPLACED, it) }
9197
allowWakeLock?.let { putBoolean(PrefsKey.ALLOW_WAKE_LOCK, it) }
9298
allowWifiLock?.let { putBoolean(PrefsKey.ALLOW_WIFI_LOCK, it) }
99+
allowAutoRestart?.let { putBoolean(PrefsKey.ALLOW_AUTO_RESTART, it) }
93100
commit()
94101
}
95102
}

android/src/main/kotlin/com/pravera/flutter_foreground_task/service/ForegroundService.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ class ForegroundService : Service() {
194194
if (::foregroundServiceStatus.isInitialized) {
195195
isCorrectlyStopped = foregroundServiceStatus.isCorrectlyStopped()
196196
}
197-
if (!isCorrectlyStopped && !ForegroundServiceUtils.isSetStopWithTaskFlag(this)) {
197+
val allowAutoRestart = foregroundTaskOptions.allowAutoRestart
198+
if (allowAutoRestart && !isCorrectlyStopped && !ForegroundServiceUtils.isSetStopWithTaskFlag(this)) {
198199
Log.e(TAG, "The service will be restarted after 5 seconds because it wasn't properly stopped.")
199200
RestartReceiver.setRestartAlarm(this, 5000)
200201
}

lib/models/foreground_task_options.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class ForegroundTaskOptions {
99
this.autoRunOnMyPackageReplaced = false,
1010
this.allowWakeLock = true,
1111
this.allowWifiLock = false,
12+
this.allowAutoRestart = true,
1213
});
1314

1415
/// The action of onRepeatEvent in [TaskHandler].
@@ -32,6 +33,11 @@ class ForegroundTaskOptions {
3233
/// https://developer.android.com/reference/android/net/wifi/WifiManager.WifiLock.html
3334
final bool allowWifiLock;
3435

36+
/// Allows an application to automatically restart when the app is killed by the system.
37+
///
38+
/// https://developer.android.com/about/versions/15/behavior-changes-15?hl=pt-br#datasync-timeout
39+
final bool allowAutoRestart;
40+
3541
/// Returns the data fields of [ForegroundTaskOptions] in JSON format.
3642
Map<String, dynamic> toJson() {
3743
return {
@@ -40,6 +46,7 @@ class ForegroundTaskOptions {
4046
'autoRunOnMyPackageReplaced': autoRunOnMyPackageReplaced,
4147
'allowWakeLock': allowWakeLock,
4248
'allowWifiLock': allowWifiLock,
49+
'allowAutoRestart': allowAutoRestart,
4350
};
4451
}
4552

@@ -50,13 +57,14 @@ class ForegroundTaskOptions {
5057
bool? autoRunOnMyPackageReplaced,
5158
bool? allowWakeLock,
5259
bool? allowWifiLock,
60+
bool? allowAutoRestart,
5361
}) =>
5462
ForegroundTaskOptions(
5563
eventAction: eventAction ?? this.eventAction,
5664
autoRunOnBoot: autoRunOnBoot ?? this.autoRunOnBoot,
57-
autoRunOnMyPackageReplaced:
58-
autoRunOnMyPackageReplaced ?? this.autoRunOnMyPackageReplaced,
65+
autoRunOnMyPackageReplaced: autoRunOnMyPackageReplaced ?? this.autoRunOnMyPackageReplaced,
5966
allowWakeLock: allowWakeLock ?? this.allowWakeLock,
6067
allowWifiLock: allowWifiLock ?? this.allowWifiLock,
68+
allowAutoRestart: allowAutoRestart ?? this.allowAutoRestart,
6169
);
6270
}

0 commit comments

Comments
 (0)