-
Notifications
You must be signed in to change notification settings - Fork 3
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 #15 from brandy-kay/feature/alarm_manager
- Loading branch information
Showing
21 changed files
with
408 additions
and
33 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,5 @@ | ||
<vector android:height="24dp" android:tint="#000000" | ||
android:viewportHeight="24" android:viewportWidth="24" | ||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<path android:fillColor="@android:color/white" android:pathData="M12,22c1.1,0 2,-0.9 2,-2h-4c0,1.1 0.89,2 2,2zM18,16v-5c0,-3.07 -1.64,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.63,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2z"/> | ||
</vector> |
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
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
26 changes: 26 additions & 0 deletions
26
app/src/main/java/com/brandyodhiambo/quench/util/AlarmReceiver.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,26 @@ | ||
package com.brandyodhiambo.quench.util | ||
|
||
import android.app.NotificationManager | ||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import androidx.core.content.ContextCompat | ||
|
||
class AlarmReceiver : BroadcastReceiver() { | ||
|
||
override fun onReceive(context: Context?, intent: Intent?) { | ||
val message = intent?.getStringExtra("EXTRA_MESSAGE") ?: return | ||
println("Alarm triggered: $message") | ||
val notificationManager = | ||
ContextCompat.getSystemService( | ||
context!!, | ||
NotificationManager::class.java, | ||
) as NotificationManager | ||
|
||
notificationManager.sendReminderNotification( | ||
context = context, | ||
title = "Drink Water", | ||
message = message, | ||
) | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/brandyodhiambo/quench/util/AlarmSchedular.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,9 @@ | ||
package com.brandyodhiambo.quench.util | ||
|
||
import com.brandyodhiambo.common.domain.model.AlarmData | ||
|
||
interface AlarmSchedular { | ||
|
||
fun schedule(item: AlarmData) | ||
fun cancel(item: AlarmData) | ||
} |
42 changes: 42 additions & 0 deletions
42
app/src/main/java/com/brandyodhiambo/quench/util/AlarmSchedularImpl.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,42 @@ | ||
package com.brandyodhiambo.quench.util | ||
|
||
import android.app.AlarmManager | ||
import android.app.PendingIntent | ||
import android.content.Context | ||
import android.content.Intent | ||
import com.brandyodhiambo.common.domain.model.AlarmData | ||
import java.time.ZoneId | ||
|
||
class AlarmSchedularImpl( | ||
private val context: Context, | ||
) : AlarmSchedular { | ||
|
||
private val alarmManager = context.getSystemService(AlarmManager::class.java) | ||
|
||
override fun schedule(item: AlarmData) { | ||
val intent = Intent(context, AlarmReceiver::class.java).apply { | ||
putExtra("EXTRA_MESSAGE", item.message) | ||
} | ||
alarmManager.setExactAndAllowWhileIdle( | ||
AlarmManager.RTC_WAKEUP, | ||
item.time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(), | ||
PendingIntent.getBroadcast( | ||
context, | ||
item.hashCode(), | ||
intent, | ||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE, | ||
), | ||
) | ||
} | ||
|
||
override fun cancel(item: AlarmData) { | ||
alarmManager.cancel( | ||
PendingIntent.getBroadcast( | ||
context, | ||
item.hashCode(), | ||
Intent(context, AlarmReceiver::class.java), | ||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE, | ||
), | ||
) | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
app/src/main/java/com/brandyodhiambo/quench/util/NotificationUtil.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,52 @@ | ||
package com.brandyodhiambo.quench.util | ||
|
||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.app.PendingIntent | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.media.RingtoneManager | ||
import androidx.core.app.NotificationCompat | ||
import com.brandyodhiambo.quench.R | ||
import com.brandyodhiambo.quench.ui.MainActivity | ||
|
||
private const val NOTIFICATION_ID = 33 | ||
private const val CHANNEL_ID = "ReminderChannel" | ||
|
||
fun createChannel(context: Context) { | ||
val notificationChannel = | ||
NotificationChannel(CHANNEL_ID, "Channel1", NotificationManager.IMPORTANCE_HIGH) | ||
val notificationManager = context.getSystemService(NotificationManager::class.java) | ||
notificationManager.createNotificationChannel(notificationChannel) | ||
} | ||
|
||
fun NotificationManager.sendReminderNotification( | ||
context: Context, | ||
title: String, | ||
message: String, | ||
|
||
) { | ||
// Opening the Notification | ||
val contentIntent = Intent(context, MainActivity::class.java) | ||
val contentPendingIntent = PendingIntent.getActivity( | ||
context, | ||
NOTIFICATION_ID, | ||
contentIntent, | ||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE, | ||
) | ||
// Building the notification | ||
val builder = NotificationCompat.Builder(context, CHANNEL_ID) | ||
.setContentTitle(title) | ||
.setContentText(message) | ||
.setStyle( | ||
NotificationCompat.BigTextStyle() | ||
.bigText(message), | ||
) | ||
.setSmallIcon(R.drawable.ic_notification) | ||
.setPriority(NotificationCompat.PRIORITY_HIGH) | ||
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) | ||
.setContentIntent(contentPendingIntent) | ||
.build() | ||
|
||
this.notify(NOTIFICATION_ID, builder) | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
<resources> | ||
<string name="app_name">Quench</string> | ||
<string name="it_s_time_to_drink_water">It\'s time to drink water</string> | ||
</resources> |
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
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" /> | ||
</manifest> |
8 changes: 8 additions & 0 deletions
8
core/common/src/main/java/com/brandyodhiambo/common/domain/model/AlarmData.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.brandyodhiambo.common.domain.model | ||
|
||
import java.time.LocalDateTime | ||
|
||
data class AlarmData( | ||
val time: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,6 @@ fun String.toInitials(): String { | |
.mapNotNull { it.firstOrNull()?.toString() } | ||
.reduce { acc, s -> acc + s } | ||
} | ||
|
||
|
||
|
Oops, something went wrong.