|
@@ -0,0 +1,97 @@
|
|
|
+package com.vpn.fastestvpnservice.viewmodels
|
|
|
+
|
|
|
+import android.Manifest
|
|
|
+import android.content.Context
|
|
|
+import android.content.pm.PackageManager
|
|
|
+import android.net.ConnectivityManager
|
|
|
+import android.net.wifi.ScanResult
|
|
|
+import android.net.wifi.SupplicantState
|
|
|
+import android.net.wifi.WifiManager
|
|
|
+import android.util.Log
|
|
|
+import androidx.core.app.ActivityCompat
|
|
|
+import androidx.core.content.ContextCompat
|
|
|
+import androidx.lifecycle.LiveData
|
|
|
+import androidx.lifecycle.MutableLiveData
|
|
|
+import androidx.lifecycle.ViewModel
|
|
|
+import de.blinkt.openvpn.core.App
|
|
|
+
|
|
|
+class SettingsViewModel constructor(context: Context): ViewModel() {
|
|
|
+
|
|
|
+ var mutableLiveDataConnectedWifi = MutableLiveData<String>("")
|
|
|
+ var liveDataConnectedWifi: LiveData<String> = mutableLiveDataConnectedWifi
|
|
|
+
|
|
|
+ var context: Context
|
|
|
+
|
|
|
+ init {
|
|
|
+ this.context = context
|
|
|
+ }
|
|
|
+
|
|
|
+ fun getConnectedWifi() {
|
|
|
+ val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
|
|
|
+
|
|
|
+ if (ActivityCompat.checkSelfPermission(
|
|
|
+ context,
|
|
|
+ Manifest.permission.ACCESS_FINE_LOCATION
|
|
|
+ ) != PackageManager.PERMISSION_GRANTED ||
|
|
|
+ ActivityCompat.checkSelfPermission(
|
|
|
+ context,
|
|
|
+ Manifest.permission.ACCESS_COARSE_LOCATION
|
|
|
+ ) != PackageManager.PERMISSION_GRANTED
|
|
|
+ ) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ val scanResults: List<ScanResult> = wifiManager.scanResults
|
|
|
+ Log.d("getConnectedWifi", "Job scanResults + " + scanResults.size)
|
|
|
+
|
|
|
+ val nonZeroList: MutableList<ScanResult> = ArrayList()
|
|
|
+
|
|
|
+ if (scanResults.isNotEmpty()) {
|
|
|
+ Log.d("getConnectedWifi 1", scanResults.size.toString() + "")
|
|
|
+ nonZeroList.addAll(scanResults)
|
|
|
+ } else {
|
|
|
+ Log.d("getConnectedWifi 2", "else - nonZeroList")
|
|
|
+ }
|
|
|
+
|
|
|
+ if (wifiManager.isWifiEnabled) {
|
|
|
+ Log.d("getConnectedWifi", "isWifiEnabled")
|
|
|
+ val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
|
|
+
|
|
|
+ val activeNetwork = connectivityManager.activeNetworkInfo
|
|
|
+
|
|
|
+ if (activeNetwork != null && activeNetwork.isConnected && activeNetwork.type == ConnectivityManager.TYPE_WIFI) {
|
|
|
+ val wifiInfo = wifiManager.connectionInfo
|
|
|
+
|
|
|
+ if (wifiInfo != null && wifiInfo.supplicantState == SupplicantState.COMPLETED) {
|
|
|
+ val connectedSSID = wifiInfo.ssid.replace("\"", "")
|
|
|
+
|
|
|
+ Log.d("getConnectedWifi cssid", connectedSSID)
|
|
|
+
|
|
|
+ mutableLiveDataConnectedWifi.value = connectedSSID
|
|
|
+
|
|
|
+// for (scanResult in nonZeroList) {
|
|
|
+//
|
|
|
+//// Log.d("getConnectedWifi list", scanResult.SSID);
|
|
|
+//
|
|
|
+// val ssid = scanResult.SSID;
|
|
|
+//
|
|
|
+// if (scanResult.capabilities != null) {
|
|
|
+//
|
|
|
+// val targetSSID = ssid;
|
|
|
+// Log.d("getConnectedWifi Conn", "Normal wifi connected " + connectedSSID);
|
|
|
+//
|
|
|
+// if (connectedSSID.equals(targetSSID)) {
|
|
|
+// Log.d("getConnectedWifi con1", "Device is connected to the open Wi-Fi network: " + connectedSSID);
|
|
|
+// return;
|
|
|
+// } else {
|
|
|
+// Log.d("getConnectedWifi con2", "Device is connected to the Normal Wi-Fi network: " + connectedSSID);
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|