Update notification icon based on traffic

Notification icon would be a good indication for current traffic
- going through proxy
- connect directly
- not going through v2rayNG
This commit is contained in:
yuhan6665
2020-07-18 13:09:53 -04:00
parent 5bf2fda990
commit 1144183da4
6 changed files with 16 additions and 6 deletions

View File

@@ -40,6 +40,7 @@ class V2RayVpnService : VpnService() {
const val NOTIFICATION_ID = 1
const val NOTIFICATION_PENDING_INTENT_CONTENT = 0
const val NOTIFICATION_PENDING_INTENT_STOP_V2RAY = 1
const val NOTIFICATION_ICON_THRESHOLD = 3000
fun startV2Ray(context: Context) {
val intent = Intent(context.applicationContext, V2RayVpnService::class.java)
@@ -359,8 +360,15 @@ class V2RayVpnService : VpnService() {
mSubscription = null
}
private fun updateNotification(contentText: String) {
private fun updateNotification(contentText: String, proxyTraffic: Long, directTraffic: Long) {
if (mBuilder != null) {
if (proxyTraffic < NOTIFICATION_ICON_THRESHOLD && directTraffic < NOTIFICATION_ICON_THRESHOLD) {
mBuilder?.setSmallIcon(R.drawable.ic_v)
} else if (proxyTraffic > directTraffic) {
mBuilder?.setSmallIcon(R.drawable.ic_stat_proxy)
} else {
mBuilder?.setSmallIcon(R.drawable.ic_stat_direct)
}
mBuilder?.setStyle(NotificationCompat.BigTextStyle().bigText(contentText))
getNotificationManager().notify(NOTIFICATION_ID, mBuilder?.build())
}
@@ -393,7 +401,8 @@ class V2RayVpnService : VpnService() {
updateNotification("proxy\t${(proxyUplink / sinceLastQueryInSeconds).toLong().toSpeedString()}" +
"${(proxyDownlink / sinceLastQueryInSeconds).toLong().toSpeedString()}\n" +
"direct\t${(directUplink / sinceLastQueryInSeconds).toLong().toSpeedString()}" +
"${(directDownlink / sinceLastQueryInSeconds).toLong().toSpeedString()}")
"${(directDownlink / sinceLastQueryInSeconds).toLong().toSpeedString()}",
proxyDownlink + proxyUplink, directDownlink + directUplink)
}
last_zero_speed = zero_speed
lastQueryTime = queryTime
@@ -408,7 +417,7 @@ class V2RayVpnService : VpnService() {
mSubscription = null
val cf_name = defaultDPreference.getPrefString(AppConfig.PREF_CURR_CONFIG_NAME, "")
updateNotification(cf_name)
updateNotification(cf_name, 0, 0)
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

View File

@@ -83,7 +83,7 @@
<string name="summary_pref_mux_enabled">开启可能会加速,关闭可能会减少断流</string>
<string name="title_pref_speed_enabled">启用速度显示</string>
<string name="summary_pref_speed_enabled">在通知中显示当前速度</string>
<string name="summary_pref_speed_enabled">在通知中显示当前速度\n小图标显示流量的路由情况</string>
<string name="title_pref_sniffing_enabled">启用流量探测</string>
<string name="summary_pref_sniffing_enabled">流量探测</string>

View File

@@ -84,7 +84,7 @@
<string name="summary_pref_mux_enabled">啟用或許會加快網路速度,切換或許會閃爍</string>
<string name="title_pref_speed_enabled">啟用速度顯示</string>
<string name="summary_pref_speed_enabled">在通知中顯示當前速度</string>
<string name="summary_pref_speed_enabled">在通知中顯示當前速度\n小圖標顯示流量的路由情況</string>
<string name="title_pref_sniffing_enabled">啟用流量探測</string>
<string name="summary_pref_sniffing_enabled">流量探測</string>

View File

@@ -84,7 +84,8 @@
<string name="summary_pref_mux_enabled">Enable maybe speed up network and switch network maybe flash</string>
<string name="title_pref_speed_enabled">Enable speed display</string>
<string name="summary_pref_speed_enabled">Display current speed in the notification</string>
<string name="summary_pref_speed_enabled">Display current speed in the notification.\nNotification icon would change based on
usage.</string>
<string name="title_pref_sniffing_enabled">Enable Sniffing</string>
<string name="summary_pref_sniffing_enabled">Sniffing</string>