Преглед на файлове

working on IKEv2 connection states of widget

Khubaib преди 1 година
родител
ревизия
47815fa2b6

+ 25 - 27
app/src/main/java/com/vpn/fastestvpnservice/utils/VPNConnectionsUtil.kt

@@ -32,7 +32,6 @@ 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.ikev2Callback
 import com.vpn.fastestvpnservice.viewmodels.HomeViewModel
 import com.vpn.fastestvpnservice.widgets.SimpleAppWidget
 import com.wireguard.android.backend.Backend
@@ -136,9 +135,10 @@ class VPNConnectionsUtil: VpnStatus.StateListener {
                         App.connection_status = App.CONNECTION_STATE_CONNECTING
                         homeViewModel.setConnectState(App.CONNECTING)
 
-//                        val widgetIntent1 = Intent(context, SimpleAppWidget::class.java)
-//                        widgetIntent1.action = SimpleAppWidget.ACTION_CONNECTING_VPN
-//                        context.sendBroadcast(widgetIntent1)
+                        val widgetIntent1 = Intent(context, SimpleAppWidget::class.java)
+                        widgetIntent1.action = SimpleAppWidget.ACTION_CONNECTING_VPN
+                        context.sendBroadcast(widgetIntent1)
+
                         intent.action = VpnProfileControlActivity.START_PROFILE
                         intent.putExtra(VpnProfileControlActivity.EXTRA_VPN_PROFILE_ID, server?.id)
                         intent.putExtra(AppConstant.SERVER, Gson().toJson(server))
@@ -587,26 +587,15 @@ class VPNConnectionsUtil: VpnStatus.StateListener {
                         App.connection_status = App.CONNECTION_STATE_CONNECTED
                         Log.d("ikev2_rec_conn", "ACTION_VPN_CONNECTED $connectState")
 
-//                        val status = isVPNConnected()
-//                        Log.d("ikev2_rec_conn", "isVPNConnected $status")
-
                         homeViewModel.setConnectState(App.CONNECTED)
 
-
-//                        if (connectState == 1) {
-//                            Log.d("ikev2_rec_conn", "setConnectState(2) $connectState")
-//                            homeViewModel.setConnectState(App.CONNECTED)
+                        Log.d("vpnConnectionCall_ip", "ACTION_VPN_CONNECTED (2) $connectState")
+                        val connectState1 = basePreferenceHelper.getConnectState()
+//                        if (connectState1 == 2) {
+//                            val widgetIntent1 = Intent(context, SimpleAppWidget::class.java)
+//                            widgetIntent1.action = SimpleAppWidget.ACTION_CONNECT_VPN
+//                            context?.sendBroadcast(widgetIntent1)
 //                        }
-//                        else if (connectState == 0) {
-//                            Log.d("ikev2_rec_conn", "setConnectState(0) $connectState")
-//                            homeViewModel.setConnectState(App.DISCONNECTED)
-//                        }
-                        Log.d("vpnConnectionCall_ip", "ACTION_VPN_CONNECTED $connectState")
-
-
-//                        val widgetIntent1 = Intent(context, SimpleAppWidget::class.java)
-//                        widgetIntent1.action = SimpleAppWidget.ACTION_CONNECT_VPN
-//                        context?.sendBroadcast(widgetIntent1)
                     }
                     CharonVpnService.ACTION_VPN_DISCONNECTED -> {
                         val connectState = homeViewModel.isConnect.value
@@ -616,6 +605,10 @@ class VPNConnectionsUtil: VpnStatus.StateListener {
                             Log.d("vpnConnectionCall_con", "CONNECTING $connectState")
                             App.connection_status = App.CONNECTION_STATE_CONNECTING
                             homeViewModel.setConnectState(App.CONNECTING)
+
+                            val widgetIntent1 = Intent(context, SimpleAppWidget::class.java)
+                            widgetIntent1.action = SimpleAppWidget.ACTION_CONNECTING_VPN
+                            context?.sendBroadcast(widgetIntent1)
                         }
 //                        else if (connectState == App.DISCONNECTED) {
 //                            App.connection_status = App.CONNECTION_STATE_DISCONNECTED
@@ -625,10 +618,12 @@ class VPNConnectionsUtil: VpnStatus.StateListener {
                             Log.d("vpnConnectionCall_ip", "connectState == App.CONNECTED $connectState")
                             App.connection_status = App.CONNECTION_STATE_DISCONNECTED
                             homeViewModel.setConnectState(App.DISCONNECTED)
+
+                            val widgetIntent1 = Intent(context, SimpleAppWidget::class.java)
+                            widgetIntent1.action = SimpleAppWidget.ACTION_DISCONNECT_VPN
+                            context?.sendBroadcast(widgetIntent1)
                         }
-//                        val widgetIntent1 = Intent(context, SimpleAppWidget::class.java)
-//                        widgetIntent1.action = SimpleAppWidget.ACTION_DISCONNECT_VPN
-//                        context?.sendBroadcast(widgetIntent1)
+
                         homeViewModel.getIp()
                     }
                     CharonVpnService.ACTION_VPN_CONNECTING -> {
@@ -636,9 +631,10 @@ class VPNConnectionsUtil: VpnStatus.StateListener {
                         Log.d("vpnConnectionCall_con", "ACTION_VPN_CONNECTING $connectState")
                         App.connection_status = App.CONNECTION_STATE_CONNECTING
                         homeViewModel.setConnectState(App.CONNECTING)
-//                        val widgetIntent1 = Intent(context, SimpleAppWidget::class.java)
-//                        widgetIntent1.action = SimpleAppWidget.ACTION_CONNECTING_VPN
-//                        context?.sendBroadcast(widgetIntent1)
+
+                        val widgetIntent1 = Intent(context, SimpleAppWidget::class.java)
+                        widgetIntent1.action = SimpleAppWidget.ACTION_CONNECTING_VPN
+                        context?.sendBroadcast(widgetIntent1)
                     }
                     CharonVpnService.ACTION_VPN_SERVER_NOT_RESPONDING -> {
                         Log.d("vpnConnectionCall_ip", "ACTION_VPN_SERVER_NOT_RESPONDING")
@@ -669,6 +665,8 @@ class VPNConnectionsUtil: VpnStatus.StateListener {
                                 ).show()
                                 MainActivity.isSnrNotify = false
                             }
+
+                            homeViewModel.setConnectState(App.DISCONNECTED)
                         }
 
 //                        if (context != null) {

+ 36 - 10
app/src/main/java/com/vpn/fastestvpnservice/utils/WireGuardService.kt

@@ -28,6 +28,7 @@ import com.vpn.fastestvpnservice.constants.AppEnum
 import com.vpn.fastestvpnservice.constants.splitList
 import com.vpn.fastestvpnservice.helpers.BasePreferenceHelper
 import com.vpn.fastestvpnservice.openVpnUtils.EncryptData
+import com.vpn.fastestvpnservice.screens.bottomNavBarScreens.homeViewModel1
 import com.vpn.fastestvpnservice.widgets.SimpleAppWidget
 import com.wireguard.android.backend.GoBackend
 import com.wireguard.android.backend.Tunnel
@@ -146,6 +147,8 @@ class WireGuardService : Service(), VpnStatus.StateListener {
                             val intent = Intent(applicationContext, VpnProfileControlActivity::class.java)
                             App.connection_status = App.CONNECTION_STATE_CONNECTING
                             MainActivity.vpnConnectionCallBacks?.onVpnConnecting()
+
+                            basePreferenceHelper.setConnectState(App.CONNECTING)
                             val widgetIntent = Intent(applicationContext, SimpleAppWidget::class.java)
                             widgetIntent.action = SimpleAppWidget.ACTION_CONNECTING_VPN
                             applicationContext?.sendBroadcast(widgetIntent)
@@ -303,7 +306,8 @@ class WireGuardService : Service(), VpnStatus.StateListener {
         try {
             basePreferenceHelper = BasePreferenceHelper(applicationContext)
 
-            Log.d("wg test s stopVpn", App.connection_status.toString())
+            val connectState = basePreferenceHelper.getConnectState()
+            Log.d("wg test s stopVpn", "connectState = $connectState")
 
             /*Disconnect Wireguard*/
             if (basePreferenceHelper.getProtocol().title.toLowerCase()
@@ -320,6 +324,9 @@ class WireGuardService : Service(), VpnStatus.StateListener {
                 intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                 applicationContext.startActivity(intent)
 
+                basePreferenceHelper.setConnectState(App.DISCONNECTED)
+
+
 //                App.connection_status = App.CONNECTION_STATE_DISCONNECTED
 //                MainActivity.vpnConnectionCallBacks?.onVpnDisconnected()
             }
@@ -639,32 +646,46 @@ class WireGuardService : Service(), VpnStatus.StateListener {
         override fun onReceive(context: Context?, intent: Intent?) {
             if (intent != null && intent.action != null) {
                 basePreferenceHelper = BasePreferenceHelper(applicationContext)
+                val connectState = basePreferenceHelper.getConnectState()
                 val action = intent.action
                 when (action) {
                     CharonVpnService.ACTION_VPN_CONNECTED -> {
-                        Log.d("vpnConnectionCall ip w", "ACTION_VPN_CONNECTED widget " + App.connection_status)
                         App.connection_status = App.CONNECTION_STATE_CONNECTED
                         MainActivity.vpnConnectionCallBacks?.onVpnConnected()
-                        val widgetIntent = Intent(applicationContext, SimpleAppWidget::class.java)
-                        widgetIntent.action = SimpleAppWidget.ACTION_CONNECT_VPN
-                        applicationContext?.sendBroadcast(widgetIntent)
+
+//                        basePreferenceHelper.setConnectState(App.CONNECTED)
+                        val connectState1 = basePreferenceHelper.getConnectState()
+                        Log.d("vpnConnectionCall ip w", "ACTION_VPN_CONNECTED widget $connectState $connectState1")
+//                        if (connectState1 == 2) {
+//                            val widgetIntent = Intent(applicationContext, SimpleAppWidget::class.java)
+//                            widgetIntent.action = SimpleAppWidget.ACTION_CONNECT_VPN
+//                            applicationContext?.sendBroadcast(widgetIntent)
+//                        }
                     }
                     CharonVpnService.ACTION_VPN_DISABLED -> {
-                        Log.d("vpnConnectionCall ip w", "ACTION_VPN_DISABLED widget " + App.connection_status)
+                        Log.d("vpnConnectionCall ip w", "ACTION_VPN_DISABLED widget $connectState")
 
-                        if (App.connection_status == App.CONNECTION_STATE_CONNECTING)
+                        if (connectState == App.CONNECTING)
                         {
+                            Log.d("vpnConnectionCall ip w", "ACTION_VPN_DISABLED ==> $connectState")
                             App.connection_status = App.CONNECTION_STATE_CONNECTING
                             MainActivity.vpnConnectionCallBacks?.onVpnConnecting()
 
+                            basePreferenceHelper.setConnectState(App.CONNECTING)
+
                             val widgetIntent = Intent(applicationContext, SimpleAppWidget::class.java)
                             widgetIntent.action = SimpleAppWidget.ACTION_CONNECTING_VPN
                             applicationContext?.sendBroadcast(widgetIntent)
                         }
                         else {
+                            Log.d("vpnConnectionCall ip w",
+                                "ACTION_VPN_DISABLED ==> else $connectState"
+                            )
                             App.connection_status = App.CONNECTION_STATE_DISCONNECTED
                             MainActivity.vpnConnectionCallBacks?.onVpnDisconnected()
 
+                            basePreferenceHelper.setConnectState(App.DISCONNECTED)
+
                             val widgetIntent = Intent(applicationContext, SimpleAppWidget::class.java)
                             widgetIntent.action = SimpleAppWidget.ACTION_DISCONNECT_VPN
                             applicationContext?.sendBroadcast(widgetIntent)
@@ -672,15 +693,15 @@ class WireGuardService : Service(), VpnStatus.StateListener {
                         }
                     }
                     CharonVpnService.ACTION_VPN_NOT_CONNECTED -> {
-                        Log.d("vpnConnectionCall ip w", "ACTION_VPN_NOT_CONNECTED widget " + App.connection_status)
-//                        App.connection_status = App.CONNECTION_STATE_DISCONNECTED
-//                        MainActivity.vpnConnectionCallBacks?.onVpnDisconnected()
+                        Log.d("vpnConnectionCall ip w", "ACTION_VPN_NOT_CONNECTED widget $connectState")
                     }
                     CharonVpnService.ACTION_VPN_CONNECTING -> {
                         Log.d("vpnConnectionCall ip w", "ACTION_VPN_CONNECTING widget")
                         App.connection_status = App.CONNECTION_STATE_CONNECTING
                         MainActivity.vpnConnectionCallBacks?.onVpnConnecting()
 
+                        basePreferenceHelper.setConnectState(App.CONNECTING)
+
                         val widgetIntent = Intent(applicationContext, SimpleAppWidget::class.java)
                         widgetIntent.action = SimpleAppWidget.ACTION_CONNECTING_VPN
                         applicationContext?.sendBroadcast(widgetIntent)
@@ -692,6 +713,11 @@ class WireGuardService : Service(), VpnStatus.StateListener {
                         {
                             Log.d("vpnConnectionCall ip w", "ACTION_VPN_SERVER_NOT_RESPONDING tcp/udp")
                             startTcpUDP()
+                        } else {
+                            basePreferenceHelper.setConnectState(App.DISCONNECTED)
+                            val widgetIntent = Intent(applicationContext, SimpleAppWidget::class.java)
+                            widgetIntent.action = SimpleAppWidget.ACTION_DISCONNECT_VPN
+                            applicationContext?.sendBroadcast(widgetIntent)
                         }
                         MainActivity.vpnConnectionCallBacks?.onServerNotResponding()
                         val widgetIntent = Intent(applicationContext, SimpleAppWidget::class.java)

+ 7 - 3
app/src/main/java/org/strongswan/android/logic/CharonVpnService.java

@@ -60,6 +60,7 @@ import com.vpn.fastestvpnservice.beans.TvEnableApps;
 import com.vpn.fastestvpnservice.constants.AppConstant;
 import com.vpn.fastestvpnservice.constants.AppEnum;
 import com.vpn.fastestvpnservice.helpers.BasePreferenceHelper;
+import com.vpn.fastestvpnservice.widgets.SimpleAppWidget;
 
 import org.strongswan.android.data.VpnProfile;
 import org.strongswan.android.data.VpnProfile.SelectedAppsHandling;
@@ -634,6 +635,7 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
 
     @Override
     public void stateChanged() {
+        BasePreferenceHelper sessionManager = new BasePreferenceHelper(getBaseContext());
         Log.d(TAG, "state: " + mService.getState());
         if (mService.getState() == State.DISABLED) {
             Intent in = new Intent(ACTION_VPN_DISCONNECTED);
@@ -652,9 +654,11 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
                 Intent in = new Intent(ACTION_VPN_CONNECTED);
                 LocalBroadcastManager.getInstance(getBaseContext()).sendBroadcast(in);
 
-//                Intent widgetIntent = new Intent(getApplicationContext(), SimpleAppWidget.class);
-//                widgetIntent.setAction(SimpleAppWidget.ACTION_CONNECT_VPN);
-//                getApplicationContext().sendBroadcast(widgetIntent);
+                Intent widgetIntent = new Intent(getApplicationContext(), SimpleAppWidget.class);
+                widgetIntent.setAction(SimpleAppWidget.ACTION_CONNECT_VPN);
+                getApplicationContext().sendBroadcast(widgetIntent);
+
+                sessionManager.setConnectState(App.CONNECTED);
 
             } else if (mService.getState() == State.DISCONNECTING) {
                 Intent in = new Intent(ACTION_VPN_DISCONNECTED);