Skip to content

Commit

Permalink
Add: CancelSchedule command
Browse files Browse the repository at this point in the history
  • Loading branch information
machiav3lli committed Oct 31, 2024
1 parent dd4d859 commit a12e60f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/machiav3lli/backup/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ const val PACKAGES_LIST_GLOBAL_ID = -1L

const val ACTION_CANCEL = "cancel"
const val ACTION_SCHEDULE = "schedule"
const val ACTION_CANCEL_SCHEDULE = "cancel_schedule"
const val ACTION_RESCHEDULE = "reschedule"
const val ACTION_CRASH = "crash"

Expand Down
32 changes: 23 additions & 9 deletions src/main/java/com/machiav3lli/backup/services/CommandReceiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.machiav3lli.backup.ACTION_CANCEL
import com.machiav3lli.backup.ACTION_CANCEL_SCHEDULE
import com.machiav3lli.backup.ACTION_CRASH
import com.machiav3lli.backup.ACTION_RESCHEDULE
import com.machiav3lli.backup.ACTION_SCHEDULE
import com.machiav3lli.backup.EXTRA_SCHEDULE_ID
import com.machiav3lli.backup.OABX
import com.machiav3lli.backup.dbs.ODatabase
import com.machiav3lli.backup.preferences.traceSchedule
import com.machiav3lli.backup.tasks.ScheduleWork
import com.machiav3lli.backup.utils.SystemUtils
import com.machiav3lli.backup.utils.scheduleNext
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import timber.log.Timber
import java.text.SimpleDateFormat
import java.util.Locale
Expand All @@ -20,33 +25,42 @@ class CommandReceiver : //TODO hg42 how to maintain security?
//TODO machiav3lli by making the receiver only internally accessible (not exported)
//TODO hg42 but it's one of the purposes to be remotely controllable from other apps like Tasker
//TODO hg42 no big prob for now: cancel, starting or changing schedule isn't very critical
BroadcastReceiver() {
BroadcastReceiver(), KoinComponent {
val database: ODatabase by inject()

override fun onReceive(context: Context, intent: Intent?) {
if (intent == null) return
val command = intent.action
Timber.i("Command: command $command")
when (command) {
ACTION_CANCEL -> {
ACTION_CANCEL -> {
val batchName = intent.getStringExtra("name")
Timber.d("################################################### command intent cancel -------------> name=$batchName")
OABX.addInfoLogText("$command $batchName")
OABX.work.cancel(batchName)
}

ACTION_SCHEDULE -> {
ACTION_SCHEDULE -> {
intent.getStringExtra("name")?.let { name ->
OABX.addInfoLogText("$command $name")
Timber.d("################################################### command intent schedule -------------> name=$name")
Thread {
val scheduleDao = OABX.db.getScheduleDao()
val scheduleDao = database.getScheduleDao()
scheduleDao.getSchedule(name)?.let { schedule ->
ScheduleWork.schedule(context, schedule, true)
}
}.start()
}
}

ACTION_RESCHEDULE -> {
ACTION_CANCEL_SCHEDULE -> {
intent.getLongExtra(EXTRA_SCHEDULE_ID, -1L).takeIf { it != -1L }?.let { id ->
Timber.d("################################################### command cancel schedule -------------> id=$id")
ScheduleWork.cancel(context, id)
}
}

ACTION_RESCHEDULE -> {
intent.getStringExtra("name")?.let { name ->
val now = SystemUtils.now
val time = intent.getStringExtra("time")
Expand All @@ -55,7 +69,7 @@ class CommandReceiver : //TODO hg42 how to maintain security?
OABX.addInfoLogText("$command $name $time -> $setTime")
Timber.d("################################################### command intent reschedule -------------> name=$name time=$time -> $setTime")
Thread {
val scheduleDao = OABX.db.getScheduleDao()
val scheduleDao = database.getScheduleDao()
scheduleDao.getSchedule(name)?.let { schedule ->
val (hour, minute) = setTime.split(":").map { it.toInt() }
traceSchedule { "[${schedule.id}] command receiver -> re-schedule to hour=$hour minute=$minute" }
Expand All @@ -70,12 +84,12 @@ class CommandReceiver : //TODO hg42 how to maintain security?
}
}

ACTION_CRASH -> {
ACTION_CRASH -> {
throw Exception("this is a crash via command intent")
}

null -> {}
else -> {
null -> {}
else -> {
OABX.addInfoLogText("Command: command '$command'")
}
}
Expand Down

0 comments on commit a12e60f

Please sign in to comment.