diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/receiver/WidgetProvider.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/receiver/WidgetProvider.kt index eb9e7854..bfe05f60 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/receiver/WidgetProvider.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/receiver/WidgetProvider.kt @@ -18,7 +18,7 @@ class WidgetProvider : AppWidgetProvider() { */ override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) { super.onUpdate(context, appWidgetManager, appWidgetIds) - updateWidgetBackground(context, appWidgetManager, appWidgetIds, V2RayServiceManager.v2rayPoint.isRunning) + updateWidgetBackground(context, appWidgetManager, appWidgetIds, V2RayServiceManager.isRunning()) } @@ -56,7 +56,7 @@ class WidgetProvider : AppWidgetProvider() { override fun onReceive(context: Context, intent: Intent) { super.onReceive(context, intent) if (AppConfig.BROADCAST_ACTION_WIDGET_CLICK == intent.action) { - if (V2RayServiceManager.v2rayPoint.isRunning) { + if (V2RayServiceManager.isRunning()) { V2RayServiceManager.stopVService(context) } else { V2RayServiceManager.startVServiceFromToggle(context) diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/service/NotificationService.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/service/NotificationService.kt index 3d0178f2..3b9fa40c 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/service/NotificationService.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/service/NotificationService.kt @@ -25,7 +25,6 @@ import kotlinx.coroutines.Job import kotlinx.coroutines.delay import kotlinx.coroutines.isActive import kotlinx.coroutines.launch -import libv2ray.V2RayPoint import kotlin.math.min object NotificationService { @@ -41,9 +40,9 @@ object NotificationService { private var mNotificationManager: NotificationManager? = null - fun startSpeedNotification(currentConfig: ProfileItem?, v2rayPoint: V2RayPoint) { + fun startSpeedNotification(currentConfig: ProfileItem?) { if (MmkvManager.decodeSettingsBool(AppConfig.PREF_SPEED_ENABLED) != true) return - if (speedNotificationJob != null || v2rayPoint.isRunning == false) return + if (speedNotificationJob != null || V2RayServiceManager.isRunning() == false) return lastQueryTime = System.currentTimeMillis() var lastZeroSpeed = false @@ -57,15 +56,15 @@ object NotificationService { var proxyTotal = 0L val text = StringBuilder() outboundTags?.forEach { - val up = v2rayPoint.queryStats(it, AppConfig.UPLINK) - val down = v2rayPoint.queryStats(it, AppConfig.DOWNLINK) + val up = V2RayServiceManager.queryStats(it, AppConfig.UPLINK) + val down = V2RayServiceManager.queryStats(it, AppConfig.DOWNLINK) if (up + down > 0) { appendSpeedString(text, it, up / sinceLastQueryInSeconds, down / sinceLastQueryInSeconds) proxyTotal += up + down } } - val directUplink = v2rayPoint.queryStats(TAG_DIRECT, AppConfig.UPLINK) - val directDownlink = v2rayPoint.queryStats(TAG_DIRECT, AppConfig.DOWNLINK) + val directUplink = V2RayServiceManager.queryStats(TAG_DIRECT, AppConfig.UPLINK) + val directDownlink = V2RayServiceManager.queryStats(TAG_DIRECT, AppConfig.DOWNLINK) val zeroSpeed = proxyTotal == 0L && directUplink == 0L && directDownlink == 0L if (!zeroSpeed || !lastZeroSpeed) { if (proxyTotal == 0L) { diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/service/QSTileService.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/service/QSTileService.kt index aa6da84c..e5d0dec5 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/service/QSTileService.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/service/QSTileService.kt @@ -26,7 +26,7 @@ class QSTileService : TileService() { qsTile?.icon = Icon.createWithResource(applicationContext, R.drawable.ic_stat_name) } else if (state == Tile.STATE_ACTIVE) { qsTile?.state = Tile.STATE_ACTIVE - qsTile?.label = V2RayServiceManager.currentConfig?.remarks + qsTile?.label = V2RayServiceManager.getRunningServerName() qsTile?.icon = Icon.createWithResource(applicationContext, R.drawable.ic_stat_name) } diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayServiceManager.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayServiceManager.kt index 29c35a59..8e273692 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayServiceManager.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayServiceManager.kt @@ -1,5 +1,6 @@ package com.v2ray.ang.service +import android.app.Service import android.content.BroadcastReceiver import android.content.Context import android.content.Intent @@ -29,8 +30,9 @@ import java.lang.ref.SoftReference object V2RayServiceManager { - val v2rayPoint: V2RayPoint = Libv2ray.newV2RayPoint(V2RayCallback(), Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) + private val v2rayPoint: V2RayPoint = Libv2ray.newV2RayPoint(V2RayCallback(), Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) private val mMsgReceive = ReceiveMessageHandler() + private var currentConfig: ProfileItem? = null var serviceControl: SoftReference? = null set(value) { @@ -38,7 +40,6 @@ object V2RayServiceManager { Seq.setContext(value?.get()?.getService()?.applicationContext) Libv2ray.initV2Env(Utils.userAssetPath(value?.get()?.getService()), Utils.getDeviceIdForXUDPBaseKey()) } - var currentConfig: ProfileItem? = null fun startVServiceFromToggle(context: Context): Boolean { if (MmkvManager.getSelectServer().isNullOrEmpty()) { @@ -61,6 +62,10 @@ object V2RayServiceManager { MessageUtil.sendMsg2Service(context, AppConfig.MSG_STATE_STOP, "") } + fun isRunning() = v2rayPoint.isRunning + + fun getRunningServerName() = currentConfig?.remarks.orEmpty() + private fun startContextService(context: Context) { if (v2rayPoint.isRunning) return val guid = MmkvManager.getSelectServer() ?: return @@ -89,52 +94,13 @@ object V2RayServiceManager { } } - private class V2RayCallback : V2RayVPNServiceSupportsSet { - override fun shutdown(): Long { - val serviceControl = serviceControl?.get() ?: return -1 - // called by go - return try { - serviceControl.stopService() - 0 - } catch (e: Exception) { - Log.d(ANG_PACKAGE, e.toString()) - -1 - } - } - - override fun prepare(): Long { - return 0 - } - - override fun protect(l: Long): Boolean { - val serviceControl = serviceControl?.get() ?: return true - return serviceControl.vpnProtect(l.toInt()) - } - - override fun onEmitStatus(l: Long, s: String?): Long { - return 0 - } - - override fun setup(s: String): Long { - val serviceControl = serviceControl?.get() ?: return -1 - return try { - serviceControl.startService() - NotificationService.startSpeedNotification(currentConfig, v2rayPoint) - 0 - } catch (e: Exception) { - Log.d(ANG_PACKAGE, e.toString()) - -1 - } - } - } - /** * Refer to the official documentation for [registerReceiver](https://developer.android.com/reference/androidx/core/content/ContextCompat#registerReceiver(android.content.Context,android.content.BroadcastReceiver,android.content.IntentFilter,int): * `registerReceiver(Context, BroadcastReceiver, IntentFilter, int)`. */ fun startV2rayPoint() { - val service = serviceControl?.get()?.getService() ?: return + val service = getService() ?: return val guid = MmkvManager.getSelectServer() ?: return val config = MmkvManager.decodeServerConfig(guid) ?: return if (v2rayPoint.isRunning) { @@ -176,7 +142,7 @@ object V2RayServiceManager { } fun stopV2rayPoint() { - val service = serviceControl?.get()?.getService() ?: return + val service = getService() ?: return if (v2rayPoint.isRunning) { CoroutineScope(Dispatchers.IO).launch { @@ -199,6 +165,84 @@ object V2RayServiceManager { PluginUtil.stopPlugin() } + fun queryStats(tag: String, link: String): Long { + return v2rayPoint.queryStats(tag, link) + } + + private fun measureV2rayDelay() { + CoroutineScope(Dispatchers.IO).launch { + val service = getService() ?: return@launch + var time = -1L + var errstr = "" + if (v2rayPoint.isRunning) { + try { + time = v2rayPoint.measureDelay(Utils.getDelayTestUrl()) + } catch (e: Exception) { + Log.d(ANG_PACKAGE, "measureV2rayDelay: $e") + errstr = e.message?.substringAfter("\":") ?: "empty message" + } + if (time == -1L) { + try { + time = v2rayPoint.measureDelay(Utils.getDelayTestUrl(true)) + } catch (e: Exception) { + Log.d(ANG_PACKAGE, "measureV2rayDelay: $e") + errstr = e.message?.substringAfter("\":") ?: "empty message" + } + } + } + val result = if (time == -1L) { + service.getString(R.string.connection_test_error, errstr) + } else { + service.getString(R.string.connection_test_available, time) + } + + MessageUtil.sendMsg2UI(service, AppConfig.MSG_MEASURE_DELAY_SUCCESS, result) + } + } + + private fun getService(): Service? { + return serviceControl?.get()?.getService() + } + + private class V2RayCallback : V2RayVPNServiceSupportsSet { + override fun shutdown(): Long { + val serviceControl = serviceControl?.get() ?: return -1 + // called by go + return try { + serviceControl.stopService() + 0 + } catch (e: Exception) { + Log.d(ANG_PACKAGE, e.toString()) + -1 + } + } + + override fun prepare(): Long { + return 0 + } + + override fun protect(l: Long): Boolean { + val serviceControl = serviceControl?.get() ?: return true + return serviceControl.vpnProtect(l.toInt()) + } + + override fun onEmitStatus(l: Long, s: String?): Long { + return 0 + } + + override fun setup(s: String): Long { + val serviceControl = serviceControl?.get() ?: return -1 + return try { + serviceControl.startService() + NotificationService.startSpeedNotification(currentConfig) + 0 + } catch (e: Exception) { + Log.d(ANG_PACKAGE, e.toString()) + -1 + } + } + } + private class ReceiveMessageHandler : BroadcastReceiver() { override fun onReceive(ctx: Context?, intent: Intent?) { val serviceControl = serviceControl?.get() ?: return @@ -244,41 +288,9 @@ object V2RayServiceManager { Intent.ACTION_SCREEN_ON -> { Log.d(ANG_PACKAGE, "SCREEN_ON, start querying stats") - NotificationService.startSpeedNotification(currentConfig, v2rayPoint) + NotificationService.startSpeedNotification(currentConfig) } } } } - - private fun measureV2rayDelay() { - CoroutineScope(Dispatchers.IO).launch { - val service = serviceControl?.get()?.getService() ?: return@launch - var time = -1L - var errstr = "" - if (v2rayPoint.isRunning) { - try { - time = v2rayPoint.measureDelay(Utils.getDelayTestUrl()) - } catch (e: Exception) { - Log.d(ANG_PACKAGE, "measureV2rayDelay: $e") - errstr = e.message?.substringAfter("\":") ?: "empty message" - } - if (time == -1L) { - try { - time = v2rayPoint.measureDelay(Utils.getDelayTestUrl(true)) - } catch (e: Exception) { - Log.d(ANG_PACKAGE, "measureV2rayDelay: $e") - errstr = e.message?.substringAfter("\":") ?: "empty message" - } - } - } - val result = if (time == -1L) { - service.getString(R.string.connection_test_error, errstr) - } else { - service.getString(R.string.connection_test_available, time) - } - - MessageUtil.sendMsg2UI(service, AppConfig.MSG_MEASURE_DELAY_SUCCESS, result) - } - } - } diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayVpnService.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayVpnService.kt index 03dcd48f..57e36ce0 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayVpnService.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayVpnService.kt @@ -190,7 +190,7 @@ class V2RayVpnService : VpnService(), ServiceControl { } // } - builder.setSession(V2RayServiceManager.currentConfig?.remarks.orEmpty()) + builder.setSession(V2RayServiceManager.getRunningServerName()) val selfPackageName = BuildConfig.APPLICATION_ID if (MmkvManager.decodeSettingsBool(AppConfig.PREF_PER_APP_PROXY)) { diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/ScSwitchActivity.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/ScSwitchActivity.kt index 3f5ce17c..0495318a 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/ScSwitchActivity.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/ScSwitchActivity.kt @@ -11,7 +11,7 @@ class ScSwitchActivity : BaseActivity() { setContentView(R.layout.activity_none) - if (V2RayServiceManager.v2rayPoint.isRunning) { + if (V2RayServiceManager.isRunning()) { V2RayServiceManager.stopVService(this) } else { V2RayServiceManager.startVServiceFromToggle(this)