Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Custom action for incrementing the hand-washed value when clicking no…
…tification
  • Loading branch information
Javinator9889 committed Jul 2, 2020
1 parent 07aeee7 commit ea713e1
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 13 deletions.
8 changes: 8 additions & 0 deletions app/src/main/AndroidManifest.xml
Expand Up @@ -55,6 +55,14 @@
</intent-filter>
</receiver>

<receiver android:name=".jobs.HandsWashedReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.javinator9889.handwashingreminder.HANDSWASHED_EVENT" />
</intent-filter>
</receiver>

<receiver
android:name=".jobs.UpdateReceiver"
android:enabled="true">
Expand Down
Expand Up @@ -146,7 +146,8 @@ class ActivityReceiver : BroadcastReceiver() {
largeIcon = R.drawable.handwashing_app_logo,
title = title,
content = content,
longContent = content
longContent = content,
notificationId = 2
)
}
withContext(Dispatchers.IO) {
Expand Down
@@ -0,0 +1,77 @@
/*
* Copyright © 2020 - present | Handwashing reminder by Javinator9889
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*
* Created by Javinator9889 on 2/07/20 - Handwashing reminder.
*/
package com.javinator9889.handwashingreminder.jobs

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.widget.Toast
import androidx.core.app.NotificationManagerCompat
import com.javinator9889.handwashingreminder.R
import com.javinator9889.handwashingreminder.data.repositories.HandwashingRepository
import com.javinator9889.handwashingreminder.data.room.db.HandwashingDatabase
import com.javinator9889.handwashingreminder.data.room.entities.Handwashing
import com.javinator9889.handwashingreminder.emoji.EmojiLoader
import com.javinator9889.handwashingreminder.utils.calendar.CalendarUtils
import com.javinator9889.handwashingreminder.utils.goAsync
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

internal const val HANDS_WASHED_CODE = 128
internal const val HANDS_WASHED_ACTION =
"com.javinator9889.handwashingreminder.HANDSWASHED_EVENT"

class HandsWashedReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val emojiLoader = EmojiLoader.loadAsync(context)
val repository =
with(HandwashingDatabase.getDatabase(context).handwashingDao()) {
HandwashingRepository(this)
}
with(NotificationManagerCompat.from(context)) {
cancel(1)
}
goAsync {
val createdItem = withContext(Dispatchers.IO) {
repository.get(CalendarUtils.today.time)
}
if (createdItem == null) {
withContext(Dispatchers.IO) {
repository.create(Handwashing(CalendarUtils.today.time, 0))
}
}
withContext(Dispatchers.IO) {
repository.increment(CalendarUtils.today.time)
}
val emojiCompat = emojiLoader.await()
val toastText = context.getText(R.string.hurray)
withContext(Dispatchers.Main) {
try {
Toast.makeText(
context,
emojiCompat.process(toastText),
Toast.LENGTH_LONG
).show()
} catch (_: IllegalStateException) {
Toast.makeText(context, toastText, Toast.LENGTH_LONG).show()
}
}
}
}
}
Expand Up @@ -18,15 +18,21 @@
*/
package com.javinator9889.handwashingreminder.jobs.workers

