diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2019-08-28 13:15:22 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2019-08-29 11:14:51 +0000 |
| commit | 786e0af192afa5ef6ce8159e6201602075c7cf49 (patch) | |
| tree | 4e8ffdbf3e9b9e86fd3b94dbd613b9216f2f90cd /android | |
| parent | adaf3236e2719354fe19802a1c82e627704fb185 (diff) | |
| download | mullvadvpn-786e0af192afa5ef6ce8159e6201602075c7cf49.tar.xz mullvadvpn-786e0af192afa5ef6ce8159e6201602075c7cf49.zip | |
Create `ForegroundNotificationManager` class
Diffstat (limited to 'android')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/ForegroundNotificationManager.kt | 33 | ||||
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadVpnService.kt | 26 |
2 files changed, 36 insertions, 23 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ForegroundNotificationManager.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ForegroundNotificationManager.kt new file mode 100644 index 0000000000..3047b7d4b7 --- /dev/null +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ForegroundNotificationManager.kt @@ -0,0 +1,33 @@ +package net.mullvad.mullvadvpn + +import android.app.Notification +import android.app.PendingIntent +import android.app.Service +import android.content.Intent +import android.support.v4.app.NotificationCompat + +val FOREGROUND_NOTIFICATION_ID: Int = 1 + +class ForegroundNotificationManager(val service: Service) { + fun onCreate() { + service.startForeground(FOREGROUND_NOTIFICATION_ID, buildNotification()) + } + + fun onDestroy() { + service.stopForeground(FOREGROUND_NOTIFICATION_ID) + } + + private fun buildNotification(): Notification { + val intent = Intent(service, MainActivity::class.java) + .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP) + .setAction(Intent.ACTION_MAIN) + + val pendingIntent = + PendingIntent.getActivity(service, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT) + + return NotificationCompat.Builder(service) + .setSmallIcon(R.drawable.notification) + .setContentIntent(pendingIntent) + .build() + } +} diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadVpnService.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadVpnService.kt index 60cd6bceeb..02466aec37 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadVpnService.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadVpnService.kt @@ -8,22 +8,15 @@ import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope -import android.app.Activity -import android.app.Notification -import android.app.PendingIntent -import android.content.Context import android.content.Intent import android.net.VpnService import android.os.Binder import android.os.IBinder -import android.support.v4.app.NotificationCompat import net.mullvad.mullvadvpn.dataproxy.AppVersionInfoFetcher import net.mullvad.mullvadvpn.dataproxy.ConnectionProxy import net.mullvad.mullvadvpn.model.TunConfig -val ONGOING_NOTIFICATION_ID: Int = 1 - class MullvadVpnService : VpnService() { private val created = CompletableDeferred<Unit>() private val binder = LocalBinder() @@ -32,10 +25,11 @@ class MullvadVpnService : VpnService() { val daemon = startDaemon() val connectionProxy = ConnectionProxy(this, daemon) + val notificationManager = ForegroundNotificationManager(this) override fun onCreate() { versionInfoFetcher = AppVersionInfoFetcher(daemon, this) - startForeground(ONGOING_NOTIFICATION_ID, buildNotification()) + notificationManager.onCreate() created.complete(Unit) } @@ -45,10 +39,10 @@ class MullvadVpnService : VpnService() { override fun onDestroy() { connectionProxy.onDestroy() + notificationManager.onDestroy() versionInfoFetcher.stop() daemon.cancel() created.cancel() - stopForeground(ONGOING_NOTIFICATION_ID) } fun createTun(config: TunConfig): Int { @@ -94,20 +88,6 @@ class MullvadVpnService : VpnService() { } } - private fun buildNotification(): Notification { - val intent = Intent(this, MainActivity::class.java) - .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP) - .setAction(Intent.ACTION_MAIN) - - val pendingIntent = - PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT) - - return NotificationCompat.Builder(this) - .setSmallIcon(R.drawable.notification) - .setContentIntent(pendingIntent) - .build() - } - private fun startDaemon() = GlobalScope.async(Dispatchers.Default) { created.await() ApiRootCaFile().extract(application) |
