move setupProxy checks to HomeViewModel.kt

This commit is contained in:
egor-white
2025-07-07 11:18:20 +03:00
parent 7d25f7e600
commit 817f3a0e7d
2 changed files with 22 additions and 20 deletions

View File

@@ -38,9 +38,9 @@ class ByeDpiVpnService : VpnService() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId) super.onStartCommand(intent, flags, startId)
startForeground(NOTIFICATION_ID, createNotification())
return when (intent?.action) { return when (intent?.action) {
"START_VPN" -> { "START_VPN" -> {
startForeground(NOTIFICATION_ID, createNotification())
setupProxy() setupProxy()
START_STICKY START_STICKY
} }
@@ -99,22 +99,13 @@ class ByeDpiVpnService : VpnService() {
} }
private fun setupProxy() { private fun setupProxy() {
if (getActiveStrategy(sharedPreferences).isNotEmpty()) { try {
try { startSocksProxy()
startSocksProxy() startByeDpi()
startByeDpi() status = ServiceStatus.Connected
status = ServiceStatus.Connected } catch (e: Exception) {
} catch (e: Exception) { Log.e("proxy", "Failed to start")
Log.e("proxy", "Failed to start") status = ServiceStatus.Failed
status = ServiceStatus.Failed
stopSelf()
}
} else {
Toast.makeText(
this@ByeDpiVpnService,
getString(R.string.toast_no_strategy_selected),
Toast.LENGTH_SHORT
).show()
stopSelf() stopSelf()
} }
} }

View File

@@ -3,6 +3,7 @@ package com.cherret.zaprett.ui.viewmodel
import android.app.Application import android.app.Application
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.widget.Toast
import androidx.compose.material3.SnackbarHostState import androidx.compose.material3.SnackbarHostState
import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
@@ -12,6 +13,7 @@ import com.cherret.zaprett.byedpi.ByeDpiVpnService
import com.cherret.zaprett.R import com.cherret.zaprett.R
import com.cherret.zaprett.byedpi.ServiceStatus import com.cherret.zaprett.byedpi.ServiceStatus
import com.cherret.zaprett.utils.download import com.cherret.zaprett.utils.download
import com.cherret.zaprett.utils.getActiveStrategy
import com.cherret.zaprett.utils.getBinVersion import com.cherret.zaprett.utils.getBinVersion
import com.cherret.zaprett.utils.getChangelog import com.cherret.zaprett.utils.getChangelog
import com.cherret.zaprett.utils.getModuleVersion import com.cherret.zaprett.utils.getModuleVersion
@@ -113,10 +115,19 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) {
} }
} else { } else {
if (ByeDpiVpnService.status == ServiceStatus.Disconnected || ByeDpiVpnService.status == ServiceStatus.Failed) { if (ByeDpiVpnService.status == ServiceStatus.Disconnected || ByeDpiVpnService.status == ServiceStatus.Failed) {
scope.launch { if (getActiveStrategy(prefs).isNotEmpty()) {
snackbarHostState.showSnackbar(context.getString(R.string.snack_starting_service)) scope.launch {
snackbarHostState.showSnackbar(context.getString(R.string.snack_starting_service))
}
_requestVpnPermission.value = true
}
else {
Toast.makeText(
context,
context.getString(R.string.toast_no_strategy_selected),
Toast.LENGTH_SHORT
).show()
} }
_requestVpnPermission.value = true
} }
else { else {
scope.launch { scope.launch {