|
@@ -5,12 +5,19 @@ import android.app.Notification;
|
|
|
import android.app.NotificationChannel;
|
|
|
import android.app.NotificationManager;
|
|
|
import android.app.Service;
|
|
|
+import android.app.job.JobParameters;
|
|
|
import android.content.Context;
|
|
|
import android.content.Intent;
|
|
|
import android.content.IntentFilter;
|
|
|
+import android.content.ServiceConnection;
|
|
|
import android.content.pm.PackageManager;
|
|
|
+import android.net.ConnectivityManager;
|
|
|
+import android.net.NetworkInfo;
|
|
|
import android.net.wifi.ScanResult;
|
|
|
+import android.net.wifi.SupplicantState;
|
|
|
+import android.net.wifi.WifiInfo;
|
|
|
import android.net.wifi.WifiManager;
|
|
|
+import android.os.Binder;
|
|
|
import android.os.Build;
|
|
|
import android.os.IBinder;
|
|
|
import android.util.Log;
|
|
@@ -20,10 +27,14 @@ import androidx.core.app.ActivityCompat;
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
|
|
import com.vpn.fastestvpnservice.R;
|
|
|
+import com.vpn.fastestvpnservice.beans.AutoConnectModel;
|
|
|
+import com.vpn.fastestvpnservice.helpers.BasePreferenceHelper;
|
|
|
+import com.vpn.fastestvpnservice.screens.settingsScreenAll.AutoConnectScreenKt;
|
|
|
import com.vpn.fastestvpnservice.utils.WifiScanReceiver;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
import java.util.Timer;
|
|
|
|
|
|
public class WiFiMonitorService extends Service {
|
|
@@ -34,18 +45,33 @@ public class WiFiMonitorService extends Service {
|
|
|
private WifiManager wifiManager;
|
|
|
private static final int NOTIFICATION_ID = 2;
|
|
|
|
|
|
+ public static String WIFI_STATE_CHANGED = "WIFI_STATE_CHANGED";
|
|
|
+
|
|
|
private IntentFilter intentFilter;
|
|
|
|
|
|
public static final List<ScanResult> scanResultsStatic = new ArrayList<>();
|
|
|
|
|
|
+ private final IBinder binder = new LocalBinder();
|
|
|
+
|
|
|
+ public class LocalBinder extends Binder {
|
|
|
+ public WiFiMonitorService getService() {
|
|
|
+ return WiFiMonitorService.this;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public void onCreate() {
|
|
|
super.onCreate();
|
|
|
|
|
|
+ Log.d("open wifi rec s Start", "Started onStartCommand");
|
|
|
+
|
|
|
+ wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
|
|
wifiScanReceiver = new WifiScanReceiver();
|
|
|
intentFilter = new IntentFilter();
|
|
|
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
|
|
|
|
|
|
+ registerReceiver(wifiScanReceiver, intentFilter);
|
|
|
+
|
|
|
// NetworkJobService.scheduleJob(getApplicationContext());
|
|
|
|
|
|
}
|
|
@@ -80,28 +106,121 @@ public class WiFiMonitorService extends Service {
|
|
|
|
|
|
startForegroundNotification();
|
|
|
|
|
|
- Log.d("open wifi rec s Start", "Started onStartCommand");
|
|
|
- wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
|
|
- wifiScanReceiver = new WifiScanReceiver();
|
|
|
- intentFilter = new IntentFilter();
|
|
|
- intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
|
|
|
+// if (Objects.equals(intent.getAction(), WIFI_STATE_CHANGED)) {
|
|
|
+// Log.d("WIFI_STATE_CHANGED", "WIFI_STATE_CHANGED in WMS");
|
|
|
+//
|
|
|
+//// getConnectedWifiService();
|
|
|
+// }
|
|
|
|
|
|
- ContextCompat.registerReceiver(getApplicationContext(), wifiScanReceiver, intentFilter, ContextCompat.RECEIVER_NOT_EXPORTED);
|
|
|
+// getConnectedWifiService();
|
|
|
|
|
|
- boolean status = wifiManager.startScan();
|
|
|
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
- return START_STICKY;
|
|
|
- }
|
|
|
+// boolean status = wifiManager.startScan();
|
|
|
+// if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+// return START_STICKY;
|
|
|
+// }
|
|
|
|
|
|
// currently commenting below ...
|
|
|
|
|
|
// List<ScanResult> results = wifiManager.getScanResults();
|
|
|
// scanResultsStatic.addAll(results);
|
|
|
-// Log.d("open wifi rec s results", results.size() + " " + status);
|
|
|
+// Log.d("wifi_mon_ser", results.size() + " & " + status);
|
|
|
|
|
|
return START_STICKY;
|
|
|
}
|
|
|
|
|
|
+ public void getConnectedWifiService() {
|
|
|
+
|
|
|
+ Log.d("WiFiMonitorService", "Started");
|
|
|
+
|
|
|
+ if (ActivityCompat.checkSelfPermission(
|
|
|
+ getApplicationContext(),
|
|
|
+ Manifest.permission.ACCESS_FINE_LOCATION
|
|
|
+ ) != PackageManager.PERMISSION_GRANTED ||
|
|
|
+ ActivityCompat.checkSelfPermission(
|
|
|
+ getApplicationContext(),
|
|
|
+ Manifest.permission.ACCESS_COARSE_LOCATION
|
|
|
+ ) != PackageManager.PERMISSION_GRANTED
|
|
|
+ ) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // currently commenting below ...
|
|
|
+
|
|
|
+ BasePreferenceHelper basePreferenceHelper = new BasePreferenceHelper(getApplicationContext());
|
|
|
+ wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
|
|
+ List<ScanResult> scanResults = wifiManager.getScanResults();
|
|
|
+ Log.d("WiFiMonitorService", "Job scanResults + " + scanResults.size());
|
|
|
+
|
|
|
+ List<ScanResult> nonZeroList = new ArrayList<>();
|
|
|
+
|
|
|
+ if (!scanResults.isEmpty()) {
|
|
|
+ Log.d("WiFiMonitorService 1", scanResults.size() + "");
|
|
|
+ nonZeroList.addAll(scanResults);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ Log.d("WiFiMonitorService 2", WiFiMonitorService.scanResultsStatic.size() + "");
|
|
|
+ }
|
|
|
+
|
|
|
+ Log.d("WiFiMonitorService in", "Inside");
|
|
|
+
|
|
|
+// WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
|
|
+
|
|
|
+ if (wifiManager.isWifiEnabled()) {
|
|
|
+ Log.d("WiFiMonitorService", "isWifiEnabled");
|
|
|
+ ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
+
|
|
|
+ NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
|
|
|
+
|
|
|
+ if (activeNetwork != null && activeNetwork.isConnected() && activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
|
|
|
+ WifiInfo wifiInfo = wifiManager.getConnectionInfo();
|
|
|
+
|
|
|
+ Log.d("WiFiMonitorService", "activeNetwork = " + activeNetwork);
|
|
|
+
|
|
|
+ if (wifiInfo != null && wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) {
|
|
|
+ String connectedWifiSsid = wifiInfo.getSSID().replace("\"", "");
|
|
|
+
|
|
|
+ Log.d("WiFiMonitorServicecssid", "wifiInfo.getSSID() = " + wifiInfo.getSSID());
|
|
|
+
|
|
|
+ ArrayList<AutoConnectModel> autoConnectWifiList = basePreferenceHelper.getAutoConnectList();
|
|
|
+ ArrayList<AutoConnectModel> wifiList = new ArrayList<>();
|
|
|
+
|
|
|
+ Log.d("WiFiMonitorService", "connectedWifiSsid = " + connectedWifiSsid);
|
|
|
+ Log.d("WiFiMonitorService", "Before autoConnectWifiList = " + (autoConnectWifiList != null ? autoConnectWifiList.size() : 0));
|
|
|
+
|
|
|
+ if (autoConnectWifiList != null && !autoConnectWifiList.isEmpty()) {
|
|
|
+ wifiList.addAll(autoConnectWifiList);
|
|
|
+
|
|
|
+ boolean isConnectedSsidInList = false;
|
|
|
+ for (AutoConnectModel model : wifiList) {
|
|
|
+ if (model.getWifiName().equals(connectedWifiSsid)) {
|
|
|
+ isConnectedSsidInList = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!isConnectedSsidInList && !connectedWifiSsid.isEmpty()) {
|
|
|
+ wifiList.add(0, new AutoConnectModel(connectedWifiSsid, false));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (!connectedWifiSsid.isEmpty()) {
|
|
|
+ wifiList.add(new AutoConnectModel(connectedWifiSsid, false));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Log.d("WiFiMonitorService", "wifiList = " + wifiList.size());
|
|
|
+ Log.d("WiFiMonitorService", "wifiList = " + wifiList.toString());
|
|
|
+
|
|
|
+ AutoConnectScreenKt.getAutoConnectCallback().autoConnectList(wifiList);
|
|
|
+ AutoConnectScreenKt.getAutoConnectCallback().autoConnectedWifi(connectedWifiSsid);
|
|
|
+
|
|
|
+// mutableLiveDataConnectedWifi.setValue(wifiList);
|
|
|
+// mutableLiveDataConnectedSsid.setValue(connectedWifiSsid);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
private void startForegroundNotification() {
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
String channelId = "FastestVPN_Foreground_Wifi";
|
|
@@ -141,12 +260,16 @@ public class WiFiMonitorService extends Service {
|
|
|
@Override
|
|
|
public void onDestroy() {
|
|
|
super.onDestroy();
|
|
|
-// unregisterReceiver(wifiScanReceiver);
|
|
|
+ if (wifiScanReceiver != null) {
|
|
|
+ Log.d("WiFiMonitorService", "unregisterReceiver(wifiScanReceiver)");
|
|
|
+ unregisterReceiver(wifiScanReceiver);
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
@Nullable
|
|
|
@Override
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
- return null;
|
|
|
+ return binder;
|
|
|
}
|
|
|
}
|