-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #258 from toquete/alarm_module
Creating alarm module
- Loading branch information
Showing
9 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
plugins { | ||
id("boxbox.android.library") | ||
id("boxbox.android.hilt") | ||
} | ||
|
||
android { | ||
namespace = "com.toquete.boxbox.core.alarm" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest> | ||
|
||
</manifest> |
8 changes: 8 additions & 0 deletions
8
core/alarm/src/main/java/com/toquete/boxbox/core/alarm/AlarmScheduler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.toquete.boxbox.core.alarm | ||
|
||
import com.toquete.boxbox.core.alarm.model.AlarmItem | ||
|
||
interface AlarmScheduler { | ||
fun setAndAllowWhileIdle(alarmItem: AlarmItem) | ||
fun cancel(alarmItem: AlarmItem) | ||
} |
43 changes: 43 additions & 0 deletions
43
core/alarm/src/main/java/com/toquete/boxbox/core/alarm/DefaultAlarmScheduler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.toquete.boxbox.core.alarm | ||
|
||
import android.app.AlarmManager | ||
import android.app.PendingIntent | ||
import android.content.Context | ||
import com.toquete.boxbox.core.alarm.model.AlarmItem | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import kotlinx.datetime.TimeZone | ||
import kotlinx.datetime.toInstant | ||
import javax.inject.Inject | ||
|
||
internal class DefaultAlarmScheduler @Inject constructor( | ||
@ApplicationContext private val context: Context, | ||
private val alarmManager: AlarmManager | ||
) : AlarmScheduler { | ||
|
||
override fun setAndAllowWhileIdle(alarmItem: AlarmItem) { | ||
alarmManager.setAndAllowWhileIdle( | ||
AlarmManager.RTC_WAKEUP, | ||
getAlarmTime(alarmItem), | ||
getPendingIntent(alarmItem) | ||
) | ||
} | ||
|
||
override fun cancel(alarmItem: AlarmItem) { | ||
alarmManager.cancel(getPendingIntent(alarmItem)) | ||
} | ||
|
||
private fun getAlarmTime(alarmItem: AlarmItem): Long { | ||
return alarmItem.dateTime | ||
.toInstant(TimeZone.currentSystemDefault()) | ||
.toEpochMilliseconds() | ||
} | ||
|
||
private fun getPendingIntent(alarmItem: AlarmItem): PendingIntent { | ||
return PendingIntent.getBroadcast( | ||
context, | ||
alarmItem.hashCode(), | ||
alarmItem.intent, | ||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE | ||
) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
core/alarm/src/main/java/com/toquete/boxbox/core/alarm/di/AlarmModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.toquete.boxbox.core.alarm.di | ||
|
||
import android.app.AlarmManager | ||
import android.content.Context | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
internal object AlarmModule { | ||
|
||
@Singleton | ||
@Provides | ||
fun provideAlarmManager(@ApplicationContext context: Context): AlarmManager { | ||
return context.getSystemService(Context.ALARM_SERVICE) as AlarmManager | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
core/alarm/src/main/java/com/toquete/boxbox/core/alarm/di/AlarmSchedulerModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.toquete.boxbox.core.alarm.di | ||
|
||
import com.toquete.boxbox.core.alarm.AlarmScheduler | ||
import com.toquete.boxbox.core.alarm.DefaultAlarmScheduler | ||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
internal abstract class AlarmSchedulerModule { | ||
|
||
@Binds | ||
abstract fun bindNotificationScheduler( | ||
defaultNotificationScheduler: DefaultAlarmScheduler | ||
): AlarmScheduler | ||
} |
10 changes: 10 additions & 0 deletions
10
core/alarm/src/main/java/com/toquete/boxbox/core/alarm/model/AlarmItem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.toquete.boxbox.core.alarm.model | ||
|
||
import android.content.Intent | ||
import kotlinx.datetime.LocalDateTime | ||
|
||
data class AlarmItem( | ||
val intent: Intent, | ||
val dateTime: LocalDateTime, | ||
val message: String | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters