Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix notification handling #109

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ apply plugin: 'com.google.gms.google-services'
apply plugin: 'io.gitlab.arturbosch.detekt'
apply plugin: 'kotlin-android'
apply plugin: 'org.jlleitschuh.gradle.ktlint'
apply plugin: 'org.jetbrains.kotlin.plugin.serialization'

repositories {
google()
Expand Down Expand Up @@ -74,6 +75,10 @@ android {
}
}

buildFeatures {
buildConfig = true
}

applicationVariants.configureEach { variant ->
variant.outputs.configureEach { output ->
output.outputFileName = "bisq-${variant.name}.apk"
Expand Down Expand Up @@ -125,7 +130,7 @@ dependencies {
implementation 'com.google.firebase:firebase-analytics-ktx:22.1.2'
implementation 'com.google.firebase:firebase-messaging:24.1.0'

implementation 'com.google.code.gson:gson:2.10.1'
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3'

Expand Down
23 changes: 10 additions & 13 deletions app/src/androidTest/java/bisq/android/tests/NotificationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ import bisq.android.rules.ScreenshotRule
import bisq.android.screens.NotificationTableScreen
import bisq.android.services.BisqFirebaseMessagingService
import bisq.android.util.CryptoUtil
import bisq.android.util.DateUtil
import com.google.firebase.messaging.RemoteMessage
import com.google.gson.GsonBuilder
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.assertj.core.api.Assertions.assertThat
import org.junit.After
import org.junit.Before
Expand Down Expand Up @@ -168,20 +168,17 @@ class NotificationTest {
private fun buildBisqNotification(): BisqNotification {
val now = Date()
val tradeId = (100000..999999).random()
val bisqNotification = BisqNotification()
bisqNotification.type = NotificationType.TRADE.name
bisqNotification.title = "Trade confirmed"
bisqNotification.message = "The trade with ID $tradeId is confirmed."
bisqNotification.sentDate = now.time - 1000 * 60
bisqNotification.receivedDate = now.time
return bisqNotification
return BisqNotification(
type = NotificationType.TRADE.name,
title = "Trade confirmed",
message = "The trade with ID $tradeId is confirmed.",
sentDate = now.time - 1000 * 60,
receivedDate = now.time
)
}

private fun serializeNotificationPayload(bisqNotification: BisqNotification): String {
val gsonBuilder = GsonBuilder()
gsonBuilder.registerTypeAdapter(Date::class.java, DateUtil())
val gson = gsonBuilder.create()
return gson.toJson(bisqNotification)
return Json.encodeToString(bisqNotification)
}

private fun buildRemoteMessage(bisqNotification: BisqNotification): RemoteMessage {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<activity android:name="bisq.android.ui.settings.SettingsActivity" />
<activity android:name="bisq.android.ui.notification.NotificationTableActivity" />
<activity android:name="bisq.android.ui.notification.NotificationDetailActivity" />
<activity android:name="bisq.android.ui.debug.DebugActivity" />

<service
android:name="bisq.android.services.BisqFirebaseMessagingService"
Expand Down
79 changes: 79 additions & 0 deletions app/src/main/java/bisq/android/Logging.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq 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 Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.android

import android.util.Log
import bisq.android.database.DebugLog
import bisq.android.database.DebugLogLevel
import bisq.android.database.DebugLogRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class Logging {
companion object {
private const val TAG = "Logging"
}

// Attempt to initialize debugRepository with the application context.
// If the context is unavailable (e.g., in unit tests where Application is not initialized),
// catch the exception and log a warning instead of throwing a NullPointerException.
// This ensures that tests still work since trying to provide a mocked context is not straight forward.
@Suppress("SwallowedException", "TooGenericExceptionCaught")
private val debugRepository: DebugLogRepository? = try {
val context = Application.applicationContext()
DebugLogRepository(context)
} catch (e: NullPointerException) {
Log.w(TAG, "Skipping debugRepository initialization due to missing context")
null
}

fun debug(tag: String, msg: String) {
Log.d(tag, msg)
insert(DebugLogLevel.DEBUG, msg)
}

fun info(tag: String, msg: String) {
Log.i(tag, msg)
insert(DebugLogLevel.INFO, msg)
}

fun warn(tag: String, msg: String) {
Log.w(tag, msg)
insert(DebugLogLevel.WARN, msg)
}

fun error(tag: String, msg: String) {
Log.e(tag, msg)
insert(DebugLogLevel.ERROR, msg)
}

private fun insert(level: DebugLogLevel, msg: String) {
debugRepository?.let {
CoroutineScope(Dispatchers.IO).launch {
it.insert(
DebugLog(
timestamp = System.currentTimeMillis(),
level = level,
text = msg
)
)
}
} ?: Log.w(TAG, "Skipping log insert; debugRepository is unavailable")
}
}
18 changes: 2 additions & 16 deletions app/src/main/java/bisq/android/database/BisqNotification.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,39 @@ package bisq.android.database
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
import kotlinx.serialization.Serializable

@Entity
@Serializable
data class BisqNotification(
@PrimaryKey(autoGenerate = true)
@SerializedName("uid")
var uid: Int = 0,

@ColumnInfo(name = "version")
@SerializedName("version")
var version: Int = 0,

@ColumnInfo(name = "type")
@SerializedName("type")
var type: String? = null,

@ColumnInfo(name = "title")
@SerializedName("title")
var title: String? = null,

@ColumnInfo(name = "message")
@SerializedName("message")
var message: String? = null,

@ColumnInfo(name = "actionRequired")
@SerializedName("actionRequired")
var actionRequired: String? = null,

@ColumnInfo(name = "txId")
@SerializedName("txId")
var txId: String? = null,

@ColumnInfo(name = "receivedDate")
@SerializedName("receivedDate")
var receivedDate: Long = 0,

@ColumnInfo(name = "sentDate")
@SerializedName("sentDate")
var sentDate: Long = 0,

@ColumnInfo(name = "read")
@SerializedName("read")
var read: Boolean = false
) {
override fun equals(other: Any?): Boolean {
Expand All @@ -81,8 +71,4 @@ data class BisqNotification(
return listOf(version, type, title, message, actionRequired, txId, sentDate)
.hashCode()
}

override fun toString(): String {
return "BisqNotification[" + Gson().toJson(this) + "]"
}
}
35 changes: 35 additions & 0 deletions app/src/main/java/bisq/android/database/DebugLog.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq 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 Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.android.database

import androidx.room.Entity
import androidx.room.PrimaryKey
import bisq.android.util.DateUtil

@Entity
data class DebugLog(
@PrimaryKey(autoGenerate = true)
var id: Int = 0,
var timestamp: Long,
var level: DebugLogLevel,
var text: String
) {
override fun toString(): String {
return "${DateUtil.format(timestamp)} [$level] $text"
}
}
35 changes: 35 additions & 0 deletions app/src/main/java/bisq/android/database/DebugLogDao.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq 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 Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.android.database

import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query

@Dao
interface DebugLogDao {
@get:Query("SELECT * FROM DebugLog ORDER BY timestamp DESC")
val all: LiveData<List<DebugLog>>

@Insert
suspend fun insert(debugLog: DebugLog): Long

@Query("DELETE FROM DebugLog")
suspend fun deleteAll()
}
48 changes: 48 additions & 0 deletions app/src/main/java/bisq/android/database/DebugLogDatabase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq 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 Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.android.database

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

@Database(entities = [DebugLog::class], version = 1, exportSchema = false)
abstract class DebugLogDatabase : RoomDatabase() {

abstract fun debugLogDao(): DebugLogDao

companion object {

private var instance: DebugLogDatabase? = null

fun getDatabase(context: Context): DebugLogDatabase {
if (instance == null) {
synchronized(DebugLogDatabase::class.java) {
if (instance == null) {
instance = Room.databaseBuilder(
context.applicationContext,
DebugLogDatabase::class.java, "debug.db"
).build()
}
}
}
return instance!!
}
}
}
25 changes: 25 additions & 0 deletions app/src/main/java/bisq/android/database/DebugLogLevel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq 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 Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.android.database

enum class DebugLogLevel {
DEBUG,
INFO,
WARN,
ERROR
}
Loading
Loading