Refactor the start and stop service

This commit is contained in:
2dust
2025-03-18 15:05:42 +08:00
parent 401f051774
commit 5f3d7c0213
9 changed files with 52 additions and 50 deletions

View File

@@ -13,6 +13,6 @@ class BootReceiver : BroadcastReceiver() {
//Check if flag is true and a server is selected
if (!MmkvManager.decodeStartOnBoot() || MmkvManager.getSelectServer().isNullOrEmpty()) return
//Start v2ray
V2RayServiceManager.startV2Ray(context)
V2RayServiceManager.startVService(context)
}
}

View File

@@ -5,9 +5,7 @@ import android.content.Context
import android.content.Intent
import android.text.TextUtils
import com.v2ray.ang.AppConfig
import com.v2ray.ang.handler.MmkvManager
import com.v2ray.ang.service.V2RayServiceManager
import com.v2ray.ang.util.Utils
class TaskerReceiver : BroadcastReceiver() {
@@ -22,13 +20,12 @@ class TaskerReceiver : BroadcastReceiver() {
return
} else if (switch) {
if (guid == AppConfig.TASKER_DEFAULT_GUID) {
Utils.startVServiceFromToggle(context)
V2RayServiceManager.startVServiceFromToggle(context)
} else {
MmkvManager.setSelectServer(guid)
V2RayServiceManager.startV2Ray(context)
V2RayServiceManager.startVService(context, guid)
}
} else {
Utils.stopVService(context)
V2RayServiceManager.stopVService(context)
}
} catch (e: Exception) {
e.printStackTrace()

View File

@@ -11,7 +11,6 @@ import android.widget.RemoteViews
import com.v2ray.ang.AppConfig
import com.v2ray.ang.R
import com.v2ray.ang.service.V2RayServiceManager
import com.v2ray.ang.util.Utils
class WidgetProvider : AppWidgetProvider() {
/**
@@ -58,9 +57,9 @@ class WidgetProvider : AppWidgetProvider() {
super.onReceive(context, intent)
if (AppConfig.BROADCAST_ACTION_WIDGET_CLICK == intent.action) {
if (V2RayServiceManager.v2rayPoint.isRunning) {
Utils.stopVService(context)
V2RayServiceManager.stopVService(context)
} else {
Utils.startVServiceFromToggle(context)
V2RayServiceManager.startVServiceFromToggle(context)
}
} else if (AppConfig.BROADCAST_ACTION_ACTIVITY == intent.action) {
AppWidgetManager.getInstance(context)?.let { manager ->

View File

@@ -64,11 +64,11 @@ class QSTileService : TileService() {
super.onClick()
when (qsTile.state) {
Tile.STATE_INACTIVE -> {
Utils.startVServiceFromToggle(this)
V2RayServiceManager.startVServiceFromToggle(this)
}
Tile.STATE_ACTIVE -> {
Utils.stopVService(this)
V2RayServiceManager.stopVService(this)
}
}
}

View File

@@ -66,7 +66,28 @@ object V2RayServiceManager {
private var speedNotificationJob: Job? = null
private var mNotificationManager: NotificationManager? = null
fun startV2Ray(context: Context) {
fun startVServiceFromToggle(context: Context): Boolean {
if (MmkvManager.getSelectServer().isNullOrEmpty()) {
context.toast(R.string.app_tile_first_use)
return false
}
startContextService(context)
return true
}
fun startVService(context: Context, guid: String? = null) {
if (guid != null) {
MmkvManager.setSelectServer(guid)
}
startContextService(context)
}
fun stopVService(context: Context) {
context.toast(R.string.toast_services_stop)
MessageUtil.sendMsg2Service(context, AppConfig.MSG_STATE_STOP, "")
}
private fun startContextService(context: Context) {
if (v2rayPoint.isRunning) return
val guid = MmkvManager.getSelectServer() ?: return
val config = MmkvManager.decodeServerConfig(guid) ?: return
@@ -234,7 +255,7 @@ object V2RayServiceManager {
Log.d(ANG_PACKAGE, "Restart Service")
serviceControl.stopService()
Thread.sleep(500L)
startV2Ray(serviceControl.getService())
startVService(serviceControl.getService())
}
AppConfig.MSG_MEASURE_DELAY -> {
@@ -449,7 +470,7 @@ object V2RayServiceManager {
private fun stopSpeedNotification() {
speedNotificationJob?.let {
it.cancel()
it.cancel()
speedNotificationJob = null
updateNotification(currentConfig?.remarks, 0, 0)
}

View File

@@ -140,7 +140,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
binding.fab.setOnClickListener {
if (mainViewModel.isRunning.value == true) {
Utils.stopVService(this)
V2RayServiceManager.stopVService(this)
} else if ((MmkvManager.decodeSettingsString(AppConfig.PREF_MODE) ?: VPN) == VPN) {
val intent = VpnService.prepare(this)
if (intent == null) {
@@ -269,12 +269,12 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
toast(R.string.title_file_chooser)
return
}
V2RayServiceManager.startV2Ray(this)
V2RayServiceManager.startVService(this)
}
fun restartV2Ray() {
if (mainViewModel.isRunning.value == true) {
Utils.stopVService(this)
V2RayServiceManager.stopVService(this)
}
lifecycleScope.launch {
delay(500)

View File

@@ -26,7 +26,6 @@ import com.v2ray.ang.service.V2RayServiceManager
import com.v2ray.ang.util.Utils
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.*
class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter<MainRecyclerAdapter.BaseViewHolder>(), ItemTouchHelperAdapter {
companion object {
@@ -165,11 +164,11 @@ class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter<Mai
}
notifyItemChanged(mActivity.mainViewModel.getPosition(guid))
if (isRunning) {
Utils.stopVService(mActivity)
V2RayServiceManager.stopVService(mActivity)
mActivity.lifecycleScope.launch {
try {
delay(500)
V2RayServiceManager.startV2Ray(mActivity)
V2RayServiceManager.startVService(mActivity)
} catch (e: Exception) {
e.printStackTrace()
}

View File

@@ -3,7 +3,6 @@ package com.v2ray.ang.ui
import android.os.Bundle
import com.v2ray.ang.R
import com.v2ray.ang.service.V2RayServiceManager
import com.v2ray.ang.util.Utils
class ScSwitchActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
@@ -13,9 +12,9 @@ class ScSwitchActivity : BaseActivity() {
setContentView(R.layout.activity_none)
if (V2RayServiceManager.v2rayPoint.isRunning) {
Utils.stopVService(this)
V2RayServiceManager.stopVService(this)
} else {
Utils.startVServiceFromToggle(this)
V2RayServiceManager.startVServiceFromToggle(this)
}
finish()
}

View File

@@ -7,7 +7,6 @@ import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.Configuration.UI_MODE_NIGHT_MASK
import android.content.res.Configuration.UI_MODE_NIGHT_NO
import android.net.Uri
import android.os.Build
import android.os.LocaleList
import android.provider.Settings
@@ -18,17 +17,18 @@ import android.util.Patterns
import android.webkit.URLUtil
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.ContextCompat
import androidx.core.net.toUri
import com.v2ray.ang.AppConfig
import com.v2ray.ang.AppConfig.ANG_PACKAGE
import com.v2ray.ang.AppConfig.LOOPBACK
import com.v2ray.ang.R
import com.v2ray.ang.dto.Language
import com.v2ray.ang.extension.toast
import com.v2ray.ang.handler.MmkvManager
import com.v2ray.ang.service.V2RayServiceManager
import java.io.IOException
import java.net.*
import java.util.*
import java.net.ServerSocket
import java.net.URLDecoder
import java.net.URLEncoder
import java.util.Locale
import java.util.UUID
object Utils {
@@ -207,13 +207,13 @@ object Utils {
return isIpv4Address(value) || isIpv6Address(value)
}
fun isIpv4Address(value: String): Boolean {
private fun isIpv4Address(value: String): Boolean {
val regV4 =
Regex("^([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$")
return regV4.matches(value)
}
fun isIpv6Address(value: String): Boolean {
private fun isIpv6Address(value: String): Boolean {
var addr = value
if (addr.indexOf("[") == 0 && addr.lastIndexOf("]") > 0) {
addr = addr.drop(1)
@@ -225,7 +225,10 @@ object Utils {
}
private fun isCoreDNSAddress(s: String): Boolean {
return s.startsWith("https") || s.startsWith("tcp") || s.startsWith("quic") || s == "localhost"
return s.startsWith("https")
|| s.startsWith("tcp")
|| s.startsWith("quic")
|| s == "localhost"
}
/**
@@ -249,25 +252,9 @@ object Utils {
return false
}
fun startVServiceFromToggle(context: Context): Boolean {
if (MmkvManager.getSelectServer().isNullOrEmpty()) {
context.toast(R.string.app_tile_first_use)
return false
}
V2RayServiceManager.startV2Ray(context)
return true
}
/**
* stopVService
*/
fun stopVService(context: Context) {
context.toast(R.string.toast_services_stop)
MessageUtil.sendMsg2Service(context, AppConfig.MSG_STATE_STOP, "")
}
fun openUri(context: Context, uriString: String) {
val uri = Uri.parse(uriString)
val uri = uriString.toUri()
context.startActivity(Intent(Intent.ACTION_VIEW, uri))
}