@@ -55,8 +55,7 @@ class V2RayVpnService : VpnService() {
55
55
}
56
56
}
57
57
58
- private val v2rayPoint = Libv2ray .newV2RayPoint()
59
- private val v2rayCallback = V2RayCallback ()
58
+ private val v2rayPoint = Libv2ray .newV2RayPoint(V2RayCallback ())
60
59
private lateinit var configContent: String
61
60
private lateinit var mInterface: ParcelFileDescriptor
62
61
val fd: Int get() = mInterface.fd
@@ -110,9 +109,13 @@ class V2RayVpnService : VpnService() {
110
109
stopV2Ray()
111
110
}
112
111
112
+ override fun onLowMemory () {
113
+ stopV2Ray()
114
+ super .onLowMemory()
115
+ }
116
+
113
117
override fun onDestroy () {
114
118
super .onDestroy()
115
-
116
119
cancelNotification()
117
120
}
118
121
@@ -180,22 +183,11 @@ class V2RayVpnService : VpnService() {
180
183
// Create a new interface using the builder and save the parameters.
181
184
mInterface = builder.establish()
182
185
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()
192
187
}
193
188
194
189
fun shutdown () {
195
- try {
196
- mInterface.close()
197
- } catch (ignored: Exception ) {
198
- }
190
+ stopV2Ray(true )
199
191
}
200
192
201
193
fun sendFd () {
@@ -231,12 +223,15 @@ class V2RayVpnService : VpnService() {
231
223
if (! v2rayPoint.isRunning) {
232
224
233
225
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)
235
231
} catch (e: Exception ) {
236
232
}
237
233
238
234
configContent = defaultDPreference.getPrefString(AppConfig .PREF_CURR_CONFIG , " " )
239
- v2rayPoint.supportSet = v2rayCallback
240
235
v2rayPoint.configureFileContent = configContent
241
236
v2rayPoint.enableLocalDNS = defaultDPreference.getPrefBoolean(SettingsActivity .PREF_LOCAL_DNS_ENABLED , false )
242
237
v2rayPoint.forwardIpv6 = defaultDPreference.getPrefBoolean(SettingsActivity .PREF_FORWARD_IPV6 , false )
@@ -321,7 +316,6 @@ class V2RayVpnService : VpnService() {
321
316
mBuilder = NotificationCompat .Builder (applicationContext, channelId)
322
317
.setSmallIcon(R .drawable.ic_v)
323
318
.setContentTitle(defaultDPreference.getPrefString(AppConfig .PREF_CURR_CONFIG_NAME , " " ))
324
- .setContentText(getString(R .string.notification_action_more))
325
319
.setPriority(NotificationCompat .PRIORITY_MIN )
326
320
.setOngoing(true )
327
321
.setShowWhen(false )
@@ -359,7 +353,7 @@ class V2RayVpnService : VpnService() {
359
353
360
354
private fun updateNotification (contentText : String ) {
361
355
if (mBuilder != null ) {
362
- mBuilder?.setContentText (contentText)
356
+ mBuilder?.setContentTitle (contentText)
363
357
getNotificationManager().notify(NOTIFICATION_ID , mBuilder?.build())
364
358
}
365
359
}
@@ -371,8 +365,41 @@ class V2RayVpnService : VpnService() {
371
365
return mNotificationManager!!
372
366
}
373
367
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
+
374
399
private inner class V2RayCallback : V2RayVPNServiceSupportsSet {
375
400
override fun shutdown (): Long {
401
+ // called by go
402
+ // shutdown the whole vpn service
376
403
try {
377
404
this @V2RayVpnService.shutdown()
378
405
return 0
@@ -447,6 +474,17 @@ class V2RayVpnService : VpnService() {
447
474
vpnService?.startV2ray()
448
475
}
449
476
}
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
+ }
450
488
}
451
489
}
452
490
}
0 commit comments