Skip to content

Commit 2b571e2

Browse files
authored
Merge pull request #44 from cutelua/dev
improved notification
2 parents 879e25b + ba30697 commit 2b571e2

File tree

1 file changed

+58
-20
lines changed

1 file changed

+58
-20
lines changed

V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt

+58-20
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ class V2RayVpnService : VpnService() {
5555
}
5656
}
5757

58-
private val v2rayPoint = Libv2ray.newV2RayPoint()
59-
private val v2rayCallback = V2RayCallback()
58+
private val v2rayPoint = Libv2ray.newV2RayPoint(V2RayCallback())
6059
private lateinit var configContent: String
6160
private lateinit var mInterface: ParcelFileDescriptor
6261
val fd: Int get() = mInterface.fd
@@ -110,9 +109,13 @@ class V2RayVpnService : VpnService() {
110109
stopV2Ray()
111110
}
112111

112+
override fun onLowMemory() {
113+
stopV2Ray()
114+
super.onLowMemory()
115+
}
116+
113117
override fun onDestroy() {
114118
super.onDestroy()
115-
116119
cancelNotification()
117120
}
118121

@@ -180,22 +183,11 @@ class V2RayVpnService : VpnService() {
180183
// Create a new interface using the builder and save the parameters.
181184
mInterface = builder.establish()
182185
sendFd()
183-
184-
if (defaultDPreference.getPrefBoolean(SettingsActivity.PREF_SPEED_ENABLED, false)) {
185-
mSubscription = Observable.interval(3, java.util.concurrent.TimeUnit.SECONDS)
186-
.subscribe {
187-
val uplink = v2rayPoint.queryStats("socks", "uplink")
188-
val downlink = v2rayPoint.queryStats("socks", "downlink")
189-
updateNotification("${(uplink / 3).toSpeedString()}${(downlink / 3).toSpeedString()}")
190-
}
191-
}
186+
startSpeedNotification()
192187
}
193188

194189
fun shutdown() {
195-
try {
196-
mInterface.close()
197-
} catch (ignored: Exception) {
198-
}
190+
stopV2Ray(true)
199191
}
200192

201193
fun sendFd() {
@@ -231,12 +223,15 @@ class V2RayVpnService : VpnService() {
231223
if (!v2rayPoint.isRunning) {
232224

233225
try {
234-
registerReceiver(mMsgReceive, IntentFilter(AppConfig.BROADCAST_ACTION_SERVICE))
226+
val mFilter = IntentFilter(AppConfig.BROADCAST_ACTION_SERVICE)
227+
mFilter.addAction(Intent.ACTION_SCREEN_ON)
228+
mFilter.addAction(Intent.ACTION_SCREEN_OFF)
229+
mFilter.addAction(Intent.ACTION_USER_PRESENT)
230+
registerReceiver(mMsgReceive, mFilter)
235231
} catch (e: Exception) {
236232
}
237233

238234
configContent = defaultDPreference.getPrefString(AppConfig.PREF_CURR_CONFIG, "")
239-
v2rayPoint.supportSet = v2rayCallback
240235
v2rayPoint.configureFileContent = configContent
241236
v2rayPoint.enableLocalDNS = defaultDPreference.getPrefBoolean(SettingsActivity.PREF_LOCAL_DNS_ENABLED, false)
242237
v2rayPoint.forwardIpv6 = defaultDPreference.getPrefBoolean(SettingsActivity.PREF_FORWARD_IPV6, false)
@@ -321,7 +316,6 @@ class V2RayVpnService : VpnService() {
321316
mBuilder = NotificationCompat.Builder(applicationContext, channelId)
322317
.setSmallIcon(R.drawable.ic_v)
323318
.setContentTitle(defaultDPreference.getPrefString(AppConfig.PREF_CURR_CONFIG_NAME, ""))
324-
.setContentText(getString(R.string.notification_action_more))
325319
.setPriority(NotificationCompat.PRIORITY_MIN)
326320
.setOngoing(true)
327321
.setShowWhen(false)
@@ -359,7 +353,7 @@ class V2RayVpnService : VpnService() {
359353

360354
private fun updateNotification(contentText: String) {
361355
if (mBuilder != null) {
362-
mBuilder?.setContentText(contentText)
356+
mBuilder?.setContentTitle(contentText)
363357
getNotificationManager().notify(NOTIFICATION_ID, mBuilder?.build())
364358
}
365359
}
@@ -371,8 +365,41 @@ class V2RayVpnService : VpnService() {
371365
return mNotificationManager!!
372366
}
373367

368+
fun startSpeedNotification() {
369+
if (mSubscription == null &&
370+
v2rayPoint.isRunning &&
371+
defaultDPreference.getPrefBoolean(SettingsActivity.PREF_SPEED_ENABLED, false)) {
372+
val cf_name = defaultDPreference.getPrefString(AppConfig.PREF_CURR_CONFIG_NAME, "")
373+
var last_zero_speed = false
374+
375+
mSubscription = Observable.interval(3, java.util.concurrent.TimeUnit.SECONDS)
376+
.subscribe {
377+
val uplink = v2rayPoint.queryStats("socks", "uplink")
378+
val downlink = v2rayPoint.queryStats("socks", "downlink")
379+
val zero_speed = (uplink == 0L && downlink == 0L)
380+
if (!zero_speed || !last_zero_speed) {
381+
updateNotification("${cf_name}${(uplink / 3).toSpeedString()}${(downlink / 3).toSpeedString()}")
382+
}
383+
last_zero_speed = zero_speed
384+
}
385+
}
386+
}
387+
388+
389+
fun stopSpeedNotification() {
390+
if (mSubscription != null) {
391+
mSubscription?.unsubscribe() //stop queryStats
392+
mSubscription = null
393+
394+
val cf_name = defaultDPreference.getPrefString(AppConfig.PREF_CURR_CONFIG_NAME, "")
395+
updateNotification(cf_name)
396+
}
397+
}
398+
374399
private inner class V2RayCallback : V2RayVPNServiceSupportsSet {
375400
override fun shutdown(): Long {
401+
// called by go
402+
// shutdown the whole vpn service
376403
try {
377404
this@V2RayVpnService.shutdown()
378405
return 0
@@ -447,6 +474,17 @@ class V2RayVpnService : VpnService() {
447474
vpnService?.startV2ray()
448475
}
449476
}
477+
478+
when (intent?.action) {
479+
Intent.ACTION_SCREEN_OFF -> {
480+
Log.d(AppConfig.ANG_PACKAGE, "SCREEN_OFF, stop querying stats")
481+
vpnService?.stopSpeedNotification()
482+
}
483+
Intent.ACTION_SCREEN_ON -> {
484+
Log.d(AppConfig.ANG_PACKAGE, "SCREEN_ON, start querying stats")
485+
vpnService?.startSpeedNotification()
486+
}
487+
}
450488
}
451489
}
452490
}

0 commit comments

Comments
 (0)