aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/service
diff options
context:
space:
mode:
authorpacien2020-12-08 18:03:20 +0100
committerpacien2020-12-08 18:03:20 +0100
commit355251694d63640f028f3e2c17235d12a8573df6 (patch)
tree966ffd45ec29797afb440ead00586f4fcdf15589 /app/src/main/java/org/pacien/tincapp/service
parent2760703484f9b12f8c21c395915f9780b1ae7e9e (diff)
downloadtincapp-355251694d63640f028f3e2c17235d12a8573df6.tar.gz
ConfigurationAccessService: prevent service from being stopped when app loses focus
This makes the ConfigurationAccessService (formerly ConfigurationFtpService) start in foreground through the use of a persistent notification so that it isn't stopped by the system after the app loses the focus on the user's screen, which happens when the user switches to an FTP client application on the same device.
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/service')
-rw-r--r--app/src/main/java/org/pacien/tincapp/service/ConfigurationAccessService.kt (renamed from app/src/main/java/org/pacien/tincapp/service/ConfigurationFtpService.kt)26
1 files changed, 25 insertions, 1 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/service/ConfigurationFtpService.kt b/app/src/main/java/org/pacien/tincapp/service/ConfigurationAccessService.kt
index 2ea4a16..b083a83 100644
--- a/app/src/main/java/org/pacien/tincapp/service/ConfigurationFtpService.kt
+++ b/app/src/main/java/org/pacien/tincapp/service/ConfigurationAccessService.kt
@@ -18,6 +18,7 @@
18 18
19package org.pacien.tincapp.service 19package org.pacien.tincapp.service
20 20
21import android.app.PendingIntent
21import android.app.Service 22import android.app.Service
22import android.content.Intent 23import android.content.Intent
23import android.os.IBinder 24import android.os.IBinder
@@ -31,7 +32,9 @@ import org.apache.ftpserver.listener.ListenerFactory
31import org.apache.ftpserver.usermanager.UsernamePasswordAuthentication 32import org.apache.ftpserver.usermanager.UsernamePasswordAuthentication
32import org.apache.ftpserver.usermanager.impl.WritePermission 33import org.apache.ftpserver.usermanager.impl.WritePermission
33import org.pacien.tincapp.R 34import org.pacien.tincapp.R
35import org.pacien.tincapp.activities.configure.ConfigureActivity
34import org.pacien.tincapp.context.App 36import org.pacien.tincapp.context.App
37import org.pacien.tincapp.context.AppNotificationManager
35import org.pacien.tincapp.extensions.Java.defaultMessage 38import org.pacien.tincapp.extensions.Java.defaultMessage
36import org.slf4j.LoggerFactory 39import org.slf4j.LoggerFactory
37import java.io.IOException 40import java.io.IOException
@@ -42,7 +45,7 @@ import java.io.IOException
42 * 45 *
43 * @author pacien 46 * @author pacien
44 */ 47 */
45class ConfigurationFtpService : Service() { 48class ConfigurationAccessService : Service() {
46 companion object { 49 companion object {
47 // Apache Mina FtpServer's INFO log level is actually VERBOSE. 50 // Apache Mina FtpServer's INFO log level is actually VERBOSE.
48 // The object holds static references to those loggers so that they stay around. 51 // The object holds static references to those loggers so that they stay around.
@@ -63,6 +66,7 @@ class ConfigurationFtpService : Service() {
63 } 66 }
64 67
65 private val log by lazy { LoggerFactory.getLogger(this.javaClass)!! } 68 private val log by lazy { LoggerFactory.getLogger(this.javaClass)!! }
69 private val notificationManager by lazy { App.notificationManager }
66 private var sftpServer: FtpServer? = null 70 private var sftpServer: FtpServer? = null
67 71
68 override fun onBind(intent: Intent): IBinder? = null // non-bindable service 72 override fun onBind(intent: Intent): IBinder? = null // non-bindable service
@@ -82,6 +86,7 @@ class ConfigurationFtpService : Service() {
82 it.start() 86 it.start()
83 runningState.set(true) 87 runningState.set(true)
84 log.info("Started FTP server on port {}", FTP_PORT) 88 log.info("Started FTP server on port {}", FTP_PORT)
89 pinInForeground()
85 } catch (e: IOException) { 90 } catch (e: IOException) {
86 log.error("Could not start FTP server", e) 91 log.error("Could not start FTP server", e)
87 App.alert(R.string.notification_error_title_unable_to_start_ftp_server, e.defaultMessage()) 92 App.alert(R.string.notification_error_title_unable_to_start_ftp_server, e.defaultMessage())
@@ -91,6 +96,25 @@ class ConfigurationFtpService : Service() {
91 return START_NOT_STICKY 96 return START_NOT_STICKY
92 } 97 }
93 98
99 /**
100 * Pins the service in the foreground so that it doesn't get stopped by the system when the
101 * application's activities are put in the background, which is the case when the user sets the
102 * focus on an FTP client app for example.
103 */
104 private fun pinInForeground() {
105 startForeground(
106 AppNotificationManager.CONFIG_ACCESS_NOTIFICATION_ID,
107 notificationManager.newConfigurationAccessNotificationBuilder()
108 .setSmallIcon(R.drawable.ic_baseline_folder_open_primary_24dp)
109 .setContentTitle(resources.getString(R.string.notification_config_access_server_running_title))
110 .setContentText(resources.getString(R.string.notification_config_access_server_running_message))
111 .setContentIntent(Intent(this, ConfigureActivity::class.java).let {
112 PendingIntent.getActivity(this, 0, it, 0)
113 })
114 .build()
115 )
116 }
117
94 private fun setupSingleUserServer(ftpUser: User): FtpServer { 118 private fun setupSingleUserServer(ftpUser: User): FtpServer {
95 return FtpServerFactory() 119 return FtpServerFactory()
96 .apply { addListener("default", ListenerFactory().apply { port = FTP_PORT }.createListener()) } 120 .apply { addListener("default", ListenerFactory().apply { port = FTP_PORT }.createListener()) }