Skip to content

Commit

Permalink
Optional Notifications For Worker (#103)
Browse files Browse the repository at this point in the history
- withNotification member variables for Worker.kt
  - default argument of boolean false
  • Loading branch information
evilthreads669966 authored Jan 1, 2021
1 parent c9073f3 commit 9d65f34
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
9 changes: 5 additions & 4 deletions app/src/main/java/com/candroid/lacedboots/Workers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class DailyWorker: Worker(222, "Daily Worker"){
}
}

class PeriodicWorker: Worker(777, "Periodic Worker"){
class PeriodicWorker: Worker(777, "Periodic Worker", withNotification = true){
override val receiver: WorkReceiver?
get() = null

Expand All @@ -85,11 +85,12 @@ class FutureWorker: Worker(999, "Future Worker"){
Log.d("Future worker", "working")
}
}
class OneTimeWorker: Worker(66,"One time work") {
class OneTimeWorker: Worker(66,"One time work", withNotification = true) {
override val receiver: WorkReceiver?
get() = null

override suspend fun doWork(ctx: Context) {
Log.d("OneTimeWorker", "working one time")
for(i in 1..10)
delay(1000)
}
Expand Down Expand Up @@ -120,7 +121,7 @@ class ScreenLockerJob: Worker(666,"Locking the screen"){
val intent = Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)
delay(500)
if (powerMgr.isInteractive)
ctx.sendBroadcast(intent)
ctx.sendBroadcast(intent)
}
}
}
}
2 changes: 1 addition & 1 deletion bootlaces/src/main/java/com/candroid/bootlaces/Work.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import kotlinx.android.parcel.Parcelize

@Parcelize
@Entity
internal data class Work(@PrimaryKey(autoGenerate = false) val id: Int, val job: String, var interval: Long? = null, var delay: Long? = null, var hourly: Boolean = false, var daily: Boolean = false, var monthly: Boolean = false, var yearly: Boolean = false): Parcelable{
data class Work(@PrimaryKey(autoGenerate = false) val id: Int, val job: String, var interval: Long? = null, var delay: Long? = null, var hourly: Boolean = false, var daily: Boolean = false, var monthly: Boolean = false, var yearly: Boolean = false): Parcelable{
fun toWorker(): Worker = Class.forName(this.job).newInstance() as Worker
companion object{
const val KEY_PARCEL = "KEY_PARCEL"
Expand Down
6 changes: 4 additions & 2 deletions bootlaces/src/main/java/com/candroid/bootlaces/WorkService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,13 @@ class WorkService: BaseWorkService() {
workers -= worker.apply {
workerCount = workers.also { it += this }.size
val intent = IntentFactory.createWorkNotificationIntent(this)
NotificatonService.enqueue(this@WorkService, intent)
if(withNotification == true)
NotificatonService.enqueue(this@WorkService, intent)
registerWorkReceiver()
doWork(this@WorkService)
unregisterWorkReceiver()
NotificatonService.enqueue(this@WorkService, intent.apply { setAction(Actions.ACTION_FINISH.action) })
if(withNotification == true)
NotificatonService.enqueue(this@WorkService, intent.apply { setAction(Actions.ACTION_FINISH.action) })
}
workerCount--
}
Expand Down
2 changes: 1 addition & 1 deletion bootlaces/src/main/java/com/candroid/bootlaces/Worker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import android.content.Intent
* @email [email protected]
* @date 10/18/20
**/
abstract class Worker(val id: Int, val description: String, var interval: Long? = null, var delay: Long? = null, var hourly: Boolean = false, var daily: Boolean = false, var monthly: Boolean = false, var yearly: Boolean = false){
abstract class Worker(val id: Int, val description: String, var withNotification: Boolean = false, var interval: Long? = null, var delay: Long? = null, var hourly: Boolean = false, var daily: Boolean = false, var monthly: Boolean = false, var yearly: Boolean = false){
abstract val receiver: WorkReceiver?

suspend abstract fun doWork(ctx: Context)
Expand Down

0 comments on commit 9d65f34

Please sign in to comment.