Skip to content

Commit

Permalink
(android) Add notification when power saver mode is on
Browse files Browse the repository at this point in the history
  • Loading branch information
dpad85 committed Jun 26, 2024
1 parent fd888a3 commit f39e32a
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ fun AppView(
factory = NoticesViewModel.Factory(
appConfigurationManager = business.appConfigurationManager,
peerManager = business.peerManager,
internalDataRepository = internalData
)
)
MonitorNotices(vm = noticesViewModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Notice>()

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<Notice.PowerSaverMode>()
}
}
}

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) {
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 <T : ViewModel> create(modelClass: Class<T>): T {
override fun <T : ViewModel> create(modelClass: Class<T>, 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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ private fun FirstNoticeView(
is Notice.MempoolFull -> onNavigateToNotificationsList

is Notice.RemoteMessage -> onNavigateToNotificationsList

is Notice.PowerSaverMode -> onNavigateToNotificationsList
}
} else {
onNavigateToNotificationsList
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ private fun PermamentNotice(
},
)
}

is Notice.PowerSaverMode -> {
ImportantNotification(
icon = R.drawable.ic_alert_triangle,
message = stringResource(id = R.string.inappnotif_powersaver)
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@
<string name="inappnotif_swapin_timeout_message">Pronto vencerá un depósito.</string>
<string name="inappnotif_swapin_timeout_action">Ver detalles</string>

<string name="inappnotif_powersaver">El modo de ahorro de energía está activado. Los pagos en segundo plano probablemente fallarán.</string>

<string name="inappnotif_watchtower_late_message">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.</string>
<string name="inappnotif_watchtower_late_action">Ignorar</string>

Expand Down
2 changes: 2 additions & 0 deletions phoenix-android/src/main/res/values-cs/important_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@
<string name="inappnotif_swapin_timeout_message">Záloha brzy vyprší.</string>
<string name="inappnotif_swapin_timeout_action">Zobrazit podrobnosti</string>

<string name="inappnotif_powersaver">Je povolen úsporný režim. Platby na pozadí pravděpodobně selžou.</string>

<string name="inappnotif_watchtower_late_message">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.</string>
<string name="inappnotif_watchtower_late_action">Odmítnout</string>

Expand Down
2 changes: 2 additions & 0 deletions phoenix-android/src/main/res/values-de/important_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@
<string name="inappnotif_swapin_timeout_message">Eine Einzahlung wird bald ablaufen.</string>
<string name="inappnotif_swapin_timeout_action">Details ansehen</string>

<string name="inappnotif_powersaver">Der Energiesparmodus ist aktiviert. Zahlungen im Hintergrund werden wahrscheinlich fehlschlagen.</string>

<string name="inappnotif_watchtower_late_message">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.</string>
<string name="inappnotif_watchtower_late_action">Schließen</string>

Expand Down
2 changes: 2 additions & 0 deletions phoenix-android/src/main/res/values-es/important_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@
<string name="inappnotif_swapin_timeout_message">Un depósito expirará pronto.</string>
<string name="inappnotif_swapin_timeout_action">Ver detalles</string>

<string name="inappnotif_powersaver">El modo de ahorro de energía está activado. Los pagos en segundo plano probablemente fallarán.</string>

<string name="inappnotif_watchtower_late_message">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.</string>
<string name="inappnotif_watchtower_late_action">Desestimar</string>

Expand Down
4 changes: 3 additions & 1 deletion phoenix-android/src/main/res/values-fr/important_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@
<string name="inappnotif_swapin_timeout_message">Un dépôt va bientôt expirer.</string>
<string name="inappnotif_swapin_timeout_action">Voir les détails</string>

<string name="inappnotif_watchtower_late_message">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.</string>
<string name="inappnotif_powersaver">Le mode d\'économie d\'énergie est activé. Les paiements en arrière-plan vont probablement échouer.</string>

<string name="inappnotif_watchtower_late_message">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.</string>
<string name="inappnotif_watchtower_late_action">Fermer</string>

<string name="inappnotif_upgrade_message">Mise à jour disponible</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@
<string name="inappnotif_swapin_timeout_message">Um depósito expirará em breve.</string>
<string name="inappnotif_swapin_timeout_action">Ver detalhes</string>

<string name="inappnotif_powersaver">O modo de economia de energia está ativado. Os pagamentos em segundo plano provavelmente falharão.</string>

<string name="inappnotif_watchtower_late_message">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.</string>
<string name="inappnotif_watchtower_late_action">Dispensar</string>

Expand Down
2 changes: 2 additions & 0 deletions phoenix-android/src/main/res/values-sk/important_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@
<string name="inappnotif_swapin_timeout_message">Vklad čoskoro vyprší.</string>
<string name="inappnotif_swapin_timeout_action">Zobraziť podrobnosti</string>

<string name="inappnotif_powersaver">Režim úspory energie je zapnutý. Platby na pozadí pravdepodobne zlyhajú.</string>

<string name="inappnotif_watchtower_late_message">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.</string>
<string name="inappnotif_watchtower_late_action">Zatvoriť</string>

Expand Down
2 changes: 2 additions & 0 deletions phoenix-android/src/main/res/values-vi/important_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@
<string name="inappnotif_swapin_timeout_message">Một khoản vốn sẽ sớm hết hạn.</string>
<string name="inappnotif_swapin_timeout_action">Xem chi tiết</string>

<string name="inappnotif_powersaver">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.</string>

<string name="inappnotif_watchtower_late_message">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.</string>
<string name="inappnotif_watchtower_late_action">Huỷ</string>

Expand Down
2 changes: 2 additions & 0 deletions phoenix-android/src/main/res/values/important_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@
<string name="inappnotif_swapin_timeout_message">A deposit will expire soon.</string>
<string name="inappnotif_swapin_timeout_action">View details</string>

<string name="inappnotif_powersaver">Power saver mode is enabled. Background payments will likely fail.</string>

<string name="inappnotif_watchtower_late_message">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.</string>
<string name="inappnotif_watchtower_late_action">Dismiss</string>

Expand Down

0 comments on commit f39e32a

Please sign in to comment.