Skip to content

Commit

Permalink
Implemented logic of ActivityNotifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Javinator9889 committed Jun 30, 2020
1 parent 272d327 commit f29cc65
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Expand Up @@ -23,18 +23,24 @@ import android.content.Context
import android.content.Intent
import androidx.annotation.StringRes
import androidx.emoji.text.EmojiCompat
import androidx.preference.PreferenceManager
import com.google.android.gms.location.ActivityTransition
import com.google.android.gms.location.ActivityTransitionResult
import com.google.android.gms.location.DetectedActivity
import com.javinator9889.handwashingreminder.R
import com.javinator9889.handwashingreminder.emoji.EmojiLoader
import com.javinator9889.handwashingreminder.notifications.NotificationsHandler
import com.javinator9889.handwashingreminder.utils.ACTIVITY_CHANNEL_ID
import com.javinator9889.handwashingreminder.utils.Preferences
import com.javinator9889.handwashingreminder.utils.calendar.CalendarUtils
import com.javinator9889.handwashingreminder.utils.goAsync
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import timber.log.Timber
import java.io.*
import java.util.*
import java.util.concurrent.TimeUnit

class ActivityReceiver : BroadcastReceiver() {
/**
Expand Down Expand Up @@ -82,6 +88,24 @@ class ActivityReceiver : BroadcastReceiver() {
detectedActivity: Int,
context: Context
) {
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
val timeInBetweenNotifications =
preferences.getInt(Preferences.ACTIVITY_MINIMUM_TIME, 15)
val timeFile = File(context.cacheDir, "activity.time")
var latestNotificationTime = 0L
withContext(Dispatchers.IO) {
if (timeFile.exists()) {
DataInputStream(FileInputStream(timeFile)).use {
latestNotificationTime = it.readLong()
}
}
}
val timeDifference = CalendarUtils.timeBetweenIn(
TimeUnit.MINUTES,
latestNotificationTime
)
if (timeDifference <= timeInBetweenNotifications)
return
val notificationContent = when (detectedActivity) {
DetectedActivity.WALKING ->
NotificationContent(
Expand Down Expand Up @@ -113,7 +137,8 @@ class ActivityReceiver : BroadcastReceiver() {
val emojiCompat = emojiLoader.await()
title = emojiCompat.process(title)
content = emojiCompat.process(content)
} catch (_: IllegalStateException) { }
} catch (_: IllegalStateException) {
}
withContext(Dispatchers.Main) {
notificationsHandler.createNotification(
iconDrawable = R.drawable.ic_stat_handwashing,
Expand All @@ -123,6 +148,11 @@ class ActivityReceiver : BroadcastReceiver() {
longContent = content
)
}
withContext(Dispatchers.IO) {
DataOutputStream(FileOutputStream(timeFile)).use {
it.writeLong(Calendar.getInstance().timeInMillis)
}
}
}
}

Expand Down
Expand Up @@ -19,6 +19,7 @@
package com.javinator9889.handwashingreminder.utils.calendar

import java.util.*
import java.util.concurrent.TimeUnit

object CalendarUtils {
val today: Calendar
Expand All @@ -41,4 +42,15 @@ object CalendarUtils {
today.add(Calendar.MONTH, -1)
today
}

fun timeBetweenIn(
unit: TimeUnit,
to: Long,
from: Long = Calendar.getInstance().timeInMillis
): Long = unit.convert(timeBetween(from, to), TimeUnit.MILLISECONDS)

fun timeBetween(
to: Long,
from: Long = Calendar.getInstance().timeInMillis
): Long = from - to
}

0 comments on commit f29cc65

Please sign in to comment.