Skip to content

Latest commit

 

History

History
184 lines (136 loc) · 5.42 KB

README.md

File metadata and controls

184 lines (136 loc) · 5.42 KB

AlarmScheduler

AlarmScheduler is a tool to schedule time exact tasks, even in very short period of time.

AlarmScheduler provides various supports:

  • Easy-to-use API
  • Android 12+ (Android S+) support
  • Alarms survive after device reboot
  • debuggable with built-in logger

AlarmScheduler is built on top of AlarmManager + Kotlin Coroutines + Room and ❤️ !!

Add AlarmScheduler to your project

Gradle

Add the dependency to your module's build.gradle file:

dependencies {
    implementation 'com.carterchen247:alarm-scheduler:x.x.x'
}

How to Use

To see the complete usage of AlarmScheduler, please build and refer to the demo app.

Schedule an Alarm

1️⃣ Define a AlarmTask callback

AlarmScheduler.setAlarmTaskFactory(object : AlarmTaskFactory {
    override fun createAlarmTask(alarmType: Int): AlarmTask {
        return MyAlarmTask()
    }
})
class MyAlarmTask : AlarmTask {
    override fun onAlarmFires(alarmId: Int, dataPayload: DataPayload) {
        // do something with dataPayload you set
    }

    companion object {
        const val TYPE = 1
    }
}

2️⃣ And then just schedule it!

val config = AlarmConfig(
    Date().time + 10000L, // trigger time
    MyAlarmTask.TYPE
) {
    dataPayload("reminder" to "have a meeting")
}

AlarmScheduler.schedule(config){ ... }

3️⃣ Handle the result on your own

AlarmScheduler.schedule(config) { result: ScheduleResult ->
    when (result) {
        is ScheduleResult.Success -> {
            // alarm scheduling success!
        }
        is ScheduleResult.Failure -> {
            when (result) {
                ScheduleResult.Failure.CannotScheduleExactAlarm -> {
                    // handle scenarios like user disables exact alarm permission
                }
                is ScheduleResult.Failure.Error -> {
                    // handle error
                }
            }
        }
    }
}

For Android 12+

When receiving a CannotScheduleExactAlarm failure result, it means the user disable the exact alarm permission. Try to use openExactAlarmSettingPage() extension function with prompts to guide your user.

fun Activity.openExactAlarmSettingPage() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        startActivity(Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM))
    }
}

get notified once user grants the exact alarm permission.

AlarmSchedulerEventObserver { event ->
    if (event is ScheduleExactAlarmPermissionGrantedEvent) {
	// get notified
    }
}

Debugging

If something is not working, enable the built-in logger to see what’s going on.

AlarmScheduler.setLogger(AlarmSchedulerLogger.DEBUG)

Other Features

AlarmScheduler.getScheduledAlarmsAsync { scheduledAlarms: List<AlarmInfo> ->
    // list the scheduled alarms 
}

There are more! refer to AlarmSchedulerContract see full APIs.

internal interface AlarmSchedulerContract {
    fun setAlarmTaskFactory(alarmTaskFactory: AlarmTaskFactory)
    fun setLogger(loggerImpl: AlarmSchedulerLogger)
    fun setErrorHandler(errorHandler: AlarmSchedulerErrorHandler)
    fun isAlarmTaskScheduled(alarmId: Int): Boolean
    fun cancelAlarmTask(alarmId: Int)
    fun cancelAllAlarmTasks()
    fun getScheduledAlarmsAsync(callback: ScheduledAlarmsCallback)
    fun addEventObserver(observer: AlarmSchedulerEventObserver)
    fun removeEventObserver(observer: AlarmSchedulerEventObserver)
    fun schedule(config: AlarmConfig, callback: ScheduleResultCallback?)
    fun canScheduleExactAlarms(): Boolean
}

Apps using AlarmScheduler

License

MIT License

Copyright (c) 2020 Carter Chen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.