diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-06-09 14:49:20 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-06-22 12:29:44 +0000 |
| commit | 142482eeb4212fe0a0b3ab51be4e548a1f224651 (patch) | |
| tree | 8db099463fa80b4dcf22d15e27e398879684fc0a /android/src/main | |
| parent | f8832497a94ea4ae07411b7f27feb13a607ac4d7 (diff) | |
| download | mullvadvpn-142482eeb4212fe0a0b3ab51be4e548a1f224651.tar.xz mullvadvpn-142482eeb4212fe0a0b3ab51be4e548a1f224651.zip | |
Create `AccountExpiryNotification` class
Diffstat (limited to 'android/src/main')
3 files changed, 90 insertions, 0 deletions
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 new file mode 100644 index 0000000000..0a40afc763 --- /dev/null +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/AccountExpiryNotification.kt @@ -0,0 +1,76 @@ +package net.mullvad.mullvadvpn.service.notifications + +import android.app.Notification +import android.app.NotificationManager +import android.app.PendingIntent +import android.content.Context +import android.content.Intent +import android.net.Uri +import kotlin.properties.Delegates.observable +import net.mullvad.mullvadvpn.R +import org.joda.time.DateTime +import org.joda.time.Duration + +class AccountExpiryNotification(val context: Context) { + companion object { + val NOTIFICATION_ID: Int = 2 + } + + private val resources = context.resources + + private val buyMoreTimeUrl = Uri.parse(resources.getString(R.string.account_url)) + + private val channel = NotificationChannel( + context, + "mullvad_account_time", + R.string.account_time_notification_channel_name, + R.string.account_time_notification_channel_description, + NotificationManager.IMPORTANCE_HIGH + ) + + var accountExpiry by observable<DateTime?>(null) { _, oldValue, newValue -> + if (oldValue != newValue) { + if (newValue != null) { + channel.notificationManager.notify(NOTIFICATION_ID, build(newValue)) + } else { + channel.notificationManager.cancel(NOTIFICATION_ID) + } + } + } + + private fun build(expiry: DateTime): Notification { + val intent = Intent(Intent.ACTION_VIEW, buyMoreTimeUrl) + val flags = PendingIntent.FLAG_UPDATE_CURRENT + val pendingIntent = PendingIntent.getActivity(context, 1, intent, flags) + + return channel.buildNotification(pendingIntent, format(expiry)) + } + + private fun format(expiry: DateTime): String { + val remainingTime = Duration(DateTime.now(), expiry) + + if (remainingTime.isShorterThan(Duration.ZERO)) { + return resources.getString(R.string.account_credit_has_expired) + } else { + val remainingTimeInfo = remainingTime.toPeriodTo(expiry) + + if (remainingTimeInfo.days >= 1) { + return getRemainingText( + R.plurals.account_credit_expires_in_days, + remainingTime.standardDays.toInt() + ) + } else if (remainingTimeInfo.hours >= 1) { + return getRemainingText( + R.plurals.account_credit_expires_in_hours, + remainingTime.standardHours.toInt() + ) + } else { + return resources.getString(R.string.account_credit_expires_in_a_few_minutes) + } + } + } + + private fun getRemainingText(pluralId: Int, quantity: Int): String { + return resources.getQuantityString(pluralId, quantity, quantity) + } +} diff --git a/android/src/main/res/values/plurals.xml b/android/src/main/res/values/plurals.xml index 679c4f46db..224cc9cb79 100644 --- a/android/src/main/res/values/plurals.xml +++ b/android/src/main/res/values/plurals.xml @@ -32,4 +32,12 @@ <item quantity="one">a year ago</item> <item quantity="other">%d years ago</item> </plurals> + <plurals name="account_credit_expires_in_days"> + <item quantity="one">Account credit expires in a day</item> + <item quantity="other">Account credit expires in %d days</item> + </plurals> + <plurals name="account_credit_expires_in_hours"> + <item quantity="one">Account credit expires in an hour</item> + <item quantity="other">Account credit expires in %d hours</item> + </plurals> </resources> diff --git a/android/src/main/res/values/strings.xml b/android/src/main/res/values/strings.xml index 1bdf764ac4..48921c07a3 100644 --- a/android/src/main/res/values/strings.xml +++ b/android/src/main/res/values/strings.xml @@ -10,6 +10,9 @@ <string name="foreground_notification_channel_name">VPN tunnel status</string> <string name="foreground_notification_channel_description">Shows current VPN tunnel status</string> + <string name="account_time_notification_channel_name">Account time reminders</string> + <string name="account_time_notification_channel_description">Shows reminders when the account + time is about to expire</string> <string name="connecting_to_daemon">Connecting to Mullvad system service...</string> <string name="login_title">Login</string> <string name="login_description">Enter your account number</string> @@ -41,6 +44,9 @@ <string name="settings_account">Account</string> <string name="less_than_a_day_left">less than a day left</string> <string name="less_than_a_minute_ago">less than a minute ago</string> + <string name="account_credit_expires_in_a_few_minutes">Account credit expires in a few + minutes</string> + <string name="account_credit_has_expired">Account credit has expired</string> <string name="out_of_time">Out of time</string> <string name="no_more_vpn_time_left">You have no more VPN time left on this account. Either buy credit on our website or redeem a voucher.</string> |
