|
@@ -9,12 +9,15 @@ import android.content.ServiceConnection
|
|
|
import android.content.pm.PackageManager
|
|
|
import android.net.ConnectivityManager
|
|
|
import android.net.NetworkCapabilities
|
|
|
+import android.net.TrafficStats
|
|
|
import android.os.AsyncTask
|
|
|
import android.os.Build
|
|
|
import android.os.CountDownTimer
|
|
|
import android.os.Handler
|
|
|
import android.os.IBinder
|
|
|
+import android.os.Process
|
|
|
import android.os.RemoteException
|
|
|
+import android.os.SystemClock
|
|
|
import android.util.Log
|
|
|
import android.widget.Toast
|
|
|
import androidx.activity.ComponentActivity
|
|
@@ -29,6 +32,11 @@ import com.vpn.fastestvpnservice.constants.splitList
|
|
|
import com.vpn.fastestvpnservice.helpers.BasePreferenceHelper
|
|
|
import com.vpn.fastestvpnservice.helpers.UIHelper
|
|
|
import com.vpn.fastestvpnservice.openVpnUtils.EncryptData
|
|
|
+import com.vpn.fastestvpnservice.screens.bottomNavBarScreens.StringDown
|
|
|
+import com.vpn.fastestvpnservice.screens.bottomNavBarScreens.StringDownUnit
|
|
|
+import com.vpn.fastestvpnservice.screens.bottomNavBarScreens.StringUp
|
|
|
+import com.vpn.fastestvpnservice.screens.bottomNavBarScreens.StringUpUnit
|
|
|
+import com.vpn.fastestvpnservice.screens.bottomNavBarScreens.networkSpeed
|
|
|
import com.vpn.fastestvpnservice.viewmodels.HomeViewModel
|
|
|
import com.vpn.fastestvpnservice.widgets.SimpleAppWidget
|
|
|
import com.wireguard.android.backend.Backend
|
|
@@ -56,6 +64,8 @@ import java.io.BufferedReader
|
|
|
import java.io.InputStream
|
|
|
import java.io.InputStreamReader
|
|
|
import java.util.SortedSet
|
|
|
+import java.util.Timer
|
|
|
+import java.util.TimerTask
|
|
|
import java.util.TreeSet
|
|
|
|
|
|
class VPNConnectionsUtil: VpnStatus.StateListener {
|
|
@@ -83,6 +93,13 @@ class VPNConnectionsUtil: VpnStatus.StateListener {
|
|
|
var pm: ProfileManager? = null
|
|
|
var thread: Thread? = null
|
|
|
|
|
|
+ /* Network Stats - WG*/
|
|
|
+ private var lastUpdateTime = 0.0
|
|
|
+ private var lastTxBytes = 0.0
|
|
|
+ private var lastRxBytes = 0.0
|
|
|
+ private var finalTxBytes = 0.0
|
|
|
+ private var finalRxBytes = 0.0
|
|
|
+
|
|
|
private lateinit var openVpnStateReceiver: OpenVpnStateReceiver
|
|
|
|
|
|
|
|
@@ -115,6 +132,11 @@ class VPNConnectionsUtil: VpnStatus.StateListener {
|
|
|
Log.d("test_wg", "start vpn")
|
|
|
Log.d("isConnect_State_vpn", "startVPN util")
|
|
|
|
|
|
+ StringDown.value = 0.0
|
|
|
+ StringUp.value = 0.0
|
|
|
+ StringDownUnit.value = " Byte/s"
|
|
|
+ StringUpUnit.value = " Byte/s"
|
|
|
+
|
|
|
countDownTimer()
|
|
|
try {
|
|
|
if (CheckInternetConnection.getInternetConnection(context).isConnectedToInternet) {
|
|
@@ -353,6 +375,7 @@ class VPNConnectionsUtil: VpnStatus.StateListener {
|
|
|
if (isUp) {
|
|
|
App.tunnelStatus = Tunnel.State.UP
|
|
|
homeViewModel.setConnectState(App.CONNECTED)
|
|
|
+ startTrafficMonitoring()
|
|
|
Log.d("test_wg", "tunnelStatus if = ${App.tunnelStatus}")
|
|
|
|
|
|
val widgetIntent = Intent(context, SimpleAppWidget::class.java)
|
|
@@ -688,6 +711,86 @@ class VPNConnectionsUtil: VpnStatus.StateListener {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private fun startTrafficMonitoring() {
|
|
|
+ lastUpdateTime = SystemClock.elapsedRealtime().toDouble()
|
|
|
+ lastTxBytes = TrafficStats.getUidTxBytes(Process.myUid()).toDouble()
|
|
|
+ lastRxBytes = TrafficStats.getUidRxBytes(Process.myUid()).toDouble()
|
|
|
+
|
|
|
+ Log.d(
|
|
|
+ "test_network_stats",
|
|
|
+ "STM: lastUpdateTime = $lastUpdateTime lastTxBytes = $lastTxBytes lastRxBytes = $lastRxBytes"
|
|
|
+ )
|
|
|
+
|
|
|
+ // Start monitoring using a timer or background thread
|
|
|
+ // For example:
|
|
|
+ val timer = Timer()
|
|
|
+ timer.schedule(object : TimerTask() {
|
|
|
+ override fun run() {
|
|
|
+// Log.d("test_network_stats_DU", "isVpnActive: " + isVpnActive);
|
|
|
+ if (App.tunnelStatus == Tunnel.State.UP) {
|
|
|
+ monitorVpnTraffic()
|
|
|
+ } else {
|
|
|
+ if (basePreferenceHelper.getConnectState() == 0) {
|
|
|
+ cancel()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, 0, 1000) // Update every 1 second
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun monitorVpnTraffic() {
|
|
|
+ val currentTxBytes = TrafficStats.getUidTxBytes(Process.myUid()).toDouble()
|
|
|
+ val currentRxBytes = TrafficStats.getUidRxBytes(Process.myUid()).toDouble()
|
|
|
+
|
|
|
+ Log.d(
|
|
|
+ "test_network_stat_IKev2",
|
|
|
+ "MVT: currentTxBytes = $currentTxBytes currentRxBytes = $currentRxBytes"
|
|
|
+ )
|
|
|
+
|
|
|
+ val txBytes = currentTxBytes - lastTxBytes
|
|
|
+ val rxBytes = currentRxBytes - lastRxBytes
|
|
|
+
|
|
|
+ // Calculate VPN traffic statistics here
|
|
|
+ finalTxBytes = txBytes + finalTxBytes
|
|
|
+ finalRxBytes = rxBytes + finalRxBytes
|
|
|
+
|
|
|
+ var StringDown2 = 0.0
|
|
|
+ var StringUp2 = 0.0
|
|
|
+ var StringDownUnit = ""
|
|
|
+ var StringUpUnit = ""
|
|
|
+
|
|
|
+ if (finalTxBytes < 1000) {
|
|
|
+ StringUp2 = finalTxBytes
|
|
|
+ StringUpUnit = "Byte/s"
|
|
|
+ } else if ((finalTxBytes >= 1000) && (finalTxBytes <= 1000000)) {
|
|
|
+ StringUp2 = finalTxBytes / 1000
|
|
|
+ StringUpUnit = "KB/S"
|
|
|
+ } else {
|
|
|
+ StringUp2 = finalTxBytes / 1000000
|
|
|
+ StringUpUnit = "MB/S"
|
|
|
+ }
|
|
|
+
|
|
|
+ if (finalRxBytes < 1000) {
|
|
|
+ StringDown2 = finalRxBytes
|
|
|
+ StringDownUnit = "Byte/s"
|
|
|
+ } else if ((finalRxBytes >= 1000) && (finalRxBytes <= 1000000)) {
|
|
|
+ StringDown2 = finalRxBytes / 1000
|
|
|
+ StringDownUnit = "KB/S"
|
|
|
+ } else {
|
|
|
+ StringDown2 = finalRxBytes / 1000000
|
|
|
+ StringDownUnit = "MB/S"
|
|
|
+ }
|
|
|
+
|
|
|
+ Log.d("test_network_stat_final", "Down: $StringDown2 Up: $StringUp2")
|
|
|
+
|
|
|
+ networkSpeed.setNetworkSpeed(StringDown2, StringUp2, StringDownUnit, StringUpUnit)
|
|
|
+
|
|
|
+ // Update last update time and byte counts for next calculation
|
|
|
+ lastUpdateTime = SystemClock.elapsedRealtime().toDouble()
|
|
|
+ lastTxBytes = currentTxBytes
|
|
|
+ lastRxBytes = currentRxBytes
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
fun onStopCallBack() {
|
|
|
Log.d("test_home_resume", "onStopCallBack!")
|