5 Commits

11 changed files with 104 additions and 60 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
+10
View File
@@ -211,6 +211,16 @@
</intent-filter>
</receiver>
<receiver
android:name=".receiver.ServiceControlReceiver"
android:exported="false"
android:process=":RunSoLibV2RayDaemon">
<intent-filter>
<action android:name="${applicationId}.action.service.stop" />
<action android:name="${applicationId}.action.service.start" />
</intent-filter>
</receiver>
<service
android:name=".service.QSTileService"
android:exported="true"
@@ -84,6 +84,8 @@ object AppConfig {
const val BROADCAST_ACTION_SERVICE = "$ANG_PACKAGE.action.service"
const val BROADCAST_ACTION_ACTIVITY = "$ANG_PACKAGE.action.activity"
const val BROADCAST_ACTION_WIDGET_CLICK = "$ANG_PACKAGE.action.widget.click"
const val BROADCAST_ACTION_SERVICE_STOP = "$ANG_PACKAGE.action.service.stop"
const val BROADCAST_ACTION_SERVICE_START = "$ANG_PACKAGE.action.service.start"
/** Tasker extras. */
const val TASKER_EXTRA_BUNDLE = "com.twofortyfouram.locale.intent.extra.BUNDLE"
@@ -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
}
@@ -32,6 +32,7 @@ object V2RayServiceManager {
private var currentConfig: ProfileItem? = null
private val operationLock = Any()
@Volatile private var isOperationInProgress = false
@Volatile var isIntentionalStop = false
var serviceControl: SoftReference<ServiceControl>? = null
set(value) {
@@ -49,6 +50,7 @@ object V2RayServiceManager {
context.toast(R.string.app_tile_first_use)
return false
}
isIntentionalStop = false
startContextService(context)
return true
}
@@ -74,6 +76,7 @@ object V2RayServiceManager {
MmkvManager.setSelectServer(guid)
}
isIntentionalStop = false
startContextService(context)
} finally {
synchronized(operationLock) {
@@ -87,8 +90,16 @@ object V2RayServiceManager {
* @param context The context from which the service is stopped.
*/
fun stopVService(context: Context) {
Log.i(AppConfig.TAG, "StartCore-Manager: stopVService called, serviceControl=${serviceControl?.get()}")
MessageUtil.sendMsg2Service(context, AppConfig.MSG_STATE_STOP, "")
Log.i(AppConfig.TAG, "StartCore-Manager: stopVService called")
isIntentionalStop = true
val svc = serviceControl?.get()
if (svc != null) {
svc.stopService()
return
}
val intent = Intent(AppConfig.BROADCAST_ACTION_SERVICE_STOP)
intent.setPackage(AppConfig.ANG_PACKAGE)
context.sendBroadcast(intent)
}
/**
@@ -355,6 +366,11 @@ 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, "")
if (isIntentionalStop) {
Log.i(AppConfig.TAG, "StartCore-Manager: Intentional stop, skipping restart")
return 0
}
CoroutineScope(Dispatchers.IO).launch {
kotlinx.coroutines.delay(1000L)
val ctx = service.applicationContext
@@ -0,0 +1,19 @@
package xyz.zarazaex.olc.receiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import xyz.zarazaex.olc.AppConfig
import xyz.zarazaex.olc.handler.V2RayServiceManager
class ServiceControlReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
AppConfig.BROADCAST_ACTION_SERVICE_STOP -> {
V2RayServiceManager.isIntentionalStop = true
V2RayServiceManager.stopVService(context)
}
AppConfig.BROADCAST_ACTION_SERVICE_START -> V2RayServiceManager.startVServiceFromToggle(context)
}
}
}
@@ -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
}
}
@@ -32,7 +32,7 @@ class V2RayProxyOnlyService : Service(), ServiceControl {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.i(AppConfig.TAG, "StartCore-Proxy: Service command received")
V2RayServiceManager.startCoreLoop(null)
return START_STICKY
return START_NOT_STICKY
}
/**
@@ -99,18 +99,16 @@ class V2RayVpnService : VpnService(), ServiceControl {
override fun onDestroy() {
super.onDestroy()
Log.i(AppConfig.TAG, "StartCore-VPN: Service destroyed")
stopAllService(false)
wakeLock?.let { if (it.isHeld) it.release() }
wakeLock = null
NotificationManager.cancelNotification()
MessageUtil.sendMsg2UI(this, AppConfig.MSG_STATE_STOP_SUCCESS, "")
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.i(AppConfig.TAG, "StartCore-VPN: Service command received")
setupVpnService()
startService()
return START_STICKY
//return super.onStartCommand(intent, flags, startId)
return START_NOT_STICKY
}
override fun getService(): Service {
@@ -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,22 @@ class MainActivity : HelperBaseActivity(), NavigationView.OnNavigationItemSelect
}
isFabOperationInProgress = true
val isRunning = mainViewModel.isRunning.value == true
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 (isRunning) {
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 = mainViewModel.isRunning.value == true)
} finally {
isFabOperationInProgress = false
}
@@ -367,6 +372,7 @@ class MainActivity : HelperBaseActivity(), NavigationView.OnNavigationItemSelect
override fun onResume() {
super.onResume()
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 }