Skip to content

Commit

Permalink
Merge pull request #15 from brandy-kay/feature/alarm_manager
Browse files Browse the repository at this point in the history
  • Loading branch information
brandyodhiambo authored Aug 1, 2023
2 parents 19acb23 + 5ec646b commit 2fde1e4
Show file tree
Hide file tree
Showing 21 changed files with 408 additions and 33 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ android {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
"proguard-rules.pro",
)
}
}
Expand Down
5 changes: 5 additions & 0 deletions app/src/debug/res/drawable/ic_notification.xml
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>
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

<uses-permission android:name="android.permission.INTERNET"/>

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />

<application
android:name=".QuenchApp"
android:allowBackup="true"
Expand All @@ -27,6 +30,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".util.AlarmReceiver" />
</application>

</manifest>
20 changes: 19 additions & 1 deletion app/src/main/java/com/brandyodhiambo/quench/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,31 @@ import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Modifier
import androidx.navigation.NavDestination
import androidx.navigation.NavDestination.Companion.hierarchy
import com.brandyodhiambo.common.domain.model.AlarmData
import com.brandyodhiambo.designsystem.theme.QuenchTheme
import com.brandyodhiambo.home.presentation.home_screen.HomeViewModel
import com.brandyodhiambo.quench.R
import com.brandyodhiambo.quench.navigation.FeatureNavigator
import com.brandyodhiambo.quench.navigation.NavGraphs
import com.brandyodhiambo.quench.util.AlarmSchedularImpl
import com.brandyodhiambo.quench.util.createChannel
import com.google.accompanist.navigation.animation.rememberAnimatedNavController
import com.ramcosta.composedestinations.DestinationsNavHost
import com.ramcosta.composedestinations.navigation.dependency
import com.ramcosta.composedestinations.rememberNavHostEngine
import com.ramcosta.composedestinations.scope.DestinationScope
import com.ramcosta.composedestinations.spec.NavGraphSpec
import dagger.hilt.android.AndroidEntryPoint
import java.time.LocalDateTime

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
Expand All @@ -29,13 +37,23 @@ class MainActivity : ComponentActivity() {
@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val viewModel: HomeViewModel by viewModels()
setContent {
createChannel(this)
QuenchTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background,
) {
val reminderTimeFromDb = viewModel.reminderTime.observeAsState()
val hours = reminderTimeFromDb.value?.hour ?: 0
val minutes = reminderTimeFromDb.value?.minute ?: 0
val scheduler = AlarmSchedularImpl(this)
val alarmItem = AlarmData(
time = LocalDateTime.now().withHour(hours).withMinute(minutes),
message = getString(R.string.it_s_time_to_drink_water),
)
alarmItem.let(scheduler::schedule)
val navController = rememberAnimatedNavController()
val navHostEngine = rememberNavHostEngine()

Expand Down
26 changes: 26 additions & 0 deletions app/src/main/java/com/brandyodhiambo/quench/util/AlarmReceiver.kt
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,
)
}
}
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)
}
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,
),
)
}
}
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)
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
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>
2 changes: 1 addition & 1 deletion buildSrc/src/main/java/AndroidConfig.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
object AndroidConfig{
const val applicationId = "com.brandyodhiambo.quench"
const val minSdk = 21
const val minSdk = 26
const val targetSdk = 33
const val compileSdk = 33
const val versionCode = 1
Expand Down
1 change: 1 addition & 0 deletions core/common/src/main/AndroidManifest.xml
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>
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,
)
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ fun String.toInitials(): String {
.mapNotNull { it.firstOrNull()?.toString() }
.reduce { acc, s -> acc + s }
}



Loading

0 comments on commit 2fde1e4

Please sign in to comment.