Skip to content

Commit

Permalink
Merge pull request #44 from cutelua/dev
Browse files Browse the repository at this point in the history
improved notification
  • Loading branch information
2dust authored Jul 30, 2019
2 parents 879e25b + ba30697 commit 2b571e2
Showing 1 changed file with 58 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ class V2RayVpnService : VpnService() {
}
}

private val v2rayPoint = Libv2ray.newV2RayPoint()
private val v2rayCallback = V2RayCallback()
private val v2rayPoint = Libv2ray.newV2RayPoint(V2RayCallback())
private lateinit var configContent: String
private lateinit var mInterface: ParcelFileDescriptor
val fd: Int get() = mInterface.fd
Expand Down Expand Up @@ -110,9 +109,13 @@ class V2RayVpnService : VpnService() {
stopV2Ray()
}

override fun onLowMemory() {
stopV2Ray()
super.onLowMemory()
}

override fun onDestroy() {
super.onDestroy()

cancelNotification()
}

Expand Down Expand Up @@ -180,22 +183,11 @@ class V2RayVpnService : VpnService() {
// Create a new interface using the builder and save the parameters.
mInterface = builder.establish()
sendFd()

if (defaultDPreference.getPrefBoolean(SettingsActivity.PREF_SPEED_ENABLED, false)) {
mSubscription = Observable.interval(3, java.util.concurrent.TimeUnit.SECONDS)
.subscribe {
val uplink = v2rayPoint.queryStats("socks", "uplink")
val downlink = v2rayPoint.queryStats("socks", "downlink")
updateNotification("${(uplink / 3).toSpeedString()}${(downlink / 3).toSpeedString()}")
}
}
startSpeedNotification()
}

fun shutdown() {
try {
mInterface.close()
} catch (ignored: Exception) {
}
stopV2Ray(true)
}

fun sendFd() {
Expand Down Expand Up @@ -231,12 +223,15 @@ class V2RayVpnService : VpnService() {
if (!v2rayPoint.isRunning) {

try {
registerReceiver(mMsgReceive, IntentFilter(AppConfig.BROADCAST_ACTION_SERVICE))
val mFilter = IntentFilter(AppConfig.BROADCAST_ACTION_SERVICE)
mFilter.addAction(Intent.ACTION_SCREEN_ON)
mFilter.addAction(Intent.ACTION_SCREEN_OFF)
mFilter.addAction(Intent.ACTION_USER_PRESENT)
registerReceiver(mMsgReceive, mFilter)
} catch (e: Exception) {
}

configContent = defaultDPreference.getPrefString(AppConfig.PREF_CURR_CONFIG, "")
v2rayPoint.supportSet = v2rayCallback
v2rayPoint.configureFileContent = configContent
v2rayPoint.enableLocalDNS = defaultDPreference.getPrefBoolean(SettingsActivity.PREF_LOCAL_DNS_ENABLED, false)
v2rayPoint.forwardIpv6 = defaultDPreference.getPrefBoolean(SettingsActivity.PREF_FORWARD_IPV6, false)
Expand Down Expand Up @@ -321,7 +316,6 @@ class V2RayVpnService : VpnService() {
mBuilder = NotificationCompat.Builder(applicationContext, channelId)
.setSmallIcon(R.drawable.ic_v)
.setContentTitle(defaultDPreference.getPrefString(AppConfig.PREF_CURR_CONFIG_NAME, ""))
.setContentText(getString(R.string.notification_action_more))
.setPriority(NotificationCompat.PRIORITY_MIN)
.setOngoing(true)
.setShowWhen(false)
Expand Down Expand Up @@ -359,7 +353,7 @@ class V2RayVpnService : VpnService() {

private fun updateNotification(contentText: String) {
if (mBuilder != null) {
mBuilder?.setContentText(contentText)
mBuilder?.setContentTitle(contentText)
getNotificationManager().notify(NOTIFICATION_ID, mBuilder?.build())
}
}
Expand All @@ -371,8 +365,41 @@ class V2RayVpnService : VpnService() {
return mNotificationManager!!
}

fun startSpeedNotification() {
if (mSubscription == null &&
v2rayPoint.isRunning &&
defaultDPreference.getPrefBoolean(SettingsActivity.PREF_SPEED_ENABLED, false)) {
val cf_name = defaultDPreference.getPrefString(AppConfig.PREF_CURR_CONFIG_NAME, "")
var last_zero_speed = false

mSubscription = Observable.interval(3, java.util.concurrent.TimeUnit.SECONDS)
.subscribe {
val uplink = v2rayPoint.queryStats("socks", "uplink")
val downlink = v2rayPoint.queryStats("socks", "downlink")
val zero_speed = (uplink == 0L && downlink == 0L)
if (!zero_speed || !last_zero_speed) {
updateNotification("${cf_name}${(uplink / 3).toSpeedString()}${(downlink / 3).toSpeedString()}")
}
last_zero_speed = zero_speed
}
}
}


fun stopSpeedNotification() {
if (mSubscription != null) {
mSubscription?.unsubscribe() //stop queryStats
mSubscription = null

val cf_name = defaultDPreference.getPrefString(AppConfig.PREF_CURR_CONFIG_NAME, "")
updateNotification(cf_name)
}
}

private inner class V2RayCallback : V2RayVPNServiceSupportsSet {
override fun shutdown(): Long {
// called by go
// shutdown the whole vpn service
try {
this@V2RayVpnService.shutdown()
return 0
Expand Down Expand Up @@ -447,6 +474,17 @@ class V2RayVpnService : VpnService() {
vpnService?.startV2ray()
}
}

when (intent?.action) {
Intent.ACTION_SCREEN_OFF -> {
Log.d(AppConfig.ANG_PACKAGE, "SCREEN_OFF, stop querying stats")
vpnService?.stopSpeedNotification()
}
Intent.ACTION_SCREEN_ON -> {
Log.d(AppConfig.ANG_PACKAGE, "SCREEN_ON, start querying stats")
vpnService?.startSpeedNotification()
}
}
}
}
}
Expand Down

0 comments on commit 2b571e2

Please sign in to comment.