@@ -131,18 +131,11 @@ class TorServiceController private constructor(): ServiceConsts() {
131131 private val torServiceNotificationBuilder : ServiceNotification .Builder ,
132132 private val backgroundManagerPolicy : BackgroundManager .Builder .Policy ,
133133 private val buildConfigVersionCode : Int ,
134- torSettings : TorSettings ,
134+ private val torSettings : TorSettings ,
135135 private val geoipAssetPath : String ,
136136 private val geoip6AssetPath : String
137137 ) {
138138
139- init {
140- // Ensure TorSettings gets initialized no matter if exceptions are thrown
141- // elsewhere in the builder
142- BaseService .initializTorSettings(torSettings)
143- }
144-
145- private var appEventBroadcaster: TorServiceEventBroadcaster ? = Companion .appEventBroadcaster
146139// private var heartbeatTime = BackgroundManager.heartbeatTime
147140 private var restartTorDelayTime = ServiceActionProcessor .restartTorDelayTime
148141 private var stopServiceDelayTime = ServiceActionProcessor .stopServiceDelayTime
@@ -203,24 +196,10 @@ class TorServiceController private constructor(): ServiceConsts() {
203196 * When your task is removed from the Recent App's tray, [TorService.onTaskRemoved] is
204197 * triggered. Default behaviour is to stop Tor, and then [TorService]. Electing this
205198 * option will inhibit the default behaviour from being carried out.
206- *
207- * @throws [IllegalArgumentException] If your selected [BackgroundManager.Builder.Policy]
208- * is *not* [ServiceConsts.BackgroundPolicy.RUN_IN_FOREGROUND], or if
209- * [BackgroundManager.Builder.killAppIfTaskIsRemoved] is *not* `true`
210199 * */
211200 @JvmOverloads
212- @Throws(IllegalArgumentException ::class )
213201 fun disableStopServiceOnTaskRemoved (disable : Boolean = true): Builder {
214- if (disable) {
215- val policy = backgroundManagerPolicy.policyBuilder.chosenPolicy
216- val killApp = backgroundManagerPolicy.policyBuilder.killAppIfTaskIsRemoved
217- require(policy == BackgroundPolicy .RUN_IN_FOREGROUND && killApp) {
218- " BackgroundManager's selected Policy must be " +
219- " ${BackgroundPolicy .RUN_IN_FOREGROUND } , and killAppIfTaskIsRemoved must " +
220- " be set to true."
221- }
222- stopServiceOnTaskRemoved = ! disable
223- }
202+ stopServiceOnTaskRemoved = ! disable
224203 return this
225204 }
226205
@@ -275,7 +254,8 @@ class TorServiceController private constructor(): ServiceConsts() {
275254 * class actually is.
276255 * */
277256 fun setEventBroadcaster (eventBroadcaster : TorServiceEventBroadcaster ): Builder {
278- this .appEventBroadcaster = eventBroadcaster
257+ if (Companion .appEventBroadcaster == null )
258+ appEventBroadcaster = eventBroadcaster
279259 return this
280260 }
281261
@@ -302,19 +282,21 @@ class TorServiceController private constructor(): ServiceConsts() {
302282 * [Companion] object w/o throwing exceptions.
303283 *
304284 * See [Builder] for code samples.
285+ *
286+ * @throws [IllegalArgumentException] If [disableStopServiceOnTaskRemoved] was elected
287+ * and your selected [BackgroundManager.Builder.Policy] is **not**
288+ * [ServiceConsts.BackgroundPolicy.RUN_IN_FOREGROUND] and/or
289+ * [BackgroundManager.Builder.killAppIfTaskIsRemoved] is **not** `true`
305290 * */
306291 fun build () {
307292
308- // If `BaseService.application` has been initialized
309- // already, return as to not overwrite things.
310- try {
311- BaseService .getAppContext()
312- return
313- } catch (e: RuntimeException ) {}
293+ // Must be called before application gets set
294+ ServiceActionProcessor .initialize(restartTorDelayTime, stopServiceDelayTime)
314295
315296 BaseService .initialize(
316297 application,
317298 buildConfigVersionCode,
299+ torSettings,
318300 buildConfigDebug,
319301 geoipAssetPath,
320302 geoip6AssetPath,
@@ -323,13 +305,16 @@ class TorServiceController private constructor(): ServiceConsts() {
323305 )
324306
325307// BackgroundManager.initialize(heartbeatTime)
326- ServiceActionProcessor .initialize(restartTorDelayTime, stopServiceDelayTime)
327-
328- Companion .appEventBroadcaster = this .appEventBroadcaster
329-
330308 torServiceNotificationBuilder.build(application.applicationContext)
331309
332- backgroundManagerPolicy.build()
310+ if (backgroundManagerPolicy.configurationIsCompliant(stopServiceOnTaskRemoved))
311+ backgroundManagerPolicy.build()
312+ else
313+ throw IllegalArgumentException (
314+ " disableStopServiceOnTaskRemoved requires a BackgroundManager Policy of " +
315+ " ${BackgroundPolicy .RUN_IN_FOREGROUND } , and " +
316+ " killAppIfTaskIsRemoved must be set to true."
317+ )
333318 }
334319 }
335320
@@ -345,6 +330,9 @@ class TorServiceController private constructor(): ServiceConsts() {
345330 /* *
346331 * Get the [TorConfigFiles] that have been set after calling [Builder.build]
347332 *
333+ * This method will *never* throw the [RuntimeException] if you call it after
334+ * [Builder.build].
335+ *
348336 * @return Instance of [TorConfigFiles] that are being used throughout TOPL-Android
349337 * @throws [RuntimeException] if called before [Builder.build]
350338 * */
@@ -359,15 +347,13 @@ class TorServiceController private constructor(): ServiceConsts() {
359347 }
360348
361349 /* *
362- * Get the [TorSettings] that have been set after instantiating [Builder]. These are
363- * the [TorSettings] you initialized [TorServiceController.Builder] with.
350+ * Get the [TorSettings] that have been set after calling [Builder.build].
364351 *
365352 * This method will *never* throw the [RuntimeException] if you call it after
366- * you have instantiated the [Builder] object, as [TorSettings] are set immediately
367- * via [BaseService.initializTorSettings].
353+ * [Builder.build].
368354 *
369355 * @return Instance of [TorSettings] that are being used throughout TOPL-Android
370- * @throws [RuntimeException] if called before [Builder] is instantiated
356+ * @throws [RuntimeException] if called before [Builder.build]
371357 * */
372358 @JvmStatic
373359 @Throws(RuntimeException ::class )
0 commit comments