diff --git a/app/build.gradle b/app/build.gradle index caa51fe8..d9ce1929 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -253,4 +253,6 @@ dependencies { implementation libs.datastore.preferences implementation libs.security.crypto + + implementation libs.lifecycle.process } diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/Amber.kt b/app/src/main/java/com/greenart7c3/nostrsigner/Amber.kt index 13101e71..00b99394 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/Amber.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/Amber.kt @@ -5,6 +5,10 @@ import android.content.Intent import android.net.NetworkCapabilities import android.util.Log import androidx.compose.runtime.mutableStateOf +import androidx.lifecycle.DefaultLifecycleObserver +import androidx.lifecycle.LifecycleObserver +import androidx.lifecycle.LifecycleOwner +import androidx.lifecycle.ProcessLifecycleOwner import androidx.work.Data import androidx.work.ExistingPeriodicWorkPolicy import androidx.work.OneTimeWorkRequest @@ -47,7 +51,7 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.launch -class Amber : Application() { +class Amber : Application(), LifecycleObserver { private var mainActivityRef: WeakReference? = null fun setMainActivity(activity: MainActivity?) { @@ -143,6 +147,17 @@ class Amber : Application() { override fun onCreate() { super.onCreate() + ProcessLifecycleOwner.get().lifecycle.addObserver(object : DefaultLifecycleObserver { + override fun onStart(owner: LifecycleOwner) { + Log.d("ProcessLifecycleOwner", "App in foreground") + isAppInForeground = true + } + + override fun onStop(owner: LifecycleOwner) { + Log.d("ProcessLifecycleOwner", "App in background") + isAppInForeground = false + } + }) isStartingApp.value = true @@ -404,6 +419,7 @@ class Amber : Application() { } companion object { + var isAppInForeground = false const val TAG = "Amber" @Volatile diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/MainActivity.kt b/app/src/main/java/com/greenart7c3/nostrsigner/MainActivity.kt index 242ee494..9c0cb03d 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/MainActivity.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/MainActivity.kt @@ -61,6 +61,7 @@ class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { Log.d(Amber.TAG, "onCreate MainActivity") Amber.instance.startService() + Amber.isAppInForeground = true enableEdgeToEdge() super.onCreate(savedInstanceState) Amber.instance.setMainActivity(this) diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/service/BunkerRequestUtils.kt b/app/src/main/java/com/greenart7c3/nostrsigner/service/BunkerRequestUtils.kt index a5f77325..c1fcdbd5 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/service/BunkerRequestUtils.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/service/BunkerRequestUtils.kt @@ -290,6 +290,14 @@ object BunkerRequestUtils { ) application.application.isConnected = true + val activity = Amber.instance.getMainActivity() + activity?.intent = null + if (application.application.closeApplication) { + activity?.finish() + } + + delay(500) + if (signPolicy != null) { AmberUtils.configureSignPolicy(application, signPolicy, key, permissions) } @@ -344,7 +352,6 @@ object BunkerRequestUtils { ), ) - EventNotificationConsumer(context).notificationManager().cancelAll() sendBunkerResponse( context, account, @@ -367,12 +374,6 @@ object BunkerRequestUtils { onLoading(false) addRequest(localBunkerRequest) - } else { - val activity = Amber.instance.getMainActivity() - activity?.intent = null - if (application.application.closeApplication) { - activity?.finish() - } } } }, diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/service/BunkerResponseWorker.kt b/app/src/main/java/com/greenart7c3/nostrsigner/service/BunkerResponseWorker.kt new file mode 100644 index 00000000..48e00cd7 --- /dev/null +++ b/app/src/main/java/com/greenart7c3/nostrsigner/service/BunkerResponseWorker.kt @@ -0,0 +1,13 @@ +package com.greenart7c3.nostrsigner.service + +import android.content.Context +import androidx.work.CoroutineWorker +import androidx.work.WorkerParameters + +class BunkerResponseWorker(appContext: Context, workerParams: WorkerParameters) : + CoroutineWorker(appContext, workerParams) { + + override suspend fun doWork(): Result { + TODO("Not yet implemented") + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 0f21d48d..de6f4c4a 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -46,6 +46,7 @@ junit = { module = "junit:junit", version.ref = "junit" } lifecycle-runtime-compose = { module = "androidx.lifecycle:lifecycle-runtime-compose", version.ref = "lifecycle_version" } lifecycle-runtime-ktx = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "lifecycle_version" } lifecycle-viewmodel-compose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "lifecycle_version" } +lifecycle-process = { module = "androidx.lifecycle:lifecycle-process", version.ref = "lifecycle_version" } material-icons-extended = { module = "androidx.compose.material:material-icons-extended", version.ref = "materialIconsExtended" } material3 = { module = "androidx.compose.material3:material3", version.ref = "material3" } mockk = { module = "io.mockk:mockk", version.ref = "mockk" }