import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import androidx.annotation.ArrayRes
import androidx.annotation.StringRes
import androidx.core.app.NotificationCompat
import com.javinator9889.handwashingreminder.R
import com.javinator9889.handwashingreminder.application.HandwashingApplication
import com.javinator9889.handwashingreminder.emoji.EmojiLoader
import com.javinator9889.handwashingreminder.jobs.HANDS_WASHED_ACTION
import com.javinator9889.handwashingreminder.jobs.HANDS_WASHED_CODE
import com.javinator9889.handwashingreminder.jobs.HandsWashedReceiver
import com.javinator9889.handwashingreminder.jobs.alarms.AlarmHandler
import com.javinator9889.handwashingreminder.jobs.alarms.Alarms
import com.javinator9889.handwashingreminder.notifications.Action
import com.javinator9889.handwashingreminder.notifications.NotificationsHandler
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
Expand Down Expand Up @@ -59,15 +65,29 @@ abstract class ScheduledNotificationWorker(context: Context) {
try {
title = emojiCompat.process(title)
content = emojiCompat.process(content)
} catch (_: IllegalStateException) { }
} catch (_: IllegalStateException) {
}
val washedPendingIntent = PendingIntent.getBroadcast(
context,
HANDS_WASHED_CODE,
Intent(context, HandsWashedReceiver::class.java).apply {
action = HANDS_WASHED_ACTION
},
PendingIntent.FLAG_UPDATE_CURRENT
)
withContext(Dispatchers.Main) {
notificationsHandler.createNotification(
iconDrawable = R.drawable.ic_stat_handwashing,
largeIcon = R.drawable.handwashing_app_logo,
title = title,
content = content,
longContent = content,
priority = NotificationCompat.PRIORITY_MAX
priority = NotificationCompat.PRIORITY_MAX,
action = Action(
R.drawable.ic_stat_handwashing,
getString(R.string.just_washed),
washedPendingIntent
)
)
}
Timber.d(
Expand Down
Expand Up @@ -48,12 +48,11 @@ class NotificationsHandler(
private val channelId: String,
private val channelName: String = "",
private val channelDesc: String = "",
private val groupId: String = "",
private val groupName: String = ""
groupId: String = "",
groupName: String = ""
) {
private val preferences: SharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context)
private val notificationId = 1
private val vibrationPattern = longArrayOf(300L, 300L, 300L, 300L)
private val manager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as
Expand All @@ -79,7 +78,9 @@ class NotificationsHandler(
@StringRes title: Int,
@StringRes content: Int,
priority: Int = NotificationCompat.PRIORITY_DEFAULT,
@StringRes longContent: Int = -1
@StringRes longContent: Int = -1,
action: Action? = null,
notificationId: Int = -1
) {
val longContentProcessed =
if (longContent != -1) context.getText(longContent) else null
Expand All @@ -89,7 +90,9 @@ class NotificationsHandler(
context.getText(title),
context.getText(content),
priority,
longContentProcessed
longContentProcessed,
action,
notificationId
)
}

Expand All @@ -99,7 +102,9 @@ class NotificationsHandler(
title: CharSequence,
content: CharSequence,
priority: Int = NotificationCompat.PRIORITY_DEFAULT,
longContent: CharSequence? = null
longContent: CharSequence? = null,
action: Action? = null,
notificationId: Int = -1
) {
val bitmapIcon = if (isAtLeast(AndroidVersion.JELLY_BEAN_MR2)) {
if (isAtLeast(AndroidVersion.P)) {
Expand All @@ -121,7 +126,9 @@ class NotificationsHandler(
title,
content,
priority,
longContent
longContent,
action,
notificationId
)
}

Expand All @@ -131,7 +138,9 @@ class NotificationsHandler(
title: CharSequence,
content: CharSequence,
priority: Int = NotificationCompat.PRIORITY_DEFAULT,
longContent: CharSequence? = null
longContent: CharSequence? = null,
action: Action? = null,
notificationId: Int = -1
) {
val notifyIntent = Intent(context, LauncherActivity::class.java).apply {
flags =
Expand All @@ -158,6 +167,9 @@ class NotificationsHandler(
setPriority(priority)
setVibrate(vibrationPattern)
setContentIntent(notifyPendingIntent)
action?.let {
addAction(action.drawable, action.text, action.pendingIntent)
}
addAction(
R.drawable.ic_share_black,
context.getString(R.string.share),
Expand All @@ -170,7 +182,8 @@ class NotificationsHandler(
build()
}.let {
with(NotificationManagerCompat.from(context)) {
notify(notificationId, it)
val id = if (notificationId == -1) 1 else notificationId
notify(id, it)
}
}
}
Expand Down Expand Up @@ -206,4 +219,10 @@ class NotificationsHandler(

private fun createChannelRequired() =
preferences.getBoolean(Preferences.CREATE_CHANNEL_KEY, true)
}
}

data class Action(
@DrawableRes val drawable: Int,
val text: CharSequence,
val pendingIntent: PendingIntent
)
2 changes: 2 additions & 0 deletions app/src/main/res/values-es/strings.xml
Expand Up @@ -295,4 +295,6 @@
<string name="animations_off">No se mostrará ninguna animación</string>
<string name="news_error">Error al cargar información sobre las noticias</string>
<string name="notification_settings">Ajustes de notificaciones</string>
<string name="just_washed">¡Acabo de lavarlas!</string>
<string name="hurray">¡Yupiiii! &#128588;&#128561;</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Expand Up @@ -323,4 +323,6 @@
<string name="animations_off">No animations will be played</string>
<string name="news_error">Error while loading news data</string>
<string name="notification_settings">Edit notification settings</string>
<string name="just_washed">Just washed them!</string>
<string name="hurray">Hurray! &#128588;&#128561;</string>
</resources>

0 comments on commit ea713e1

Please sign in to comment.