9090package io.matthewnelson.topl_core
9191
9292import android.content.Context
93- import android.content.IntentFilter
9493import android.net.ConnectivityManager
9594import io.matthewnelson.topl_core_base.EventBroadcaster
9695import io.matthewnelson.topl_core.listener.BaseEventListener
97- import io.matthewnelson.topl_core.receiver.NetworkStateReceiver
9896import io.matthewnelson.topl_core.settings.TorSettingsBuilder
9997import io.matthewnelson.topl_core.broadcaster.BroadcastLogger
10098import io.matthewnelson.topl_core.broadcaster.BroadcastLoggerHelper
@@ -220,8 +218,6 @@ class OnionProxyManager(
220218 broadcastLogger.warn(" TorControlConnection is not responding properly to $methodCall " )
221219 }
222220
223- @Volatile
224- private var networkStateReceiver: NetworkStateReceiver ? = null
225221 @Volatile
226222 private var controlSocket: Socket ? = null
227223 // If controlConnection is not null then this means that a connection exists and the Tor OP
@@ -384,21 +380,6 @@ class OnionProxyManager(
384380 }
385381
386382 torStateMachine.setTorState(TorState .OFF )
387-
388- if (networkStateReceiver == null ) return
389-
390- try {
391- appContext.unregisterReceiver(networkStateReceiver)
392- } catch (e: IllegalArgumentException ) {
393- // There is a race condition where if someone calls stop before
394- // installAndStartTorOp is done then we could get an exception because
395- // the network state receiver might not be properly registered.
396- broadcastLogger.exception(
397- IllegalArgumentException (
398- " Someone tried calling stop before registering of NetworkStateReceiver" , e
399- )
400- )
401- }
402383 }
403384 }
404385
@@ -416,6 +397,13 @@ class OnionProxyManager(
416397 false
417398 }
418399
400+ @Suppress(" DEPRECATION" )
401+ fun hasNetworkConnectivity (): Boolean {
402+ val connectivityManager = appContext
403+ .getSystemService(Context .CONNECTIVITY_SERVICE ) as ConnectivityManager ?
404+ return connectivityManager?.activeNetworkInfo?.isConnected ? : false
405+ }
406+
419407 private val disableNetworkLock = Object ()
420408 /* *
421409 * Tells the Tor OP if it should accept network connections.
@@ -424,11 +412,12 @@ class OnionProxyManager(
424412 * such that [torStateMachine] will reflect the proper
425413 * [io.matthewnelson.topl_core_base.BaseConsts.TorNetworkState].
426414 *
427- * @param [disable] If true then the Tor OP will **not** accept SOCKS connections, otherwise yes.
415+ * @param [disable] Sets Tor config DisableNetwork (1 if `true`, 0 if `false`)
428416 * @throws [IOException] if having issues with TorControlConnection#setConf
429- * @throws [KotlinNullPointerException ] if [controlConnection] is null even after checking.
417+ * @throws [NullPointerException ] if [controlConnection] is null even after checking.
430418 */
431- @Throws(IOException ::class , KotlinNullPointerException ::class )
419+ @Synchronized
420+ @Throws(IOException ::class , NullPointerException ::class )
432421 fun disableNetwork (disable : Boolean ) {
433422 synchronized(disableNetworkLock) {
434423 if (! hasControlConnection) return
@@ -518,6 +507,11 @@ class OnionProxyManager(
518507 /* *
519508 * Starts tor control service if it isn't already running.
520509 *
510+ * If the device does not have connectivity, [disableNetwork] will not be called to set
511+ * Tor's config for DisableNetwork to false (0). Handling connectivity changes should be done
512+ * via your own [android.content.BroadcastReceiver] and by calling [disableNetwork] when
513+ * appropriate.
514+ *
521515 * @throws [IOException] File errors
522516 * @throws [SecurityException] Unauthorized access to file/directory.
523517 * @throws [IllegalArgumentException] if [onionProxyContext] methods are passed incorrect
@@ -589,7 +583,13 @@ class OnionProxyManager(
589583 controlConnection.setEvents(listOf (* eventListener.CONTROL_COMMAND_EVENTS ))
590584 }
591585
592- disableNetwork(false )
586+ if (hasNetworkConnectivity())
587+ disableNetwork(false )
588+ else
589+ broadcastLogger.warn(
590+ " No Network Connectivity. Foregoing enabling of Tor Network."
591+ )
592+
593593 } catch (e: Exception ) {
594594 torProcess?.destroy()
595595 this .controlConnection = null
@@ -599,12 +599,6 @@ class OnionProxyManager(
599599 }
600600
601601 torStateMachine.setTorState(TorState .ON )
602-
603- networkStateReceiver = NetworkStateReceiver (this )
604-
605- @Suppress(" DEPRECATION" )
606- val filter = IntentFilter (ConnectivityManager .CONNECTIVITY_ACTION )
607- appContext.registerReceiver(networkStateReceiver, filter)
608602 broadcastLogger.notice(" Completed starting of Tor" )
609603 }
610604
@@ -894,7 +888,7 @@ class OnionProxyManager(
894888 @Synchronized
895889 suspend fun signalNewNym () {
896890 if (! hasControlConnection || ! isBootstrapped) return
897- if (networkStateReceiver?.networkConnectivity != true ) {
891+ if (! hasNetworkConnectivity() ) {
898892 broadcastLogger.notice(" NEWNYM: $NEWNYM_NO_NETWORK " )
899893 return
900894 }
0 commit comments