diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/receiver/BootReceiver.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/receiver/BootReceiver.kt index acc86730..1d96f750 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/receiver/BootReceiver.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/receiver/BootReceiver.kt @@ -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) } } diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/receiver/TaskerReceiver.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/receiver/TaskerReceiver.kt index c6e49af4..b0aa16f4 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/receiver/TaskerReceiver.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/receiver/TaskerReceiver.kt @@ -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() 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 5aa6c2de..eb9e7854 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 @@ -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 -> 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 151d5c38..aa6da84c 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 @@ -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) } } } 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 0de09c2f..62466007 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 @@ -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) } diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/MainActivity.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/MainActivity.kt index f9fdce8e..6d441abf 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/MainActivity.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/MainActivity.kt @@ -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) diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/MainRecyclerAdapter.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/MainRecyclerAdapter.kt index 9801cd90..54cdecf8 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/MainRecyclerAdapter.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/MainRecyclerAdapter.kt @@ -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(), ItemTouchHelperAdapter { companion object { @@ -165,11 +164,11 @@ class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter 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)) }