From f39e32a90d3e765a3eaeb7d14d718b72bfe02396 Mon Sep 17 00:00:00 2001 From: Dominique Padiou <5765435+dpad85@users.noreply.github.com> Date: Wed, 26 Jun 2024 09:51:01 +0200 Subject: [PATCH] (android) Add notification when power saver mode is on --- .../fr/acinq/phoenix/android/AppView.kt | 1 - .../acinq/phoenix/android/NoticesViewModel.kt | 63 ++++++++++++++----- .../acinq/phoenix/android/home/HomeNotices.kt | 6 ++ .../android/settings/NotificationsView.kt | 7 +++ .../res/values-b+es+419/important_strings.xml | 2 + .../main/res/values-cs/important_strings.xml | 2 + .../main/res/values-de/important_strings.xml | 2 + .../main/res/values-es/important_strings.xml | 2 + .../main/res/values-fr/important_strings.xml | 4 +- .../res/values-pt-rBR/important_strings.xml | 2 + .../main/res/values-sk/important_strings.xml | 2 + .../main/res/values-vi/important_strings.xml | 2 + .../src/main/res/values/important_strings.xml | 2 + 13 files changed, 81 insertions(+), 16 deletions(-) diff --git a/phoenix-android/src/main/kotlin/fr/acinq/phoenix/android/AppView.kt b/phoenix-android/src/main/kotlin/fr/acinq/phoenix/android/AppView.kt index fc6eab40c..3bf3e371b 100644 --- a/phoenix-android/src/main/kotlin/fr/acinq/phoenix/android/AppView.kt +++ b/phoenix-android/src/main/kotlin/fr/acinq/phoenix/android/AppView.kt @@ -151,7 +151,6 @@ fun AppView( factory = NoticesViewModel.Factory( appConfigurationManager = business.appConfigurationManager, peerManager = business.peerManager, - internalDataRepository = internalData ) ) MonitorNotices(vm = noticesViewModel) diff --git a/phoenix-android/src/main/kotlin/fr/acinq/phoenix/android/NoticesViewModel.kt b/phoenix-android/src/main/kotlin/fr/acinq/phoenix/android/NoticesViewModel.kt index deeaddfad..5b9945269 100644 --- a/phoenix-android/src/main/kotlin/fr/acinq/phoenix/android/NoticesViewModel.kt +++ b/phoenix-android/src/main/kotlin/fr/acinq/phoenix/android/NoticesViewModel.kt @@ -16,10 +16,16 @@ package fr.acinq.phoenix.android +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.content.IntentFilter +import android.os.PowerManager import androidx.compose.runtime.mutableStateListOf import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.viewModelScope +import androidx.lifecycle.viewmodel.CreationExtras import fr.acinq.phoenix.android.utils.datastore.InternalDataRepository import fr.acinq.phoenix.data.WalletNotice import fr.acinq.phoenix.managers.AppConfigurationManager @@ -33,29 +39,54 @@ sealed class Notice() { abstract val priority: Int sealed class ShowInHome(override val priority: Int) : Notice() - object MigrationFromLegacy : ShowInHome(1) + data object MigrationFromLegacy : ShowInHome(1) data class RemoteMessage(val notice: WalletNotice) : ShowInHome(1) - object CriticalUpdateAvailable : ShowInHome(2) - object SwapInCloseToTimeout : ShowInHome(3) - object BackupSeedReminder : ShowInHome(5) - object MempoolFull : ShowInHome(10) - object UpdateAvailable : ShowInHome(20) - object NotificationPermission : ShowInHome(30) + data object CriticalUpdateAvailable : ShowInHome(2) + data object SwapInCloseToTimeout : ShowInHome(3) + data object PowerSaverMode : ShowInHome(4) + data object BackupSeedReminder : ShowInHome(5) + data object MempoolFull : ShowInHome(10) + data object UpdateAvailable : ShowInHome(20) + data object NotificationPermission : ShowInHome(30) // less important notices sealed class DoNotShowInHome(override val priority: Int = 999) : Notice() - object WatchTowerLate : DoNotShowInHome() + data object WatchTowerLate : DoNotShowInHome() } -class NoticesViewModel(val appConfigurationManager: AppConfigurationManager, val peerManager: PeerManager, val internalDataRepository: InternalDataRepository) : ViewModel() { +class NoticesViewModel( + val appConfigurationManager: AppConfigurationManager, + val peerManager: PeerManager, + val internalDataRepository: InternalDataRepository, + val context: Context + +) : ViewModel() { private val log = LoggerFactory.getLogger(this::class.java) val notices = mutableStateListOf() + private val receiver = object : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent?) { + val powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager + log.info("power_saver=${powerManager.isPowerSaveMode}") + if (powerManager.isPowerSaveMode) { + addNotice(Notice.PowerSaverMode) + } else { + removeNotice() + } + } + } + init { viewModelScope.launch { monitorWalletContext() } viewModelScope.launch { monitorSwapInCloseToTimeout() } viewModelScope.launch { monitorWalletNotice() } + context.registerReceiver(receiver, IntentFilter(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED)) + } + + override fun onCleared() { + super.onCleared() + context.unregisterReceiver(receiver) } fun addNotice(notice: Notice) { @@ -70,7 +101,7 @@ class NoticesViewModel(val appConfigurationManager: AppConfigurationManager, val private suspend fun monitorWalletContext() { appConfigurationManager.walletContext.collect { - log.debug("collecting wallet-context=$it") + log.debug("collecting wallet-context={}", it) val isMempoolFull = it?.isMempoolFull ?: false val isUpdateAvailable = it?.androidLatestVersion?.let { it > BuildConfig.VERSION_CODE } ?: false val isCriticalUpdateAvailable = it?.androidLatestCriticalVersion?.let { it > BuildConfig.VERSION_CODE } ?: false @@ -98,7 +129,7 @@ class NoticesViewModel(val appConfigurationManager: AppConfigurationManager, val combine(appConfigurationManager.walletNotice, internalDataRepository.getLastReadWalletNoticeIndex) { notice, lastReadIndex -> notice to lastReadIndex }.collect { (notice, lastReadIndex) -> - log.debug("collecting wallet-notice=$notice") + log.debug("collecting wallet-notice={}", notice) if (notice != null && notice.index > lastReadIndex) { addNotice(Notice.RemoteMessage(notice)) } else { @@ -119,11 +150,15 @@ class NoticesViewModel(val appConfigurationManager: AppConfigurationManager, val class Factory( private val appConfigurationManager: AppConfigurationManager, private val peerManager: PeerManager, - private val internalDataRepository: InternalDataRepository, ) : ViewModelProvider.Factory { - override fun create(modelClass: Class): T { + override fun create(modelClass: Class, extras: CreationExtras): T { + val application = checkNotNull(extras[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY] as? PhoenixApplication) @Suppress("UNCHECKED_CAST") - return NoticesViewModel(appConfigurationManager, peerManager, internalDataRepository) as T + return NoticesViewModel( + appConfigurationManager, peerManager, + internalDataRepository = application.internalDataRepository, + application.applicationContext + ) as T } } } \ No newline at end of file diff --git a/phoenix-android/src/main/kotlin/fr/acinq/phoenix/android/home/HomeNotices.kt b/phoenix-android/src/main/kotlin/fr/acinq/phoenix/android/home/HomeNotices.kt index 28faf805a..0fbccf6bc 100644 --- a/phoenix-android/src/main/kotlin/fr/acinq/phoenix/android/home/HomeNotices.kt +++ b/phoenix-android/src/main/kotlin/fr/acinq/phoenix/android/home/HomeNotices.kt @@ -130,6 +130,8 @@ private fun FirstNoticeView( is Notice.MempoolFull -> onNavigateToNotificationsList is Notice.RemoteMessage -> onNavigateToNotificationsList + + is Notice.PowerSaverMode -> onNavigateToNotificationsList } } else { onNavigateToNotificationsList @@ -183,6 +185,10 @@ private fun FirstNoticeView( is Notice.RemoteMessage -> { NoticeTextView(text = notice.notice.message, icon = R.drawable.ic_info) } + + is Notice.PowerSaverMode -> { + NoticeTextView(text = stringResource(id = R.string.inappnotif_powersaver)) + } } if (messagesCount > 1) { diff --git a/phoenix-android/src/main/kotlin/fr/acinq/phoenix/android/settings/NotificationsView.kt b/phoenix-android/src/main/kotlin/fr/acinq/phoenix/android/settings/NotificationsView.kt index ee7b80484..12efcac6b 100644 --- a/phoenix-android/src/main/kotlin/fr/acinq/phoenix/android/settings/NotificationsView.kt +++ b/phoenix-android/src/main/kotlin/fr/acinq/phoenix/android/settings/NotificationsView.kt @@ -244,6 +244,13 @@ private fun PermamentNotice( }, ) } + + is Notice.PowerSaverMode -> { + ImportantNotification( + icon = R.drawable.ic_alert_triangle, + message = stringResource(id = R.string.inappnotif_powersaver) + ) + } } } diff --git a/phoenix-android/src/main/res/values-b+es+419/important_strings.xml b/phoenix-android/src/main/res/values-b+es+419/important_strings.xml index 4d0fa8218..a90d69d8a 100644 --- a/phoenix-android/src/main/res/values-b+es+419/important_strings.xml +++ b/phoenix-android/src/main/res/values-b+es+419/important_strings.xml @@ -188,6 +188,8 @@ Pronto vencerá un depósito. Ver detalles + El modo de ahorro de energía está activado. Los pagos en segundo plano probablemente fallarán. + Phoenix monitorea regularmente la cadena de bloques cuando está funcionando en segundo plano, pero no ha podido hacerlo en los últimos días.\n\nComprueba que Android no bloquee Phoenix y que la billetera pueda conectarse a Electrum. Ignorar diff --git a/phoenix-android/src/main/res/values-cs/important_strings.xml b/phoenix-android/src/main/res/values-cs/important_strings.xml index a6673ee4c..010313782 100644 --- a/phoenix-android/src/main/res/values-cs/important_strings.xml +++ b/phoenix-android/src/main/res/values-cs/important_strings.xml @@ -190,6 +190,8 @@ Záloha brzy vyprší. Zobrazit podrobnosti + Je povolen úsporný režim. Platby na pozadí pravděpodobně selžou. + Phoenix pravidelně monitoruje Blockchain na pozadí, ale v posledních dnech se mu to nedařilo.\n\nUjistěte se, že systém Android neblokuje aplikaci Phoenix a že se může připojit ke službě Electrum. Odmítnout diff --git a/phoenix-android/src/main/res/values-de/important_strings.xml b/phoenix-android/src/main/res/values-de/important_strings.xml index 403050cf2..fc823eaf4 100644 --- a/phoenix-android/src/main/res/values-de/important_strings.xml +++ b/phoenix-android/src/main/res/values-de/important_strings.xml @@ -187,6 +187,8 @@ Eine Einzahlung wird bald ablaufen. Details ansehen + Der Energiesparmodus ist aktiviert. Zahlungen im Hintergrund werden wahrscheinlich fehlschlagen. + Phoenix beobachtet die Blockchain regelmäßig im Hintergrund, war aber in den letzten Tagen nicht in der Lage, dies zu tun.\n\nStellen Sie sicher, dass Phoenix nicht durch Android blockiert wird, sodass eine Verbindung zu Electrum möglich ist. Schließen diff --git a/phoenix-android/src/main/res/values-es/important_strings.xml b/phoenix-android/src/main/res/values-es/important_strings.xml index ea4cbd174..53d3558ea 100644 --- a/phoenix-android/src/main/res/values-es/important_strings.xml +++ b/phoenix-android/src/main/res/values-es/important_strings.xml @@ -191,6 +191,8 @@ Un depósito expirará pronto. Ver detalles + El modo de ahorro de energía está activado. Los pagos en segundo plano probablemente fallarán. + Phoenix monitorea regularmente el blockchain cuando está en segundo plano, pero no pudo hacerlo los últimos días.\n\nAsegúrate de que Android no bloquee Phoenix, y que pueda conectarse a Electrum. Desestimar diff --git a/phoenix-android/src/main/res/values-fr/important_strings.xml b/phoenix-android/src/main/res/values-fr/important_strings.xml index 98be98d42..66b82b141 100644 --- a/phoenix-android/src/main/res/values-fr/important_strings.xml +++ b/phoenix-android/src/main/res/values-fr/important_strings.xml @@ -186,7 +186,9 @@ Un dépôt va bientôt expirer. Voir les détails - Phoenix vérifie périodiquement la blockchain lorsque l\'application est éteinte. Ces derniers jours elle n\'a pas pu le faire.\n\nAssurez vous qu\'Android ne restreigne pas Phoenix, et que l\'application puisse se connecter à Electrum. + Le mode d\'économie d\'énergie est activé. Les paiements en arrière-plan vont probablement échouer. + + Phoenix vérifie périodiquement la blockchain lorsque l\'application est éteinte. Dernièrement, elle n\'a pas pu le faire.\n\nAssurez vous qu\'Android n\'applique pas de restrictions à Phoenix, et que l\'application puisse se connecter à Electrum. Fermer Mise à jour disponible diff --git a/phoenix-android/src/main/res/values-pt-rBR/important_strings.xml b/phoenix-android/src/main/res/values-pt-rBR/important_strings.xml index 51dfbb264..bb7c1610b 100644 --- a/phoenix-android/src/main/res/values-pt-rBR/important_strings.xml +++ b/phoenix-android/src/main/res/values-pt-rBR/important_strings.xml @@ -189,6 +189,8 @@ Um depósito expirará em breve. Ver detalhes + O modo de economia de energia está ativado. Os pagamentos em segundo plano provavelmente falharão. + O Phoenix monitora regularmente o blockchain quando está em segundo plano, mas não conseguiu fazer isso nos últimos dias.\n\nCertifique-se de que o Android não bloqueie o Phoenix e que ele possa se conectar à Electrum. Dispensar diff --git a/phoenix-android/src/main/res/values-sk/important_strings.xml b/phoenix-android/src/main/res/values-sk/important_strings.xml index 9b9e46786..c1c6469d3 100644 --- a/phoenix-android/src/main/res/values-sk/important_strings.xml +++ b/phoenix-android/src/main/res/values-sk/important_strings.xml @@ -190,6 +190,8 @@ Vklad čoskoro vyprší. Zobraziť podrobnosti + Režim úspory energie je zapnutý. Platby na pozadí pravdepodobne zlyhajú. + Phoenix pravidelne monitoruje blockchain na pozadí, ale v posledných dňoch sa mu to nedarilo.\n\nUistite sa, že systém Android neblokuje aplikáciu Phoenix a že sa môže pripojiť k službe Electrum. Zatvoriť diff --git a/phoenix-android/src/main/res/values-vi/important_strings.xml b/phoenix-android/src/main/res/values-vi/important_strings.xml index ddabcdfc6..1dd246313 100644 --- a/phoenix-android/src/main/res/values-vi/important_strings.xml +++ b/phoenix-android/src/main/res/values-vi/important_strings.xml @@ -197,6 +197,8 @@ Một khoản vốn sẽ sớm hết hạn. Xem chi tiết + Chế độ tiết kiệm năng lượng được bật. Thanh toán nền có thể sẽ không thành công. + Phoenix thường xuyên quản lý blockchain khi ở chế độ chạy trên nền, nhưng gần đây Phoenix không thể thực hiện việc này.\n\nHãy đảm bảo rằng Android không chặn Phoenix và Phoenix có thể kết nối với Electrum. Huỷ diff --git a/phoenix-android/src/main/res/values/important_strings.xml b/phoenix-android/src/main/res/values/important_strings.xml index 7887a4799..0d08a2b61 100644 --- a/phoenix-android/src/main/res/values/important_strings.xml +++ b/phoenix-android/src/main/res/values/important_strings.xml @@ -193,6 +193,8 @@ A deposit will expire soon. View details + Power saver mode is enabled. Background payments will likely fail. + Phoenix regularly monitors the blockchain when in the background, but was unable to do so the last few days.\n\nMake sure Android does not block Phoenix, and that it can connect to Electrum. Dismiss