4 Commits

6 changed files with 64 additions and 53 deletions
+1 -1
View File
@@ -167,5 +167,5 @@ jobs:
with:
files: ${{ github.workspace }}/V2rayNG/app/build/outputs/apk/*/release/*.apk
tag_name: ${{ github.event.inputs.release_tag || github.ref_name }}
prerelease: true
prerelease: false
generate_release_notes: true
@@ -2,9 +2,12 @@ package xyz.zarazaex.olc.dto
data class ServerAffiliationInfo(var testDelayMillis: Long = 0L) {
fun getTestDelayString(): String {
if (testDelayMillis == 0L) {
return ""
return when {
testDelayMillis == 0L -> ""
testDelayMillis < 0L -> "Error"
else -> "${testDelayMillis}ms"
}
return testDelayMillis.toString() + "ms"
}
fun isReachable(): Boolean = testDelayMillis > 0L
}
@@ -355,6 +355,7 @@ object V2RayServiceManager {
return try {
Log.w(AppConfig.TAG, "StartCore-Manager: Core shutdown callback, attempting restart")
val service = serviceControl.getService()
MessageUtil.sendMsg2UI(service, AppConfig.MSG_STATE_NOT_RUNNING, "")
CoroutineScope(Dispatchers.IO).launch {
kotlinx.coroutines.delay(1000L)
val ctx = service.applicationContext
@@ -77,50 +77,25 @@ class RealPingWorkerService(
}
private suspend fun startRealPing(guid: String): Long {
val retFailure = -1L
val configResult = V2rayConfigManager.getV2rayConfig4Speedtest(context, guid)
if (!configResult.status) {
return retFailure
}
if (!configResult.status) return -1L
var bestDelay = retFailure
for (attempt in 0 until 2) {
val urls = listOf(
SettingsManager.getDelayTestUrl(),
SettingsManager.getDelayTestUrl(true)
)
for (url in urls) {
try {
val delay = withTimeout(10000L) {
V2RayNativeManager.measureOutboundDelay(
configResult.content,
SettingsManager.getDelayTestUrl()
)
}
if (delay > 0 && (bestDelay == retFailure || delay < bestDelay)) {
bestDelay = delay
}
if (bestDelay > 0) {
break
}
} catch (e: Exception) {
if (attempt == 0) {
try {
val delay = withTimeout(10000L) {
V2RayNativeManager.measureOutboundDelay(
configResult.content,
SettingsManager.getDelayTestUrl(true)
)
}
if (delay > 0 && (bestDelay == retFailure || delay < bestDelay)) {
bestDelay = delay
}
} catch (_: Exception) {
}
V2RayNativeManager.measureOutboundDelay(configResult.content, url)
}
if (delay > 0) return delay
} catch (_: Exception) {
}
}
return bestDelay
return -1L
}
}
@@ -34,6 +34,7 @@ import xyz.zarazaex.olc.handler.SettingsChangeManager
import xyz.zarazaex.olc.handler.SettingsManager
import xyz.zarazaex.olc.handler.UpdateCheckerManager
import xyz.zarazaex.olc.handler.V2RayServiceManager
import xyz.zarazaex.olc.util.MessageUtil
import xyz.zarazaex.olc.util.Utils
import xyz.zarazaex.olc.viewmodel.MainViewModel
import kotlinx.coroutines.Dispatchers
@@ -163,14 +164,16 @@ class MainActivity : HelperBaseActivity(), NavigationView.OnNavigationItemSelect
isLiteTesting = false
mainViewModel.sortByTestResults()
mainViewModel.reloadServerList()
val firstServer = mainViewModel.serversCache.firstOrNull()
if (firstServer != null) {
MmkvManager.setSelectServer(firstServer.guid)
val firstReachable = mainViewModel.serversCache.firstOrNull { cache ->
(MmkvManager.decodeServerAffiliationInfo(cache.guid)?.testDelayMillis ?: 0L) > 0L
}
if (firstReachable != null) {
MmkvManager.setSelectServer(firstReachable.guid)
showStatus("Подключаемся к быстрейшему серверу")
startV2RayWithPermission()
} else {
showStatus("Серверы не найдены!")
showStatus("Нет доступных серверов!")
}
}
}
@@ -205,20 +208,28 @@ class MainActivity : HelperBaseActivity(), NavigationView.OnNavigationItemSelect
}
isFabOperationInProgress = true
val actuallyRunning = V2RayServiceManager.isRunning()
if (mainViewModel.isRunning.value != actuallyRunning) {
Log.w(AppConfig.TAG, "FAB: UI state mismatch, syncing: vm=${mainViewModel.isRunning.value}, actual=$actuallyRunning")
mainViewModel.isRunning.value = actuallyRunning
isFabOperationInProgress = false
return
}
applyRunningState(isLoading = true, isRunning = false)
lifecycleScope.launch {
try {
if (mainViewModel.isRunning.value == true) {
Log.d(AppConfig.TAG, "FAB: stopping service, isRunning=${mainViewModel.isRunning.value}")
if (actuallyRunning) {
Log.d(AppConfig.TAG, "FAB: stopping service")
V2RayServiceManager.stopVService(this@MainActivity)
} else {
Log.d(AppConfig.TAG, "FAB: starting service, isRunning=${mainViewModel.isRunning.value}")
Log.d(AppConfig.TAG, "FAB: starting service")
startV2RayWithPermission()
}
} catch (e: Exception) {
Log.e(AppConfig.TAG, "FAB: error", e)
applyRunningState(isLoading = false, isRunning = mainViewModel.isRunning.value ?: false)
applyRunningState(isLoading = false, isRunning = V2RayServiceManager.isRunning())
} finally {
isFabOperationInProgress = false
}
@@ -367,6 +378,12 @@ class MainActivity : HelperBaseActivity(), NavigationView.OnNavigationItemSelect
override fun onResume() {
super.onResume()
val actuallyRunning = V2RayServiceManager.isRunning()
if (mainViewModel.isRunning.value != actuallyRunning) {
Log.w(AppConfig.TAG, "onResume: syncing state vm=${mainViewModel.isRunning.value}, actual=$actuallyRunning")
mainViewModel.isRunning.value = actuallyRunning
}
MessageUtil.sendMsg2Service(this, AppConfig.MSG_REGISTER_CLIENT, "")
}
override fun onPause() {
@@ -96,7 +96,12 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
val subServers = MmkvManager.decodeServerList(sub.guid)
subServers.forEach { guid ->
val delay = MmkvManager.decodeServerAffiliationInfo(guid)?.testDelayMillis ?: 0L
allServers.add(ServerWithDelay(guid, if (delay <= 0L) 999999 else delay))
val sortKey = when {
delay > 0L -> delay
delay == 0L -> Long.MAX_VALUE - 1
else -> Long.MAX_VALUE
}
allServers.add(ServerWithDelay(guid, sortKey))
}
}
@@ -482,7 +487,12 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
val serverList = MmkvManager.decodeServerList(sub.guid)
serverList.forEach { guid ->
val delay = MmkvManager.decodeServerAffiliationInfo(guid)?.testDelayMillis ?: 0L
allServerDelays.add(ServerDelay(guid, if (delay <= 0L) 999999 else delay, sub.guid))
val sortKey = when {
delay > 0L -> delay
delay == 0L -> Long.MAX_VALUE - 1
else -> Long.MAX_VALUE
}
allServerDelays.add(ServerDelay(guid, sortKey, sub.guid))
}
}
@@ -507,7 +517,12 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
serverListToSort.forEach { key ->
val delay = MmkvManager.decodeServerAffiliationInfo(key)?.testDelayMillis ?: 0L
serverDelays.add(ServerDelay(key, if (delay <= 0L) 999999 else delay))
val sortKey = when {
delay > 0L -> delay
delay == 0L -> Long.MAX_VALUE - 1
else -> Long.MAX_VALUE
}
serverDelays.add(ServerDelay(key, sortKey))
}
serverDelays.sortBy { it.testDelayMillis }