diff --git a/src/main/java/com/machiav3lli/backup/Constants.kt b/src/main/java/com/machiav3lli/backup/Constants.kt index af04f0a605..b61d20b52a 100644 --- a/src/main/java/com/machiav3lli/backup/Constants.kt +++ b/src/main/java/com/machiav3lli/backup/Constants.kt @@ -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" diff --git a/src/main/java/com/machiav3lli/backup/services/CommandReceiver.kt b/src/main/java/com/machiav3lli/backup/services/CommandReceiver.kt index 89b02c13a3..20961adda0 100644 --- a/src/main/java/com/machiav3lli/backup/services/CommandReceiver.kt +++ b/src/main/java/com/machiav3lli/backup/services/CommandReceiver.kt @@ -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 @@ -20,25 +25,27 @@ 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) } @@ -46,7 +53,14 @@ class CommandReceiver : //TODO hg42 how to maintain security? } } - 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") @@ -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" } @@ -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'") } }