@@ -50,7 +50,7 @@ const val START_VPN_REQUEST = 123
5050const val INSTALL_CERT_REQUEST = 456
5151const val ENABLE_NOTIFICATIONS_REQUEST = 101
5252
53- enum class MainState {
53+ enum class ConnectionState {
5454 DISCONNECTED ,
5555 CONNECTING ,
5656 CONNECTED ,
@@ -71,17 +71,17 @@ class MainActivity : ComponentActivity(), CoroutineScope by MainScope() {
7171 private val broadcastReceiver = object : BroadcastReceiver () {
7272 override fun onReceive (context : Context , intent : Intent ) {
7373 if (intent.action == VPN_STARTED_BROADCAST ) {
74- mainState = MainState .CONNECTED
74+ mainState = ConnectionState .CONNECTED
7575 currentProxyConfig = intent.getParcelableExtra(IntentExtras .PROXY_CONFIG_EXTRA )
7676 updateAppCounts()
7777 } else if (intent.action == VPN_STOPPED_BROADCAST ) {
78- mainState = MainState .DISCONNECTED
78+ mainState = ConnectionState .DISCONNECTED
7979 currentProxyConfig = null
8080 }
8181 }
8282 }
8383
84- private var mainState: MainState by mutableStateOf(if (isVpnActive()) MainState .CONNECTED else MainState .DISCONNECTED )
84+ private var mainState: ConnectionState by mutableStateOf(if (isVpnActive()) ConnectionState .CONNECTED else ConnectionState .DISCONNECTED )
8585
8686 // If connected/late-stage connecting, the proxy we're connected/trying to connect to. Otherwise null.
8787 private var currentProxyConfig: ProxyConfig ? by mutableStateOf(activeVpnConfig())
@@ -179,21 +179,25 @@ class MainActivity : ComponentActivity(), CoroutineScope by MainScope() {
179179 setContent {
180180 HttpToolkitTheme {
181181 MainScreen (
182- state = mainState,
183- proxyConfig = currentProxyConfig,
184- hasCamera = packageManager.hasSystemFeature(PackageManager .FEATURE_CAMERA_ANY ),
185- lastProxy = app.lastProxy,
186- totalAppCount = totalAppCount,
187- interceptedAppCount = interceptedAppCount,
188- interceptedPorts = interceptedPorts,
189- onScanQRCode = { checkCameraPermission() },
190- onReconnect = { reconnect() },
191- onDisconnect = { disconnect() },
192- onRecoverAfterFailure = { recoverAfterFailure() },
193- onTestInterception = { testInterception() },
194- onOpenDocs = { openDocs() },
195- onChooseApps = { chooseApps() },
196- onChoosePorts = { choosePorts() }
182+ screenState = MainScreenState (
183+ connectionState = mainState,
184+ proxyConfig = currentProxyConfig,
185+ hasCamera = packageManager.hasSystemFeature(PackageManager .FEATURE_CAMERA_ANY ),
186+ lastProxy = app.lastProxy,
187+ totalAppCount = totalAppCount,
188+ interceptedAppCount = interceptedAppCount,
189+ interceptedPorts = interceptedPorts
190+ ),
191+ actions = MainScreenActions (
192+ onScanQRCode = { checkCameraPermission() },
193+ onReconnect = { reconnect() },
194+ onDisconnect = { disconnect() },
195+ onRecoverAfterFailure = { recoverAfterFailure() },
196+ onTestInterception = { testInterception() },
197+ onOpenDocs = { openDocs() },
198+ onChooseApps = { chooseApps() },
199+ onChoosePorts = { choosePorts() }
200+ )
197201 )
198202 }
199203 }
@@ -359,7 +363,7 @@ class MainActivity : ComponentActivity(), CoroutineScope by MainScope() {
359363 Log .i(TAG , " Connect to VPN" )
360364
361365 this .currentProxyConfig = config
362- this .mainState = MainState .CONNECTING
366+ this .mainState = ConnectionState .CONNECTING
363367
364368 val vpnIntent = VpnService .prepare(this )
365369 Log .i(TAG , if (vpnIntent != null ) " got intent" else " no intent" )
@@ -377,15 +381,15 @@ class MainActivity : ComponentActivity(), CoroutineScope by MainScope() {
377381
378382 private fun disconnect () {
379383 currentProxyConfig = null
380- mainState = MainState .DISCONNECTING
384+ mainState = ConnectionState .DISCONNECTING
381385
382386 startService(Intent (this , ProxyVpnService ::class .java).apply {
383387 action = STOP_VPN_ACTION
384388 })
385389 }
386390
387391 private suspend fun reconnect (lastProxy : ProxyConfig ) {
388- mainState = MainState .CONNECTING
392+ mainState = ConnectionState .CONNECTING
389393
390394 try {
391395 // Revalidates the config, to ensure the server is available (and drop retries if not)
@@ -404,7 +408,7 @@ class MainActivity : ComponentActivity(), CoroutineScope by MainScope() {
404408 Log .e(TAG , e.toString())
405409 e.printStackTrace()
406410
407- mainState = MainState .FAILED
411+ mainState = ConnectionState .FAILED
408412
409413 // We report errors only that aren't simple connection failures
410414 if (e !is SocketTimeoutException && e !is ConnectException ) {
@@ -415,7 +419,7 @@ class MainActivity : ComponentActivity(), CoroutineScope by MainScope() {
415419
416420 private fun recoverAfterFailure () {
417421 currentProxyConfig = null
418- mainState = MainState .DISCONNECTED
422+ mainState = ConnectionState .DISCONNECTED
419423 }
420424
421425 private fun openDocs () {
@@ -517,7 +521,7 @@ class MainActivity : ComponentActivity(), CoroutineScope by MainScope() {
517521 showActiveVpnFailureAlert()
518522
519523 // Then go back to the disconnected state:
520- mainState = MainState .DISCONNECTED
524+ mainState = ConnectionState .DISCONNECTED
521525 } else if (
522526 requestCode == INSTALL_CERT_REQUEST &&
523527 Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q // Required for promptToManuallyInstallCert
@@ -538,14 +542,14 @@ class MainActivity : ComponentActivity(), CoroutineScope by MainScope() {
538542 requestNotificationPermission(true )
539543 } else {
540544 Sentry .captureMessage(" Non-OK result $resultCode for requestCode $requestCode " )
541- mainState = MainState .FAILED
545+ mainState = ConnectionState .FAILED
542546 }
543547 }
544548
545549 private fun startVpn () {
546550 Log .i(TAG , " Starting VPN" )
547551
548- mainState = MainState .CONNECTING
552+ mainState = ConnectionState .CONNECTING
549553
550554 startService(Intent (this , ProxyVpnService ::class .java).apply {
551555 action = START_VPN_ACTION
@@ -562,11 +566,11 @@ class MainActivity : ComponentActivity(), CoroutineScope by MainScope() {
562566 private suspend fun connectToVpnFromUrl (uri : Uri ) {
563567 Log .i(TAG , " Connecting to VPN from URL: $uri " )
564568 if (
565- mainState != MainState .DISCONNECTED &&
566- mainState != MainState .FAILED
569+ mainState != ConnectionState .DISCONNECTED &&
570+ mainState != ConnectionState .FAILED
567571 ) return
568572
569- mainState = MainState .CONNECTING
573+ mainState = ConnectionState .CONNECTING
570574
571575 withContext(Dispatchers .IO ) {
572576 try {
@@ -576,7 +580,7 @@ class MainActivity : ComponentActivity(), CoroutineScope by MainScope() {
576580 Log .e(TAG , e.toString())
577581 e.printStackTrace()
578582
579- mainState = MainState .FAILED
583+ mainState = ConnectionState .FAILED
580584
581585 // We report errors only that aren't simple connection failures
582586 if (e !is SocketTimeoutException && e !is ConnectException ) {
0 commit comments