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"?>
<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
android:name=".MainApplication"
android:allowBackup="true"
@ -22,7 +25,8 @@
</activity>
<service
android:name=".service.SyncService"
android:exported="false" />
android:exported="false"
android:foregroundServiceType="dataSync" />
</application>
</manifest>

View File

@ -1,10 +1,15 @@
package im.molly.monero.demo.service
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.content.pm.ServiceInfo
import android.os.Binder
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.lifecycle.LifecycleService
import androidx.lifecycle.lifecycleScope
import im.molly.monero.demo.AppModule
@ -16,6 +21,8 @@ import kotlin.time.Duration.Companion.seconds
const val TAG = "SyncService"
const val NOTIFICATION_CHANNEL_ID = "SyncService"
class SyncService(
private val walletRepository: WalletRepository = AppModule.walletRepository,
private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO,
@ -82,6 +89,8 @@ class SyncService(
Log.d(TAG, "onCreate")
super.onCreate()
startForeground()
lifecycleScope.launch(ioDispatcher) {
doSync()
}
@ -95,7 +104,31 @@ class SyncService(
companion object {
fun start(context: Context) {
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 = {
Text("No remote nodes selected")
}, 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 = {
TextButton(onClick = {
showOffLineConfirmationDialog = false