demo: display foreground notification while syncing

This commit is contained in:
Oscar Mira 2024-01-04 10:56:05 +01:00
parent 6560ee79ad
commit f91af7aabd
3 changed files with 40 additions and 3 deletions

View File

@ -1,6 +1,9 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> <manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<application <application
android:name=".MainApplication" android:name=".MainApplication"
android:allowBackup="true" android:allowBackup="true"
@ -22,7 +25,8 @@
</activity> </activity>
<service <service
android:name=".service.SyncService" android:name=".service.SyncService"
android:exported="false" /> android:exported="false"
android:foregroundServiceType="dataSync" />
</application> </application>
</manifest> </manifest>

View File

@ -1,10 +1,15 @@
package im.molly.monero.demo.service package im.molly.monero.demo.service
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.ServiceInfo
import android.os.Binder import android.os.Binder
import android.os.Build
import android.os.IBinder import android.os.IBinder
import android.util.Log import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.lifecycle.LifecycleService import androidx.lifecycle.LifecycleService
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import im.molly.monero.demo.AppModule import im.molly.monero.demo.AppModule
@ -16,6 +21,8 @@ import kotlin.time.Duration.Companion.seconds
const val TAG = "SyncService" const val TAG = "SyncService"
const val NOTIFICATION_CHANNEL_ID = "SyncService"
class SyncService( class SyncService(
private val walletRepository: WalletRepository = AppModule.walletRepository, private val walletRepository: WalletRepository = AppModule.walletRepository,
private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO, private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO,
@ -82,6 +89,8 @@ class SyncService(
Log.d(TAG, "onCreate") Log.d(TAG, "onCreate")
super.onCreate() super.onCreate()
startForeground()
lifecycleScope.launch(ioDispatcher) { lifecycleScope.launch(ioDispatcher) {
doSync() doSync()
} }
@ -95,7 +104,31 @@ class SyncService(
companion object { companion object {
fun start(context: Context) { fun start(context: Context) {
val intent = Intent(context, SyncService::class.java) val intent = Intent(context, SyncService::class.java)
context.startService(intent) context.startForegroundService(intent)
} }
} }
private fun startForeground() {
ensureNotificationChannel()
val notification = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.build()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(100, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC)
} else {
startForeground(100, notification)
}
}
private fun ensureNotificationChannel() {
val notificationManager = getSystemService(NotificationManager::class.java)!!
notificationManager.createNotificationChannel(
NotificationChannel(
NOTIFICATION_CHANNEL_ID,
"Wallet Foreground Service",
NotificationManager.IMPORTANCE_LOW,
)
)
}
} }

View File

@ -261,7 +261,7 @@ private fun SecondStepScreen(
AlertDialog(onDismissRequest = { showOffLineConfirmationDialog = false }, title = { AlertDialog(onDismissRequest = { showOffLineConfirmationDialog = false }, title = {
Text("No remote nodes selected") Text("No remote nodes selected")
}, text = { }, text = {
Text("There are no remote nodes added to your wallet settings. Are you sure you want to start an offline wallet?") Text("There are no remote nodes added to your wallet settings. Are you sure you want to create an offline wallet?")
}, confirmButton = { }, confirmButton = {
TextButton(onClick = { TextButton(onClick = {
showOffLineConfirmationDialog = false showOffLineConfirmationDialog = false