Skip to content

Commit

Permalink
Handle 'hideNotification' in notification list and poller
Browse files Browse the repository at this point in the history
Signed-off-by: mueller-ma <[email protected]>
  • Loading branch information
mueller-ma committed Jul 16, 2024
1 parent fcd993c commit 9afff58
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.core.content.edit
import org.json.JSONArray
import org.json.JSONException
import org.openhab.habdroid.core.connection.ConnectionFactory
import org.openhab.habdroid.model.CloudNotificationType
import org.openhab.habdroid.model.toCloudNotification
import org.openhab.habdroid.util.HttpClient
import org.openhab.habdroid.util.PrefKeys
Expand Down Expand Up @@ -65,8 +66,18 @@ object NotificationPoller {
val newMessages = if (lastSeenIndex >= 0) messages.subList(0, lastSeenIndex) else messages
val notifHelper = NotificationHelper(context)

newMessages.forEach { message ->
notifHelper.showNotification(message)
// Reverse list, so old notifications are processed first and can be hidden by newer notifications.
newMessages.reversed().forEach { message ->
when (message.type) {
CloudNotificationType.NOTIFICATION -> notifHelper.showNotification(message)
CloudNotificationType.HIDE_NOTIFICATION -> {
if (!message.tag.isNullOrEmpty()) {
notifHelper.cancelNotificationsByTag(message.tag)
} else {
notifHelper.cancelNotificationById(message.id)
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,21 @@ data class CloudNotificationId internal constructor(
val notificationId get() = (referenceId ?: persistedId).hashCode()
}

@Parcelize
enum class CloudNotificationType : Parcelable {
NOTIFICATION,
HIDE_NOTIFICATION
}

fun String.toCloudNotificationType() = when (this) {
"notification" -> CloudNotificationType.NOTIFICATION
"hideNotification" -> CloudNotificationType.HIDE_NOTIFICATION
else -> null
}

@Parcelize
data class CloudNotification internal constructor(
val type: CloudNotificationType,
val id: CloudNotificationId,
val title: String,
val message: String,
Expand Down Expand Up @@ -117,9 +130,11 @@ fun JSONObject.toCloudNotification(): CloudNotification {

val payload = optJSONObject("payload")
return CloudNotification(
// Old notifications don't contain "type", so fallback to normal notifications here.
type = payload?.optString("type")?.toCloudNotificationType() ?: CloudNotificationType.NOTIFICATION,
id = CloudNotificationId(getString("_id"), payload?.optStringOrNull("reference-id")),
title = payload?.optString("title").orEmpty(),
message = payload?.getString("message") ?: getString("message"),
message = payload?.optString("message") ?: optString("message"),
createdTimestamp = created,
icon = payload?.optStringOrNull("icon").toOH2IconResource() ?: optStringOrNull("icon").toOH2IconResource(),
tag = payload?.optStringOrNull("tag") ?: optStringOrNull("severity"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import org.json.JSONArray
import org.json.JSONException
import org.openhab.habdroid.R
import org.openhab.habdroid.core.connection.ConnectionFactory
import org.openhab.habdroid.model.CloudNotificationType
import org.openhab.habdroid.model.ServerConfiguration
import org.openhab.habdroid.model.toCloudNotification
import org.openhab.habdroid.util.HttpClient
Expand Down Expand Up @@ -152,6 +153,7 @@ class CloudNotificationListFragment : Fragment(), View.OnClickListener, SwipeRef
try {
val response = conn.httpClient.get(url).asText().response
val items = JSONArray(response).map { obj -> obj.toCloudNotification() }
.filter { notification -> notification.type == CloudNotificationType.NOTIFICATION }
Log.d(TAG, "Notifications request success, got ${items.size} items")
loadOffset += items.size
adapter.addLoadedItems(items, items.size == PAGE_SIZE)
Expand Down
11 changes: 10 additions & 1 deletion mobile/src/main/java/org/openhab/habdroid/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,8 @@ class MainActivity : AbstractBaseActivity(), ConnectionFactory.UpdateListener {
connection != null && SpeechRecognizer.isRecognitionAvailable(this)
val debugItems = listOf(
R.id.mainmenu_debug_crash,
R.id.mainmenu_debug_clear_mtm
R.id.mainmenu_debug_clear_mtm,
R.id.mainmenu_poll_notifications
)
debugItems.forEach {
menu.findItem(it).isVisible = BuildConfig.DEBUG
Expand Down Expand Up @@ -454,6 +455,14 @@ class MainActivity : AbstractBaseActivity(), ConnectionFactory.UpdateListener {
}
true
}
R.id.mainmenu_poll_notifications -> {
if (CloudMessagingHelper.needsPollingForNotifications(this)) {
launch {
CloudMessagingHelper.pollForNotifications(this@MainActivity)
}
}
true
}
else -> super.onOptionsItemSelected(item)
}
}
Expand Down
6 changes: 6 additions & 0 deletions mobile/src/main/res/menu/main_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
android:title="@string/mainmenu_openhab_voice_recognition"
app:showAsAction="always"
app:iconTint="?android:textColorSecondary" />
<item
android:id="@+id/mainmenu_poll_notifications"
android:orderInCategory="1000"
android:title="Poll notifications"
app:showAsAction="never"
tools:ignore="HardcodedText" />
<item
android:id="@+id/mainmenu_debug_crash"
android:orderInCategory="1000"
Expand Down

0 comments on commit 9afff58

Please sign in to comment.