diff options
| author | Kalle Lindström <karl.lindstrom@mullvad.net> | 2025-06-09 17:16:45 +0200 |
|---|---|---|
| committer | Kalle Lindström <karl.lindstrom@mullvad.net> | 2025-06-19 09:36:26 +0200 |
| commit | cdc2c4d700cd41f37cf4b1607a0396019b1fbee5 (patch) | |
| tree | 10fbfcebf826159d515b5599634ce5b7be256f54 /android/app/src/main | |
| parent | 1748d8a7b3e8044f6b2718d25903bf1b38afad4e (diff) | |
| download | mullvadvpn-cdc2c4d700cd41f37cf4b1607a0396019b1fbee5.tar.xz mullvadvpn-cdc2c4d700cd41f37cf4b1607a0396019b1fbee5.zip | |
Use AlarmManager for notifications
Instead of scheduling system notifications from a flow we now
schedule them independently from the app lifecycle via AlarmManager.
This is done so that for example an expiry notification that the user
dismissed won't get redisplayed if the app process gets killed and
then restarted.
When the account exiry time is fetched we schedule an alarm that will
show a notification 3 days before the account time expires. This alarm
then also schedules a new alarm to show the following notification and
so on.
To make this work this PR also introduces two new broadcast receivers;
one on boot received listener and one on time time/timezone changed
listener.
Beause Android clears alarms when the devices is rebooted/the time is
changed we need these listeners to re-trigger the alarm.
To enable the broadcast receivers to re-trigger the alarm we also have
to persist the expiry time in the DataStore preferences.
Diffstat (limited to 'android/app/src/main')
16 files changed, 400 insertions, 36 deletions
diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 10d1123e3f..4e28e48c82 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -122,13 +122,33 @@ <action android:name="android.intent.action.LOCALE_CHANGED" /> </intent-filter> </receiver> - <receiver android:name=".receiver.BootCompletedReceiver" - android:enabled="false" - android:exported="false"> - <intent-filter> - <action android:name="android.intent.action.QUICKBOOT_POWERON" /> - <action android:name="android.intent.action.BOOT_COMPLETED" /> - </intent-filter> - </receiver> + <receiver + android:name=".receiver.NotificationAlarmReceiver" + android:exported="false" /> + <receiver + android:name=".receiver.TimeChangedReceiver" + android:exported="false"> + <intent-filter> + <action android:name="android.intent.action.TIME_SET" /> + <action android:name="android.intent.action.TIMEZONE_CHANGED" /> + </intent-filter> + </receiver> + <receiver + android:name=".receiver.AutoStartVpnBootCompletedReceiver" + android:enabled="false" + android:exported="false"> + <intent-filter> + <action android:name="android.intent.action.QUICKBOOT_POWERON" /> + <action android:name="android.intent.action.BOOT_COMPLETED" /> + </intent-filter> + </receiver> + <receiver + android:name=".receiver.ScheduleNotificationBootCompletedReceiver" + android:exported="false"> + <intent-filter> + <action android:name="android.intent.action.QUICKBOOT_POWERON" /> + <action android:name="android.intent.action.BOOT_COMPLETED" /> + </intent-filter> + </receiver> </application> </manifest> diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/MullvadApplication.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/MullvadApplication.kt index 617f538e87..3af04a9b6c 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/MullvadApplication.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/MullvadApplication.kt @@ -4,6 +4,9 @@ import android.app.Application import co.touchlab.kermit.Logger import co.touchlab.kermit.Severity import net.mullvad.mullvadvpn.di.appModule +import net.mullvad.mullvadvpn.service.notifications.NotificationChannelFactory +import net.mullvad.mullvadvpn.service.notifications.NotificationManager +import org.koin.android.ext.android.getKoin import org.koin.android.ext.koin.androidContext import org.koin.core.context.loadKoinModules import org.koin.core.context.startKoin @@ -19,5 +22,9 @@ class MullvadApplication : Application() { } startKoin { androidContext(this@MullvadApplication) } loadKoinModules(listOf(appModule)) + with(getKoin()) { + get<NotificationChannelFactory>() + get<NotificationManager>() + } } } diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/di/AppModule.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/di/AppModule.kt index d7a1bfc8cb..f9627cdcae 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/di/AppModule.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/di/AppModule.kt @@ -1,5 +1,9 @@ package net.mullvad.mullvadvpn.di +import android.content.Context +import androidx.core.app.NotificationManagerCompat +import androidx.datastore.core.DataStore +import androidx.datastore.dataStore import java.io.File import kotlinx.coroutines.MainScope import net.mullvad.mullvadvpn.BuildConfig @@ -8,14 +12,29 @@ import net.mullvad.mullvadvpn.lib.common.constant.GRPC_SOCKET_FILE_NAMED_ARGUMEN import net.mullvad.mullvadvpn.lib.daemon.grpc.ManagementService import net.mullvad.mullvadvpn.lib.endpoint.ApiEndpointFromIntentHolder import net.mullvad.mullvadvpn.lib.model.BuildVersion +import net.mullvad.mullvadvpn.lib.model.NotificationChannel import net.mullvad.mullvadvpn.lib.shared.AccountRepository import net.mullvad.mullvadvpn.lib.shared.ConnectionProxy import net.mullvad.mullvadvpn.lib.shared.DeviceRepository import net.mullvad.mullvadvpn.lib.shared.LocaleRepository import net.mullvad.mullvadvpn.lib.shared.PrepareVpnUseCase import net.mullvad.mullvadvpn.lib.shared.RelayLocationTranslationRepository +import net.mullvad.mullvadvpn.repository.UserPreferences +import net.mullvad.mullvadvpn.repository.UserPreferencesMigration +import net.mullvad.mullvadvpn.repository.UserPreferencesRepository +import net.mullvad.mullvadvpn.repository.UserPreferencesSerializer +import net.mullvad.mullvadvpn.service.notifications.NotificationChannelFactory +import net.mullvad.mullvadvpn.service.notifications.NotificationManager +import net.mullvad.mullvadvpn.service.notifications.NotificationProvider +import net.mullvad.mullvadvpn.service.notifications.accountexpiry.AccountExpiryNotificationProvider +import net.mullvad.mullvadvpn.service.notifications.tunnelstate.TunnelStateNotificationProvider +import net.mullvad.mullvadvpn.usecase.AccountExpiryNotificationActionUseCase +import net.mullvad.mullvadvpn.usecase.ScheduleNotificationAlarmUseCase import org.koin.android.ext.koin.androidContext +import org.koin.core.module.dsl.createdAtStart +import org.koin.core.module.dsl.withOptions import org.koin.core.qualifier.named +import org.koin.dsl.bind import org.koin.dsl.module val appModule = module { @@ -32,12 +51,43 @@ val appModule = module { single { PrepareVpnUseCase(androidContext()) } + single { androidContext().resources } + single { androidContext().userPreferencesStore } single { BuildVersion(BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE) } single { ApiEndpointFromIntentHolder() } single { AccountRepository(get(), get(), MainScope()) } single { DeviceRepository(get()) } + single { UserPreferencesRepository(get(), get()) } single { ConnectionProxy(get(), get(), get()) } single { LocaleRepository(get()) } single { RelayLocationTranslationRepository(get(), get(), MainScope()) } - single { androidContext().resources } + single { ScheduleNotificationAlarmUseCase(get()) } + single { AccountExpiryNotificationActionUseCase(get(), get()) } + + single { NotificationChannel.TunnelUpdates } bind NotificationChannel::class + single { NotificationChannel.AccountUpdates } bind NotificationChannel::class + single { NotificationChannelFactory(get(), get(), getAll()) } withOptions { createdAtStart() } + single { NotificationManagerCompat.from(androidContext()) } + single { NotificationManager(get(), getAll(), get(), MainScope()) } withOptions + { + createdAtStart() + } + single { + TunnelStateNotificationProvider( + get(), + get(), + get(), + get<NotificationChannel.TunnelUpdates>().id, + MainScope(), + ) + } bind NotificationProvider::class + single { AccountExpiryNotificationProvider(get<NotificationChannel.AccountUpdates>().id) } bind + NotificationProvider::class } + +private val Context.userPreferencesStore: DataStore<UserPreferences> by + dataStore( + fileName = APP_PREFERENCES_NAME, + serializer = UserPreferencesSerializer, + produceMigrations = UserPreferencesMigration::migrations, + ) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/di/UiModule.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/di/UiModule.kt index 7e798a69c8..a75aaef553 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/di/UiModule.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/di/UiModule.kt @@ -1,10 +1,7 @@ package net.mullvad.mullvadvpn.di import android.content.ComponentName -import android.content.Context import android.content.pm.PackageManager -import androidx.datastore.core.DataStore -import androidx.datastore.dataStore import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.MainScope import net.mullvad.mullvadvpn.BuildConfig @@ -15,7 +12,7 @@ import net.mullvad.mullvadvpn.constant.IS_PLAY_BUILD import net.mullvad.mullvadvpn.dataproxy.MullvadProblemReport import net.mullvad.mullvadvpn.lib.payment.PaymentProvider import net.mullvad.mullvadvpn.lib.shared.VoucherRepository -import net.mullvad.mullvadvpn.receiver.BootCompletedReceiver +import net.mullvad.mullvadvpn.receiver.AutoStartVpnBootCompletedReceiver import net.mullvad.mullvadvpn.repository.ApiAccessRepository import net.mullvad.mullvadvpn.repository.AutoStartAndConnectOnBootRepository import net.mullvad.mullvadvpn.repository.ChangelogRepository @@ -29,10 +26,6 @@ import net.mullvad.mullvadvpn.repository.RelayOverridesRepository import net.mullvad.mullvadvpn.repository.SettingsRepository import net.mullvad.mullvadvpn.repository.SplashCompleteRepository import net.mullvad.mullvadvpn.repository.SplitTunnelingRepository -import net.mullvad.mullvadvpn.repository.UserPreferences -import net.mullvad.mullvadvpn.repository.UserPreferencesMigration -import net.mullvad.mullvadvpn.repository.UserPreferencesRepository -import net.mullvad.mullvadvpn.repository.UserPreferencesSerializer import net.mullvad.mullvadvpn.repository.WireguardConstraintsRepository import net.mullvad.mullvadvpn.service.DaemonConfig import net.mullvad.mullvadvpn.ui.MainActivity @@ -114,13 +107,11 @@ import org.koin.core.qualifier.named import org.koin.dsl.module val uiModule = module { - single<DataStore<UserPreferences>> { androidContext().userPreferencesStore } - single<PackageManager> { androidContext().packageManager } single<String>(named(SELF_PACKAGE_NAME)) { androidContext().packageName } single<ComponentName>(named(BOOT_COMPLETED_RECEIVER_COMPONENT_NAME)) { - ComponentName(androidContext(), BootCompletedReceiver::class.java) + ComponentName(androidContext(), AutoStartVpnBootCompletedReceiver::class.java) } viewModel { SplitTunnelingViewModel(get(), get(), get(), Dispatchers.Default) } @@ -132,7 +123,6 @@ val uiModule = module { single { androidContext().contentResolver } single { ChangelogRepository(get(), get(), get()) } - single { UserPreferencesRepository(get(), get()) } single { SettingsRepository(get()) } single { MullvadProblemReport(get(), get<DaemonConfig>().apiEndpointOverride, get()) } single { RelayOverridesRepository(get()) } @@ -294,10 +284,3 @@ val uiModule = module { const val SELF_PACKAGE_NAME = "SELF_PACKAGE_NAME" const val APP_PREFERENCES_NAME = "${BuildConfig.APPLICATION_ID}.app_preferences" const val BOOT_COMPLETED_RECEIVER_COMPONENT_NAME = "BOOT_COMPLETED_RECEIVER_COMPONENT_NAME" - -private val Context.userPreferencesStore: DataStore<UserPreferences> by - dataStore( - fileName = APP_PREFERENCES_NAME, - serializer = UserPreferencesSerializer, - produceMigrations = UserPreferencesMigration::migrations, - ) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/receiver/BootCompletedReceiver.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/receiver/AutoStartVpnBootCompletedReceiver.kt index 3ab3750c5e..5c3a0a714e 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/receiver/BootCompletedReceiver.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/receiver/AutoStartVpnBootCompletedReceiver.kt @@ -7,8 +7,9 @@ import co.touchlab.kermit.Logger import net.mullvad.mullvadvpn.lib.common.constant.KEY_CONNECT_ACTION import net.mullvad.mullvadvpn.lib.common.constant.VPN_SERVICE_CLASS import net.mullvad.mullvadvpn.lib.common.util.prepareVpnSafe +import org.koin.core.component.KoinComponent -class BootCompletedReceiver : BroadcastReceiver() { +class AutoStartVpnBootCompletedReceiver : BroadcastReceiver(), KoinComponent { override fun onReceive(context: Context?, intent: Intent?) { if (intent?.action == Intent.ACTION_BOOT_COMPLETED) { context?.let { startAndConnectTunnel(context) } diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/receiver/NotificationAlarmReceiver.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/receiver/NotificationAlarmReceiver.kt new file mode 100644 index 0000000000..7b8ab4e04a --- /dev/null +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/receiver/NotificationAlarmReceiver.kt @@ -0,0 +1,43 @@ +package net.mullvad.mullvadvpn.receiver + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import co.touchlab.kermit.Logger +import java.time.Duration +import java.time.ZonedDateTime +import kotlinx.coroutines.runBlocking +import net.mullvad.mullvadvpn.service.notifications.accountexpiry.AccountExpiryNotificationProvider +import net.mullvad.mullvadvpn.usecase.ScheduleNotificationAlarmUseCase +import net.mullvad.mullvadvpn.util.serializable +import org.koin.core.component.KoinComponent +import org.koin.core.component.inject + +class NotificationAlarmReceiver : BroadcastReceiver(), KoinComponent { + private val notificationProvider by inject<AccountExpiryNotificationProvider>() + private val scheduleNotificationAlarmUseCase by inject<ScheduleNotificationAlarmUseCase>() + + override fun onReceive(context: Context?, intent: Intent?) { + + val expiry: ZonedDateTime? = intent?.serializable(ACCOUNT_EXPIRY_KEY) + if (expiry == null) { + Logger.e("NotificationAlarmReceiver: Intent missing account expiry") + return + } + + Logger.d("Account expiry alarm triggered") + val untilExpiry = Duration.between(ZonedDateTime.now(), expiry) + + runBlocking { + notificationProvider.showNotification(untilExpiry) + // Only schedule the next alarm if we still have time left on the account. + if (context != null && expiry > ZonedDateTime.now()) { + scheduleNotificationAlarmUseCase(context = context, accountExpiry = expiry) + } + } + } + + companion object { + const val ACCOUNT_EXPIRY_KEY: String = "account_expiry_key" + } +} diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/receiver/ScheduleNotificationBootCompletedReceiver.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/receiver/ScheduleNotificationBootCompletedReceiver.kt new file mode 100644 index 0000000000..3bd8c9be79 --- /dev/null +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/receiver/ScheduleNotificationBootCompletedReceiver.kt @@ -0,0 +1,32 @@ +package net.mullvad.mullvadvpn.receiver + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import co.touchlab.kermit.Logger +import kotlinx.coroutines.runBlocking +import net.mullvad.mullvadvpn.repository.UserPreferencesRepository +import net.mullvad.mullvadvpn.usecase.ScheduleNotificationAlarmUseCase +import org.koin.core.component.KoinComponent +import org.koin.core.component.inject + +class ScheduleNotificationBootCompletedReceiver : BroadcastReceiver(), KoinComponent { + private val userPreferencesRepository by inject<UserPreferencesRepository>() + private val scheduleNotificationAlarmUseCase by inject<ScheduleNotificationAlarmUseCase>() + + override fun onReceive(context: Context?, intent: Intent?) { + if (intent?.action == Intent.ACTION_BOOT_COMPLETED) { + context?.let { + Logger.d( + "Scheduling notification alarm from ScheduleNotificationBootCompletedReceiver" + ) + runBlocking { scheduleAccountExpiryNotification(context) } + } + } + } + + private suspend fun scheduleAccountExpiryNotification(context: Context) { + val expiry = userPreferencesRepository.accountExpiry() ?: return + scheduleNotificationAlarmUseCase(context, expiry) + } +} diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/receiver/TimeChangedReceiver.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/receiver/TimeChangedReceiver.kt new file mode 100644 index 0000000000..3b37aaa1db --- /dev/null +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/receiver/TimeChangedReceiver.kt @@ -0,0 +1,31 @@ +package net.mullvad.mullvadvpn.receiver + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import co.touchlab.kermit.Logger +import kotlinx.coroutines.runBlocking +import net.mullvad.mullvadvpn.repository.UserPreferencesRepository +import net.mullvad.mullvadvpn.usecase.ScheduleNotificationAlarmUseCase +import org.koin.core.component.KoinComponent +import org.koin.core.component.inject + +class TimeChangedReceiver : BroadcastReceiver(), KoinComponent { + private val userPreferencesRepository by inject<UserPreferencesRepository>() + private val scheduleNotificationAlarmUseCase by inject<ScheduleNotificationAlarmUseCase>() + + override fun onReceive(context: Context?, intent: Intent?) { + if ( + intent?.action == Intent.ACTION_TIME_CHANGED || + intent?.action == Intent.ACTION_TIMEZONE_CHANGED + ) { + runBlocking { + val expiry = userPreferencesRepository.accountExpiry() + if (context != null && expiry != null) { + Logger.d("Scheduling notification alarm from TimeChangedReceiver") + scheduleNotificationAlarmUseCase(context, expiry) + } + } + } + } +} diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/UserPreferencesRepository.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/UserPreferencesRepository.kt index 8a6dfd59a6..85d70c6109 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/UserPreferencesRepository.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/UserPreferencesRepository.kt @@ -3,6 +3,9 @@ package net.mullvad.mullvadvpn.repository import androidx.datastore.core.DataStore import co.touchlab.kermit.Logger import java.io.IOException +import java.time.Instant +import java.time.ZoneId +import java.time.ZonedDateTime import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.first @@ -39,4 +42,18 @@ class UserPreferencesRepository( prefs.toBuilder().setLastShownChangelogVersionCode(buildVersion.code).build() } } + + suspend fun setAccountExpiry(expiry: ZonedDateTime) { + userPreferencesStore.updateData { prefs -> + prefs.toBuilder().setAccountExpiryUnixTimeSeconds(expiry.toEpochSecond()).build() + } + } + + // Returns the account expiry time or null if the account expiry has not been set yet. + suspend fun accountExpiry(): ZonedDateTime? = + preferences().let { prefs -> + val expiryTime = prefs.accountExpiryUnixTimeSeconds + if (expiryTime == 0L) return null + Instant.ofEpochSecond(expiryTime).atZone(ZoneId.systemDefault()) + } } diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt index de4fc2d046..e10f71af95 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt @@ -36,8 +36,12 @@ import net.mullvad.mullvadvpn.lib.model.Prepared import net.mullvad.mullvadvpn.lib.theme.AppTheme import net.mullvad.mullvadvpn.repository.SplashCompleteRepository import net.mullvad.mullvadvpn.repository.UserPreferencesRepository +import net.mullvad.mullvadvpn.service.notifications.accountexpiry.AccountExpiryNotificationProvider import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionManager import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionState +import net.mullvad.mullvadvpn.usecase.AccountExpiryNotificationActionUseCase +import net.mullvad.mullvadvpn.usecase.NotificationAction +import net.mullvad.mullvadvpn.usecase.ScheduleNotificationAlarmUseCase import net.mullvad.mullvadvpn.viewmodel.MullvadAppViewModel import org.koin.android.ext.android.inject import org.koin.android.scope.AndroidScopeComponent @@ -61,6 +65,10 @@ class MainActivity : ComponentActivity(), AndroidScopeComponent { private val serviceConnectionManager by inject<ServiceConnectionManager>() private val splashCompleteRepository by inject<SplashCompleteRepository>() private val managementService by inject<ManagementService>() + private val scheduleNotificationAlarmUseCase by inject<ScheduleNotificationAlarmUseCase>() + private val accountExpiryNotificationActionUseCase by + inject<AccountExpiryNotificationActionUseCase>() + private val accountExpiryNotificationProvider by inject<AccountExpiryNotificationProvider>() private var isReadyNextDraw: Boolean = false @@ -100,6 +108,28 @@ class MainActivity : ComponentActivity(), AndroidScopeComponent { } } } + + lifecycleScope.launch { + repeatOnLifecycle(Lifecycle.State.STARTED) { + accountExpiryNotificationActionUseCase().collect { action -> + when (action) { + NotificationAction.CancelExisting -> { + accountExpiryNotificationProvider.cancelNotification() + scheduleNotificationAlarmUseCase( + context = this@MainActivity, + accountExpiry = null, + ) + } + + is NotificationAction.ScheduleAlarm -> + scheduleNotificationAlarmUseCase( + context = this@MainActivity, + accountExpiry = action.alarmTime, + ) + } + } + } + } } override fun onRestoreInstanceState(savedInstanceState: Bundle) { diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/AccountExpiryInAppNotificationUseCase.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/AccountExpiryInAppNotificationUseCase.kt index a39afe9c39..9d78b47902 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/AccountExpiryInAppNotificationUseCase.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/AccountExpiryInAppNotificationUseCase.kt @@ -8,8 +8,8 @@ import kotlinx.coroutines.flow.map import net.mullvad.mullvadvpn.lib.model.InAppNotification import net.mullvad.mullvadvpn.lib.shared.AccountRepository import net.mullvad.mullvadvpn.service.notifications.accountexpiry.ACCOUNT_EXPIRY_CLOSE_TO_EXPIRY_THRESHOLD -import net.mullvad.mullvadvpn.service.notifications.accountexpiry.ACCOUNT_EXPIRY_IN_APP_NOTIFICATION_UPDATE_INTERVAL -import net.mullvad.mullvadvpn.service.notifications.accountexpiry.AccountExpiryTicker +import net.mullvad.mullvadvpn.service.notifications.accountexpiry.ACCOUNT_EXPIRY_NOTIFICATION_UPDATE_INTERVAL +import net.mullvad.mullvadvpn.service.notifications.accountexpiry.InAppAccountExpiryTicker class AccountExpiryInAppNotificationUseCase(private val accountRepository: AccountRepository) { @@ -18,15 +18,15 @@ class AccountExpiryInAppNotificationUseCase(private val accountRepository: Accou accountRepository.accountData .flatMapLatest { accountData -> if (accountData != null) { - AccountExpiryTicker.tickerFlow( + InAppAccountExpiryTicker.tickerFlow( expiry = accountData.expiryDate, tickStart = ACCOUNT_EXPIRY_CLOSE_TO_EXPIRY_THRESHOLD, - updateInterval = { ACCOUNT_EXPIRY_IN_APP_NOTIFICATION_UPDATE_INTERVAL }, + updateInterval = { ACCOUNT_EXPIRY_NOTIFICATION_UPDATE_INTERVAL }, ) .map { tick -> when (tick) { - AccountExpiryTicker.NotWithinThreshold -> emptyList() - is AccountExpiryTicker.Tick -> + InAppAccountExpiryTicker.NotWithinThreshold -> emptyList() + is InAppAccountExpiryTicker.Tick -> listOf(InAppNotification.AccountExpiry(tick.expiresIn)) } } diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/AccountExpiryNotificationActionUseCase.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/AccountExpiryNotificationActionUseCase.kt new file mode 100644 index 0000000000..4e8ed620bf --- /dev/null +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/AccountExpiryNotificationActionUseCase.kt @@ -0,0 +1,61 @@ +package net.mullvad.mullvadvpn.usecase + +import java.time.ZonedDateTime +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.filter +import kotlinx.coroutines.flow.flattenConcat +import kotlinx.coroutines.flow.flow +import net.mullvad.mullvadvpn.lib.daemon.grpc.ManagementService +import net.mullvad.mullvadvpn.lib.model.DeviceState +import net.mullvad.mullvadvpn.lib.shared.AccountRepository +import net.mullvad.mullvadvpn.service.notifications.accountexpiry.ACCOUNT_EXPIRY_CLOSE_TO_EXPIRY_THRESHOLD + +sealed interface NotificationAction { + data object CancelExisting : NotificationAction + + data class ScheduleAlarm(val alarmTime: ZonedDateTime) : NotificationAction +} + +class AccountExpiryNotificationActionUseCase( + private val accountRepository: AccountRepository, + private val managementService: ManagementService, +) { + @OptIn(ExperimentalCoroutinesApi::class) + operator fun invoke(): Flow<NotificationAction> = + combine(managementService.deviceState, accountRepository.accountData) { + deviceState, + accountData -> + flow { + when (deviceState) { + is DeviceState.LoggedIn -> { + // There are cases where the current device's account number isn't the + // same as the account data device number. This can happen when logging + // out of one account and logging in to another (the deviceState will + // update before the new accountData is available). + if (deviceState.accountNumber == accountData?.accountNumber) { + if (shouldCancelExisting(accountData.expiryDate)) { + emit(NotificationAction.CancelExisting) + } + emit(NotificationAction.ScheduleAlarm(accountData.expiryDate)) + } + } + + DeviceState.LoggedOut, + DeviceState.Revoked -> emit(NotificationAction.CancelExisting) + } + } + } + .flattenConcat() + .filter { !accountRepository.isNewAccount.value } + .distinctUntilChanged() + + private fun shouldCancelExisting(expiry: ZonedDateTime): Boolean { + val expiryTimeIsAfterThreshold = + expiry.isAfter(ZonedDateTime.now().plus(ACCOUNT_EXPIRY_CLOSE_TO_EXPIRY_THRESHOLD)) + + return expiryTimeIsAfterThreshold || accountRepository.isNewAccount.value + } +} diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/ScheduleNotificationAlarmUseCase.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/ScheduleNotificationAlarmUseCase.kt new file mode 100644 index 0000000000..57ed5c5dd2 --- /dev/null +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/ScheduleNotificationAlarmUseCase.kt @@ -0,0 +1,67 @@ +package net.mullvad.mullvadvpn.usecase + +import android.app.AlarmManager +import android.app.PendingIntent +import android.content.Context +import android.content.Intent +import co.touchlab.kermit.Logger +import java.time.ZoneOffset +import java.time.ZonedDateTime +import net.mullvad.mullvadvpn.receiver.NotificationAlarmReceiver +import net.mullvad.mullvadvpn.repository.UserPreferencesRepository +import net.mullvad.mullvadvpn.service.notifications.accountexpiry.accountExpiryNotificationTriggerAt + +class ScheduleNotificationAlarmUseCase( + private val userPreferencesRepository: UserPreferencesRepository +) { + suspend operator fun invoke(context: Context, accountExpiry: ZonedDateTime?) { + val appContext = context.applicationContext + val alarmManager = appContext.getSystemService(AlarmManager::class.java) ?: return + cancelExisting(appContext, alarmManager) + + if (accountExpiry == null) return + + val triggerAt = + accountExpiryNotificationTriggerAt(now = ZonedDateTime.now(), expiry = accountExpiry) + val triggerAtMillis = triggerAt.toInstant().toEpochMilli() + + val intent = alarmIntent(appContext, accountExpiry) + alarmManager.set(AlarmManager.RTC, triggerAtMillis, intent) + + // Change to UTC to avoid leaking the user's time zone in the logs + Logger.d( + "Scheduling next account expiry alarm for ${triggerAt.withZoneSameInstant(ZoneOffset.UTC)}" + ) + userPreferencesRepository.setAccountExpiry(accountExpiry) + } + + private fun alarmIntent(context: Context, accountExpiry: ZonedDateTime): PendingIntent = + Intent(context, NotificationAlarmReceiver::class.java).let { intent -> + intent.putExtra(NotificationAlarmReceiver.ACCOUNT_EXPIRY_KEY, accountExpiry) + PendingIntent.getBroadcast( + context, + ALARM_INTENT_REQUEST_CODE, + intent, + PendingIntent.FLAG_UPDATE_CURRENT + PendingIntent.FLAG_IMMUTABLE, + ) + } + + private fun cancelExisting(context: Context, alarmManager: AlarmManager) { + existingAlarmIntent(context)?.let { pendingIntent -> + alarmManager.cancel(pendingIntent) + Logger.d("Cancelled existing account expiry alarm") + } + } + + private fun existingAlarmIntent(context: Context): PendingIntent? = + PendingIntent.getBroadcast( + context, + ALARM_INTENT_REQUEST_CODE, + Intent(context, NotificationAlarmReceiver::class.java), + PendingIntent.FLAG_UPDATE_CURRENT + + PendingIntent.FLAG_IMMUTABLE + + PendingIntent.FLAG_NO_CREATE, + ) +} + +private const val ALARM_INTENT_REQUEST_CODE = 0 diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/util/ContextExtensions.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/util/ContextExtensions.kt index fd004562a3..e8a5986dac 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/util/ContextExtensions.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/util/ContextExtensions.kt @@ -3,6 +3,10 @@ package net.mullvad.mullvadvpn.util import android.app.Activity import android.content.Context import android.content.ContextWrapper +import android.content.Intent +import android.os.Build +import android.os.Bundle +import java.io.Serializable fun Context.getActivity(): Activity? { return when (this) { @@ -11,3 +15,17 @@ fun Context.getActivity(): Activity? { else -> null } } + +inline fun <reified T : Serializable> Bundle.serializable(key: String): T? = + when { + Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> getSerializable(key, T::class.java) + else -> @Suppress("DEPRECATION") getSerializable(key) as? T + } + +inline fun <reified T : Serializable> Intent.serializable(key: String): T? = + when { + Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> + getSerializableExtra(key, T::class.java) + + else -> @Suppress("DEPRECATION") getSerializableExtra(key) as? T + } diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/WelcomeViewModel.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/WelcomeViewModel.kt index dcda3ee917..334a360cfd 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/WelcomeViewModel.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/WelcomeViewModel.kt @@ -77,7 +77,10 @@ class WelcomeViewModel( accountRepository.accountData .filterNotNull() .filter { it.expiryDate.minusHours(MIN_HOURS_PAST_ACCOUNT_EXPIRY).isAfterNowInstant() } - .onEach { paymentUseCase.resetPurchaseResult() } + .onEach { + paymentUseCase.resetPurchaseResult() + accountRepository.resetIsNewAccount() + } .map { UiSideEffect.OpenConnectScreen } fun onSitePaymentClick() { diff --git a/android/app/src/main/proto/user_prefs.proto b/android/app/src/main/proto/user_prefs.proto index 6f9661970f..ca8ba3e0ba 100644 --- a/android/app/src/main/proto/user_prefs.proto +++ b/android/app/src/main/proto/user_prefs.proto @@ -6,4 +6,5 @@ option java_multiple_files = true; message UserPreferences { bool is_privacy_disclosure_accepted = 1; int32 last_shown_changelog_version_code = 2; + int64 account_expiry_unix_time_seconds = 3; } |
