Clarify the data classes for the Json parsing

This commit is contained in:
Benoit Marty 2022-06-01 19:51:26 +02:00 committed by Benoit Marty
parent 45768c5251
commit cdfb728a73
4 changed files with 140 additions and 34 deletions

View File

@ -24,12 +24,14 @@ import android.widget.Toast
import androidx.lifecycle.Lifecycle import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ProcessLifecycleOwner import androidx.lifecycle.ProcessLifecycleOwner
import androidx.localbroadcastmanager.content.LocalBroadcastManager import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.BuildConfig import im.vector.app.BuildConfig
import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.network.WifiDetector import im.vector.app.core.network.WifiDetector
import im.vector.app.core.pushers.model.PushData
import im.vector.app.core.pushers.model.PushDataFcm
import im.vector.app.core.pushers.model.PushDataUnifiedPush
import im.vector.app.core.pushers.model.toPushData
import im.vector.app.core.services.GuardServiceStarter import im.vector.app.core.services.GuardServiceStarter
import im.vector.app.features.notifications.NotifiableEventResolver import im.vector.app.features.notifications.NotifiableEventResolver
import im.vector.app.features.notifications.NotificationDrawerManager import im.vector.app.features.notifications.NotificationDrawerManager
@ -51,24 +53,6 @@ import org.unifiedpush.android.connector.MessagingReceiver
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
@JsonClass(generateAdapter = true)
data class UnifiedPushMessage(
val notification: Notification = Notification()
)
@JsonClass(generateAdapter = true)
data class Notification(
@Json(name = "event_id") val eventId: String = "",
@Json(name = "room_id") val roomId: String = "",
var unread: Int = 0,
val counts: Counts = Counts()
)
@JsonClass(generateAdapter = true)
data class Counts(
val unread: Int = 0
)
private val loggerTag = LoggerTag("Push", LoggerTag.SYNC) private val loggerTag = LoggerTag("Push", LoggerTag.SYNC)
/** /**
@ -113,17 +97,14 @@ class VectorMessagingReceiver : MessagingReceiver() {
} }
val moshi = MatrixJsonParser.getMoshi() val moshi = MatrixJsonParser.getMoshi()
val notification: Notification = if (unifiedPushHelper.isEmbeddedDistributor()) { val pushData = if (unifiedPushHelper.isEmbeddedDistributor()) {
moshi.adapter(Notification::class.java).fromJson(sMessage) moshi.adapter(PushDataFcm::class.java).fromJson(sMessage)?.toPushData()
} else { } else {
val data = moshi.adapter(UnifiedPushMessage::class.java).fromJson(sMessage) moshi.adapter(PushDataUnifiedPush::class.java).fromJson(sMessage)?.toPushData()
data?.notification?.also { } ?: return Unit.also { Timber.tag(loggerTag.value).w("Invalid received data Json format") }
it.unread = it.counts.unread
}
} ?: return Unit.also { Timber.w("Invalid received data") }
// Diagnostic Push // Diagnostic Push
if (notification.eventId == PushersManager.TEST_EVENT_ID) { if (pushData.eventId == PushersManager.TEST_EVENT_ID) {
val intent = Intent(NotificationUtils.PUSH_ACTION) val intent = Intent(NotificationUtils.PUSH_ACTION)
LocalBroadcastManager.getInstance(context).sendBroadcast(intent) LocalBroadcastManager.getInstance(context).sendBroadcast(intent)
return return
@ -139,7 +120,7 @@ class VectorMessagingReceiver : MessagingReceiver() {
// we are in foreground, let the sync do the things? // we are in foreground, let the sync do the things?
Timber.tag(loggerTag.value).d("PUSH received in a foreground state, ignore") Timber.tag(loggerTag.value).d("PUSH received in a foreground state, ignore")
} else { } else {
onMessageReceivedInternal(notification) onMessageReceivedInternal(pushData)
} }
} }
} }
@ -197,12 +178,12 @@ class VectorMessagingReceiver : MessagingReceiver() {
/** /**
* Internal receive method. * Internal receive method.
* *
* @param notification Notification containing message data. * @param pushData Object containing message data.
*/ */
private fun onMessageReceivedInternal(notification: Notification) { private fun onMessageReceivedInternal(pushData: PushData) {
try { try {
if (BuildConfig.LOW_PRIVACY_LOG_ENABLE) { if (BuildConfig.LOW_PRIVACY_LOG_ENABLE) {
Timber.tag(loggerTag.value).d("## onMessageReceivedInternal() : $notification") Timber.tag(loggerTag.value).d("## onMessageReceivedInternal() : $pushData")
} else { } else {
Timber.tag(loggerTag.value).d("## onMessageReceivedInternal()") Timber.tag(loggerTag.value).d("## onMessageReceivedInternal()")
} }
@ -212,12 +193,12 @@ class VectorMessagingReceiver : MessagingReceiver() {
if (session == null) { if (session == null) {
Timber.tag(loggerTag.value).w("## Can't sync from push, no current session") Timber.tag(loggerTag.value).w("## Can't sync from push, no current session")
} else { } else {
if (isEventAlreadyKnown(notification.eventId, notification.roomId)) { if (isEventAlreadyKnown(pushData.eventId, pushData.roomId)) {
Timber.tag(loggerTag.value).d("Ignoring push, event already known") Timber.tag(loggerTag.value).d("Ignoring push, event already known")
} else { } else {
// Try to get the Event content faster // Try to get the Event content faster
Timber.tag(loggerTag.value).d("Requesting event in fast lane") Timber.tag(loggerTag.value).d("Requesting event in fast lane")
getEventFastLane(session, notification.roomId, notification.eventId) getEventFastLane(session, pushData.roomId, pushData.eventId)
Timber.tag(loggerTag.value).d("Requesting background sync") Timber.tag(loggerTag.value).d("Requesting background sync")
session.syncService().requireBackgroundSync() session.syncService().requireBackgroundSync()

View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.core.pushers.model
data class PushData(
val eventId: String,
val roomId: String,
var unread: Int,
)

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.core.pushers.model
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
/**
* In this case, the format is:
* <pre>
* {
* "event_id":"$anEventId",
* "room_id":"!aRoomId",
* "unread":"1",
* "prio":"high",
* }
* </pre>
*/
@JsonClass(generateAdapter = true)
data class PushDataFcm(
@Json(name = "event_id") val eventId: String = "",
@Json(name = "room_id") val roomId: String = "",
@Json(name = "unread") var unread: Int = 0,
)
fun PushDataFcm.toPushData() = PushData(
eventId = eventId,
roomId = roomId,
unread = unread
)

View File

@ -0,0 +1,58 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.core.pushers.model
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
/**
* In this case, the format is:
* <pre>
* {
* "notification":{
* "event_id":"$anEventId",
* "room_id":"!aRoomId",
* "counts":{
* "unread":1
* },
* "prio":"high",
* }
* }
* </pre>
*/
@JsonClass(generateAdapter = true)
data class PushDataUnifiedPush(
@Json(name = "notification") val notification: PushDataUnifiedPushNotification = PushDataUnifiedPushNotification()
)
@JsonClass(generateAdapter = true)
data class PushDataUnifiedPushNotification(
@Json(name = "event_id") val eventId: String = "",
@Json(name = "room_id") val roomId: String = "",
@Json(name = "counts") var counts: PushDataUnifiedPushCounts = PushDataUnifiedPushCounts(),
)
@JsonClass(generateAdapter = true)
data class PushDataUnifiedPushCounts(
@Json(name = "unread") val unread: Int = 0
)
fun PushDataUnifiedPush.toPushData() = PushData(
eventId = notification.eventId,
roomId = notification.roomId,
unread = notification.counts.unread
)