diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 19a171c..7558e6a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -55,6 +55,14 @@ + + + + + + diff --git a/app/src/main/java/com/javinator9889/handwashingreminder/gms/activity/ActivityReceiver.kt b/app/src/main/java/com/javinator9889/handwashingreminder/gms/activity/ActivityReceiver.kt index cd52308..d7b2845 100644 --- a/app/src/main/java/com/javinator9889/handwashingreminder/gms/activity/ActivityReceiver.kt +++ b/app/src/main/java/com/javinator9889/handwashingreminder/gms/activity/ActivityReceiver.kt @@ -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) { diff --git a/app/src/main/java/com/javinator9889/handwashingreminder/jobs/HandsWashedReceiver.kt b/app/src/main/java/com/javinator9889/handwashingreminder/jobs/HandsWashedReceiver.kt new file mode 100644 index 0000000..08b0fbd --- /dev/null +++ b/app/src/main/java/com/javinator9889/handwashingreminder/jobs/HandsWashedReceiver.kt @@ -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() + } + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/javinator9889/handwashingreminder/jobs/workers/ScheduledNotificationWorker.kt b/app/src/main/java/com/javinator9889/handwashingreminder/jobs/workers/ScheduledNotificationWorker.kt index e48872b..ac7f67e 100644 --- a/app/src/main/java/com/javinator9889/handwashingreminder/jobs/workers/ScheduledNotificationWorker.kt +++ b/app/src/main/java/com/javinator9889/handwashingreminder/jobs/workers/ScheduledNotificationWorker.kt @@ -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 @@ -59,7 +65,16 @@ 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, @@ -67,7 +82,12 @@ abstract class ScheduledNotificationWorker(context: Context) { 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( diff --git a/app/src/main/java/com/javinator9889/handwashingreminder/notifications/NotificationsHandler.kt b/app/src/main/java/com/javinator9889/handwashingreminder/notifications/NotificationsHandler.kt index a59953e..5b1c3f7 100644 --- a/app/src/main/java/com/javinator9889/handwashingreminder/notifications/NotificationsHandler.kt +++ b/app/src/main/java/com/javinator9889/handwashingreminder/notifications/NotificationsHandler.kt @@ -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 @@ -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 @@ -89,7 +90,9 @@ class NotificationsHandler( context.getText(title), context.getText(content), priority, - longContentProcessed + longContentProcessed, + action, + notificationId ) } @@ -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)) { @@ -121,7 +126,9 @@ class NotificationsHandler( title, content, priority, - longContent + longContent, + action, + notificationId ) } @@ -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 = @@ -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), @@ -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) } } } @@ -206,4 +219,10 @@ class NotificationsHandler( private fun createChannelRequired() = preferences.getBoolean(Preferences.CREATE_CHANNEL_KEY, true) -} \ No newline at end of file +} + +data class Action( + @DrawableRes val drawable: Int, + val text: CharSequence, + val pendingIntent: PendingIntent +) \ No newline at end of file diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index ca5ad10..8e25685 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -295,4 +295,6 @@ No se mostrará ninguna animación Error al cargar información sobre las noticias Ajustes de notificaciones + ¡Acabo de lavarlas! + ¡Yupiiii! 🙌😱 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b08dcaf..aff6b05 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -323,4 +323,6 @@ No animations will be played Error while loading news data Edit notification settings + Just washed them! + Hurray! 🙌😱