diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-06-09 21:07:41 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-06-22 12:29:44 +0000 |
| commit | 99ab246be4def8cf1ee70e3040fdfc9ecb6c97d8 (patch) | |
| tree | 84712a64234218bdc109f75558a834161218317e | |
| parent | 74408e14fde254530e55c1ee8f335a157f6b6bc4 (diff) | |
| download | mullvadvpn-99ab246be4def8cf1ee70e3040fdfc9ecb6c97d8.tar.xz mullvadvpn-99ab246be4def8cf1ee70e3040fdfc9ecb6c97d8.zip | |
Use authentication token for notification intent
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt | 13 | ||||
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/AccountExpiryNotification.kt | 17 |
2 files changed, 21 insertions, 9 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt index 681a70b089..795f5596df 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt @@ -49,6 +49,10 @@ class MullvadVpnService : TalpidVpnService() { if (newInstance != oldInstance) { oldInstance?.onDestroy() + accountExpiryNotification = newInstance?.daemon?.let { daemon -> + AccountExpiryNotification(this, daemon) + } + accountNumberEvents = newInstance?.accountCache?.onAccountNumberChange accountExpiryEvents = newInstance?.accountCache?.onAccountExpiryChange @@ -61,10 +65,14 @@ class MullvadVpnService : TalpidVpnService() { } private var accountExpiryEvents by autoSubscribable<DateTime?>(this, null) { expiry -> - accountExpiryNotification.accountExpiry = expiry + accountExpiryNotification?.accountExpiry = expiry } - private lateinit var accountExpiryNotification: AccountExpiryNotification + private var accountExpiryNotification + by observable<AccountExpiryNotification?>(null) { _, oldNotification, _ -> + oldNotification?.accountExpiry = null + } + private lateinit var keyguardManager: KeyguardManager private lateinit var notificationManager: ForegroundNotificationManager private lateinit var tunnelStateUpdater: TunnelStateUpdater @@ -92,7 +100,6 @@ class MullvadVpnService : TalpidVpnService() { super.onCreate() Log.d(TAG, "Initializing service") - accountExpiryNotification = AccountExpiryNotification(this) keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager notificationManager = ForegroundNotificationManager(this, serviceNotifier, keyguardManager) tunnelStateUpdater = TunnelStateUpdater(this, serviceNotifier) diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/AccountExpiryNotification.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/AccountExpiryNotification.kt index 34c1360f72..34ac1d5a7d 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/AccountExpiryNotification.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/AccountExpiryNotification.kt @@ -9,11 +9,12 @@ import android.net.Uri import kotlin.properties.Delegates.observable import kotlinx.coroutines.delay import net.mullvad.mullvadvpn.R +import net.mullvad.mullvadvpn.service.MullvadDaemon import net.mullvad.mullvadvpn.util.JobTracker import org.joda.time.DateTime import org.joda.time.Duration -class AccountExpiryNotification(val context: Context) { +class AccountExpiryNotification(val context: Context, val daemon: MullvadDaemon) { companion object { val NOTIFICATION_ID: Int = 2 val REMAINING_TIME_FOR_REMINDERS = Duration.standardDays(2) @@ -23,7 +24,7 @@ class AccountExpiryNotification(val context: Context) { private val jobTracker = JobTracker() private val resources = context.resources - private val buyMoreTimeUrl = Uri.parse(resources.getString(R.string.account_url)) + private val buyMoreTimeUrl = resources.getString(R.string.account_url) private val channel = NotificationChannel( context, @@ -35,11 +36,11 @@ class AccountExpiryNotification(val context: Context) { var accountExpiry by observable<DateTime?>(null) { _, oldValue, newValue -> if (oldValue != newValue) { - update(newValue) + jobTracker.newUiJob("update") { update(newValue) } } } - private fun update(accountExpiry: DateTime?) { + private suspend fun update(accountExpiry: DateTime?) { val remainingTime = accountExpiry?.let { expiry -> Duration(DateTime.now(), expiry) } if (remainingTime != null && remainingTime.isShorterThan(REMAINING_TIME_FOR_REMINDERS)) { @@ -59,8 +60,12 @@ class AccountExpiryNotification(val context: Context) { update(accountExpiry) } - private fun build(expiry: DateTime, remainingTime: Duration): Notification { - val intent = Intent(Intent.ACTION_VIEW, buyMoreTimeUrl) + private suspend fun build(expiry: DateTime, remainingTime: Duration): Notification { + val url = jobTracker.runOnBackground { + Uri.parse("$buyMoreTimeUrl?token=${daemon.getWwwAuthToken()}") + } + + val intent = Intent(Intent.ACTION_VIEW, url) val flags = PendingIntent.FLAG_UPDATE_CURRENT val pendingIntent = PendingIntent.getActivity(context, 1, intent, flags) |
