Skip to content
This repository was archived by the owner on Dec 18, 2022. It is now read-only.

Commit ffb0064

Browse files
authored
Merge pull request #63 from 05nelsonm/mn/feature/no-stop-on-task-removed-option
Adds Builder option to disable stopping of service onTaskRemoved
2 parents ae7cbc0 + 4fc84b4 commit ffb0064

File tree

5 files changed

+44
-9
lines changed

5 files changed

+44
-9
lines changed

sampleapp/src/main/java/io/matthewnelson/sampleapp/App.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ class App: Application() {
123123
return BackgroundManager.Builder()
124124

125125
// All available options present. Only 1 is able to be chosen.
126-
//.respectResourcesWhileInBackground(secondsFrom5To45 = 20)
127-
.runServiceInForeground(killAppIfTaskIsRemoved = true)
126+
.respectResourcesWhileInBackground(secondsFrom5To45 = 20)
127+
//.runServiceInForeground(killAppIfTaskIsRemoved = true)
128128
// }
129129
}
130130

@@ -149,6 +149,7 @@ class App: Application() {
149149
)
150150
.addTimeToRestartTorDelay(milliseconds = 100L)
151151
.addTimeToStopServiceDelay(milliseconds = 100L)
152+
.disableStopServiceOnTaskRemoved(disable = false)
152153
.setBuildConfigDebug(buildConfigDebug = BuildConfig.DEBUG)
153154

154155
// Can instantiate directly here then access it from

topl-service/src/main/java/io/matthewnelson/topl_service/TorServiceController.kt

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ class TorServiceController private constructor(): ServiceConsts() {
138138
// private var heartbeatTime = BackgroundManager.heartbeatTime
139139
private var restartTorDelayTime = ServiceActionProcessor.restartTorDelayTime
140140
private var stopServiceDelayTime = ServiceActionProcessor.stopServiceDelayTime
141+
private var stopServiceOnTaskRemoved = true
141142
private var torConfigFiles: TorConfigFiles? = null
142143

143144
// On published releases of this Library, this value will **always** be `false`.
@@ -190,6 +191,31 @@ class TorServiceController private constructor(): ServiceConsts() {
190191
return this
191192
}
192193

194+
/**
195+
* When your task is removed from the Recent App's tray, [TorService.onTaskRemoved] is
196+
* triggered. Default behaviour is to stop Tor, and then [TorService]. Electing this
197+
* option will inhibit the default behaviour from being carried out.
198+
*
199+
* @throws [IllegalArgumentException] If your selected [BackgroundManager.Builder.Policy]
200+
* is *not* [ServiceConsts.BackgroundPolicy.RUN_IN_FOREGROUND], or if
201+
* [BackgroundManager.Builder.killAppIfTaskIsRemoved] is *not* `true`
202+
* */
203+
@JvmOverloads
204+
@Throws(IllegalArgumentException::class)
205+
fun disableStopServiceOnTaskRemoved(disable: Boolean = true): Builder {
206+
if (disable) {
207+
val policy = backgroundManagerPolicy.policyBuilder.chosenPolicy
208+
val killApp = backgroundManagerPolicy.policyBuilder.killAppIfTaskIsRemoved
209+
require(policy == BackgroundPolicy.RUN_IN_FOREGROUND && killApp) {
210+
"BackgroundManager's selected Policy must be " +
211+
"${BackgroundPolicy.RUN_IN_FOREGROUND}, and killAppIfTaskIsRemoved must " +
212+
"be set to true."
213+
}
214+
stopServiceOnTaskRemoved = !disable
215+
}
216+
return this
217+
}
218+
193219
// /**
194220
// * Default is set to 30_000ms
195221
// *
@@ -285,7 +311,8 @@ class TorServiceController private constructor(): ServiceConsts() {
285311
geoipAssetPath,
286312
geoip6AssetPath,
287313
torConfigFiles ?: TorConfigFiles.createConfig(application.applicationContext),
288-
torSettings
314+
torSettings,
315+
stopServiceOnTaskRemoved
289316
)
290317

291318
// BackgroundManager.initialize(heartbeatTime)

topl-service/src/main/java/io/matthewnelson/topl_service/lifecycle/BackgroundManager.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ class BackgroundManager internal constructor(
151151
class Builder {
152152

153153
@BackgroundPolicy
154-
private lateinit var chosenPolicy: String
154+
internal lateinit var chosenPolicy: String
155155
private var executionDelay: Long = 30_000L
156-
private var killAppIfTaskIsRemoved = false
156+
internal var killAppIfTaskIsRemoved = false
157157

158158
/**
159159
* Stops [TorService] after being in the background for the declared [secondsFrom5To45].
@@ -221,7 +221,7 @@ class BackgroundManager internal constructor(
221221
*
222222
* @param [policyBuilder] The [BackgroundManager.Builder] to be built during initialization
223223
* */
224-
class Policy(private val policyBuilder: Builder) {
224+
class Policy(internal val policyBuilder: Builder) {
225225

226226
/**
227227
* Only available internally, so this is where we intercept for integration testing.

topl-service/src/main/java/io/matthewnelson/topl_service/notification/ServiceNotification.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,9 @@ class ServiceNotification internal constructor(
496496
if (inForeground) {
497497
torService.stopForeground(!showNotification)
498498
inForeground = false
499-
launchRefreshNotificationJob(torService)
499+
notificationBuilder?.let {
500+
notify(torService, it)
501+
}
500502
}
501503
return serviceNotification
502504
}

topl-service/src/main/java/io/matthewnelson/topl_service/service/BaseService.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ internal abstract class BaseService: Service() {
108108
private set
109109
lateinit var torConfigFiles: TorConfigFiles
110110
lateinit var torSettings: TorSettings
111+
var stopServiceOnTaskRemoved: Boolean = true
112+
private set
111113

112114
fun initialize(
113115
application: Application,
@@ -116,7 +118,8 @@ internal abstract class BaseService: Service() {
116118
geoipAssetPath: String,
117119
geoip6AssetPath: String,
118120
torConfigFiles: TorConfigFiles,
119-
torSettings: TorSettings
121+
torSettings: TorSettings,
122+
stopServiceOnTaskRemoved: Boolean
120123
) {
121124
this.application = application
122125
this.buildConfigVersionCode = buildConfigVersionCode
@@ -125,6 +128,7 @@ internal abstract class BaseService: Service() {
125128
this.geoip6AssetPath = geoip6AssetPath
126129
this.torConfigFiles = torConfigFiles
127130
this.torSettings = torSettings
131+
this.stopServiceOnTaskRemoved = stopServiceOnTaskRemoved
128132
}
129133

130134
@Throws(RuntimeException::class)
@@ -385,6 +389,7 @@ internal abstract class BaseService: Service() {
385389
startForegroundService()
386390

387391
// Shutdown Tor and stop the Service.
388-
processServiceAction(ServiceActions.Stop(updateLastServiceAction = false))
392+
if (stopServiceOnTaskRemoved)
393+
processServiceAction(ServiceActions.Stop(updateLastServiceAction = false))
389394
}
390395
}

0 commit comments

Comments
 (0)