Skip to content

Commit

Permalink
Created IntentFilter for ActivityReceiver.kt Broadcast (issue #12) - …
Browse files Browse the repository at this point in the history
…activity recognition notifications should be working
  • Loading branch information
Javinator9889 committed Jun 27, 2020
1 parent e30df0c commit c132263
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 31 deletions.
6 changes: 5 additions & 1 deletion app/src/main/AndroidManifest.xml
Expand Up @@ -49,7 +49,11 @@
<receiver
android:name=".gms.activity.ActivityReceiver"
android:enabled="true"
android:exported="true" />
android:exported="true">
<intent-filter>
<action android:name="com.javinator9889.handwashingreminder.TRANSITIONS_RECEIVER_ACTION" />
</intent-filter>
</receiver>

<receiver
android:name=".jobs.UpdateReceiver"
Expand Down
Expand Up @@ -33,7 +33,7 @@ import timber.log.Timber

internal const val ACTIVITY_REQUEST_CODE = 64
internal const val TRANSITIONS_RECEIVER_ACTION =
"${BuildConfig.APPLICATION_ID}/TRANSITIONS_RECEIVER_ACTION"
"${BuildConfig.APPLICATION_ID}.TRANSITIONS_RECEIVER_ACTION"
internal val TRANSITIONS = listOf<ActivityTransition>(
ActivityTransition.Builder()
.setActivityType(DetectedActivity.IN_VEHICLE)
Expand Down Expand Up @@ -95,8 +95,8 @@ class ActivityHandler private constructor(private val context: Context) {
}

private fun createPendingIntent(): PendingIntent =
with(Intent(TRANSITIONS_RECEIVER_ACTION)) {
setClass(context, ActivityReceiver::class.java)
with(Intent(context, ActivityReceiver::class.java)) {
action = TRANSITIONS_RECEIVER_ACTION
PendingIntent.getBroadcast(
context,
ACTIVITY_REQUEST_CODE,
Expand Down
Expand Up @@ -21,7 +21,6 @@ package com.javinator9889.handwashingreminder.gms.activity
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.text.TextUtils
import androidx.annotation.StringRes
import androidx.emoji.text.EmojiCompat
import com.google.android.gms.location.ActivityTransition
Expand All @@ -35,42 +34,45 @@ import com.javinator9889.handwashingreminder.utils.goAsync
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import timber.log.Timber

class ActivityReceiver : BroadcastReceiver() {
/**
* {@inheritDoc}
*/
override fun onReceive(context: Context, intent: Intent) {
if (ActivityTransitionResult.hasResult(intent)
&& TextUtils.equals(TRANSITIONS_RECEIVER_ACTION, intent.action)) {
Timber.d("Detected user activity - ${intent.action}")
if (ActivityTransitionResult.hasResult(intent)) {
val emojiLoader = EmojiLoader.loadAsync(context)
val result = ActivityTransitionResult.extractResult(intent)!!
for (event in result.transitionEvents) {
if (event.transitionType !=
ActivityTransition.ACTIVITY_TRANSITION_EXIT ||
event.activityType == DetectedActivity.UNKNOWN
)
continue
val notificationHandler = NotificationsHandler(
context,
ACTIVITY_CHANNEL_ID,
context.getString(
R.string.activity_notification_channel_name
),
context.getString(
R.string.activity_notification_channel_desc
val result = ActivityTransitionResult.extractResult(intent)
result?.let {
for (event in result.transitionEvents) {
if (event.transitionType !=
ActivityTransition.ACTIVITY_TRANSITION_EXIT ||
event.activityType == DetectedActivity.UNKNOWN
)
)
goAsync {
putNotification(
notificationHandler,
emojiLoader,
event.activityType,
context
continue
val notificationHandler = NotificationsHandler(
context,
ACTIVITY_CHANNEL_ID,
context.getString(
R.string.activity_notification_channel_name
),
context.getString(
R.string.activity_notification_channel_desc
)
)
goAsync {
putNotification(
notificationHandler,
emojiLoader,
event.activityType,
context
)
}
break
}
break
}
} ?: Timber.w("Received unmatched activity - $intent")
}
}

Expand Down

0 comments on commit c132263

Please sign in to comment.