From 3cda7fa87776b6f4937a1c7c4980b4e84738ff9b Mon Sep 17 00:00:00 2001 From: Rowdy Chotkan <22516119+InvictusRMC@users.noreply.github.com> Date: Tue, 27 Feb 2024 16:56:26 +0100 Subject: [PATCH] Make Service Notification Visible on SDK 31+ (#185) * Ask for notification permission * Update notification style * Bump `kotlin-ipv8` version --- .../app/service/TrustChainService.kt | 21 +++++++++-- .../app/ui/dashboard/DashboardActivity.kt | 37 ++++++++++--------- kotlin-ipv8 | 2 +- 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/nl/tudelft/trustchain/app/service/TrustChainService.kt b/app/src/main/java/nl/tudelft/trustchain/app/service/TrustChainService.kt index bceee139b..93d17053b 100644 --- a/app/src/main/java/nl/tudelft/trustchain/app/service/TrustChainService.kt +++ b/app/src/main/java/nl/tudelft/trustchain/app/service/TrustChainService.kt @@ -4,11 +4,24 @@ import android.app.PendingIntent import android.content.Intent import androidx.core.app.NotificationCompat import androidx.core.app.TaskStackBuilder +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import nl.tudelft.ipv8.android.IPv8Android import nl.tudelft.ipv8.android.service.IPv8Service import nl.tudelft.trustchain.app.R import nl.tudelft.trustchain.app.ui.dashboard.DashboardActivity class TrustChainService : IPv8Service() { + override fun onCreate() { + super.onCreate() + scope.launch { + while (true) { + updateNotification() + delay(1000) + } + } + } + override fun createNotification(): NotificationCompat.Builder { val trustChainDashboardIntent = Intent(this, DashboardActivity::class.java) val flags = PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE @@ -18,9 +31,11 @@ class TrustChainService : IPv8Service() { .getPendingIntent(0, flags) return NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_CONNECTION) - .setContentTitle("IPv8") - .setContentText("Running") - .setSmallIcon(R.drawable.ic_insert_link_black_24dp) + .setContentTitle("IPv8 Service") + .setContentText("Connected to ${IPv8Android.getInstance().network.verifiedPeers.size} peers") + .setSmallIcon(R.drawable.ic_device_hub_black_24dp) + .setColor(getColor(R.color.colorPrimary)) .setContentIntent(pendingIntent) + .setOnlyAlertOnce(true) } } diff --git a/app/src/main/java/nl/tudelft/trustchain/app/ui/dashboard/DashboardActivity.kt b/app/src/main/java/nl/tudelft/trustchain/app/ui/dashboard/DashboardActivity.kt index d4010a479..ae9a37f10 100644 --- a/app/src/main/java/nl/tudelft/trustchain/app/ui/dashboard/DashboardActivity.kt +++ b/app/src/main/java/nl/tudelft/trustchain/app/ui/dashboard/DashboardActivity.kt @@ -32,8 +32,8 @@ class DashboardActivity : AppCompatActivity() { // Handle init of IPv8 after requesting permissions; only if Android 12 or higher. // onPermissionsDenied() is run until user has accepted permissions. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - if (!hasBluetoothPermissions()) { - requestBluetoothPermissions() + if (!hasBluetoothPermissions() || !hasNotificationPermissions()) { + requestPermissions() } else { // Only initialize IPv8 if it has not been initialized yet. try { @@ -71,14 +71,19 @@ class DashboardActivity : AppCompatActivity() { checkSelfPermission(BLUETOOTH_PERMISSIONS_SCAN) == PackageManager.PERMISSION_GRANTED } - private fun requestBluetoothPermissions() { + private fun hasNotificationPermissions(): Boolean { + return checkSelfPermission(NOTIFICATION_PERMISSION) == PackageManager.PERMISSION_GRANTED + } + + private fun requestPermissions() { requestPermissions( arrayOf( BLUETOOTH_PERMISSIONS_ADVERTISE, BLUETOOTH_PERMISSIONS_CONNECT, - BLUETOOTH_PERMISSIONS_SCAN + BLUETOOTH_PERMISSIONS_SCAN, + NOTIFICATION_PERMISSION ), - BLUETOOTH_PERMISSIONS_REQUEST_CODE + PERMISSIONS_REQUEST_CODE ) } @@ -87,19 +92,17 @@ class DashboardActivity : AppCompatActivity() { permissions: Array, grantResults: IntArray ) { - when (requestCode) { - BLUETOOTH_PERMISSIONS_REQUEST_CODE -> { - if (hasBluetoothPermissions()) { - (application as TrustChainApplication).initIPv8() - } else { - onPermissionsDenied() - } + if (requestCode == PERMISSIONS_REQUEST_CODE) { + if (!hasBluetoothPermissions() || !hasNotificationPermissions()) { + onPermissionsDenied() + } else { + (application as TrustChainApplication).initIPv8() } - else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults) + } else { + super.onRequestPermissionsResult(requestCode, permissions, grantResults) } } - @Suppress("DEPRECATION") override fun onActivityResult( requestCode: Int, resultCode: Int, @@ -113,12 +116,11 @@ class DashboardActivity : AppCompatActivity() { onPermissionsDenied() } } + else -> { - @Suppress("DEPRECATION") // TODO: Fix deprecation issue. super.onActivityResult(requestCode, resultCode, data) } } - @Suppress("DEPRECATION") // TODO: Fix deprecation issue. super.onActivityResult(requestCode, resultCode, data) } @@ -141,10 +143,11 @@ class DashboardActivity : AppCompatActivity() { } companion object { - private const val BLUETOOTH_PERMISSIONS_REQUEST_CODE = 200 + private const val PERMISSIONS_REQUEST_CODE = 200 private const val SETTINGS_INTENT_CODE = 1000 private const val BLUETOOTH_PERMISSIONS_SCAN = "android.permission.BLUETOOTH_SCAN" private const val BLUETOOTH_PERMISSIONS_CONNECT = "android.permission.BLUETOOTH_CONNECT" private const val BLUETOOTH_PERMISSIONS_ADVERTISE = "android.permission.BLUETOOTH_ADVERTISE" + private const val NOTIFICATION_PERMISSION = "android.permission.POST_NOTIFICATIONS" } } diff --git a/kotlin-ipv8 b/kotlin-ipv8 index 18cd64134..476dc36d0 160000 --- a/kotlin-ipv8 +++ b/kotlin-ipv8 @@ -1 +1 @@ -Subproject commit 18cd641348368f9e5b96f16ba6513374dfc59c7c +Subproject commit 476dc36d0a5208d4a6ef21d953e00e4a440046a4