Skip to content

Commit

Permalink
ExportWorker: Avoid cancelling job when scheduling the next run
Browse files Browse the repository at this point in the history
The androidx work library does not clear the foreground notification
when a job is cancelled.

Signed-off-by: Andrew Gunnerson <[email protected]>
  • Loading branch information
chenxiaolong committed May 26, 2024
1 parent ef9b472 commit 6d988c7
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class TimePickerPreference(context: Context, attrs: AttributeSet?) : DialogPrefe
fun persistMinutesFromMidnight(minutesFromMidnight: Int) {
super.persistInt(minutesFromMidnight)
notifyChanged()
updateExportWork(context)
updateExportWork(context, true)
}

override fun onSetInitialValue(defaultValue: Any?) {
Expand Down
8 changes: 5 additions & 3 deletions app/src/main/java/com/github/tmo1/sms_ie/ExportWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,16 @@ class ExportWorker(appContext: Context, workerParams: WorkerParameters) :
}
}
}
updateExportWork(context)
updateExportWork(context, false)
//FIXME: as written, this always returns success, since the work is launched asynchronously and these lines execute immediately upon coroutine launch
return result
}
}

fun updateExportWork(context: Context) {
WorkManager.getInstance(context).cancelAllWorkByTag(EXPORT_WORK_TAG)
fun updateExportWork(context: Context, cancel: Boolean) {
if (cancel) {
WorkManager.getInstance(context).cancelAllWorkByTag(EXPORT_WORK_TAG)
}
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
if (prefs.getBoolean("schedule_export", false)) {
// https://stackoverflow.com/questions/4389500/how-can-i-find-the-amount-of-seconds-passed-from-the-midnight-with-java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class SettingsActivity : AppCompatActivity() {
// https://stackoverflow.com/questions/66449883/kotlin-onsharedpreferencechangelistener
val prefListener = SharedPreferences.OnSharedPreferenceChangeListener { sharedPrefs, key ->
if (key == "schedule_export") {
context?.let { updateExportWork(it) }
context?.let { updateExportWork(it, true) }
if (SDK_INT >= 33 && sharedPrefs.getBoolean(key, false)) {
context?.let {
if (ContextCompat.checkSelfPermission(
Expand Down

0 comments on commit 6d988c7

Please sign in to comment.