From 9271857b1e4864ebd523635fba562810fe5a8b2e Mon Sep 17 00:00:00 2001
From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com>
Date: Fri, 4 Sep 2020 18:19:51 -0400
Subject: [PATCH] Remove VPN related logic in toggle components
---
V2rayNG/app/src/main/AndroidManifest.xml | 1 +
.../com/v2ray/ang/receiver/WidgetProvider.kt | 34 ++++---
.../com/v2ray/ang/service/QSTileService.kt | 11 +--
.../v2ray/ang/service/V2RayServiceManager.kt | 3 +-
.../com/v2ray/ang/ui/ScSwitchActivity.kt | 94 ++-----------------
.../com/v2ray/ang/ui/SettingsActivity.kt | 4 +-
.../main/kotlin/com/v2ray/ang/util/Utils.kt | 31 +-----
.../src/main/res/values-zh-rCN/strings.xml | 2 +-
.../src/main/res/values-zh-rTW/strings.xml | 2 +-
V2rayNG/app/src/main/res/values/strings.xml | 2 +-
10 files changed, 40 insertions(+), 144 deletions(-)
diff --git a/V2rayNG/app/src/main/AndroidManifest.xml b/V2rayNG/app/src/main/AndroidManifest.xml
index 15c788d5..71ddbab5 100644
--- a/V2rayNG/app/src/main/AndroidManifest.xml
+++ b/V2rayNG/app/src/main/AndroidManifest.xml
@@ -110,6 +110,7 @@
+
diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/receiver/WidgetProvider.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/receiver/WidgetProvider.kt
index dfda98b9..0f79a90b 100644
--- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/receiver/WidgetProvider.kt
+++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/receiver/WidgetProvider.kt
@@ -9,6 +9,7 @@ import android.content.Intent
import android.widget.RemoteViews
import com.v2ray.ang.R
import com.v2ray.ang.AppConfig
+import com.v2ray.ang.service.V2RayServiceManager
import com.v2ray.ang.util.Utils
class WidgetProvider : AppWidgetProvider() {
@@ -17,21 +18,19 @@ class WidgetProvider : AppWidgetProvider() {
*/
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
super.onUpdate(context, appWidgetManager, appWidgetIds)
-
- val isRunning = Utils.isServiceRun(context, "com.v2ray.ang.service.V2RayVpnService")
- updateWidgetBackground(context, appWidgetManager, appWidgetIds, isRunning)
+ updateWidgetBackground(context, appWidgetManager, appWidgetIds, V2RayServiceManager.v2rayPoint.isRunning)
}
private fun updateWidgetBackground(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray, isRunning: Boolean) {
val remoteViews = RemoteViews(context.packageName, R.layout.widget_switch)
val intent = Intent(context, WidgetProvider::class.java)
- intent.setAction(AppConfig.BROADCAST_ACTION_WIDGET_CLICK)
+ intent.action = AppConfig.BROADCAST_ACTION_WIDGET_CLICK
val pendingIntent = PendingIntent.getBroadcast(context, R.id.layout_switch, intent, PendingIntent.FLAG_UPDATE_CURRENT)
remoteViews.setOnClickPendingIntent(R.id.layout_switch, pendingIntent)
if (isRunning) {
- remoteViews.setInt(R.id.layout_switch, "setBackgroundResource", R.drawable.ic_rounded_corner_theme);
+ remoteViews.setInt(R.id.layout_switch, "setBackgroundResource", R.drawable.ic_rounded_corner_theme)
} else {
- remoteViews.setInt(R.id.layout_switch, "setBackgroundResource", R.drawable.ic_rounded_corner_grey);
+ remoteViews.setInt(R.id.layout_switch, "setBackgroundResource", R.drawable.ic_rounded_corner_grey)
}
for (appWidgetId in appWidgetIds) {
@@ -40,23 +39,28 @@ class WidgetProvider : AppWidgetProvider() {
}
/**
- * 接收窗口小部件点击时发送的广播
+ * 接收窗口小部件发送的广播
*/
override fun onReceive(context: Context, intent: Intent) {
super.onReceive(context, intent)
if (AppConfig.BROADCAST_ACTION_WIDGET_CLICK == intent.action) {
-
- val isRunning = Utils.isServiceRun(context, "com.v2ray.ang.service.V2RayVpnService")
- if (isRunning) {
-// context.toast(R.string.toast_services_stop)
+ if (V2RayServiceManager.v2rayPoint.isRunning) {
Utils.stopVService(context)
} else {
-// context.toast(R.string.toast_services_start)
- Utils.startVService(context)
+ Utils.startVServiceFromToggle(context)
}
+ } else if (AppConfig.BROADCAST_ACTION_ACTIVITY == intent.action) {
val manager = AppWidgetManager.getInstance(context)
- updateWidgetBackground(context, manager, manager.getAppWidgetIds(ComponentName(context, WidgetProvider::class.java)),
- !isRunning);
+ when (intent.getIntExtra("key", 0)) {
+ AppConfig.MSG_STATE_RUNNING, AppConfig.MSG_STATE_START_SUCCESS -> {
+ updateWidgetBackground(context, manager, manager.getAppWidgetIds(ComponentName(context, WidgetProvider::class.java)),
+ true)
+ }
+ AppConfig.MSG_STATE_NOT_RUNNING, AppConfig.MSG_STATE_START_FAILURE, AppConfig.MSG_STATE_STOP_SUCCESS -> {
+ updateWidgetBackground(context, manager, manager.getAppWidgetIds(ComponentName(context, WidgetProvider::class.java)),
+ false)
+ }
+ }
}
}
}
diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/QSTileService.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/QSTileService.kt
index cbb82425..3c9320e9 100644
--- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/QSTileService.kt
+++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/QSTileService.kt
@@ -6,7 +6,6 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.drawable.Icon
-import android.net.VpnService
import android.os.Build
import android.service.quicksettings.Tile
import android.service.quicksettings.TileService
@@ -15,7 +14,6 @@ import com.v2ray.ang.R
import com.v2ray.ang.extension.defaultDPreference
import com.v2ray.ang.util.MessageUtil
import com.v2ray.ang.util.Utils
-import org.jetbrains.anko.toast
import java.lang.ref.SoftReference
@@ -33,7 +31,6 @@ class QSTileService : TileService() {
qsTile?.icon = Icon.createWithResource(applicationContext, R.drawable.ic_v)
}
-
qsTile?.updateTile()
}
@@ -56,11 +53,7 @@ class QSTileService : TileService() {
super.onClick()
when (qsTile.state) {
Tile.STATE_INACTIVE -> {
- val intent = VpnService.prepare(this)
- if (intent == null)
- if (!Utils.startVService(this)) {
- toast(R.string.app_tile_first_use)
- }
+ Utils.startVServiceFromToggle(this)
}
Tile.STATE_ACTIVE -> {
Utils.stopVService(this)
@@ -93,4 +86,4 @@ class QSTileService : TileService() {
}
}
}
-}
\ No newline at end of file
+}
diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayServiceManager.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayServiceManager.kt
index 07907e82..0fcb708a 100644
--- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayServiceManager.kt
+++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayServiceManager.kt
@@ -24,6 +24,7 @@ import com.v2ray.ang.util.MessageUtil
import com.v2ray.ang.util.Utils
import go.Seq
import libv2ray.Libv2ray
+import libv2ray.V2RayPoint
import libv2ray.V2RayVPNServiceSupportsSet
import rx.Observable
import rx.Subscription
@@ -36,7 +37,7 @@ object V2RayServiceManager {
private const val NOTIFICATION_PENDING_INTENT_STOP_V2RAY = 1
private const val NOTIFICATION_ICON_THRESHOLD = 3000
- private val v2rayPoint = Libv2ray.newV2RayPoint(V2RayCallback())
+ val v2rayPoint: V2RayPoint = Libv2ray.newV2RayPoint(V2RayCallback())
private val mMsgReceive = ReceiveMessageHandler()
var serviceControl: SoftReference? = null
diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/ScSwitchActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/ScSwitchActivity.kt
index 5b023055..3d3101ef 100644
--- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/ScSwitchActivity.kt
+++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/ScSwitchActivity.kt
@@ -1,104 +1,22 @@
package com.v2ray.ang.ui
-import android.content.*
-import android.net.VpnService
import com.v2ray.ang.R
import com.v2ray.ang.util.Utils
import android.os.Bundle
-import com.v2ray.ang.AppConfig
-import com.v2ray.ang.util.MessageUtil
-import java.lang.ref.SoftReference
-import android.content.IntentFilter
-import kotlinx.android.synthetic.main.activity_main.*
-import rx.Observable
-import rx.android.schedulers.AndroidSchedulers
-import java.util.concurrent.TimeUnit
+import com.v2ray.ang.service.V2RayServiceManager
class ScSwitchActivity : BaseActivity() {
- companion object {
- private const val REQUEST_CODE_VPN_PREPARE = 0
- }
-
- var isRunning = false
- set(value) {
- field = value
- if (value) {
- Utils.stopVService(this)
- } else {
- val intent = VpnService.prepare(this)
- if (intent == null) {
- Utils.startVService(this)
- } else {
- startActivityForResult(intent, REQUEST_CODE_VPN_PREPARE)
- }
- }
- finishActivity()
- }
-
- fun finishActivity() {
- try {
- Observable.timer(5000, TimeUnit.MILLISECONDS)
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe {
- finish()
- }
- } catch (e: Exception) {
- }
- }
-
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
moveTaskToBack(true)
setContentView(R.layout.activity_none)
- val isRunning = Utils.isServiceRun(this, "com.v2ray.ang.service.V2RayVpnService")
- if (isRunning) {
- //Utils.stopVService(this)
- mMsgReceive = ReceiveMessageHandler(this@ScSwitchActivity)
- registerReceiver(mMsgReceive, IntentFilter(AppConfig.BROADCAST_ACTION_ACTIVITY))
- MessageUtil.sendMsg2Service(this, AppConfig.MSG_REGISTER_CLIENT, "")
-
+ if (V2RayServiceManager.v2rayPoint.isRunning) {
+ Utils.stopVService(this)
} else {
- Utils.startVService(this)
- finishActivity()
+ Utils.startVServiceFromToggle(this)
}
+ finish()
}
-
- override fun onStop() {
- super.onStop()
- if (mMsgReceive != null) {
- unregisterReceiver(mMsgReceive)
- mMsgReceive = null
- }
- }
-
- private var mMsgReceive: BroadcastReceiver? = null
-
- private class ReceiveMessageHandler(activity: ScSwitchActivity) : BroadcastReceiver() {
- internal var mReference: SoftReference = SoftReference(activity)
- override fun onReceive(ctx: Context?, intent: Intent?) {
- val activity = mReference.get()
- when (intent?.getIntExtra("key", 0)) {
- AppConfig.MSG_STATE_RUNNING -> {
- activity?.isRunning = true
- }
- AppConfig.MSG_STATE_NOT_RUNNING -> {
- activity?.isRunning = false
- }
-// AppConfig.MSG_STATE_START_SUCCESS -> {
-// activity?.toast(R.string.toast_services_success)
-// activity?.isRunning = true
-// }
-// AppConfig.MSG_STATE_START_FAILURE -> {
-// activity?.toast(R.string.toast_services_failure)
-// activity?.isRunning = false
-// }
-// AppConfig.MSG_STATE_STOP_SUCCESS -> {
-// activity?.isRunning = false
-// }
- }
- }
- }
-
-}
\ No newline at end of file
+}
diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SettingsActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SettingsActivity.kt
index f83ae661..0bf13dff 100644
--- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SettingsActivity.kt
+++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SettingsActivity.kt
@@ -88,7 +88,7 @@ class SettingsActivity : BaseActivity() {
}
private fun isRunning(): Boolean {
- return Utils.isServiceRun(activity, "com.v2ray.ang.service.V2RayVpnService")
+ return false //TODO no point of adding logic now since Settings will be changed soon
}
override fun onCreate(savedInstanceState: Bundle?) {
@@ -247,4 +247,4 @@ class SettingsActivity : BaseActivity() {
}
}
-}
\ No newline at end of file
+}
diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt
index 11bb67ce..b61172cb 100644
--- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt
+++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt
@@ -11,7 +11,6 @@ import com.google.zxing.qrcode.QRCodeWriter
import com.google.zxing.EncodeHintType
import java.util.*
import kotlin.collections.HashMap
-import android.app.ActivityManager
import android.content.ClipData
import android.content.Intent
import android.net.Uri
@@ -273,32 +272,12 @@ object Utils {
return false
}
-
- /**
- * 判断服务是否后台运行
-
- * @param context
- * * Context
- * *
- * @param className
- * * 判断的服务名字
- * *
- * @return true 在运行 false 不在运行
- */
- fun isServiceRun(context: Context, className: String): Boolean {
- var isRun = false
- val activityManager = context
- .getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
- val serviceList = activityManager
- .getRunningServices(999)
- val size = serviceList.size
- for (i in 0..size - 1) {
- if (serviceList[i].service.className == className) {
- isRun = true
- break
- }
+ fun startVServiceFromToggle(context: Context): Boolean {
+ val result = startVService(context)
+ if (!result) {
+ context.toast(R.string.app_tile_first_use)
}
- return isRun
+ return result
}
/**
diff --git a/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml b/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml
index b08a711a..fe574e6b 100644
--- a/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml
+++ b/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml
@@ -3,7 +3,7 @@
v2rayNG
开关
开关
- 初次使用此功能请先用APP激活VPN
+ 初次使用此功能请先用APP添加配置
Open navigation drawer
Close navigation drawer
diff --git a/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml b/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml
index d7310a34..6f69a937 100644
--- a/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml
+++ b/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml
@@ -3,7 +3,7 @@
v2rayNG
切換
切換
- 首次使用此功能,請使用此應用程式來啟用 VPN
+ 首次使用此功能,請使用此應用程式新增組態
Open navigation drawer
Close navigation drawer
diff --git a/V2rayNG/app/src/main/res/values/strings.xml b/V2rayNG/app/src/main/res/values/strings.xml
index 129d0264..18e712cf 100644
--- a/V2rayNG/app/src/main/res/values/strings.xml
+++ b/V2rayNG/app/src/main/res/values/strings.xml
@@ -3,7 +3,7 @@
v2rayNG
Switch
Switch
- First use of this feature, please use the app to activate VPN
+ First use of this feature, please use the app to add server
Open navigation drawer
Close navigation drawer